Skip to content

Enable reuse of DefaultActiveProfilesResolver [SPR-12611] #17212

Closed

Description

@spring-projects-issues

Tadaya Tsuyukubo opened SPR-12611 and commented

Background

I want to write a custom profile resolver for @ActiveProfiles.

The profile resolution logic is to dynamically append profiles on top of profiles resolved by default logic. Since it is appending profiles, I wanted to either extend or delegate to the DefaultActiveProfilesResolver class to reuse the default logic.

public class MyProfileResolver extends DefaultActiveProfilesResolver {
  @Override
  public String[] resolve(Class<?> testClass) {
    String[] profiles = super.resolve(testClass);
    // my logic to append profiles
  }
}
public class MyProfileResolver implements ActiveProfilesResolver {
  private final ActiveProfilesResolver defaultResolver = new DefaultActiveProfilesResolver();
  @Override
  public String[] resolve(Class<?> testClass) {
    String[] profiles = defaultResolver.resolve(testClass);
    // my logic to append profiles
  }
}

However, in DefaultActiveProfilesResolver, there is this check logic:

Class<? extends ActiveProfilesResolver> resolverClass = annAttrs.getClass("resolver");
if (!ActiveProfilesResolver.class.equals(resolverClass)) {
  String msg = String.format("Configuration error for test class [%s]: %s cannot be used "
    + "in conjunction with custom resolver [%s].", rootDeclaringClass.getName(),
    getClass().getSimpleName(), resolverClass.getName());
  logger.error(msg);
  throw new IllegalStateException(msg);
}

Basically, this check logic prevents the reuse of the DefaultActiveProfilesResolver class.


Proposal

Can this check can be removed, or should this check be isAssignableFrom() instead of equals()?


Affects: 4.1 GA

Issue Links:

Activity

  1. spring-projects-issues commented on Jan 10, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    Tadaya Tsuyukubo, this was a use case that I did not foresee when I wrote that check. So thanks for bringing it to my attention.

    After a cursory review of the changes introduced in conjunction with #14588, I believe that it should be possible to simply get rid of that check altogether.

    Regards,

    Sam

  2. spring-projects-issues commented on Jan 10, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    Completed as described in GitHub commit 276712d:

    Enable reuse of DefaultActiveProfilesResolver

    In order to allow DefaultActiveProfilesResolver to be reused (e.g., via
    extension or delegation), the check which asserts that the 'resolver'
    attribute of @ActiveProfiles is not set to a customer resolver class
    has been removed.

  3. spring-projects-issues commented on Jan 10, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    Backported to Spring Framework 4.1.5: a76a650

  4. added
    in: test
    type: enhancement
    on Jan 11, 2019
  5. added this to the 4.1.5 milestone on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: testtype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions