Class DefaultCacheAwareContextLoaderDelegate
- All Implemented Interfaces:
CacheAwareContextLoaderDelegate
CacheAwareContextLoaderDelegate
interface.
To use a static DefaultContextCache
, invoke the
DefaultCacheAwareContextLoaderDelegate()
constructor; otherwise,
invoke the DefaultCacheAwareContextLoaderDelegate(ContextCache)
and provide a custom ContextCache
implementation.
- Since:
- 4.1
- Author:
- Sam Brannen
-
Constructor Summary
ConstructorDescriptionConstruct a newDefaultCacheAwareContextLoaderDelegate
using a staticDefaultContextCache
.DefaultCacheAwareContextLoaderDelegate
(ContextCache contextCache) Construct a newDefaultCacheAwareContextLoaderDelegate
using the suppliedContextCache
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
closeContext
(MergedContextConfiguration mergedContextConfiguration, DirtiesContext.HierarchyMode hierarchyMode) Remove the application context for the suppliedMergedContextConfiguration
from theContextCache
and close it if it is an instance ofConfigurableApplicationContext
.protected ContextCache
Get theContextCache
used by this context loader delegate.boolean
isContextLoaded
(MergedContextConfiguration mergedContextConfiguration) Determine if the application context for the suppliedMergedContextConfiguration
has been loaded (i.e., is present in theContextCache
).loadContext
(MergedContextConfiguration mergedContextConfiguration) Load the application context for the suppliedMergedContextConfiguration
by delegating to theContextLoader
configured in the givenMergedContextConfiguration
.protected ApplicationContext
loadContextInAotMode
(org.springframework.test.context.cache.AotMergedContextConfiguration aotMergedConfig) protected ApplicationContext
loadContextInternal
(MergedContextConfiguration mergedContextConfiguration) Load theApplicationContext
for the supplied merged context configuration.void
setContextFailureProcessor
(ApplicationContextFailureProcessor contextFailureProcessor) Set theApplicationContextFailureProcessor
to use.
-
Constructor Details
-
DefaultCacheAwareContextLoaderDelegate
public DefaultCacheAwareContextLoaderDelegate()Construct a newDefaultCacheAwareContextLoaderDelegate
using a staticDefaultContextCache
.This 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.
-
DefaultCacheAwareContextLoaderDelegate
Construct a newDefaultCacheAwareContextLoaderDelegate
using the suppliedContextCache
.- See Also:
-
-
Method Details
-
isContextLoaded
Description copied from interface:CacheAwareContextLoaderDelegate
Determine if the application context for the suppliedMergedContextConfiguration
has been loaded (i.e., is present in theContextCache
).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
. CustomCacheAwareContextLoaderDelegate
implementations are therefore highly encouraged to override this method with a more meaningful implementation. Note that the standardCacheAwareContextLoaderDelegate
implementation in Spring overrides this method appropriately.- Specified by:
isContextLoaded
in interfaceCacheAwareContextLoaderDelegate
- Parameters:
mergedContextConfiguration
- the merged context configuration used to load the application context; nevernull
- Returns:
true
if the application context has been loaded- See Also:
-
loadContext
Description copied from interface:CacheAwareContextLoaderDelegate
Load the application context for the suppliedMergedContextConfiguration
by delegating to theContextLoader
configured in the givenMergedContextConfiguration
.If the context is present in the
ContextCache
it will simply be returned; otherwise, it will be loaded, stored in the cache, and returned.The cache statistics should be logged by invoking
ContextCache.logStatistics()
.- Specified by:
loadContext
in interfaceCacheAwareContextLoaderDelegate
- Parameters:
mergedContextConfiguration
- the merged context configuration to use to load the application context; nevernull
- 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.setContextFailureProcessor(org.springframework.test.context.ApplicationContextFailureProcessor)
-
closeContext
public void closeContext(MergedContextConfiguration mergedContextConfiguration, @Nullable DirtiesContext.HierarchyMode hierarchyMode) Description copied from interface:CacheAwareContextLoaderDelegate
Remove the application context for the suppliedMergedContextConfiguration
from theContextCache
and close it if it is an instance ofConfigurableApplicationContext
.The semantics of the supplied
HierarchyMode
must be honored when removing the context from the cache. See the Javadoc forDirtiesContext.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 interfaceCacheAwareContextLoaderDelegate
- Parameters:
mergedContextConfiguration
- the merged context configuration for the application context to close; nevernull
hierarchyMode
- the hierarchy mode; may benull
if the context is not part of a hierarchy- See Also:
-
setContextFailureProcessor
Description copied from interface:CacheAwareContextLoaderDelegate
Set theApplicationContextFailureProcessor
to use.The default implementation ignores the supplied processor.
Concrete implementations should override this method to store a reference to the supplied processor and use it to process
ContextLoadExceptions
thrown from context loaders inCacheAwareContextLoaderDelegate.loadContext(MergedContextConfiguration)
.- Specified by:
setContextFailureProcessor
in interfaceCacheAwareContextLoaderDelegate
- Parameters:
contextFailureProcessor
- the context failure processor to use
-
getContextCache
Get theContextCache
used by this context loader delegate. -
loadContextInternal
protected ApplicationContext loadContextInternal(MergedContextConfiguration mergedContextConfiguration) throws Exception Load theApplicationContext
for the supplied merged context configuration.Supports both the
SmartContextLoader
andContextLoader
SPIs.- Throws:
Exception
- if an error occurs while loading the application context
-
loadContextInAotMode
protected ApplicationContext loadContextInAotMode(org.springframework.test.context.cache.AotMergedContextConfiguration aotMergedConfig) throws Exception - Throws:
Exception
-