From 5f7d1758f8eaaa9fbfb36945ae21a2160422e079 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sat, 22 Feb 2014 17:15:11 +0100 Subject: [PATCH] Always pass test class to ActiveProfilesResolver Prior to this commit, if @ActiveProfiles were used as a meta-annotation on a composed annotation, then the composed annotation's class would be passed to the ActiveProfilesResolver.resolve() method instead of the test class, which breaks the contract for ActiveProfilesResolver. This commit addresses this issue by ensuring that the actual test class is always passed to ActiveProfilesResolver.resolve(). Issue: SPR-11467 --- .../test/context/ContextLoaderUtils.java | 7 +++-- ...ContextLoaderUtilsActiveProfilesTests.java | 31 +++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/ContextLoaderUtils.java b/spring-test/src/main/java/org/springframework/test/context/ContextLoaderUtils.java index bb22a5a39f..b2d98ea768 100644 --- a/spring-test/src/main/java/org/springframework/test/context/ContextLoaderUtils.java +++ b/spring-test/src/main/java/org/springframework/test/context/ContextLoaderUtils.java @@ -507,6 +507,7 @@ abstract class ContextLoaderUtils { final Set activeProfiles = new HashSet(); while (descriptor != null) { + Class rootDeclaringClass = descriptor.getRootDeclaringClass(); Class declaringClass = descriptor.getDeclaringClass(); AnnotationAttributes annAttrs = descriptor.getAnnotationAttributes(); @@ -530,12 +531,12 @@ abstract class ContextLoaderUtils { } catch (Exception e) { String msg = String.format("Could not instantiate ActiveProfilesResolver of " - + "type [%s] for test class [%s].", resolverClass.getName(), declaringClass.getName()); + + "type [%s] for test class [%s].", resolverClass.getName(), rootDeclaringClass.getName()); logger.error(msg); throw new IllegalStateException(msg, e); } - profiles = resolver.resolve(declaringClass); + profiles = resolver.resolve(rootDeclaringClass); if (profiles == null) { String msg = String.format( "ActiveProfilesResolver [%s] returned a null array of bean definition profiles.", @@ -555,7 +556,7 @@ abstract class ContextLoaderUtils { } descriptor = annAttrs.getBoolean("inheritProfiles") ? findAnnotationDescriptor( - descriptor.getRootDeclaringClass().getSuperclass(), annotationType) : null; + rootDeclaringClass.getSuperclass(), annotationType) : null; } return StringUtils.toStringArray(activeProfiles); diff --git a/spring-test/src/test/java/org/springframework/test/context/ContextLoaderUtilsActiveProfilesTests.java b/spring-test/src/test/java/org/springframework/test/context/ContextLoaderUtilsActiveProfilesTests.java index af81a22d16..781b2e2772 100644 --- a/spring-test/src/test/java/org/springframework/test/context/ContextLoaderUtilsActiveProfilesTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/ContextLoaderUtilsActiveProfilesTests.java @@ -205,6 +205,18 @@ public class ContextLoaderUtilsActiveProfilesTests extends AbstractContextLoader resolveActiveProfiles(NullActiveProfilesResolverTestCase.class); } + /** + * This test verifies that the actual test class, not the composed annotation, + * is passed to the resolver. + * + * @since 4.0.3 + */ + @Test + public void resolveActiveProfilesWithMetaAnnotationAndTestClassVerifyingResolver() { + Class testClass = TestClassVerifyingActiveProfilesResolverTestCase.class; + assertResolvedProfiles(testClass, testClass.getSimpleName()); + } + // ------------------------------------------------------------------------- @@ -239,6 +251,12 @@ public class ContextLoaderUtilsActiveProfilesTests extends AbstractContextLoader boolean inheritProfiles() default false; } + @ActiveProfiles(resolver = TestClassVerifyingActiveProfilesResolver.class) + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.TYPE) + private static @interface MetaResolverConfig { + } + @MetaAnimalsConfig private static class MetaAnimals extends MetaLocationsBar { } @@ -282,6 +300,10 @@ public class ContextLoaderUtilsActiveProfilesTests extends AbstractContextLoader private static class ConflictingResolverAndValueTestCase { } + @MetaResolverConfig + private static class TestClassVerifyingActiveProfilesResolverTestCase { + } + @ActiveProfiles(profiles = "conflict", value = "conflict") private static class ConflictingProfilesAndValueTestCase { } @@ -322,4 +344,13 @@ public class ContextLoaderUtilsActiveProfilesTests extends AbstractContextLoader } } + private static class TestClassVerifyingActiveProfilesResolver implements ActiveProfilesResolver { + + @Override + public String[] resolve(Class testClass) { + return testClass.isAnnotation() ? new String[] { "@" + testClass.getSimpleName() } + : new String[] { testClass.getSimpleName() }; + } + } + }