Skip to content

Commit a13cb01

Browse files
committed
Load ApplicationContextFailureProcessors from spring.factories
At the request of the Spring Boot team, ApplicationContextFailureProcessor implementations are now loaded via the spring.factories mechanism instead of supporting a single processor registered via subclasses of AbstractTestContextBootstrapper. This makes the retrieval and handling of processors internal to DefaultCacheAwareContextLoaderDelegate, while simultaneously supporting multiple processors that can be registered by anyone (i.e., not limited to projects that implement custom TestContextBootstrappers). See spring-projectsgh-28826 Closes spring-projectsgh-29387
1 parent 273e38c commit a13cb01

File tree

6 files changed

+141
-75
lines changed

6 files changed

+141
-75
lines changed

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,18 +101,4 @@ default boolean isContextLoaded(MergedContextConfiguration mergedContextConfigur
101101
*/
102102
void closeContext(MergedContextConfiguration mergedContextConfiguration, @Nullable HierarchyMode hierarchyMode);
103103

104-
/**
105-
* Set the {@link ApplicationContextFailureProcessor} to use.
106-
* <p>The default implementation ignores the supplied processor.
107-
* <p>Concrete implementations should override this method to store a reference
108-
* to the supplied processor and use it to process {@link ContextLoadException
109-
* ContextLoadExceptions} thrown from context loaders in
110-
* {@link #loadContext(MergedContextConfiguration)}.
111-
* @param contextFailureProcessor the context failure processor to use
112-
* @since 6.0
113-
*/
114-
default void setContextFailureProcessor(ApplicationContextFailureProcessor contextFailureProcessor) {
115-
// no-op
116-
}
117-
118104
}

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

Lines changed: 84 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@
1616

1717
package org.springframework.test.context.cache;
1818

19+
import java.lang.reflect.InvocationTargetException;
20+
import java.util.Collection;
21+
import java.util.List;
22+
1923
import org.apache.commons.logging.Log;
2024
import org.apache.commons.logging.LogFactory;
2125

2226
import org.springframework.context.ApplicationContext;
2327
import org.springframework.context.ApplicationContextInitializer;
2428
import org.springframework.context.ConfigurableApplicationContext;
2529
import org.springframework.context.support.GenericApplicationContext;
30+
import org.springframework.core.io.support.SpringFactoriesLoader;
2631
import org.springframework.lang.Nullable;
2732
import org.springframework.test.annotation.DirtiesContext.HierarchyMode;
2833
import org.springframework.test.context.ApplicationContextFailureProcessor;
@@ -44,13 +49,21 @@
4449
* invoke the {@link #DefaultCacheAwareContextLoaderDelegate(ContextCache)}
4550
* and provide a custom {@link ContextCache} implementation.
4651
*
52+
* <p>As of Spring Framework 6.0, this class loads {@link ApplicationContextFailureProcessor}
53+
* implementations via the {@link SpringFactoriesLoader} mechanism and delegates to
54+
* them in {@link #loadContext(MergedContextConfiguration)} to process context
55+
* load failures.
56+
*
4757
* @author Sam Brannen
4858
* @since 4.1
4959
*/
5060
public class DefaultCacheAwareContextLoaderDelegate implements CacheAwareContextLoaderDelegate {
5161

5262
private static final Log logger = LogFactory.getLog(DefaultCacheAwareContextLoaderDelegate.class);
5363

64+
private static List<ApplicationContextFailureProcessor> contextFailureProcessors =
65+
getApplicationContextFailureProcessors();
66+
5467
/**
5568
* Default static cache of Spring application contexts.
5669
*/
@@ -60,9 +73,6 @@ public class DefaultCacheAwareContextLoaderDelegate implements CacheAwareContext
6073

6174
private final ContextCache contextCache;
6275

63-
@Nullable
64-
private ApplicationContextFailureProcessor contextFailureProcessor;
65-
6676

6777
/**
6878
* Construct a new {@code DefaultCacheAwareContextLoaderDelegate} using
@@ -117,14 +127,14 @@ public ApplicationContext loadContext(MergedContextConfiguration mergedContextCo
117127
Throwable cause = ex;
118128
if (ex instanceof ContextLoadException cle) {
119129
cause = cle.getCause();
120-
if (this.contextFailureProcessor != null) {
130+
for (ApplicationContextFailureProcessor contextFailureProcessor : contextFailureProcessors) {
121131
try {
122-
this.contextFailureProcessor.processLoadFailure(cle.getApplicationContext(), cause);
132+
contextFailureProcessor.processLoadFailure(cle.getApplicationContext(), cause);
123133
}
124134
catch (Throwable throwable) {
125135
if (logger.isDebugEnabled()) {
126136
logger.debug("Ignoring exception thrown from ApplicationContextFailureProcessor [%s]: %s"
127-
.formatted(this.contextFailureProcessor, throwable));
137+
.formatted(contextFailureProcessor, throwable));
128138
}
129139
}
130140
}
@@ -153,12 +163,6 @@ public void closeContext(MergedContextConfiguration mergedContextConfiguration,
153163
}
154164
}
155165

156-
@Override
157-
public void setContextFailureProcessor(ApplicationContextFailureProcessor contextFailureProcessor) {
158-
this.contextFailureProcessor = contextFailureProcessor;
159-
}
160-
161-
162166
/**
163167
* Get the {@link ContextCache} used by this context loader delegate.
164168
*/
@@ -236,6 +240,7 @@ private ContextLoader getContextLoader(MergedContextConfiguration mergedConfig)
236240
* unmodified.
237241
* <p>This allows for transparent {@link org.springframework.test.context.cache.ContextCache ContextCache}
238242
* support for AOT-optimized application contexts.
243+
* @since 6.0
239244
*/
240245
@SuppressWarnings("unchecked")
241246
private MergedContextConfiguration replaceIfNecessary(MergedContextConfiguration mergedConfig) {
@@ -248,4 +253,71 @@ private MergedContextConfiguration replaceIfNecessary(MergedContextConfiguration
248253
return mergedConfig;
249254
}
250255

256+
/**
257+
* Get the {@link ApplicationContextFailureProcessor} implementations to use,
258+
* loaded via the {@link SpringFactoriesLoader} mechanism.
259+
* @return the context failure processors to use
260+
* @since 6.0
261+
*/
262+
private static List<ApplicationContextFailureProcessor> getApplicationContextFailureProcessors() {
263+
SpringFactoriesLoader loader = SpringFactoriesLoader.forDefaultResourceLocation(
264+
DefaultCacheAwareContextLoaderDelegate.class.getClassLoader());
265+
List<ApplicationContextFailureProcessor> processors = loader.load(ApplicationContextFailureProcessor.class,
266+
DefaultCacheAwareContextLoaderDelegate::handleInstantiationFailure);
267+
if (logger.isTraceEnabled()) {
268+
logger.trace("Loaded default ApplicationContextFailureProcessor implementations from location [%s]: %s"
269+
.formatted(SpringFactoriesLoader.FACTORIES_RESOURCE_LOCATION, classNames(processors)));
270+
}
271+
else if (logger.isDebugEnabled()) {
272+
logger.debug("Loaded default ApplicationContextFailureProcessor implementations from location [%s]: %s"
273+
.formatted(SpringFactoriesLoader.FACTORIES_RESOURCE_LOCATION, classSimpleNames(processors)));
274+
}
275+
return processors;
276+
}
277+
278+
private static void handleInstantiationFailure(
279+
Class<?> factoryType, String factoryImplementationName, Throwable failure) {
280+
281+
Throwable ex = (failure instanceof InvocationTargetException ite ?
282+
ite.getTargetException() : failure);
283+
if (ex instanceof ClassNotFoundException || ex instanceof NoClassDefFoundError) {
284+
logSkippedComponent(factoryType, factoryImplementationName, ex);
285+
}
286+
else if (ex instanceof LinkageError) {
287+
if (logger.isDebugEnabled()) {
288+
logger.debug("""
289+
Could not load %1$s [%2$s]. Specify custom %1$s classes or make the default %1$s classes \
290+
available.""".formatted(factoryType.getSimpleName(), factoryImplementationName), ex);
291+
}
292+
}
293+
else {
294+
if (ex instanceof RuntimeException runtimeException) {
295+
throw runtimeException;
296+
}
297+
if (ex instanceof Error error) {
298+
throw error;
299+
}
300+
throw new IllegalStateException(
301+
"Failed to load %s [%s].".formatted(factoryType.getSimpleName(), factoryImplementationName), ex);
302+
}
303+
}
304+
305+
private static void logSkippedComponent(Class<?> factoryType, String factoryImplementationName, Throwable ex) {
306+
if (logger.isDebugEnabled()) {
307+
logger.debug("""
308+
Skipping candidate %1$s [%2$s] due to a missing dependency. \
309+
Specify custom %1$s classes or make the default %1$s classes \
310+
and their required dependencies available. Offending class: [%3$s]"""
311+
.formatted(factoryType.getSimpleName(), factoryImplementationName, ex.getMessage()));
312+
}
313+
}
314+
315+
private static List<String> classNames(Collection<?> components) {
316+
return components.stream().map(Object::getClass).map(Class::getName).toList();
317+
}
318+
319+
private static List<String> classSimpleNames(Collection<?> components) {
320+
return components.stream().map(Object::getClass).map(Class::getSimpleName).toList();
321+
}
322+
251323
}

spring-test/src/main/java/org/springframework/test/context/support/AbstractTestContextBootstrapper.java

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import org.springframework.core.io.support.SpringFactoriesLoader;
3636
import org.springframework.core.io.support.SpringFactoriesLoader.FailureHandler;
3737
import org.springframework.lang.Nullable;
38-
import org.springframework.test.context.ApplicationContextFailureProcessor;
3938
import org.springframework.test.context.BootstrapContext;
4039
import org.springframework.test.context.CacheAwareContextLoaderDelegate;
4140
import org.springframework.test.context.ContextConfiguration;
@@ -523,29 +522,7 @@ else if (logger.isDebugEnabled()) {
523522
* @see #getApplicationContextFailureProcessor()
524523
*/
525524
protected CacheAwareContextLoaderDelegate getCacheAwareContextLoaderDelegate() {
526-
CacheAwareContextLoaderDelegate delegate = getBootstrapContext().getCacheAwareContextLoaderDelegate();
527-
ApplicationContextFailureProcessor contextFailureProcessor = getApplicationContextFailureProcessor();
528-
if (contextFailureProcessor != null) {
529-
delegate.setContextFailureProcessor(contextFailureProcessor);
530-
}
531-
return delegate;
532-
}
533-
534-
/**
535-
* Get the {@link ApplicationContextFailureProcessor} to use.
536-
* <p>The default implementation returns {@code null}.
537-
* <p>Concrete subclasses may choose to override this method to provide an
538-
* {@code ApplicationContextFailureProcessor} that will be supplied to the
539-
* configured {@code CacheAwareContextLoaderDelegate} in
540-
* {@link #getCacheAwareContextLoaderDelegate()}.
541-
* @return the context failure processor to use, or {@code null} if no processor
542-
* should be used
543-
* @since 6.0
544-
* @see #getCacheAwareContextLoaderDelegate()
545-
*/
546-
@Nullable
547-
protected ApplicationContextFailureProcessor getApplicationContextFailureProcessor() {
548-
return null;
525+
return getBootstrapContext().getCacheAwareContextLoaderDelegate();
549526
}
550527

551528
/**

spring-test/src/test/java/org/springframework/test/context/failures/ContextLoadFailureTests.java

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616

1717
package org.springframework.test.context.failures;
1818

19-
import java.util.ArrayList;
20-
import java.util.List;
21-
2219
import org.junit.jupiter.api.AfterEach;
2320
import org.junit.jupiter.api.BeforeEach;
2421
import org.junit.jupiter.api.Test;
@@ -30,11 +27,9 @@
3027
import org.springframework.context.annotation.Bean;
3128
import org.springframework.context.annotation.Configuration;
3229
import org.springframework.context.support.GenericApplicationContext;
33-
import org.springframework.test.context.ApplicationContextFailureProcessor;
34-
import org.springframework.test.context.BootstrapWith;
30+
import org.springframework.test.context.failures.TrackingApplicationContextFailureProcessor.LoadFailure;
3531
import org.springframework.test.context.junit.jupiter.FailingTestCase;
3632
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
37-
import org.springframework.test.context.support.DefaultTestContextBootstrapper;
3833

3934
import static org.assertj.core.api.Assertions.assertThat;
4035
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
@@ -47,27 +42,24 @@
4742
*/
4843
class ContextLoadFailureTests {
4944

50-
static List<LoadFailure> loadFailures = new ArrayList<>();
51-
52-
5345
@BeforeEach
5446
@AfterEach
5547
void clearFailures() {
56-
loadFailures.clear();
48+
TrackingApplicationContextFailureProcessor.loadFailures.clear();
5749
}
5850

5951
@Test
6052
void customBootstrapperAppliesApplicationContextFailureProcessor() {
61-
assertThat(loadFailures).isEmpty();
53+
assertThat(TrackingApplicationContextFailureProcessor.loadFailures).isEmpty();
6254

6355
EngineTestKit.engine("junit-jupiter")
6456
.selectors(selectClass(ExplosiveContextTestCase.class))//
6557
.execute()
6658
.testEvents()
6759
.assertStatistics(stats -> stats.started(1).succeeded(0).failed(1));
6860

69-
assertThat(loadFailures).hasSize(1);
70-
LoadFailure loadFailure = loadFailures.get(0);
61+
assertThat(TrackingApplicationContextFailureProcessor.loadFailures).hasSize(1);
62+
LoadFailure loadFailure = TrackingApplicationContextFailureProcessor.loadFailures.get(0);
7163
assertThat(loadFailure.context()).isExactlyInstanceOf(GenericApplicationContext.class);
7264
assertThat(loadFailure.exception())
7365
.isInstanceOf(BeanCreationException.class)
@@ -78,7 +70,6 @@ void customBootstrapperAppliesApplicationContextFailureProcessor() {
7870

7971
@FailingTestCase
8072
@SpringJUnitConfig
81-
@BootstrapWith(CustomTestContextBootstrapper.class)
8273
static class ExplosiveContextTestCase {
8374

8475
@Test
@@ -96,14 +87,4 @@ String explosion() {
9687
}
9788
}
9889

99-
static class CustomTestContextBootstrapper extends DefaultTestContextBootstrapper {
100-
101-
@Override
102-
protected ApplicationContextFailureProcessor getApplicationContextFailureProcessor() {
103-
return (context, exception) -> loadFailures.add(new LoadFailure(context, exception));
104-
}
105-
}
106-
107-
record LoadFailure(ApplicationContext context, Throwable exception) {}
108-
10990
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context.failures;
18+
19+
import java.util.ArrayList;
20+
import java.util.Collections;
21+
import java.util.List;
22+
23+
import org.springframework.context.ApplicationContext;
24+
import org.springframework.test.context.ApplicationContextFailureProcessor;
25+
26+
/**
27+
* Demo {@link ApplicationContextFailureProcessor} for tests that tracks
28+
* {@linkplain LoadFailure load failures} that can be queried via
29+
* {@link #loadFailures}.
30+
*
31+
* @author Sam Brannen
32+
* @since 6.0
33+
*/
34+
public class TrackingApplicationContextFailureProcessor implements ApplicationContextFailureProcessor {
35+
36+
public static List<LoadFailure> loadFailures = Collections.synchronizedList(new ArrayList<>());
37+
38+
39+
@Override
40+
public void processLoadFailure(ApplicationContext context, Throwable exception) {
41+
loadFailures.add(new LoadFailure(context, exception));
42+
}
43+
44+
public record LoadFailure(ApplicationContext context, Throwable exception) {}
45+
46+
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
# Test configuration file containing a non-existent default TestExecutionListener and a demo ContextCustomizerFactory.
1+
# Test configuration file containing a non-existent default TestExecutionListener,
2+
# a demo ContextCustomizerFactory, and a demo ApplicationContextFailureProcessor.
23

34
org.springframework.test.context.TestExecutionListener = org.example.FooListener
45

56
org.springframework.test.context.ContextCustomizerFactory =\
67
org.springframework.test.context.aot.samples.basic.ImportsContextCustomizerFactory
8+
9+
org.springframework.test.context.ApplicationContextFailureProcessor =\
10+
org.springframework.test.context.failures.TrackingApplicationContextFailureProcessor

0 commit comments

Comments
 (0)