1
1
/*
2
- * Copyright 2002-2013 the original author or authors.
2
+ * Copyright 2002-2014 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.
18
18
19
19
import org .apache .commons .logging .Log ;
20
20
import org .apache .commons .logging .LogFactory ;
21
+
21
22
import org .springframework .context .ApplicationContext ;
22
23
import org .springframework .util .Assert ;
23
24
@@ -37,6 +38,8 @@ public class CacheAwareContextLoaderDelegate {
37
38
38
39
private static final Log logger = LogFactory .getLog (CacheAwareContextLoaderDelegate .class );
39
40
41
+ private static final Log statsLogger = LogFactory .getLog ("org.springframework.test.context.cache" );
42
+
40
43
private final ContextCache contextCache ;
41
44
42
45
@@ -45,6 +48,7 @@ public class CacheAwareContextLoaderDelegate {
45
48
this .contextCache = contextCache ;
46
49
}
47
50
51
+
48
52
/**
49
53
* Load the {@code ApplicationContext} for the supplied merged context
50
54
* configuration. Supports both the {@link SmartContextLoader} and
@@ -53,9 +57,10 @@ public class CacheAwareContextLoaderDelegate {
53
57
*/
54
58
private ApplicationContext loadContextInternal (MergedContextConfiguration mergedContextConfiguration )
55
59
throws Exception {
60
+
56
61
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." );
59
64
60
65
ApplicationContext applicationContext ;
61
66
@@ -65,8 +70,8 @@ private ApplicationContext loadContextInternal(MergedContextConfiguration merged
65
70
}
66
71
else {
67
72
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." );
70
75
applicationContext = contextLoader .loadContext (locations );
71
76
}
72
77
@@ -76,35 +81,39 @@ private ApplicationContext loadContextInternal(MergedContextConfiguration merged
76
81
/**
77
82
* Load the {@link ApplicationContext application context} for the supplied
78
83
* merged context configuration.
79
- *
80
84
* <p>If the context is present in the cache it will simply be returned;
81
85
* otherwise, it will be loaded, stored in the cache, and returned.
82
86
* @return the application context
83
87
* @throws IllegalStateException if an error occurs while retrieving or
84
88
* loading the application context
85
89
*/
86
90
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 );
89
93
if (context == null ) {
90
94
try {
91
95
context = loadContextInternal (mergedContextConfiguration );
92
96
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 ));
95
99
}
96
- contextCache .put (mergedContextConfiguration , context );
100
+ this . contextCache .put (mergedContextConfiguration , context );
97
101
}
98
102
catch (Exception ex ) {
99
103
throw new IllegalStateException ("Failed to load ApplicationContext" , ex );
100
104
}
101
105
}
102
106
else {
103
107
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 ));
106
110
}
107
111
}
112
+
113
+ if (statsLogger .isDebugEnabled ()) {
114
+ statsLogger .debug ("Spring test ApplicationContext cache statistics: " + this .contextCache );
115
+ }
116
+
108
117
return context ;
109
118
}
110
119
}
0 commit comments