Package org.springframework.test.context.cache

# Class DefaultCacheAwareContextLoaderDelegate

java.lang.Object
org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate

All Implemented Interfaces:
  `CacheAwareContextLoaderDelegate`

public class DefaultCacheAwareContextLoaderDelegate extends Object implements CacheAwareContextLoaderDelegate
Default implementation of the `CacheAwareContextLoaderDelegate` strategy.

To use a static `DefaultContextCache`, invoke the `DefaultCacheAwareContextLoaderDelegate()` constructor; otherwise, invoke the `DefaultCacheAwareContextLoaderDelegate(ContextCache)` and provide a custom `ContextCache` implementation.

As of Spring Framework 6.0, this class loads `ApplicationContextFailureProcessor` implementations via the `SpringFactoriesLoader` mechanism and delegates to them in `loadContext(MergedContextConfiguration)` to process context load failures.

As of Spring Framework 6.1, this class supports the failure threshold feature described in `CacheAwareContextLoaderDelegate.loadContext(org.springframework.test.context.MergedContextConfiguration)`, delegating to `ContextCacheUtils.retrieveContextFailureThreshold()` to obtain the threshold value to use.

Since:
  4.1
Author:
  Sam Brannen

## Field Summary

### Fields inherited from interface org.springframework.test.context.CacheAwareContextLoaderDelegate

`CONTEXT_FAILURE_THRESHOLD_PROPERTY_NAME, DEFAULT_CONTEXT_FAILURE_THRESHOLD`

## Constructor Summary

Constructors
Constructor
Description
`DefaultCacheAwareContextLoaderDelegate()`
Construct a new `DefaultCacheAwareContextLoaderDelegate` using a static `DefaultContextCache`.
`DefaultCacheAwareContextLoaderDelegate(ContextCache contextCache)`
Construct a new `DefaultCacheAwareContextLoaderDelegate` using the supplied `ContextCache` and the default or user-configured context failure threshold.

## Method Summary

Modifier and Type
Method
Description
`void`
`closeContext(MergedContextConfiguration mergedConfig, DirtiesContext.HierarchyMode hierarchyMode)`
Remove the application context for the supplied `MergedContextConfiguration` from the `ContextCache` and close it if it is an instance of `ConfigurableApplicationContext`.
`protected ContextCache`
`getContextCache()`
Get the `ContextCache` used by this context loader delegate.
`boolean`
`isContextLoaded(MergedContextConfiguration mergedConfig)`
Determine if the application context for the supplied `MergedContextConfiguration` has been loaded (i.e., is present in the `ContextCache`).
`org.springframework.context.ApplicationContext`
`loadContext(MergedContextConfiguration mergedConfig)`
Load the application context for the supplied `MergedContextConfiguration` by delegating to the `ContextLoader` configured in the given `MergedContextConfiguration`.
`protected org.springframework.context.ApplicationContext`
`loadContextInAotMode(org.springframework.test.context.cache.AotMergedContextConfiguration aotMergedConfig)`
`protected org.springframework.context.ApplicationContext`
`loadContextInternal(MergedContextConfiguration mergedConfig)`
Load the `ApplicationContext` for the supplied merged context configuration.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### DefaultCacheAwareContextLoaderDelegate

public DefaultCacheAwareContextLoaderDelegate()
Construct a new `DefaultCacheAwareContextLoaderDelegate` using a static `DefaultContextCache`.

The default cache is static so that each context can be cached and reused for all subsequent tests that declare the same unique context configuration within the same JVM process.

See Also:
  - `DefaultCacheAwareContextLoaderDelegate(ContextCache)`

### DefaultCacheAwareContextLoaderDelegate

public DefaultCacheAwareContextLoaderDelegate(ContextCache contextCache)
Construct a new `DefaultCacheAwareContextLoaderDelegate` using the supplied `ContextCache` and the default or user-configured context failure threshold.

See Also:
  - `DefaultCacheAwareContextLoaderDelegate()`
  - `ContextCacheUtils.retrieveContextFailureThreshold()`

## Method Details

### isContextLoaded

public boolean isContextLoaded(MergedContextConfiguration mergedConfig)
Description copied from interface: `CacheAwareContextLoaderDelegate`
Determine if the application context for the supplied `MergedContextConfiguration` has been loaded (i.e., is present in the `ContextCache`).

Implementations of this method must not load the application context as a side effect. In addition, implementations of this method should not log the cache statistics via `ContextCache.logStatistics()`.

The default implementation of this method always returns `false`. Custom `CacheAwareContextLoaderDelegate` implementations are therefore highly encouraged to override this method with a more meaningful implementation. Note that the standard `CacheAwareContextLoaderDelegate` implementation in Spring overrides this method appropriately.

Specified by:
  `isContextLoaded` in interface `CacheAwareContextLoaderDelegate`
Parameters:
  `mergedConfig` - the merged context configuration used to load the application context; never `null`
Returns:
  `true` if the application context has been loaded
See Also:
  - `CacheAwareContextLoaderDelegate.loadContext(org.springframework.test.context.MergedContextConfiguration)`
  - `CacheAwareContextLoaderDelegate.closeContext(org.springframework.test.context.MergedContextConfiguration, org.springframework.test.annotation.DirtiesContext.HierarchyMode)`

### loadContext

public org.springframework.context.ApplicationContext loadContext(MergedContextConfiguration mergedConfig)
Description copied from interface: `CacheAwareContextLoaderDelegate`
Load the application context for the supplied `MergedContextConfiguration` by delegating to the `ContextLoader` configured in the given `MergedContextConfiguration`.

If the context is present in the `ContextCache` it will simply be returned; otherwise, it will be loaded, stored in the cache, and returned.

As of Spring Framework 6.0, implementations of this method should load `ApplicationContextFailureProcessor` implementations via the `SpringFactoriesLoader` mechanism, catch any exception thrown by the `ContextLoader`, and delegate to each of the configured failure processors to process the context load failure if the exception is an instance of `ContextLoadException`.

As of Spring Framework 6.1, implementations of this method are encouraged to support the failure threshold feature. Specifically, if repeated attempts are made to load an application context and that application context consistently fails to load — for example, due to a configuration error that prevents the context from successfully loading — this method should preemptively throw an `IllegalStateException` if the configured failure threshold has been exceeded. Note that the `ContextCache` provides support for tracking and incrementing the failure count for a given context cache key.

The cache statistics should be logged by invoking `ContextCache.logStatistics()`.

Specified by:
  `loadContext` in interface `CacheAwareContextLoaderDelegate`
Parameters:
  `mergedConfig` - the merged context configuration to use to load the application context; never `null`
Returns:
  the application context (never `null`)
See Also:
  - `CacheAwareContextLoaderDelegate.isContextLoaded(org.springframework.test.context.MergedContextConfiguration)`
  - `CacheAwareContextLoaderDelegate.closeContext(org.springframework.test.context.MergedContextConfiguration, org.springframework.test.annotation.DirtiesContext.HierarchyMode)`
  - `CacheAwareContextLoaderDelegate.CONTEXT_FAILURE_THRESHOLD_PROPERTY_NAME`

### closeContext

public void closeContext(MergedContextConfiguration mergedConfig, @Nullable DirtiesContext.HierarchyMode hierarchyMode)
Description copied from interface: `CacheAwareContextLoaderDelegate`
Remove the application context for the supplied `MergedContextConfiguration` from the `ContextCache` and close it if it is an instance of `ConfigurableApplicationContext`.

The semantics of the supplied `HierarchyMode` must be honored when removing the context from the cache. See the Javadoc for `DirtiesContext.HierarchyMode` for details.

Generally speaking, this method should only be called if the state of a singleton bean has been changed (potentially affecting future interaction with the context) or if the context needs to be prematurely removed from the cache.

Specified by:
  `closeContext` in interface `CacheAwareContextLoaderDelegate`
Parameters:
  `mergedConfig` - the merged context configuration for the application context to close; never `null`
  `hierarchyMode` - the hierarchy mode; may be `null` if the context is not part of a hierarchy
See Also:
  - `CacheAwareContextLoaderDelegate.isContextLoaded(org.springframework.test.context.MergedContextConfiguration)`
  - `CacheAwareContextLoaderDelegate.loadContext(org.springframework.test.context.MergedContextConfiguration)`

### getContextCache

protected ContextCache getContextCache()
Get the `ContextCache` used by this context loader delegate.

### loadContextInternal

protected org.springframework.context.ApplicationContext loadContextInternal(MergedContextConfiguration mergedConfig) throws Exception
Load the `ApplicationContext` for the supplied merged context configuration.

Supports both the `SmartContextLoader` and `ContextLoader` SPIs.

Throws:
  `Exception` - if an error occurs while loading the application context

### loadContextInAotMode

protected org.springframework.context.ApplicationContext loadContextInAotMode(org.springframework.test.context.cache.AotMergedContextConfiguration aotMergedConfig) throws Exception

Throws:
  `Exception`