Polish ApplicationContextFailureProcessor support

See gh-29387
This commit is contained in:
Sam Brannen
2022-10-27 14:29:04 +02:00
parent 86ff0371c2
commit a4d36a8b83
3 changed files with 18 additions and 6 deletions

View File

@@ -22,6 +22,10 @@ import org.springframework.context.ApplicationContext;
* Strategy for components that process failures related to application contexts
* within the <em>Spring TestContext Framework</em>.
*
* <p>Implementations must be registered via the
* {@link org.springframework.core.io.support.SpringFactoriesLoader SpringFactoriesLoader}
* mechanism.
*
* @author Sam Brannen
* @since 6.0
* @see ContextLoadException
@@ -32,9 +36,10 @@ public interface ApplicationContextFailureProcessor {
* Invoked when a failure was encountered while attempting to load an
* {@link ApplicationContext}.
* <p>Implementations of this method must not throw any exceptions. Consequently,
* any exception thrown by an implementation of this method will be ignored.
* any exception thrown by an implementation of this method will be ignored, though
* potentially logged.
* @param context the application context that did not load successfully
* @param exception the exception caught while loading the application context
* @param exception the exception thrown while loading the application context
*/
void processLoadFailure(ApplicationContext context, Throwable exception);

View File

@@ -66,6 +66,12 @@ public interface CacheAwareContextLoaderDelegate {
* configured in the given {@code MergedContextConfiguration}.
* <p>If the context is present in the {@code ContextCache} it will simply
* be returned; otherwise, it will be loaded, stored in the cache, and returned.
* <p>As of Spring Framework 6.0, implementations of this method should load
* {@link ApplicationContextFailureProcessor} implementations via the
* {@link org.springframework.core.io.support.SpringFactoriesLoader SpringFactoriesLoader}
* mechanism, catch any exception thrown by the {@link ContextLoader}, and
* delegate to each of the configured failure processors to process the context
* load failure if the thrown exception is an instance of {@link ContextLoadException}.
* <p>The cache statistics should be logged by invoking
* {@link org.springframework.test.context.cache.ContextCache#logStatistics()}.
* @param mergedContextConfiguration the merged context configuration to use

View File

@@ -61,14 +61,15 @@ public class DefaultCacheAwareContextLoaderDelegate implements CacheAwareContext
private static final Log logger = LogFactory.getLog(DefaultCacheAwareContextLoaderDelegate.class);
private static List<ApplicationContextFailureProcessor> contextFailureProcessors =
getApplicationContextFailureProcessors();
/**
* Default static cache of Spring application contexts.
*/
static final ContextCache defaultContextCache = new DefaultContextCache();
private List<ApplicationContextFailureProcessor> contextFailureProcessors =
loadApplicationContextFailureProcessors();
private final AotTestContextInitializers aotTestContextInitializers = new AotTestContextInitializers();
private final ContextCache contextCache;
@@ -127,7 +128,7 @@ public class DefaultCacheAwareContextLoaderDelegate implements CacheAwareContext
Throwable cause = ex;
if (ex instanceof ContextLoadException cle) {
cause = cle.getCause();
for (ApplicationContextFailureProcessor contextFailureProcessor : contextFailureProcessors) {
for (ApplicationContextFailureProcessor contextFailureProcessor : this.contextFailureProcessors) {
try {
contextFailureProcessor.processLoadFailure(cle.getApplicationContext(), cause);
}
@@ -259,7 +260,7 @@ public class DefaultCacheAwareContextLoaderDelegate implements CacheAwareContext
* @return the context failure processors to use
* @since 6.0
*/
private static List<ApplicationContextFailureProcessor> getApplicationContextFailureProcessors() {
private static List<ApplicationContextFailureProcessor> loadApplicationContextFailureProcessors() {
SpringFactoriesLoader loader = SpringFactoriesLoader.forDefaultResourceLocation(
DefaultCacheAwareContextLoaderDelegate.class.getClassLoader());
List<ApplicationContextFailureProcessor> processors = loader.load(ApplicationContextFailureProcessor.class,