11/*
2- * Copyright 2002-2014 the original author or authors.
2+ * Copyright 2002-2015 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
3131import org .springframework .util .Assert ;
3232
3333/**
34- * Cache for Spring {@link ApplicationContext ApplicationContexts} in a test environment.
34+ * Cache for Spring {@link ApplicationContext ApplicationContexts} in a test
35+ * environment.
3536 *
36- * <p>Maintains a cache of {@code ApplicationContexts} keyed by
37- * {@link MergedContextConfiguration} instances.
37+ * <p>{@code ContextCache} maintains a cache of {@code ApplicationContexts}
38+ * keyed by {@link MergedContextConfiguration} instances.
3839 *
39- * <p>This has significant performance benefits if initializing the context would take time.
40- * While initializing a Spring context itself is very quick, some beans in a context, such
41- * as a {@code LocalSessionFactoryBean} for working with Hibernate, may take some time to
42- * initialize. Hence it often makes sense to perform that initialization only once per
43- * test suite.
40+ * <p>Caching has significant performance benefits if initializing the context
41+ * takes a considerable about of time. Although initializing a Spring context
42+ * itself is very quick, some beans in a context, such as a
43+ * {@code LocalSessionFactoryBean} for working with Hibernate, may take some
44+ * time to initialize. Hence it often makes sense to perform that initialization
45+ * only once per test suite.
4446 *
4547 * @author Sam Brannen
4648 * @author Juergen Hoeller
@@ -67,21 +69,36 @@ class ContextCache {
6769
6870 private final AtomicInteger missCount = new AtomicInteger ();
6971
72+ /**
73+ * Reset all state maintained by this cache.
74+ * @see #clear()
75+ * @see #clearStatistics()
76+ */
77+ public void reset () {
78+ synchronized (contextMap ) {
79+ clear ();
80+ clearStatistics ();
81+ }
82+ }
7083
7184 /**
7285 * Clear all contexts from the cache and clear context hierarchy information as well.
7386 */
7487 public void clear () {
75- this .contextMap .clear ();
76- this .hierarchyMap .clear ();
88+ synchronized (contextMap ) {
89+ this .contextMap .clear ();
90+ this .hierarchyMap .clear ();
91+ }
7792 }
7893
7994 /**
8095 * Clear hit and miss count statistics for the cache (i.e., reset counters to zero).
8196 */
8297 public void clearStatistics () {
83- this .hitCount .set (0 );
84- this .missCount .set (0 );
98+ synchronized (contextMap ) {
99+ this .hitCount .set (0 );
100+ this .missCount .set (0 );
101+ }
85102 }
86103
87104 /**
@@ -117,24 +134,25 @@ public ApplicationContext get(MergedContextConfiguration key) {
117134
118135 /**
119136 * Get the overall hit count for this cache.
120- * <p>A <em>hit</em> is an access to the cache, which returned a non-null context
121- * for a queried key.
137+ * <p>A <em>hit</em> is any access to the cache that returns a non-null
138+ * context for the queried key.
122139 */
123140 public int getHitCount () {
124141 return this .hitCount .get ();
125142 }
126143
127144 /**
128145 * Get the overall miss count for this cache.
129- * <p>A <em>miss</em> is an access to the cache, which returned a {@code null} context
130- * for a queried key.
146+ * <p>A <em>miss</em> is any access to the cache that returns a {@code null}
147+ * context for the queried key.
131148 */
132149 public int getMissCount () {
133150 return this .missCount .get ();
134151 }
135152
136153 /**
137- * Explicitly add an {@code ApplicationContext} instance to the cache under the given key.
154+ * Explicitly add an {@code ApplicationContext} instance to the cache
155+ * under the given key.
138156 * @param key the context key (never {@code null})
139157 * @param context the {@code ApplicationContext} instance (never {@code null})
140158 */
@@ -240,9 +258,11 @@ public int getParentContextCount() {
240258 }
241259
242260 /**
243- * Generate a text string, which contains the {@linkplain #size} as well
244- * as the {@linkplain #getHitCount() hit}, {@linkplain #getMissCount() miss},
245- * and {@linkplain #getParentContextCount() parent context} counts.
261+ * Generate a text string containing the statistics for this cache.
262+ * <p>Specifically, the returned string contains the {@linkplain #size},
263+ * {@linkplain #getHitCount() hit count}, {@linkplain #getMissCount() miss count},
264+ * and {@linkplain #getParentContextCount() parent context count}.
265+ * @return the statistics for this cache
246266 */
247267 @ Override
248268 public String toString () {
0 commit comments