class ContextCache
extends java.lang.Object
ApplicationContexts
in a test environment.
Maintains a cache of ApplicationContexts
keyed by
MergedContextConfiguration
instances.
This has significant performance benefits if initializing the context would take time.
While initializing a Spring context itself is very quick, some beans in a context, such
as a LocalSessionFactoryBean
for working with Hibernate, may take some time to
initialize. Hence it often makes sense to perform that initialization only once per
test suite.
Modifier and Type | Field and Description |
---|---|
private java.util.Map<MergedContextConfiguration,ApplicationContext> |
contextMap
Map of context keys to Spring
ApplicationContext instances. |
private java.util.Map<MergedContextConfiguration,java.util.Set<MergedContextConfiguration>> |
hierarchyMap
Map of parent keys to sets of children keys, representing a top-down tree
of context hierarchies.
|
private java.util.concurrent.atomic.AtomicInteger |
hitCount |
private java.util.concurrent.atomic.AtomicInteger |
missCount |
Constructor and Description |
---|
ContextCache() |
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clear all contexts from the cache and clear context hierarchy information as well.
|
void |
clearStatistics()
Clear hit and miss count statistics for the cache (i.e., reset counters to zero).
|
boolean |
contains(MergedContextConfiguration key)
Determine whether there is a cached context for the given key.
|
ApplicationContext |
get(MergedContextConfiguration key)
Obtain a cached
ApplicationContext for the given key. |
int |
getHitCount()
Get the overall hit count for this cache.
|
int |
getMissCount()
Get the overall miss count for this cache.
|
int |
getParentContextCount()
Determine the number of parent contexts currently tracked within the cache.
|
void |
put(MergedContextConfiguration key,
ApplicationContext context)
Explicitly add an
ApplicationContext instance to the cache under the given key. |
private void |
remove(java.util.List<MergedContextConfiguration> removedContexts,
MergedContextConfiguration key) |
void |
remove(MergedContextConfiguration key,
DirtiesContext.HierarchyMode hierarchyMode)
Remove the context with the given key from the cache and explicitly
close it if it is an
instance of
ConfigurableApplicationContext . |
int |
size()
Determine the number of contexts currently stored in the cache.
|
java.lang.String |
toString()
Generate a text string, which contains the size() as well
as the hit, miss,
and parent context counts.
|
private final java.util.Map<MergedContextConfiguration,ApplicationContext> contextMap
ApplicationContext
instances.private final java.util.Map<MergedContextConfiguration,java.util.Set<MergedContextConfiguration>> hierarchyMap
private final java.util.concurrent.atomic.AtomicInteger hitCount
private final java.util.concurrent.atomic.AtomicInteger missCount
public void clear()
public void clearStatistics()
public boolean contains(MergedContextConfiguration key)
key
- the context key (never null
)true
if the cache contains a context with the given keypublic ApplicationContext get(MergedContextConfiguration key)
ApplicationContext
for the given key.
key
- the context key (never null
)ApplicationContext
instance, or null
if not found in the cacheremove(org.springframework.test.context.MergedContextConfiguration, org.springframework.test.annotation.DirtiesContext.HierarchyMode)
public int getHitCount()
A hit is an access to the cache, which returned a non-null context for a queried key.
public int getMissCount()
A miss is an access to the cache, which returned a null
context
for a queried key.
public void put(MergedContextConfiguration key, ApplicationContext context)
ApplicationContext
instance to the cache under the given key.key
- the context key (never null
)context
- the ApplicationContext
instance (never null
)public void remove(MergedContextConfiguration key, DirtiesContext.HierarchyMode hierarchyMode)
ConfigurableApplicationContext
.
Generally speaking, you would only call this method if you change the state of a singleton bean, potentially affecting future interaction with the context.
In addition, the semantics of the supplied HierarchyMode
will
be honored. See the Javadoc for DirtiesContext.HierarchyMode
for details.
key
- the context key; never null
hierarchyMode
- the hierarchy mode; may be null
if the context
is not part of a hierarchyprivate void remove(java.util.List<MergedContextConfiguration> removedContexts, MergedContextConfiguration key)
public int size()
If the cache contains more than Integer.MAX_VALUE
elements,
this method returns Integer.MAX_VALUE
.
public int getParentContextCount()
public java.lang.String toString()
toString
in class java.lang.Object