diff --git a/org.springframework.test/src/main/java/org/springframework/test/context/ActivateProfiles.java b/org.springframework.test/src/main/java/org/springframework/test/context/ActiveProfiles.java similarity index 87% rename from org.springframework.test/src/main/java/org/springframework/test/context/ActivateProfiles.java rename to org.springframework.test/src/main/java/org/springframework/test/context/ActiveProfiles.java index 28a9acf915d..2781e4cdf78 100644 --- a/org.springframework.test/src/main/java/org/springframework/test/context/ActivateProfiles.java +++ b/org.springframework.test/src/main/java/org/springframework/test/context/ActiveProfiles.java @@ -24,9 +24,9 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** - * {@code ActivateProfiles} is a class-level annotation that is used to - * activate the bean definition profiles to use when loading an - * {@link org.springframework.context.ApplicationContext ApplicationContext} + * {@code ActiveProfiles} is a class-level annotation that is used to declare + * which active bean definition profiles should be used when loading + * an {@link org.springframework.context.ApplicationContext ApplicationContext} * for test classes. * * @author Sam Brannen @@ -40,7 +40,7 @@ import java.lang.annotation.Target; @Inherited @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) -public @interface ActivateProfiles { +public @interface ActiveProfiles { /** * Alias for {@link #profiles() profiles}. diff --git a/org.springframework.test/src/main/java/org/springframework/test/context/ContextLoaderUtils.java b/org.springframework.test/src/main/java/org/springframework/test/context/ContextLoaderUtils.java index cc277d8f05e..18c9fd4c64d 100644 --- a/org.springframework.test/src/main/java/org/springframework/test/context/ContextLoaderUtils.java +++ b/org.springframework.test/src/main/java/org/springframework/test/context/ContextLoaderUtils.java @@ -54,9 +54,9 @@ abstract class ContextLoaderUtils { /** - * Resolves the {@link ContextLoader} - * {@link Class} to use for the supplied {@link Class testClass} and - * then instantiates and returns that ContextLoader. + * Resolves the {@link ContextLoader} {@link Class} to use for the + * supplied {@link Class testClass} and then instantiates and returns + * that ContextLoader. * *

If the supplied defaultContextLoaderClassName is * null or empty, the standard @@ -68,7 +68,7 @@ abstract class ContextLoaderUtils { * @param defaultContextLoaderClassName the name of the default * ContextLoader class to use (may be null) * @return the resolved ContextLoader for the supplied - * testClass + * testClass (never null) * @see #resolveContextLoaderClass(Class, String) */ static ContextLoader resolveContextLoader(Class testClass, String defaultContextLoaderClassName) { @@ -85,8 +85,9 @@ abstract class ContextLoaderUtils { } /** - * Resolve the {@link ContextLoader} {@link Class} to use for the supplied + * Resolves the {@link ContextLoader} {@link Class} to use for the supplied * {@link Class test class}. + * *

    *
  1. If the {@link ContextConfiguration#loader() loader} attribute of * {@link ContextConfiguration @ContextConfiguration} is configured @@ -98,11 +99,13 @@ abstract class ContextLoaderUtils { * the class hierarchy, an attempt will be made to load and return the class * with the supplied defaultContextLoaderClassName.
  2. *
- * @param clazz the class for which to retrieve ContextLoader + * + * @param clazz the class for which to resolve the ContextLoader * class; must not be null * @param defaultContextLoaderClassName the name of the default * ContextLoader class to use; must not be null or empty * @return the ContextLoader class to use for the specified class + * (never null) * @throws IllegalArgumentException if {@link ContextConfiguration * @ContextConfiguration} is not present on the supplied class */ @@ -155,7 +158,7 @@ abstract class ContextLoaderUtils { } /** - * Retrieve {@link ApplicationContext} resource locations for the supplied + * Resolves {@link ApplicationContext} resource locations for the supplied * {@link Class class}, using the supplied {@link ContextLoader} to * {@link ContextLoader#processLocations(Class, String...) process} the * locations. @@ -166,12 +169,14 @@ abstract class ContextLoaderUtils { * Specifically, if the inheritLocations flag is set to * true, locations defined in the annotated class will be * appended to the locations defined in superclasses. + * * @param contextLoader the ContextLoader to use for processing the * locations (must not be null) - * @param clazz the class for which to retrieve the resource locations (must + * @param clazz the class for which to resolve the resource locations (must * not be null) * @return the list of ApplicationContext resource locations for the * specified class, including locations from superclasses if appropriate + * (never null) * @throws IllegalArgumentException if {@link ContextConfiguration * @ContextConfiguration} is not present on the supplied class */ @@ -190,7 +195,7 @@ abstract class ContextLoaderUtils { "Could not find an 'annotation declaring class' for annotation type [%s] and class [%s]", annotationType, clazz)); - List locationsList = new ArrayList(); + final List locationsList = new ArrayList(); while (declaringClass != null) { ContextConfiguration contextConfiguration = declaringClass.getAnnotation(annotationType); @@ -202,25 +207,34 @@ abstract class ContextLoaderUtils { String[] resolvedLocations = locationsResolver.resolveLocations(contextConfiguration, declaringClass); String[] processedLocations = contextLoader.processLocations(declaringClass, resolvedLocations); - locationsList.addAll(0, Arrays. asList(processedLocations)); + locationsList.addAll(0, Arrays.asList(processedLocations)); declaringClass = contextConfiguration.inheritLocations() ? AnnotationUtils.findAnnotationDeclaringClass( annotationType, declaringClass.getSuperclass()) : null; } - return locationsList.toArray(new String[locationsList.size()]); + return StringUtils.toStringArray(locationsList); } /** - * TODO Document resolveActiveProfiles(). + * Resolves active bean definition profiles for the supplied + * {@link Class class}. + * + *

Note that the {@link ActiveProfiles#inheritProfiles() inheritProfiles} + * flag of {@link ActiveProfiles @ActiveProfiles} will be taken into + * consideration. Specifically, if the inheritProfiles flag is + * set to true, profiles defined in the annotated class will be + * merged with those defined in superclasses. * - * @param clazz - * @return + * @param clazz the class for which to resolve the active profiles (must + * not be null) + * @return the set of active profiles for the specified class, including + * active profiles from superclasses if appropriate (never null) */ static String[] resolveActiveProfiles(Class clazz) { Assert.notNull(clazz, "Class must not be null"); - Class annotationType = ActivateProfiles.class; + Class annotationType = ActiveProfiles.class; Class declaringClass = AnnotationUtils.findAnnotationDeclaringClass(annotationType, clazz); if (declaringClass == null && logger.isDebugEnabled()) { @@ -232,20 +246,20 @@ abstract class ContextLoaderUtils { final Set activeProfiles = new LinkedHashSet(); while (declaringClass != null) { - ActivateProfiles activateProfiles = declaringClass.getAnnotation(annotationType); + ActiveProfiles annotation = declaringClass.getAnnotation(annotationType); if (logger.isTraceEnabled()) { - logger.trace(String.format("Retrieved @ActivateProfiles [%s] for declaring class [%s].", - activateProfiles, declaringClass)); + logger.trace(String.format("Retrieved @ActiveProfiles [%s] for declaring class [%s].", activeProfiles, + declaringClass)); } - String[] profiles = activateProfiles.profiles(); - String[] valueProfiles = activateProfiles.value(); + String[] profiles = annotation.profiles(); + String[] valueProfiles = annotation.value(); if (!ObjectUtils.isEmpty(valueProfiles) && !ObjectUtils.isEmpty(profiles)) { - String msg = String.format("Test class [%s] has been configured with @ActivateProfiles' 'value' [%s] " - + "and 'profiles' [%s] attributes. Only one declaration of bean " - + "definition profiles is permitted per @ActivateProfiles annotation.", declaringClass, + String msg = String.format("Test class [%s] has been configured with @ActiveProfiles' 'value' [%s] " + + "and 'profiles' [%s] attributes. Only one declaration of active bean " + + "definition profiles is permitted per @ActiveProfiles annotation.", declaringClass, ObjectUtils.nullSafeToString(valueProfiles), ObjectUtils.nullSafeToString(profiles)); logger.error(msg); throw new IllegalStateException(msg); @@ -260,7 +274,7 @@ abstract class ContextLoaderUtils { } } - declaringClass = activateProfiles.inheritProfiles() ? AnnotationUtils.findAnnotationDeclaringClass( + declaringClass = annotation.inheritProfiles() ? AnnotationUtils.findAnnotationDeclaringClass( annotationType, declaringClass.getSuperclass()) : null; } diff --git a/org.springframework.test/src/test/java/org/springframework/test/context/ContextLoaderUtilsTests.java b/org.springframework.test/src/test/java/org/springframework/test/context/ContextLoaderUtilsTests.java index def732642b6..22d8433d9ea 100644 --- a/org.springframework.test/src/test/java/org/springframework/test/context/ContextLoaderUtilsTests.java +++ b/org.springframework.test/src/test/java/org/springframework/test/context/ContextLoaderUtilsTests.java @@ -108,30 +108,30 @@ public class ContextLoaderUtilsTests { private static class Enigma { } - @ActivateProfiles + @ActiveProfiles private static class NoProfilesDeclared { } - @ActivateProfiles({ " ", "\t" }) + @ActiveProfiles({ " ", "\t" }) private static class EmptyProfiles { } - @ActivateProfiles({ "foo", "bar", "foo", "bar", "baz" }) + @ActiveProfiles({ "foo", "bar", "foo", "bar", "baz" }) private static class DuplicatedProfiles { } - @ActivateProfiles(profiles = "foo") + @ActiveProfiles(profiles = "foo") private static class Foo { } private static class InheritedFoo extends Foo { } - @ActivateProfiles("bar") + @ActiveProfiles("bar") private static class Bar extends Foo { } - @ActivateProfiles(profiles = { "dog", "cat" }, inheritProfiles = false) + @ActiveProfiles(profiles = { "dog", "cat" }, inheritProfiles = false) private static class Animals extends Bar { }