Class AbstractContextLoader

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

public abstract class AbstractContextLoader extends 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.

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, Phillip Webb
See Also:
  • Constructor Details

    • AbstractContextLoader

      public AbstractContextLoader()
  • Method Details

    • processContextConfiguration

      public void processContextConfiguration(ContextConfigurationAttributes configAttributes)
      The default implementation processes locations analogous to processLocations(Class, String...), using the declaring class as the test class and the resource locations retrieved from the supplied configuration attributes as the locations to process. 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:
    • prepareContext

      protected void prepareContext(org.springframework.context.ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig)
      Prepare the ConfigurableApplicationContext created by this SmartContextLoader before bean definitions are read.

      The default implementation:

      • Sets the active bean definition profiles from the supplied MergedContextConfiguration in the Environment of the context.
      • Adds PropertySources for all resource locations and inlined properties from the supplied MergedContextConfiguration to the Environment of the context.
      • Determines what (if any) context initializer classes have been supplied via the MergedContextConfiguration and instantiates and invokes each with the given application context.
        • Any ApplicationContextInitializers implementing Ordered or annotated with @Order will be sorted appropriately.
      Parameters:
      context - the newly created application context
      mergedConfig - the merged context configuration
      Since:
      3.2
      See Also:
    • customizeContext

      protected void customizeContext(org.springframework.context.ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig)
      Customize the ConfigurableApplicationContext created by this ContextLoader after bean definitions have been loaded into the context but before the context has been refreshed.

      The default implementation delegates to all context customizers that have been registered with the supplied mergedConfig.

      Parameters:
      context - the newly created application context
      mergedConfig - the merged context configuration
      Since:
      4.3
    • processLocations

      public final String[] processLocations(Class<?> clazz, String... locations)
      If the supplied locations are null or empty and isGenerateDefaultLocations() returns true, default locations will be generated (i.e., detected) for the specified class and the configured resource suffixes; otherwise, the supplied locations will be modified if necessary and returned.
      Specified by:
      processLocations in interface ContextLoader
      Specified by:
      processLocations in interface SmartContextLoader
      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:
    • generateDefaultLocations

      protected String[] generateDefaultLocations(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 first configured resource suffix for which the generated location actually exists in the classpath.

      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:
    • modifyLocations

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

      The default implementation simply delegates to TestContextResourceUtils.convertToClasspathResourcePaths(java.lang.Class<?>, java.lang.String...).

      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 default resource locations should be generated if the locations provided to processLocations(Class, String...) are null or empty.

      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 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
    • getResourceSuffixes

      protected String[] getResourceSuffixes()
      Get the suffixes to append to ApplicationContext resource locations when detecting default locations.

      The default implementation simply wraps the value returned by getResourceSuffix() in a single-element array, but this can be overridden by subclasses in order to support multiple suffixes.

      Returns:
      the resource suffixes; never null or empty
      Since:
      4.1
      See Also:
    • getResourceSuffix

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

      Subclasses must provide an implementation of this method that returns a single suffix. Alternatively subclasses may provide a no-op implementation of this method and override getResourceSuffixes() in order to provide multiple custom suffixes.

      Returns:
      the resource suffix; never null or empty
      Since:
      2.5
      See Also: