Skip to content

Commit b4ba80b

Browse files
committed
Merge branch '6.0.x'
# Conflicts: # spring-test/src/main/java/org/springframework/test/context/cache/DefaultCacheAwareContextLoaderDelegate.java
2 parents f6045e8 + 1a26e17 commit b4ba80b

File tree

1 file changed

+44
-41
lines changed

1 file changed

+44
-41
lines changed

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

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -135,56 +135,59 @@ public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) {
135135
mergedConfig = replaceIfNecessary(mergedConfig);
136136
synchronized (this.contextCache) {
137137
ApplicationContext context = this.contextCache.get(mergedConfig);
138-
if (context == null) {
139-
Integer failureCount = this.contextCache.getFailureCount(mergedConfig);
140-
if (failureCount >= this.failureThreshold) {
141-
throw new IllegalStateException("""
142-
ApplicationContext failure threshold (%d) exceeded: \
143-
skipping repeated attempt to load context for %s"""
144-
.formatted(this.failureThreshold, mergedConfig));
145-
}
146-
try {
147-
if (mergedConfig instanceof AotMergedContextConfiguration aotMergedConfig) {
148-
context = loadContextInAotMode(aotMergedConfig);
149-
}
150-
else {
151-
context = loadContextInternal(mergedConfig);
138+
try {
139+
if (context == null) {
140+
Integer failureCount = this.contextCache.getFailureCount(mergedConfig);
141+
if (failureCount >= this.failureThreshold) {
142+
throw new IllegalStateException("""
143+
ApplicationContext failure threshold (%d) exceeded: \
144+
skipping repeated attempt to load context for %s"""
145+
.formatted(this.failureThreshold, mergedConfig));
152146
}
153-
if (logger.isTraceEnabled()) {
154-
logger.trace("Storing ApplicationContext [%s] in cache under key %s".formatted(
155-
System.identityHashCode(context), mergedConfig));
147+
try {
148+
if (mergedConfig instanceof AotMergedContextConfiguration aotMergedConfig) {
149+
context = loadContextInAotMode(aotMergedConfig);
150+
}
151+
else {
152+
context = loadContextInternal(mergedConfig);
153+
}
154+
if (logger.isTraceEnabled()) {
155+
logger.trace("Storing ApplicationContext [%s] in cache under key %s".formatted(
156+
System.identityHashCode(context), mergedConfig));
157+
}
158+
this.contextCache.put(mergedConfig, context);
156159
}
157-
this.contextCache.put(mergedConfig, context);
158-
}
159-
catch (Exception ex) {
160-
this.contextCache.incrementFailureCount(mergedConfig);
161-
Throwable cause = ex;
162-
if (ex instanceof ContextLoadException cle) {
163-
cause = cle.getCause();
164-
for (ApplicationContextFailureProcessor contextFailureProcessor : this.contextFailureProcessors) {
165-
try {
166-
contextFailureProcessor.processLoadFailure(cle.getApplicationContext(), cause);
167-
}
168-
catch (Throwable throwable) {
169-
if (logger.isDebugEnabled()) {
170-
logger.debug("Ignoring exception thrown from ApplicationContextFailureProcessor [%s]: %s"
171-
.formatted(contextFailureProcessor, throwable));
160+
catch (Exception ex) {
161+
this.contextCache.incrementFailureCount(mergedConfig);
162+
Throwable cause = ex;
163+
if (ex instanceof ContextLoadException cle) {
164+
cause = cle.getCause();
165+
for (ApplicationContextFailureProcessor contextFailureProcessor : this.contextFailureProcessors) {
166+
try {
167+
contextFailureProcessor.processLoadFailure(cle.getApplicationContext(), cause);
168+
}
169+
catch (Throwable throwable) {
170+
if (logger.isDebugEnabled()) {
171+
logger.debug("Ignoring exception thrown from ApplicationContextFailureProcessor [%s]: %s"
172+
.formatted(contextFailureProcessor, throwable));
173+
}
172174
}
173175
}
174176
}
177+
throw new IllegalStateException(
178+
"Failed to load ApplicationContext for " + mergedConfig, cause);
175179
}
176-
throw new IllegalStateException(
177-
"Failed to load ApplicationContext for " + mergedConfig, cause);
178180
}
179-
}
180-
else {
181-
if (logger.isTraceEnabled()) {
182-
logger.trace("Retrieved ApplicationContext [%s] from cache with key %s".formatted(
183-
System.identityHashCode(context), mergedConfig));
181+
else {
182+
if (logger.isTraceEnabled()) {
183+
logger.trace("Retrieved ApplicationContext [%s] from cache with key %s".formatted(
184+
System.identityHashCode(context), mergedConfig));
185+
}
184186
}
185187
}
186-
187-
this.contextCache.logStatistics();
188+
finally {
189+
this.contextCache.logStatistics();
190+
}
188191

189192
return context;
190193
}

0 commit comments

Comments
 (0)