org.springframework.test.context
Class ContextCache

java.lang.Object
  extended by org.springframework.test.context.ContextCache

 class ContextCache
extends java.lang.Object

Cache for Spring ApplicationContexts in a test environment.

Maintains a cache of contexts by serializable key. 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 once.

Since:
2.5
Author:
Sam Brannen, Juergen Hoeller

Field Summary
private  java.util.Map<java.lang.String,ApplicationContext> contextKeyToContextMap
          Map of context keys to Spring ApplicationContext instances.
private  int hitCount
           
private  int missCount
           
 
Constructor Summary
ContextCache()
           
 
Method Summary
(package private)  void clear()
          Clears all contexts from the cache.
(package private)  void clearStatistics()
          Clears hit and miss count statistics for the cache (i.e., resets counters to zero).
(package private)  boolean contains(java.lang.String key)
          Return whether there is a cached context for the given key.
(package private)  ApplicationContext get(java.lang.String 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.
private  void incrementHitCount()
          Increment the hit count by one.
private  void incrementMissCount()
          Increment the miss count by one.
(package private)  void put(java.lang.String key, ApplicationContext context)
          Explicitly add a ApplicationContext instance to the cache under the given key.
(package private)  ApplicationContext remove(java.lang.String key)
          Remove the context with the given key.
(package private)  void setDirty(java.lang.String key)
          Mark the context with the given key as dirty, effectively removing the context from the cache and explicitly closing 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 and miss counts.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

contextKeyToContextMap

private final java.util.Map<java.lang.String,ApplicationContext> contextKeyToContextMap
Map of context keys to Spring ApplicationContext instances.


hitCount

private int hitCount

missCount

private int missCount
Constructor Detail

ContextCache

ContextCache()
Method Detail

clear

void clear()
Clears all contexts from the cache.


clearStatistics

void clearStatistics()
Clears hit and miss count statistics for the cache (i.e., resets counters to zero).


contains

boolean contains(java.lang.String key)
Return whether there is a cached context for the given key.

Parameters:
key - the context key (never null)

get

ApplicationContext get(java.lang.String key)
Obtain a cached ApplicationContext for the given key.

The hit and miss counts will be updated accordingly.

Parameters:
key - the context key (never null)
Returns:
the corresponding ApplicationContext instance, or null if not found in the cache.
See Also:
remove(java.lang.String)

incrementHitCount

private void incrementHitCount()
Increment the hit count by one. A hit is an access to the cache, which returned a non-null context for a queried key.


incrementMissCount

private void incrementMissCount()
Increment the miss count by one. A miss is an access to the cache, which returned a null context for a queried key.


getHitCount

int getHitCount()
Get the overall hit count for this cache. A hit is an access to the cache, which returned a non-null context for a queried key.


getMissCount

int getMissCount()
Get the overall miss count for this cache. A miss is an access to the cache, which returned a null context for a queried key.


put

void put(java.lang.String key,
         ApplicationContext context)
Explicitly add a ApplicationContext instance to the cache under the given key.

Parameters:
key - the context key (never null)
context - the ApplicationContext instance (never null)

remove

ApplicationContext remove(java.lang.String key)
Remove the context with the given key.

Parameters:
key - the context key (never null)
Returns:
the corresponding ApplicationContext instance, or null if not found in the cache.
See Also:
setDirty(java.lang.String)

setDirty

void setDirty(java.lang.String key)
Mark the context with the given key as dirty, effectively removing the context from the cache and explicitly closing it if it is an instance of ConfigurableApplicationContext.

Generally speaking, you would only call this method only if you change the state of a singleton bean, potentially affecting future interaction with the context.

Parameters:
key - the context key (never null)
See Also:
remove(java.lang.String)

size

int size()
Determine the number of contexts currently stored in the cache. If the cache contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.


toString

public java.lang.String toString()
Generates a text string, which contains the size as well as the hit and miss counts.

Overrides:
toString in class java.lang.Object