public abstract class AbstractDelegatingSmartContextLoader extends Object implements SmartContextLoader
AbstractDelegatingSmartContextLoader
serves as an abstract base class
for implementations of the SmartContextLoader
SPI that delegate to a
set of candidate SmartContextLoaders (i.e., one that supports XML
configuration files or Groovy scripts and one that supports annotated classes)
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
and enclosing class hierarchy that is annotated with
@ContextConfiguration
, and the candidate that
supports the merged, processed configuration will be used to actually
load the context.
Any reference to an XML-based loader can be interpreted to mean a context loader that supports only XML configuration files or one that supports both XML configuration files and Groovy scripts simultaneously.
Placing an empty @ContextConfiguration
annotation on a test class signals
that default resource locations (e.g., XML configuration files or Groovy scripts)
or default
configuration classes
should be detected. Furthermore, if a specific ContextLoader
or
SmartContextLoader
is not explicitly declared via
@ContextConfiguration
, a concrete subclass of
AbstractDelegatingSmartContextLoader
will be used as the default loader,
thus providing automatic support for either path-based resource locations
(e.g., XML configuration files and Groovy scripts) or annotated classes,
but not both simultaneously.
As of Spring 3.2, a test class may optionally declare neither path-based resource locations nor annotated classes and instead declare only application context initializers. In such cases, an attempt will still be made to detect defaults, but their absence will not result in an exception.
SmartContextLoader
Constructor and Description |
---|
AbstractDelegatingSmartContextLoader() |
Modifier and Type | Method and Description |
---|---|
protected abstract SmartContextLoader |
getAnnotationConfigLoader()
Get the delegate
SmartContextLoader that supports annotated classes. |
protected abstract SmartContextLoader |
getXmlLoader()
Get the delegate
SmartContextLoader that supports XML configuration
files and/or Groovy scripts. |
ApplicationContext |
loadContext(MergedContextConfiguration mergedConfig)
Delegates to an appropriate candidate
SmartContextLoader to load
an ApplicationContext . |
ApplicationContext |
loadContext(String... locations)
AbstractDelegatingSmartContextLoader does not support the
ContextLoader.loadContext(String...) method. |
void |
processContextConfiguration(ContextConfigurationAttributes configAttributes)
Delegates to candidate
SmartContextLoaders to process the supplied
ContextConfigurationAttributes . |
String[] |
processLocations(Class<?> clazz,
String... locations)
AbstractDelegatingSmartContextLoader does not support the
ContextLoader.processLocations(Class, String...) method. |
public AbstractDelegatingSmartContextLoader()
protected abstract SmartContextLoader getXmlLoader()
SmartContextLoader
that supports XML configuration
files and/or Groovy scripts.protected abstract SmartContextLoader getAnnotationConfigLoader()
SmartContextLoader
that supports annotated classes.public final String[] processLocations(Class<?> clazz, @Nullable String... locations)
AbstractDelegatingSmartContextLoader
does not support the
ContextLoader.processLocations(Class, String...)
method. Call
processContextConfiguration(ContextConfigurationAttributes)
instead.processLocations
in interface ContextLoader
clazz
- the class with which the locations are associated: used to
determine how to process the supplied locationslocations
- the unmodified locations to use for loading the
application context (can be null
or empty)UnsupportedOperationException
- in this implementationpublic final ApplicationContext loadContext(String... locations) throws Exception
AbstractDelegatingSmartContextLoader
does not support the
ContextLoader.loadContext(String...)
method. Call
loadContext(MergedContextConfiguration)
instead.loadContext
in interface ContextLoader
locations
- the resource locations to use to load the application contextUnsupportedOperationException
- in this implementationException
- if context loading failedpublic void processContextConfiguration(ContextConfigurationAttributes configAttributes)
SmartContextLoaders
to process the supplied
ContextConfigurationAttributes
.
Delegation is based on explicit knowledge of the implementations of the default loaders for XML configuration files and Groovy scripts and annotated classes. Specifically, the delegation algorithm is as follows:
ContextConfigurationAttributes
are not empty, the appropriate
candidate loader will be allowed to process the configuration as is,
without any checks for detection of defaults.info
message will be logged.info
message will be logged.processContextConfiguration
in interface SmartContextLoader
configAttributes
- the context configuration attributes to processIllegalArgumentException
- if the supplied configuration attributes are
null
, or if the supplied configuration attributes include both
resource locations and annotated classesIllegalStateException
- if the XML-based loader detects default
configuration classes; if the annotation-based loader 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 configurationpublic ApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception
SmartContextLoader
to load
an ApplicationContext
.
Delegation is based on explicit knowledge of the implementations of the default loaders for XML configuration files and Groovy scripts and annotated classes. Specifically, the delegation algorithm is as follows:
MergedContextConfiguration
are not empty and the annotated classes are empty,
the XML-based loader will load the ApplicationContext
.MergedContextConfiguration
are not empty and the resource locations are empty,
the annotation-based loader will load the ApplicationContext
.loadContext
in interface SmartContextLoader
mergedConfig
- the merged context configuration to use to load the application contextIllegalArgumentException
- if the supplied merged configuration is null
IllegalStateException
- if neither candidate loader is capable of loading an
ApplicationContext
from the supplied merged context configurationException
- if context loading failedSmartContextLoader.processContextConfiguration(ContextConfigurationAttributes)
,
#registerAnnotationConfigProcessors(org.springframework.beans.factory.support.BeanDefinitionRegistry)
,
MergedContextConfiguration.getActiveProfiles()
,
ConfigurableApplicationContext.getEnvironment()