org.springframework.test.context
Class ContextLoaderUtils

java.lang.Object
  extended by org.springframework.test.context.ContextLoaderUtils

abstract class ContextLoaderUtils
extends java.lang.Object

Utility methods for working with ContextLoaders and SmartContextLoaders and resolving resource locations, configuration classes, and active bean definition profiles.

Since:
3.1
Author:
Sam Brannen
See Also:
ContextLoader, SmartContextLoader, ContextConfiguration, ContextConfigurationAttributes, ActiveProfiles, MergedContextConfiguration

Field Summary
private static java.lang.String DEFAULT_CONTEXT_LOADER_CLASS_NAME
           
private static Log logger
           
 
Constructor Summary
private ContextLoaderUtils()
           
 
Method Summary
(package private) static MergedContextConfiguration buildMergedContextConfiguration(java.lang.Class<?> testClass, java.lang.String defaultContextLoaderClassName)
          Build the merged context configuration for the supplied testClass and defaultContextLoaderClassName.
(package private) static java.lang.String[] resolveActiveProfiles(java.lang.Class<?> clazz)
          Resolve active bean definition profiles for the supplied Class.
(package private) static java.util.List<ContextConfigurationAttributes> resolveContextConfigurationAttributes(java.lang.Class<?> clazz)
          Resolve the list of configuration attributes for the supplied class and its superclasses.
(package private) static ContextLoader resolveContextLoader(java.lang.Class<?> testClass, java.lang.String defaultContextLoaderClassName)
          Resolve the ContextLoader class to use for the supplied testClass and then instantiate and return that ContextLoader.
(package private) static java.lang.Class<? extends ContextLoader> resolveContextLoaderClass(java.lang.Class<?> testClass, java.lang.String defaultContextLoaderClassName)
          Resolve the ContextLoader Class to use for the supplied testClass.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

private static final Log logger

DEFAULT_CONTEXT_LOADER_CLASS_NAME

private static final java.lang.String DEFAULT_CONTEXT_LOADER_CLASS_NAME
See Also:
Constant Field Values
Constructor Detail

ContextLoaderUtils

private ContextLoaderUtils()
Method Detail

resolveContextLoader

static ContextLoader resolveContextLoader(java.lang.Class<?> testClass,
                                          java.lang.String defaultContextLoaderClassName)
Resolve the ContextLoader class to use for the supplied testClass and then instantiate and return that ContextLoader.

If the supplied defaultContextLoaderClassName is null or empty, the standard default context loader class name "org.springframework.test.context.support.DelegatingSmartContextLoader" will be used. For details on the class resolution process, see #resolveContextLoaderClass().

Parameters:
testClass - the test class for which the ContextLoader should be resolved (must not be null)
defaultContextLoaderClassName - the name of the default ContextLoader class to use (may be null)
Returns:
the resolved ContextLoader for the supplied testClass (never null)
See Also:
#resolveContextLoaderClass()

resolveContextLoaderClass

static java.lang.Class<? extends ContextLoader> resolveContextLoaderClass(java.lang.Class<?> testClass,
                                                                          java.lang.String defaultContextLoaderClassName)
Resolve the ContextLoader Class to use for the supplied testClass.
  1. If the loader attribute of @ContextConfiguration is configured with an explicit class, that class will be returned.
  2. If a loader class is not specified, the class hierarchy will be traversed to find a parent class annotated with @ContextConfiguration; go to step #1.
  3. If no explicit loader class is found after traversing the class hierarchy, an attempt will be made to load and return the class with the supplied defaultContextLoaderClassName.

Parameters:
testClass - the class for which to resolve the ContextLoader class; must not be null
defaultContextLoaderClassName - the name of the default ContextLoader class to use; must not be null or empty
Returns:
the ContextLoader class to use for the supplied test class
Throws:
java.lang.IllegalArgumentException - if @ContextConfiguration is not present on the supplied test class
java.lang.IllegalStateException - if the default ContextLoader class could not be loaded

resolveContextConfigurationAttributes

static java.util.List<ContextConfigurationAttributes> resolveContextConfigurationAttributes(java.lang.Class<?> clazz)
Resolve the list of configuration attributes for the supplied class and its superclasses.

Note that the inheritLocations flag of @ContextConfiguration will be taken into consideration. Specifically, if the inheritLocations flag is set to true, configuration attributes defined in the annotated class will be appended to the configuration attributes defined in superclasses.

Parameters:
clazz - the class for which to resolve the configuration attributes (must not be null)
Returns:
the list of configuration attributes for the specified class, including configuration attributes from superclasses if appropriate (never null)
Throws:
java.lang.IllegalArgumentException - if the supplied class is null or if @ContextConfiguration is not present on the supplied class

resolveActiveProfiles

static java.lang.String[] resolveActiveProfiles(java.lang.Class<?> clazz)
Resolve active bean definition profiles for the supplied Class.

Note that the inheritProfiles flag of @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.

Parameters:
clazz - the class for which to resolve the active profiles (must not be null)
Returns:
the set of active profiles for the specified class, including active profiles from superclasses if appropriate (never null)
See Also:
ActiveProfiles, Profile

buildMergedContextConfiguration

static MergedContextConfiguration buildMergedContextConfiguration(java.lang.Class<?> testClass,
                                                                  java.lang.String defaultContextLoaderClassName)
Build the merged context configuration for the supplied testClass and defaultContextLoaderClassName.

Parameters:
testClass - the test class for which the MergedContextConfiguration should be built (must not be null)
defaultContextLoaderClassName - the name of the default ContextLoader class to use (may be null)
Returns:
the merged context configuration
See Also:
#resolveContextLoader(), #resolveContextConfigurationAttributes(), SmartContextLoader#processContextConfiguration(), ContextLoader#processLocations(), #resolveActiveProfiles(), MergedContextConfiguration