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 int |
hitCount |
private int |
missCount |
private java.lang.Object |
monitor |
Constructor and Description |
---|
ContextCache() |
Modifier and Type | Method and Description |
---|---|
(package private) void |
clear()
Clears all contexts from the cache and clears context hierarchy information as
well.
|
(package private) void |
clearStatistics()
Clears hit and miss count statistics for the cache (i.e., resets counters to zero).
|
(package private) boolean |
contains(MergedContextConfiguration key)
Return whether there is a cached context for the given key.
|
(package private) ApplicationContext |
get(MergedContextConfiguration key)
Obtain a cached
ApplicationContext for the given key. |
(package private) int |
getHitCount()
Get the overall hit count for this cache.
|
(package private) int |
getMissCount()
Get the overall miss count for this cache.
|
(package private) int |
getParentContextCount()
Determine the number of parent contexts currently tracked within the cache.
|
private void |
incrementHitCount()
Increment the hit count by one.
|
private void |
incrementMissCount()
Increment the miss count by one.
|
(package private) 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) |
(package private) 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 . |
(package private) int |
size()
Determine the number of contexts currently stored in the cache.
|
java.lang.String |
toString()
Generates a text string, which contains the size as well
as the hit, miss, and
parent context counts.
|
private final java.lang.Object monitor
private final java.util.Map<MergedContextConfiguration,ApplicationContext> contextMap
ApplicationContext
instances.private final java.util.Map<MergedContextConfiguration,java.util.Set<MergedContextConfiguration>> hierarchyMap
private int hitCount
private int missCount
void clear()
void clearStatistics()
boolean contains(MergedContextConfiguration key)
key
- the context key (never null
)ApplicationContext get(MergedContextConfiguration key)
ApplicationContext
for the given key.
key
- the context key (never null
)ApplicationContext
instance, or null
if
not found in the cache.remove(org.springframework.test.context.MergedContextConfiguration, org.springframework.test.annotation.DirtiesContext.HierarchyMode)
private void incrementHitCount()
private void incrementMissCount()
null
context for a queried key.int getHitCount()
int getMissCount()
null
context for a queried key.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
)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)
int size()
int getParentContextCount()
public java.lang.String toString()
toString
in class java.lang.Object