From 1a26e17f416e5d1fe184fe7dee3fc654ff2b57bc Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sun, 11 Jun 2023 15:46:28 +0200 Subject: [PATCH] Ensure context cache stats are logged when ApplicationContext fails to load Closes gh-30635 --- ...efaultCacheAwareContextLoaderDelegate.java | 71 ++++++++++--------- 1 file changed, 37 insertions(+), 34 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/cache/DefaultCacheAwareContextLoaderDelegate.java b/spring-test/src/main/java/org/springframework/test/context/cache/DefaultCacheAwareContextLoaderDelegate.java index 8f537a9e557..00c1d78922d 100644 --- a/spring-test/src/main/java/org/springframework/test/context/cache/DefaultCacheAwareContextLoaderDelegate.java +++ b/spring-test/src/main/java/org/springframework/test/context/cache/DefaultCacheAwareContextLoaderDelegate.java @@ -109,48 +109,51 @@ public class DefaultCacheAwareContextLoaderDelegate implements CacheAwareContext mergedContextConfiguration = replaceIfNecessary(mergedContextConfiguration); synchronized (this.contextCache) { ApplicationContext context = this.contextCache.get(mergedContextConfiguration); - if (context == null) { - try { - if (mergedContextConfiguration instanceof AotMergedContextConfiguration aotMergedConfig) { - context = loadContextInAotMode(aotMergedConfig); - } - else { - context = loadContextInternal(mergedContextConfiguration); - } - if (logger.isTraceEnabled()) { - logger.trace("Storing ApplicationContext [%s] in cache under key %s".formatted( - System.identityHashCode(context), mergedContextConfiguration)); + try { + if (context == null) { + try { + if (mergedContextConfiguration instanceof AotMergedContextConfiguration aotMergedConfig) { + context = loadContextInAotMode(aotMergedConfig); + } + else { + context = loadContextInternal(mergedContextConfiguration); + } + if (logger.isTraceEnabled()) { + logger.trace("Storing ApplicationContext [%s] in cache under key %s".formatted( + System.identityHashCode(context), mergedContextConfiguration)); + } + this.contextCache.put(mergedContextConfiguration, context); } - this.contextCache.put(mergedContextConfiguration, context); - } - catch (Exception ex) { - Throwable cause = ex; - if (ex instanceof ContextLoadException cle) { - cause = cle.getCause(); - for (ApplicationContextFailureProcessor contextFailureProcessor : this.contextFailureProcessors) { - try { - contextFailureProcessor.processLoadFailure(cle.getApplicationContext(), cause); - } - catch (Throwable throwable) { - if (logger.isDebugEnabled()) { - logger.debug("Ignoring exception thrown from ApplicationContextFailureProcessor [%s]: %s" - .formatted(contextFailureProcessor, throwable)); + catch (Exception ex) { + Throwable cause = ex; + if (ex instanceof ContextLoadException cle) { + cause = cle.getCause(); + for (ApplicationContextFailureProcessor contextFailureProcessor : this.contextFailureProcessors) { + try { + contextFailureProcessor.processLoadFailure(cle.getApplicationContext(), cause); + } + catch (Throwable throwable) { + if (logger.isDebugEnabled()) { + logger.debug("Ignoring exception thrown from ApplicationContextFailureProcessor [%s]: %s" + .formatted(contextFailureProcessor, throwable)); + } } } } + throw new IllegalStateException( + "Failed to load ApplicationContext for " + mergedContextConfiguration, cause); } - throw new IllegalStateException( - "Failed to load ApplicationContext for " + mergedContextConfiguration, cause); } - } - else { - if (logger.isTraceEnabled()) { - logger.trace("Retrieved ApplicationContext [%s] from cache with key %s".formatted( - System.identityHashCode(context), mergedContextConfiguration)); + else { + if (logger.isTraceEnabled()) { + logger.trace("Retrieved ApplicationContext [%s] from cache with key %s".formatted( + System.identityHashCode(context), mergedContextConfiguration)); + } } } - - this.contextCache.logStatistics(); + finally { + this.contextCache.logStatistics(); + } return context; }