Skip to content

Commit 2a4f4cd

Browse files
committed
Introduce reset() method in ContextCache
1 parent c29eae3 commit 2a4f4cd

File tree

4 files changed

+44
-27
lines changed

4 files changed

+44
-27
lines changed

spring-test/src/main/java/org/springframework/test/context/ContextCache.java

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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.
@@ -31,16 +31,18 @@
3131
import 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() {

spring-test/src/test/java/org/springframework/test/context/ClassLevelDirtiesContextTestNGTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ private static final void runTestClassAndAssertStats(Class<?> testClass, int exp
8080
@BeforeClass
8181
public static void verifyInitialCacheState() {
8282
ContextCache contextCache = TestContextManager.contextCache;
83-
contextCache.clear();
84-
contextCache.clearStatistics();
83+
contextCache.reset();
8584
cacheHits.set(0);
8685
cacheMisses.set(0);
8786
assertContextCacheStatistics("BeforeClass", 0, cacheHits.get(), cacheMisses.get());

spring-test/src/test/java/org/springframework/test/context/ClassLevelDirtiesContextTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ private static final void runTestClassAndAssertStats(Class<?> testClass, int exp
7575
@BeforeClass
7676
public static void verifyInitialCacheState() {
7777
ContextCache contextCache = TestContextManager.contextCache;
78-
contextCache.clear();
79-
contextCache.clearStatistics();
78+
contextCache.reset();
8079
cacheHits.set(0);
8180
cacheMisses.set(0);
8281
assertContextCacheStatistics("BeforeClass", 0, cacheHits.get(), cacheMisses.get());

spring-test/src/test/java/org/springframework/test/context/SpringRunnerContextCacheTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ public class SpringRunnerContextCacheTests {
5959
public static void verifyInitialCacheState() {
6060
dirtiedApplicationContext = null;
6161
ContextCache contextCache = TestContextManager.contextCache;
62-
contextCache.clear();
63-
contextCache.clearStatistics();
62+
contextCache.reset();
6463
assertContextCacheStatistics("BeforeClass", 0, 0, 0);
6564
}
6665

0 commit comments

Comments
 (0)