Skip to content

Profiles should be comparable when created via Profiles.of() #25340

Closed

Description

@stsypanov

Suppose we have this code in production:

private boolean isExternalProfileEnabled() {
  return environment.acceptsProfiles(Profiles.of("external"));
}

Then I want to mock this in tests and I write something like:

when(environment.acceptsProfiles(Profiles.of("external"))).thenReturn(true);

And at runtime the mock fails to return true, because comparison of arguments fails due to missing equals() and hashCode() implementations in ParsedProfiles.

This makes us use the deprecated API and rewrite the example code like:

private boolean isExternalProfileEnabled() {
  return environment.acceptsProfiles("external");
}

and use of the deprecated API in turn makes Sonar unhappy.

Activity

  1. added
    status: waiting-for-triage
    on Jun 30, 2020
  2. self-assigned this
    on Jul 6, 2020
  3. added
    in: core
    type: enhancement
    and removed
    status: waiting-for-triage
    on Jul 6, 2020
  4. added this to the 5.2.8 milestone on Jul 6, 2020
  5. jhoeller commented on Jul 6, 2020

    @jhoeller
    Contributor

    @sbrannen Let's backport this to 5.1.x as well since that specific Profiles API got introduced in 5.1.

  6. added
    status: backported
    and removed on Jul 6, 2020
  7. sbrannen commented on Jul 6, 2020

    @sbrannen
    Member

    @jhoeller, I was literally just about to discuss this with you.

    It turns out it's not as simple as it may appear at a quick glance.

    I've experimented locally but haven't checked anything into a branch.

    In summary...

    In order to properly implement equals() and hashCode() we might have to break down the individual expressions using something like an AST in order to determine the equivalence between logically equivalent expressions such as spring & java and java & spring. The same applies for ! and | support.

    Currently, ParsedProfiles stores each expression as a single String. So we cannot reliably use that in any robust way. It works if the tokens of the expressions are specified in the same order. For example, spring&java and spring & java can easily be checked to be equivalent (ignoring all whitespace -- and I have that working locally); whereas, without analyzing the meaning of the expression, spring & java and java & spring cannot be determined to be logically equivalent. It's even more complex than that due to the tree-based structure of expressions (e.g., (spring & framework) | (spring & java)).

    Now... the information is present in the internal Profiles[] parsed field, but that's not a data structure that we can query. The lambda expression returned by org.springframework.core.env.ProfilesParser.equals(String) captures the individual token (e.g., "spring" or "java"). So again the information is there, but not in any form that we can access for comparison.

    Let's discuss our options...

  8. jhoeller commented on Jul 6, 2020

    @jhoeller
    Contributor

    Maybe we should simply restrict it to literal equality of the input String... probably good enough for the mocking scenario here. We could even preserve whitespace in the comparison, avoiding any special processing of the String for comparison purposes.

  9. changed the title [-]Profiles should be comparable to be used with Mockito correctly[/-] [+]Profiles should be comparable when created via Profiles.of()[/+] on Jul 7, 2020
  10. added a commit that references this issue on Jul 7, 2020
    2c9e794
  11. added a commit that references this issue on Jul 7, 2020
    14d5390
  12. added a commit that references this issue on Aug 16, 2020
    be001f4
  13. added a commit that references this issue on Feb 18, 2022
    65639a2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: corestatus: backportedtype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions