Spring Framework

org.springframework.test.context.support
Class AbstractContextLoader

java.lang.Object
  extended by org.springframework.test.context.support.AbstractContextLoader
All Implemented Interfaces:
ContextLoader, SmartContextLoader
Direct Known Subclasses:
AbstractGenericContextLoader

public abstract class AbstractContextLoader
extends java.lang.Object
implements SmartContextLoader

Abstract application context loader that provides a basis for all concrete implementations of the ContextLoader SPI. Provides a Template Method based approach for processing resource locations.

As of Spring 3.1, AbstractContextLoader also provides a basis for all concrete implementations of the SmartContextLoader SPI. For backwards compatibility with the ContextLoader SPI, processContextConfiguration(ContextConfigurationAttributes) delegates to processLocations(Class, String...).

Since:
2.5
Author:
Sam Brannen, Juergen Hoeller
See Also:
generateDefaultLocations(java.lang.Class), modifyLocations(java.lang.Class, java.lang.String...)

Constructor Summary
AbstractContextLoader()
           
 
Method Summary
protected  java.lang.String[] generateDefaultLocations(java.lang.Class<?> clazz)
          Generate the default classpath resource locations array based on the supplied class.
protected abstract  java.lang.String getResourceSuffix()
          Get the suffix to append to ApplicationContext resource locations when generating default locations.
protected  boolean isGenerateDefaultLocations()
          Determine whether or not default resource locations should be generated if the locations provided to processLocations(Class, String...) are null or empty.
protected  java.lang.String[] modifyLocations(java.lang.Class<?> clazz, java.lang.String... locations)
          Generate a modified version of the supplied locations array and return it.
 void processContextConfiguration(ContextConfigurationAttributes configAttributes)
          For backwards compatibility with the ContextLoader SPI, the default implementation simply delegates to processLocations(Class, String...), passing it the declaring class and resource locations retrieved from the supplied configuration attributes.
 java.lang.String[] processLocations(java.lang.Class<?> clazz, java.lang.String... locations)
          If the supplied locations are null or empty and isGenerateDefaultLocations() returns true, default locations will be generated for the specified class and the configured resource suffix; otherwise, the supplied locations will be modified if necessary and returned.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.springframework.test.context.SmartContextLoader
loadContext
 
Methods inherited from interface org.springframework.test.context.ContextLoader
loadContext
 

Constructor Detail

AbstractContextLoader

public AbstractContextLoader()
Method Detail

processContextConfiguration

public void processContextConfiguration(ContextConfigurationAttributes configAttributes)
For backwards compatibility with the ContextLoader SPI, the default implementation simply delegates to processLocations(Class, String...), passing it the declaring class and resource locations retrieved from the supplied configuration attributes. The processed locations are then set in the supplied configuration attributes.

Can be overridden in subclasses — for example, to process annotated classes instead of resource locations.

Specified by:
processContextConfiguration in interface SmartContextLoader
Parameters:
configAttributes - the context configuration attributes to process
Since:
3.1
See Also:
processLocations(Class, String...)

processLocations

public final java.lang.String[] processLocations(java.lang.Class<?> clazz,
                                                 java.lang.String... locations)
If the supplied locations are null or empty and isGenerateDefaultLocations() returns true, default locations will be generated for the specified class and the configured resource suffix; otherwise, the supplied locations will be modified if necessary and returned.

Specified by:
processLocations in interface ContextLoader
Parameters:
clazz - the class with which the locations are associated: to be used when generating default locations
locations - the unmodified locations to use for loading the application context (can be null or empty)
Returns:
a processed array of application context resource locations
Since:
2.5
See Also:
isGenerateDefaultLocations(), generateDefaultLocations(Class), modifyLocations(Class, String...), ContextLoader.processLocations(Class, String...), processContextConfiguration(ContextConfigurationAttributes)

generateDefaultLocations

protected java.lang.String[] generateDefaultLocations(java.lang.Class<?> clazz)
Generate the default classpath resource locations array based on the supplied class.

For example, if the supplied class is com.example.MyTest, the generated locations will contain a single string with a value of "classpath:/com/example/MyTest<suffix>", where <suffix> is the value of the resource suffix string.

As of Spring 3.1, the implementation of this method adheres to the contract defined in the SmartContextLoader SPI. Specifically, this method will preemptively verify that the generated default location actually exists. If it does not exist, this method will log a warning and return an empty array.

Subclasses can override this method to implement a different default location generation strategy.

Parameters:
clazz - the class for which the default locations are to be generated
Returns:
an array of default application context resource locations
Since:
2.5
See Also:
getResourceSuffix()

modifyLocations

protected java.lang.String[] modifyLocations(java.lang.Class<?> clazz,
                                             java.lang.String... locations)
Generate a modified version of the supplied locations array and return it.

A plain path — for example, "context.xml" — will be treated as a classpath resource that is relative to the package in which the specified class is defined. A path starting with a slash is treated as an absolute classpath location, for example: "/org/springframework/whatever/foo.xml". A path which references a URL (e.g., a path prefixed with classpath:, file:, http:, etc.) will be added to the results unchanged.

Subclasses can override this method to implement a different location modification strategy.

Parameters:
clazz - the class with which the locations are associated
locations - the resource locations to be modified
Returns:
an array of modified application context resource locations
Since:
2.5

isGenerateDefaultLocations

protected boolean isGenerateDefaultLocations()
Determine whether or not default resource locations should be generated if the locations provided to processLocations(Class, String...) are null or empty.

As of Spring 3.1, the semantics of this method have been overloaded to include detection of either default resource locations or default configuration classes. Consequently, this method can also be used to determine whether or not default configuration classes should be detected if the classes present in the configuration attributes supplied to processContextConfiguration(ContextConfigurationAttributes) are null or empty.

Can be overridden by subclasses to change the default behavior.

Returns:
always true by default
Since:
2.5

getResourceSuffix

protected abstract java.lang.String getResourceSuffix()
Get the suffix to append to ApplicationContext resource locations when generating default locations.

Must be implemented by subclasses.

Returns:
the resource suffix; should not be null or empty
Since:
2.5
See Also:
generateDefaultLocations(Class)

Spring Framework