From 2a4f4cd258967494ba41fba1ef48d98fedaf1e85 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 17 Apr 2015 22:51:04 +0200 Subject: [PATCH] Introduce reset() method in ContextCache --- .../test/context/ContextCache.java | 62 ++++++++++++------- .../ClassLevelDirtiesContextTestNGTests.java | 3 +- .../ClassLevelDirtiesContextTests.java | 3 +- .../SpringRunnerContextCacheTests.java | 3 +- 4 files changed, 44 insertions(+), 27 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/ContextCache.java b/spring-test/src/main/java/org/springframework/test/context/ContextCache.java index 8189f385a20..f14e6560628 100644 --- a/spring-test/src/main/java/org/springframework/test/context/ContextCache.java +++ b/spring-test/src/main/java/org/springframework/test/context/ContextCache.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,16 +31,18 @@ import org.springframework.test.annotation.DirtiesContext.HierarchyMode; import org.springframework.util.Assert; /** - * Cache for Spring {@link ApplicationContext ApplicationContexts} in a test environment. + * Cache for Spring {@link ApplicationContext ApplicationContexts} in a test + * environment. * - *

Maintains a cache of {@code ApplicationContexts} keyed by - * {@link MergedContextConfiguration} instances. + *

{@code ContextCache} maintains a cache of {@code ApplicationContexts} + * keyed by {@link 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 {@code 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. + *

Caching has significant performance benefits if initializing the context + * takes a considerable about of time. Although initializing a Spring context + * itself is very quick, some beans in a context, such as a + * {@code 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. * * @author Sam Brannen * @author Juergen Hoeller @@ -67,21 +69,36 @@ class ContextCache { private final AtomicInteger missCount = new AtomicInteger(); + /** + * Reset all state maintained by this cache. + * @see #clear() + * @see #clearStatistics() + */ + public void reset() { + synchronized (contextMap) { + clear(); + clearStatistics(); + } + } /** * Clear all contexts from the cache and clear context hierarchy information as well. */ public void clear() { - this.contextMap.clear(); - this.hierarchyMap.clear(); + synchronized (contextMap) { + this.contextMap.clear(); + this.hierarchyMap.clear(); + } } /** * Clear hit and miss count statistics for the cache (i.e., reset counters to zero). */ public void clearStatistics() { - this.hitCount.set(0); - this.missCount.set(0); + synchronized (contextMap) { + this.hitCount.set(0); + this.missCount.set(0); + } } /** @@ -117,8 +134,8 @@ class ContextCache { /** * 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. + *

A hit is any access to the cache that returns a non-null + * context for the queried key. */ public int getHitCount() { return this.hitCount.get(); @@ -126,15 +143,16 @@ class ContextCache { /** * Get the overall miss count for this cache. - *

A miss is an access to the cache, which returned a {@code null} context - * for a queried key. + *

A miss is any access to the cache that returns a {@code null} + * context for the queried key. */ public int getMissCount() { return this.missCount.get(); } /** - * Explicitly add an {@code ApplicationContext} instance to the cache under the given key. + * Explicitly add an {@code ApplicationContext} instance to the cache + * under the given key. * @param key the context key (never {@code null}) * @param context the {@code ApplicationContext} instance (never {@code null}) */ @@ -240,9 +258,11 @@ class ContextCache { } /** - * Generate a text string, which contains the {@linkplain #size} as well - * as the {@linkplain #getHitCount() hit}, {@linkplain #getMissCount() miss}, - * and {@linkplain #getParentContextCount() parent context} counts. + * Generate a text string containing the statistics for this cache. + *

Specifically, the returned string contains the {@linkplain #size}, + * {@linkplain #getHitCount() hit count}, {@linkplain #getMissCount() miss count}, + * and {@linkplain #getParentContextCount() parent context count}. + * @return the statistics for this cache */ @Override public String toString() { diff --git a/spring-test/src/test/java/org/springframework/test/context/ClassLevelDirtiesContextTestNGTests.java b/spring-test/src/test/java/org/springframework/test/context/ClassLevelDirtiesContextTestNGTests.java index 473edfeb6b4..9009e73229a 100644 --- a/spring-test/src/test/java/org/springframework/test/context/ClassLevelDirtiesContextTestNGTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/ClassLevelDirtiesContextTestNGTests.java @@ -80,8 +80,7 @@ public class ClassLevelDirtiesContextTestNGTests { @BeforeClass public static void verifyInitialCacheState() { ContextCache contextCache = TestContextManager.contextCache; - contextCache.clear(); - contextCache.clearStatistics(); + contextCache.reset(); cacheHits.set(0); cacheMisses.set(0); assertContextCacheStatistics("BeforeClass", 0, cacheHits.get(), cacheMisses.get()); diff --git a/spring-test/src/test/java/org/springframework/test/context/ClassLevelDirtiesContextTests.java b/spring-test/src/test/java/org/springframework/test/context/ClassLevelDirtiesContextTests.java index 0d69b44c879..1c51ad8072e 100644 --- a/spring-test/src/test/java/org/springframework/test/context/ClassLevelDirtiesContextTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/ClassLevelDirtiesContextTests.java @@ -75,8 +75,7 @@ public class ClassLevelDirtiesContextTests { @BeforeClass public static void verifyInitialCacheState() { ContextCache contextCache = TestContextManager.contextCache; - contextCache.clear(); - contextCache.clearStatistics(); + contextCache.reset(); cacheHits.set(0); cacheMisses.set(0); assertContextCacheStatistics("BeforeClass", 0, cacheHits.get(), cacheMisses.get()); diff --git a/spring-test/src/test/java/org/springframework/test/context/SpringRunnerContextCacheTests.java b/spring-test/src/test/java/org/springframework/test/context/SpringRunnerContextCacheTests.java index c427538a9f8..3e09f2731e9 100644 --- a/spring-test/src/test/java/org/springframework/test/context/SpringRunnerContextCacheTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/SpringRunnerContextCacheTests.java @@ -59,8 +59,7 @@ public class SpringRunnerContextCacheTests { public static void verifyInitialCacheState() { dirtiedApplicationContext = null; ContextCache contextCache = TestContextManager.contextCache; - contextCache.clear(); - contextCache.clearStatistics(); + contextCache.reset(); assertContextCacheStatistics("BeforeClass", 0, 0, 0); }