org.springframework.test.context.support
Class DelegatingSmartContextLoader

java.lang.Object
  extended by org.springframework.test.context.support.DelegatingSmartContextLoader
All Implemented Interfaces:
ContextLoader, SmartContextLoader

public class DelegatingSmartContextLoader
extends java.lang.Object
implements SmartContextLoader

DelegatingSmartContextLoader is an implementation of the SmartContextLoader SPI that delegates to a set of candidate SmartContextLoaders (i.e., GenericXmlContextLoader and AnnotationConfigContextLoader) to determine which context loader is appropriate for a given test class�s configuration. Each candidate is given a chance to process the ContextConfigurationAttributes for each class in the test class hierarchy that is annotated with @ContextConfiguration, and the candidate that supports the merged, processed configuration will be used to actually load the context.

Placing an empty @ContextConfiguration annotation on a test class signals that default resource locations (i.e., XML configuration files) or default configuration classes should be detected. Furthermore, if a specific ContextLoader or SmartContextLoader is not explicitly declared via @ContextConfiguration, DelegatingSmartContextLoader will be used as the default loader, thus providing automatic support for either XML configuration files or configuration classes, but not both simultaneously.

Since:
3.1
Author:
Sam Brannen
See Also:
SmartContextLoader, GenericXmlContextLoader, AnnotationConfigContextLoader

Field Summary
private  SmartContextLoader annotationConfigLoader
           
private static Log logger
           
private  SmartContextLoader xmlLoader
           
 
Constructor Summary
DelegatingSmartContextLoader()
           
 
Method Summary
private static void delegateProcessing(SmartContextLoader loader, ContextConfigurationAttributes configAttributes)
           
 ApplicationContext loadContext(MergedContextConfiguration mergedConfig)
          Delegates to an appropriate candidate SmartContextLoader to load an ApplicationContext.
 ApplicationContext loadContext(java.lang.String... locations)
          DelegatingSmartContextLoader does not support the ContextLoader.loadContext(String...) method.
private static java.lang.String name(SmartContextLoader loader)
           
 void processContextConfiguration(ContextConfigurationAttributes configAttributes)
          Delegates to candidate SmartContextLoaders to process the supplied ContextConfigurationAttributes.
 java.lang.String[] processLocations(java.lang.Class<?> clazz, java.lang.String... locations)
          DelegatingSmartContextLoader does not support the ContextLoader.processLocations(Class, String...) method.
private static boolean supports(SmartContextLoader loader, MergedContextConfiguration mergedConfig)
           
 
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

xmlLoader

private final SmartContextLoader xmlLoader

annotationConfigLoader

private final SmartContextLoader annotationConfigLoader
Constructor Detail

DelegatingSmartContextLoader

public DelegatingSmartContextLoader()
Method Detail

name

private static java.lang.String name(SmartContextLoader loader)

delegateProcessing

private static void delegateProcessing(SmartContextLoader loader,
                                       ContextConfigurationAttributes configAttributes)

supports

private static boolean supports(SmartContextLoader loader,
                                MergedContextConfiguration mergedConfig)

processContextConfiguration

public void processContextConfiguration(ContextConfigurationAttributes configAttributes)
Delegates to candidate SmartContextLoaders to process the supplied ContextConfigurationAttributes.

Delegation is based on explicit knowledge of the implementations of GenericXmlContextLoader and AnnotationConfigContextLoader. Specifically, the delegation algorithm is as follows:

Specified by:
processContextConfiguration in interface SmartContextLoader
Parameters:
configAttributes - the context configuration attributes to process
Throws:
java.lang.IllegalArgumentException - if the supplied configuration attributes are null, or if the supplied configuration attributes include both resource locations and configuration classes
java.lang.IllegalStateException - if GenericXmlContextLoader detects default configuration classes; if AnnotationConfigContextLoader detects default resource locations; if neither candidate loader detects defaults for the supplied context configuration; or if both candidate loaders detect defaults for the supplied context configuration

loadContext

public ApplicationContext loadContext(MergedContextConfiguration mergedConfig)
                               throws java.lang.Exception
Delegates to an appropriate candidate SmartContextLoader to load an ApplicationContext.

Delegation is based on explicit knowledge of the implementations of GenericXmlContextLoader and AnnotationConfigContextLoader. Specifically, the delegation algorithm is as follows:

Specified by:
loadContext in interface SmartContextLoader
Parameters:
mergedConfig - the merged context configuration to use to load the application context
Returns:
a new application context
Throws:
java.lang.IllegalArgumentException - if the supplied merged configuration is null
java.lang.IllegalStateException - if neither candidate loader is capable of loading an ApplicationContext from the supplied merged context configuration
java.lang.Exception - if context loading failed
See Also:
SmartContextLoader.processContextConfiguration(ContextConfigurationAttributes), org.springframework.context.annotation.AnnotationConfigUtils#registerAnnotationConfigProcessors(), MergedContextConfiguration.getActiveProfiles(), ConfigurableApplicationContext.getEnvironment()

processLocations

public java.lang.String[] processLocations(java.lang.Class<?> clazz,
                                           java.lang.String... locations)
DelegatingSmartContextLoader does not support the ContextLoader.processLocations(Class, String...) method. Call processContextConfiguration(ContextConfigurationAttributes) instead.

Specified by:
processLocations in interface ContextLoader
Parameters:
clazz - the class with which the locations are associated: used to determine how to process the supplied locations
locations - the unmodified locations to use for loading the application context (can be null or empty)
Returns:
an array of application context resource locations
Throws:
java.lang.UnsupportedOperationException

loadContext

public ApplicationContext loadContext(java.lang.String... locations)
                               throws java.lang.Exception
DelegatingSmartContextLoader does not support the ContextLoader.loadContext(String...) method. Call loadContext(MergedContextConfiguration) instead.

Specified by:
loadContext in interface ContextLoader
Parameters:
locations - the resource locations to use to load the application context
Returns:
a new application context
Throws:
java.lang.UnsupportedOperationException
java.lang.Exception - if context loading failed