1
1
/*
2
- * Copyright 2002-2014 the original author or authors.
2
+ * Copyright 2002-2015 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
31
31
import org .springframework .util .Assert ;
32
32
33
33
/**
34
- * Cache for Spring {@link ApplicationContext ApplicationContexts} in a test environment.
34
+ * Cache for Spring {@link ApplicationContext ApplicationContexts} in a test
35
+ * environment.
35
36
*
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.
38
39
*
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.
44
46
*
45
47
* @author Sam Brannen
46
48
* @author Juergen Hoeller
@@ -67,21 +69,36 @@ class ContextCache {
67
69
68
70
private final AtomicInteger missCount = new AtomicInteger ();
69
71
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
+ }
70
83
71
84
/**
72
85
* Clear all contexts from the cache and clear context hierarchy information as well.
73
86
*/
74
87
public void clear () {
75
- this .contextMap .clear ();
76
- this .hierarchyMap .clear ();
88
+ synchronized (contextMap ) {
89
+ this .contextMap .clear ();
90
+ this .hierarchyMap .clear ();
91
+ }
77
92
}
78
93
79
94
/**
80
95
* Clear hit and miss count statistics for the cache (i.e., reset counters to zero).
81
96
*/
82
97
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
+ }
85
102
}
86
103
87
104
/**
@@ -117,24 +134,25 @@ public ApplicationContext get(MergedContextConfiguration key) {
117
134
118
135
/**
119
136
* 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.
122
139
*/
123
140
public int getHitCount () {
124
141
return this .hitCount .get ();
125
142
}
126
143
127
144
/**
128
145
* 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.
131
148
*/
132
149
public int getMissCount () {
133
150
return this .missCount .get ();
134
151
}
135
152
136
153
/**
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.
138
156
* @param key the context key (never {@code null})
139
157
* @param context the {@code ApplicationContext} instance (never {@code null})
140
158
*/
@@ -240,9 +258,11 @@ public int getParentContextCount() {
240
258
}
241
259
242
260
/**
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
246
266
*/
247
267
@ Override
248
268
public String toString () {
0 commit comments