Skip to content

Commit 1a26e17

Browse files
committed
Ensure context cache stats are logged when ApplicationContext fails to load
Closes gh-30635
1 parent 439bcd6 commit 1a26e17

File tree

1 file changed

+37
-34
lines changed

1 file changed

+37
-34
lines changed

spring-test/src/main/java/org/springframework/test/context/cache/DefaultCacheAwareContextLoaderDelegate.java

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -109,48 +109,51 @@ public ApplicationContext loadContext(MergedContextConfiguration mergedContextCo
109109
mergedContextConfiguration = replaceIfNecessary(mergedContextConfiguration);
110110
synchronized (this.contextCache) {
111111
ApplicationContext context = this.contextCache.get(mergedContextConfiguration);
112-
if (context == null) {
113-
try {
114-
if (mergedContextConfiguration instanceof AotMergedContextConfiguration aotMergedConfig) {
115-
context = loadContextInAotMode(aotMergedConfig);
116-
}
117-
else {
118-
context = loadContextInternal(mergedContextConfiguration);
119-
}
120-
if (logger.isTraceEnabled()) {
121-
logger.trace("Storing ApplicationContext [%s] in cache under key %s".formatted(
122-
System.identityHashCode(context), mergedContextConfiguration));
112+
try {
113+
if (context == null) {
114+
try {
115+
if (mergedContextConfiguration instanceof AotMergedContextConfiguration aotMergedConfig) {
116+
context = loadContextInAotMode(aotMergedConfig);
117+
}
118+
else {
119+
context = loadContextInternal(mergedContextConfiguration);
120+
}
121+
if (logger.isTraceEnabled()) {
122+
logger.trace("Storing ApplicationContext [%s] in cache under key %s".formatted(
123+
System.identityHashCode(context), mergedContextConfiguration));
124+
}
125+
this.contextCache.put(mergedContextConfiguration, context);
123126
}
124-
this.contextCache.put(mergedContextConfiguration, context);
125-
}
126-
catch (Exception ex) {
127-
Throwable cause = ex;
128-
if (ex instanceof ContextLoadException cle) {
129-
cause = cle.getCause();
130-
for (ApplicationContextFailureProcessor contextFailureProcessor : this.contextFailureProcessors) {
131-
try {
132-
contextFailureProcessor.processLoadFailure(cle.getApplicationContext(), cause);
133-
}
134-
catch (Throwable throwable) {
135-
if (logger.isDebugEnabled()) {
136-
logger.debug("Ignoring exception thrown from ApplicationContextFailureProcessor [%s]: %s"
137-
.formatted(contextFailureProcessor, throwable));
127+
catch (Exception ex) {
128+
Throwable cause = ex;
129+
if (ex instanceof ContextLoadException cle) {
130+
cause = cle.getCause();
131+
for (ApplicationContextFailureProcessor contextFailureProcessor : this.contextFailureProcessors) {
132+
try {
133+
contextFailureProcessor.processLoadFailure(cle.getApplicationContext(), cause);
134+
}
135+
catch (Throwable throwable) {
136+
if (logger.isDebugEnabled()) {
137+
logger.debug("Ignoring exception thrown from ApplicationContextFailureProcessor [%s]: %s"
138+
.formatted(contextFailureProcessor, throwable));
139+
}
138140
}
139141
}
140142
}
143+
throw new IllegalStateException(
144+
"Failed to load ApplicationContext for " + mergedContextConfiguration, cause);
141145
}
142-
throw new IllegalStateException(
143-
"Failed to load ApplicationContext for " + mergedContextConfiguration, cause);
144146
}
145-
}
146-
else {
147-
if (logger.isTraceEnabled()) {
148-
logger.trace("Retrieved ApplicationContext [%s] from cache with key %s".formatted(
149-
System.identityHashCode(context), mergedContextConfiguration));
147+
else {
148+
if (logger.isTraceEnabled()) {
149+
logger.trace("Retrieved ApplicationContext [%s] from cache with key %s".formatted(
150+
System.identityHashCode(context), mergedContextConfiguration));
151+
}
150152
}
151153
}
152-
153-
this.contextCache.logStatistics();
154+
finally {
155+
this.contextCache.logStatistics();
156+
}
154157

155158
return context;
156159
}

0 commit comments

Comments
 (0)