[SPR-8386] Completed JavaDoc for ContextLoaderUtils regarding the SmartContextLoader SPI.

This commit is contained in:
Sam Brannen 2011-06-20 19:44:30 +00:00
parent 40fb1b21e1
commit 102674be20
1 changed files with 80 additions and 72 deletions

View File

@ -32,12 +32,14 @@ import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
* Utility methods for working with {@link ContextLoader ContextLoaders}, * Utility methods for working with {@link ContextLoader ContextLoaders} and
* resource locations and classes, and active bean definition profiles. * {@link SmartContextLoader SmartContextLoaders} and resolving resource locations,
* configuration classes, and active bean definition profiles.
* *
* @author Sam Brannen * @author Sam Brannen
* @since 3.1 * @since 3.1
* @see ContextLoader * @see ContextLoader
* @see SmartContextLoader
* @see ContextConfiguration * @see ContextConfiguration
* @see ContextConfigurationAttributes * @see ContextConfigurationAttributes
* @see ActiveProfiles * @see ActiveProfiles
@ -47,11 +49,31 @@ abstract class ContextLoaderUtils {
private static final Log logger = LogFactory.getLog(ContextLoaderUtils.class); private static final Log logger = LogFactory.getLog(ContextLoaderUtils.class);
private static final String STANDARD_DEFAULT_CONTEXT_LOADER_CLASS_NAME = "org.springframework.test.context.support.GenericXmlContextLoader"; private static final String DEFAULT_CONTEXT_LOADER_CLASS_NAME = "org.springframework.test.context.support.GenericXmlContextLoader";
private ContextLoaderUtils() {
/* no-op */
}
/** /**
* TODO Document resolveContextConfigurationAttributes(). * Resolve the list of {@link ContextConfigurationAttributes configuration
* attributes} for the supplied {@link Class class} and its superclasses.
* <p>Note that the {@link ContextConfiguration#inheritLocations
* inheritLocations} flag of {@link ContextConfiguration
* &#064;ContextConfiguration} will be taken into consideration.
* Specifically, if the <code>inheritLocations</code> flag is set to
* <code>true</code>, configuration attributes defined in the annotated
* class will be appended to the configuration attributes defined in
* superclasses.
* @param clazz the class for which to resolve the configuration attributes (must
* not be <code>null</code>)
* @return the list of configuration attributes for the specified class,
* including configuration attributes from superclasses if appropriate
* (never <code>null</code>)
* @throws IllegalArgumentException if the supplied class is <code>null</code>
* or if {@link ContextConfiguration &#064;ContextConfiguration} is not
* <em>present</em> on the supplied class
*/ */
static List<ContextConfigurationAttributes> resolveContextConfigurationAttributes(Class<?> clazz) { static List<ContextConfigurationAttributes> resolveContextConfigurationAttributes(Class<?> clazz) {
Assert.notNull(clazz, "Class must not be null"); Assert.notNull(clazz, "Class must not be null");
@ -88,25 +110,24 @@ abstract class ContextLoaderUtils {
} }
/** /**
* Resolves the {@link ContextLoader} {@link Class} to use for the * Resolve the {@link ContextLoader} {@link Class class} to use for the
* supplied {@link Class testClass} and then instantiates and returns * supplied {@link Class testClass} and {@link ContextConfigurationAttributes}
* that <code>ContextLoader</code>. * and then instantiate and return that {@code ContextLoader}.
*
* <p>If the supplied <code>defaultContextLoaderClassName</code> is * <p>If the supplied <code>defaultContextLoaderClassName</code> is
* <code>null</code> or <em>empty</em>, the <em>standard</em> * <code>null</code> or <em>empty</em>, the <em>standard</em>
* default context loader class name ({@value #STANDARD_DEFAULT_CONTEXT_LOADER_CLASS_NAME}) * default context loader class name {@value #DEFAULT_CONTEXT_LOADER_CLASS_NAME}
* will be used. For details on the class resolution process, see * will be used. For details on the class resolution process, see
* {@link #resolveContextLoaderClass(Class, String)}. * {@link #resolveContextLoaderClass()}.
* * @param testClass the test class for which the {@code ContextLoader}
* @param testClass the test class for which the <code>ContextLoader</code>
* should be resolved (must not be <code>null</code>) * should be resolved (must not be <code>null</code>)
* @param configAttributesList TODO Document configAttributesList parameter * @param configAttributesList the resolved configuration attributes for the
* test class hierarchy
* @param defaultContextLoaderClassName the name of the default * @param defaultContextLoaderClassName the name of the default
* <code>ContextLoader</code> class to use (may be <code>null</code>) * {@code ContextLoader} class to use (may be <code>null</code>)
* * @return the resolved {@code ContextLoader} for the supplied
* @return the resolved <code>ContextLoader</code> for the supplied
* <code>testClass</code> (never <code>null</code>) * <code>testClass</code> (never <code>null</code>)
* @see #resolveContextLoaderClass(Class, String) * @see #resolveContextLoaderClass()
* @see #resolveContextConfigurationAttributes()
*/ */
static ContextLoader resolveContextLoader(Class<?> testClass, static ContextLoader resolveContextLoader(Class<?> testClass,
List<ContextConfigurationAttributes> configAttributesList, String defaultContextLoaderClassName) { List<ContextConfigurationAttributes> configAttributesList, String defaultContextLoaderClassName) {
@ -114,7 +135,7 @@ abstract class ContextLoaderUtils {
Assert.notEmpty(configAttributesList, "ContextConfigurationAttributes list must not be null or empty"); Assert.notEmpty(configAttributesList, "ContextConfigurationAttributes list must not be null or empty");
if (!StringUtils.hasText(defaultContextLoaderClassName)) { if (!StringUtils.hasText(defaultContextLoaderClassName)) {
defaultContextLoaderClassName = STANDARD_DEFAULT_CONTEXT_LOADER_CLASS_NAME; defaultContextLoaderClassName = DEFAULT_CONTEXT_LOADER_CLASS_NAME;
} }
Class<? extends ContextLoader> contextLoaderClass = resolveContextLoaderClass(testClass, configAttributesList, Class<? extends ContextLoader> contextLoaderClass = resolveContextLoaderClass(testClass, configAttributesList,
@ -124,31 +145,33 @@ abstract class ContextLoaderUtils {
} }
/** /**
* Resolves the {@link ContextLoader} {@link Class} to use for the supplied * Resolve the {@link ContextLoader} {@link Class} to use for the supplied
* {@link Class test class}. * {@link ContextConfigurationAttributes} list.
* * <p>This method will iterate over the supplied configuration attributes
* and execute the following algorithm:
* <ol> * <ol>
* <li>If the {@link ContextConfiguration#loader() loader} attribute of * <li>If {@link ContextConfigurationAttributes#getContextLoaderClass()}
* {@link ContextConfiguration &#064;ContextConfiguration} is configured * returns an explicit implementation class, that class will be returned.</li>
* with an explicit class, that class will be returned.</li> * <li>If an explicit {@code ContextLoader} implementation class is not
* <li>If a <code>loader</code> class is not specified, the class hierarchy * specified, the next {@link ContextConfigurationAttributes} instance in
* will be traversed to find a parent class annotated with * the supplied list will be processed; go to step #1.</li>
* <code>&#064;ContextConfiguration</code>; go to step #1.</li> * <li>If no explicit <code>loader</code> class is found after processing
* <li>If no explicit <code>loader</code> class is found after traversing * all {@link ContextConfigurationAttributes} instances, an attempt will be
* the class hierarchy, an attempt will be made to load and return the class * made to load and return the class with the supplied
* with the supplied <code>defaultContextLoaderClassName</code>.</li> * <code>defaultContextLoaderClassName</code>.</li>
* </ol> * </ol>
* * @param testClass the class for which to resolve the {@code ContextLoader}
* @param testClass the class for which to resolve the <code>ContextLoader</code> * class; used solely for logging purposes; must not be <code>null</code>
* class; must not be <code>null</code> * @param configAttributesList the resolved configuration attributes for the
* @param configAttributesList TODO Document configAttributesList parameter * test class hierarchy; must not be <code>null</code> or empty
* @param defaultContextLoaderClassName the name of the default * @param defaultContextLoaderClassName the name of the default
* <code>ContextLoader</code> class to use; must not be <code>null</code> or empty * {@code ContextLoader} class to use; must not be <code>null</code> or empty
* * @return the {@code ContextLoader} class to use for the specified class
* @return the <code>ContextLoader</code> class to use for the specified class
* (never <code>null</code>) * (never <code>null</code>)
* @throws IllegalArgumentException if {@link ContextConfiguration * @throws IllegalArgumentException if {@code @ContextConfiguration} is not
* &#064;ContextConfiguration} is not <em>present</em> on the supplied class * <em>present</em> on the supplied test class
* @see #resolveContextLoader()
* @see #resolveContextConfigurationAttributes()
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
static Class<? extends ContextLoader> resolveContextLoaderClass(Class<?> testClass, static Class<? extends ContextLoader> resolveContextLoaderClass(Class<?> testClass,
@ -191,20 +214,18 @@ abstract class ContextLoaderUtils {
} }
/** /**
* Resolves <em>active bean definition profiles</em> for the supplied * Resolve <em>active bean definition profiles</em> for the supplied {@link Class}.
* {@link Class class}. * <p>Note that the {@link ActiveProfiles#inheritProfiles inheritProfiles}
*
* <p>Note that the {@link ActiveProfiles#inheritProfiles() inheritProfiles}
* flag of {@link ActiveProfiles &#064;ActiveProfiles} will be taken into * flag of {@link ActiveProfiles &#064;ActiveProfiles} will be taken into
* consideration. Specifically, if the <code>inheritProfiles</code> flag is * consideration. Specifically, if the <code>inheritProfiles</code> flag is
* set to <code>true</code>, profiles defined in the annotated class will be * set to <code>true</code>, profiles defined in the annotated class will be
* merged with those defined in superclasses. * merged with those defined in superclasses.
*
* @param clazz the class for which to resolve the active profiles (must * @param clazz the class for which to resolve the active profiles (must
* not be <code>null</code>) * not be <code>null</code>)
*
* @return the set of active profiles for the specified class, including * @return the set of active profiles for the specified class, including
* active profiles from superclasses if appropriate (never <code>null</code>) * active profiles from superclasses if appropriate (never <code>null</code>)
* @see org.springframework.test.context.ActiveProfiles
* @see org.springframework.context.annotation.Profile
*/ */
static String[] resolveActiveProfiles(Class<?> clazz) { static String[] resolveActiveProfiles(Class<?> clazz) {
Assert.notNull(clazz, "Class must not be null"); Assert.notNull(clazz, "Class must not be null");
@ -256,34 +277,21 @@ abstract class ContextLoaderUtils {
return StringUtils.toStringArray(activeProfiles); return StringUtils.toStringArray(activeProfiles);
} }
/*
* 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.
*
* <p>Note that the {@link ContextConfiguration#inheritLocations()
* inheritLocations} flag of {@link ContextConfiguration
* &#064;ContextConfiguration} will be taken into consideration.
* Specifically, if the <code>inheritLocations</code> flag is set to
* <code>true</code>, 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 <code>null</code>)
*
* @param clazz the class for which to resolve the resource locations (must
* not be <code>null</code>)
*
* @return the list of ApplicationContext resource locations for the
* specified class, including locations from superclasses if appropriate
* (never <code>null</code>)
*
* @throws IllegalArgumentException if {@link ContextConfiguration
* &#064;ContextConfiguration} is not <em>present</em> on the supplied class
*/
/** /**
* TODO Document buildMergedContextConfiguration(). * Build the {@link MergedContextConfiguration merged context configuration}
* for the supplied {@link Class testClass} and
* <code>defaultContextLoaderClassName</code>.
* @param testClass the test class for which the {@code MergedContextConfiguration}
* should be built (must not be <code>null</code>)
* @param defaultContextLoaderClassName the name of the default
* {@code ContextLoader} class to use (may be <code>null</code>)
* @return the merged context configuration
* @see #resolveContextConfigurationAttributes()
* @see #resolveContextLoader()
* @see SmartContextLoader#processContextConfiguration()
* @see ContextLoader#processLocations()
* @see #resolveActiveProfiles()
* @see MergedContextConfiguration
*/ */
static MergedContextConfiguration buildMergedContextConfiguration(Class<?> testClass, static MergedContextConfiguration buildMergedContextConfiguration(Class<?> testClass,
String defaultContextLoaderClassName) { String defaultContextLoaderClassName) {