Skip to content

Commit 983ebaf

Browse files
committed
Log context cache statistics in the TCF
Prior to this commit, finding out how many application contexts had been loaded within a test suite required the use of reflection and a bit of hacking. This commit addresses this issue by logging ContextCache statistics whenever an application context is loaded by the Spring TestContext Framework (TCF). The log output can be enabled by setting the "org.springframework.test.context.cache" logging category to DEBUG. Issue: SPR-12409 (cherry picked from commit a09bc9b)
1 parent d5e4592 commit 983ebaf

File tree

3 files changed

+136
-168
lines changed

3 files changed

+136
-168
lines changed

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

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -18,6 +18,7 @@
1818

1919
import org.apache.commons.logging.Log;
2020
import org.apache.commons.logging.LogFactory;
21+
2122
import org.springframework.context.ApplicationContext;
2223
import org.springframework.util.Assert;
2324

@@ -37,6 +38,8 @@ public class CacheAwareContextLoaderDelegate {
3738

3839
private static final Log logger = LogFactory.getLog(CacheAwareContextLoaderDelegate.class);
3940

41+
private static final Log statsLogger = LogFactory.getLog("org.springframework.test.context.cache");
42+
4043
private final ContextCache contextCache;
4144

4245

@@ -45,6 +48,7 @@ public class CacheAwareContextLoaderDelegate {
4548
this.contextCache = contextCache;
4649
}
4750

51+
4852
/**
4953
* Load the {@code ApplicationContext} for the supplied merged context
5054
* configuration. Supports both the {@link SmartContextLoader} and
@@ -53,9 +57,10 @@ public class CacheAwareContextLoaderDelegate {
5357
*/
5458
private ApplicationContext loadContextInternal(MergedContextConfiguration mergedContextConfiguration)
5559
throws Exception {
60+
5661
ContextLoader contextLoader = mergedContextConfiguration.getContextLoader();
57-
Assert.notNull(contextLoader, "Cannot load an ApplicationContext with a NULL 'contextLoader'. "
58-
+ "Consider annotating your test class with @ContextConfiguration or @ContextHierarchy.");
62+
Assert.notNull(contextLoader, "Cannot load an ApplicationContext with a NULL 'contextLoader'. " +
63+
"Consider annotating your test class with @ContextConfiguration or @ContextHierarchy.");
5964

6065
ApplicationContext applicationContext;
6166

@@ -65,8 +70,8 @@ private ApplicationContext loadContextInternal(MergedContextConfiguration merged
6570
}
6671
else {
6772
String[] locations = mergedContextConfiguration.getLocations();
68-
Assert.notNull(locations, "Cannot load an ApplicationContext with a NULL 'locations' array. "
69-
+ "Consider annotating your test class with @ContextConfiguration or @ContextHierarchy.");
73+
Assert.notNull(locations, "Cannot load an ApplicationContext with a NULL 'locations' array. " +
74+
"Consider annotating your test class with @ContextConfiguration or @ContextHierarchy.");
7075
applicationContext = contextLoader.loadContext(locations);
7176
}
7277

@@ -76,35 +81,39 @@ private ApplicationContext loadContextInternal(MergedContextConfiguration merged
7681
/**
7782
* Load the {@link ApplicationContext application context} for the supplied
7883
* merged context configuration.
79-
*
8084
* <p>If the context is present in the cache it will simply be returned;
8185
* otherwise, it will be loaded, stored in the cache, and returned.
8286
* @return the application context
8387
* @throws IllegalStateException if an error occurs while retrieving or
8488
* loading the application context
8589
*/
8690
public ApplicationContext loadContext(MergedContextConfiguration mergedContextConfiguration) {
87-
synchronized (contextCache) {
88-
ApplicationContext context = contextCache.get(mergedContextConfiguration);
91+
synchronized (this.contextCache) {
92+
ApplicationContext context = this.contextCache.get(mergedContextConfiguration);
8993
if (context == null) {
9094
try {
9195
context = loadContextInternal(mergedContextConfiguration);
9296
if (logger.isDebugEnabled()) {
93-
logger.debug(String.format("Storing ApplicationContext in cache under key [%s].",
94-
mergedContextConfiguration));
97+
logger.debug(String.format("Storing ApplicationContext in cache under key [%s]",
98+
mergedContextConfiguration));
9599
}
96-
contextCache.put(mergedContextConfiguration, context);
100+
this.contextCache.put(mergedContextConfiguration, context);
97101
}
98102
catch (Exception ex) {
99103
throw new IllegalStateException("Failed to load ApplicationContext", ex);
100104
}
101105
}
102106
else {
103107
if (logger.isDebugEnabled()) {
104-
logger.debug(String.format("Retrieved ApplicationContext from cache with key [%s].",
105-
mergedContextConfiguration));
108+
logger.debug(String.format("Retrieved ApplicationContext from cache with key [%s]",
109+
mergedContextConfiguration));
106110
}
107111
}
112+
113+
if (statsLogger.isDebugEnabled()) {
114+
statsLogger.debug("Spring test ApplicationContext cache statistics: " + this.contextCache);
115+
}
116+
108117
return context;
109118
}
110119
}

0 commit comments

Comments
 (0)