Skip to content

Commit a4d36a8

Browse files
committed
Polish ApplicationContextFailureProcessor support
See gh-29387
1 parent 86ff037 commit a4d36a8

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
* Strategy for components that process failures related to application contexts
2323
* within the <em>Spring TestContext Framework</em>.
2424
*
25+
* <p>Implementations must be registered via the
26+
* {@link org.springframework.core.io.support.SpringFactoriesLoader SpringFactoriesLoader}
27+
* mechanism.
28+
*
2529
* @author Sam Brannen
2630
* @since 6.0
2731
* @see ContextLoadException
@@ -32,9 +36,10 @@ public interface ApplicationContextFailureProcessor {
3236
* Invoked when a failure was encountered while attempting to load an
3337
* {@link ApplicationContext}.
3438
* <p>Implementations of this method must not throw any exceptions. Consequently,
35-
* any exception thrown by an implementation of this method will be ignored.
39+
* any exception thrown by an implementation of this method will be ignored, though
40+
* potentially logged.
3641
* @param context the application context that did not load successfully
37-
* @param exception the exception caught while loading the application context
42+
* @param exception the exception thrown while loading the application context
3843
*/
3944
void processLoadFailure(ApplicationContext context, Throwable exception);
4045

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ default boolean isContextLoaded(MergedContextConfiguration mergedContextConfigur
6666
* configured in the given {@code MergedContextConfiguration}.
6767
* <p>If the context is present in the {@code ContextCache} it will simply
6868
* be returned; otherwise, it will be loaded, stored in the cache, and returned.
69+
* <p>As of Spring Framework 6.0, implementations of this method should load
70+
* {@link ApplicationContextFailureProcessor} implementations via the
71+
* {@link org.springframework.core.io.support.SpringFactoriesLoader SpringFactoriesLoader}
72+
* mechanism, catch any exception thrown by the {@link ContextLoader}, and
73+
* delegate to each of the configured failure processors to process the context
74+
* load failure if the thrown exception is an instance of {@link ContextLoadException}.
6975
* <p>The cache statistics should be logged by invoking
7076
* {@link org.springframework.test.context.cache.ContextCache#logStatistics()}.
7177
* @param mergedContextConfiguration the merged context configuration to use

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,15 @@ public class DefaultCacheAwareContextLoaderDelegate implements CacheAwareContext
6161

6262
private static final Log logger = LogFactory.getLog(DefaultCacheAwareContextLoaderDelegate.class);
6363

64-
private static List<ApplicationContextFailureProcessor> contextFailureProcessors =
65-
getApplicationContextFailureProcessors();
6664

6765
/**
6866
* Default static cache of Spring application contexts.
6967
*/
7068
static final ContextCache defaultContextCache = new DefaultContextCache();
7169

70+
private List<ApplicationContextFailureProcessor> contextFailureProcessors =
71+
loadApplicationContextFailureProcessors();
72+
7273
private final AotTestContextInitializers aotTestContextInitializers = new AotTestContextInitializers();
7374

7475
private final ContextCache contextCache;
@@ -127,7 +128,7 @@ public ApplicationContext loadContext(MergedContextConfiguration mergedContextCo
127128
Throwable cause = ex;
128129
if (ex instanceof ContextLoadException cle) {
129130
cause = cle.getCause();
130-
for (ApplicationContextFailureProcessor contextFailureProcessor : contextFailureProcessors) {
131+
for (ApplicationContextFailureProcessor contextFailureProcessor : this.contextFailureProcessors) {
131132
try {
132133
contextFailureProcessor.processLoadFailure(cle.getApplicationContext(), cause);
133134
}
@@ -259,7 +260,7 @@ private MergedContextConfiguration replaceIfNecessary(MergedContextConfiguration
259260
* @return the context failure processors to use
260261
* @since 6.0
261262
*/
262-
private static List<ApplicationContextFailureProcessor> getApplicationContextFailureProcessors() {
263+
private static List<ApplicationContextFailureProcessor> loadApplicationContextFailureProcessors() {
263264
SpringFactoriesLoader loader = SpringFactoriesLoader.forDefaultResourceLocation(
264265
DefaultCacheAwareContextLoaderDelegate.class.getClassLoader());
265266
List<ApplicationContextFailureProcessor> processors = loader.load(ApplicationContextFailureProcessor.class,

0 commit comments

Comments
 (0)