Skip to content

Commit 2207d65

Browse files
committed
Deprecate methods in ContextLoader API in the TestContext framework
For Spring Framework 6.0 we have decided to deprecate the obsolete methods in the ContextLoader API in the Spring TestContext Framework in favor of the methods in the SmartContextLoader API which has been available since Spring Framework 3.1. Closes gh-28905
1 parent 9c91375 commit 2207d65

10 files changed

+73
-66
lines changed
Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,33 +19,29 @@
1919
import org.springframework.context.ApplicationContext;
2020

2121
/**
22-
* Strategy interface for loading an {@link ApplicationContext application context}
23-
* for an integration test managed by the Spring TestContext Framework.
22+
* Strategy interface for loading an {@link ApplicationContext} for an integration
23+
* test managed by the Spring TestContext Framework.
2424
*
25-
* <p><b>Note</b>: as of Spring 3.1, implement {@link SmartContextLoader} instead
26-
* of this interface in order to provide support for annotated classes, active
27-
* bean definition profiles, and application context initializers.
25+
* <p><strong>NOTE</strong>: as of Spring Framework 6.0, {@code ContextLoader} is
26+
* effectively a marker interface and should not be implemented directly. Implement
27+
* {@link SmartContextLoader} instead of this interface in order to provide support
28+
* for annotated classes, active bean definition profiles, application context
29+
* initializers, and various other features not supported by methods defined in
30+
* the {@code ContextLoader} SPI.
2831
*
29-
* <p>Clients of a ContextLoader should call
32+
* <p>Clients of a {@code ContextLoader} should call
3033
* {@link #processLocations(Class, String...) processLocations()} prior to
3134
* calling {@link #loadContext(String...) loadContext()} in case the
32-
* ContextLoader provides custom support for modifying or generating locations.
35+
* {@code ContextLoader} provides custom support for modifying or generating locations.
3336
* The results of {@link #processLocations(Class, String...) processLocations()}
3437
* should then be supplied to {@link #loadContext(String...) loadContext()}.
3538
*
3639
* <p>Concrete implementations must provide a {@code public} no-args constructor.
3740
*
38-
* <p>Spring provides the following out-of-the-box implementations:
39-
* <ul>
40-
* <li>{@link org.springframework.test.context.support.GenericXmlContextLoader GenericXmlContextLoader}</li>
41-
* <li>{@link org.springframework.test.context.support.GenericPropertiesContextLoader GenericPropertiesContextLoader}</li>
42-
* </ul>
43-
*
4441
* @author Sam Brannen
4542
* @author Juergen Hoeller
4643
* @since 2.5
4744
* @see SmartContextLoader
48-
* @see org.springframework.test.context.support.AnnotationConfigContextLoader AnnotationConfigContextLoader
4945
*/
5046
public interface ContextLoader {
5147

@@ -58,31 +54,37 @@ public interface ContextLoader {
5854
* @param locations the unmodified locations to use for loading the
5955
* application context (can be {@code null} or empty)
6056
* @return an array of application context resource locations
57+
* @deprecated as of Spring Framework 6.0, in favor of methods defined in the
58+
* {@link SmartContextLoader} SPI
6159
*/
60+
@Deprecated
6261
String[] processLocations(Class<?> clazz, String... locations);
6362

6463
/**
65-
* Loads a new {@link ApplicationContext context} based on the supplied
64+
* Loads a new {@link ApplicationContext} based on the supplied
6665
* {@code locations}, configures the context, and finally returns
6766
* the context in fully <em>refreshed</em> state.
6867
* <p>Configuration locations are generally considered to be classpath
6968
* resources by default.
7069
* <p>Concrete implementations should register annotation configuration
71-
* processors with bean factories of {@link ApplicationContext application
72-
* contexts} loaded by this ContextLoader. Beans will therefore automatically
73-
* be candidates for annotation-based dependency injection using
70+
* processors with bean factories of application contexts loaded by this
71+
* {@code ContextLoader}. Beans will therefore automatically be candidates
72+
* for annotation-based dependency injection using
7473
* {@link org.springframework.beans.factory.annotation.Autowired @Autowired},
7574
* {@link jakarta.annotation.Resource @Resource}, and
7675
* {@link jakarta.inject.Inject @Inject}.
77-
* <p>Any ApplicationContext loaded by a ContextLoader <strong>must</strong>
78-
* register a JVM shutdown hook for itself. Unless the context gets closed
79-
* early, all context instances will be automatically closed on JVM
80-
* shutdown. This allows for freeing external resources held by beans within
81-
* the context, e.g. temporary files.
76+
* <p>Any {@code ApplicationContext} loaded by a {@code ContextLoader}
77+
* <strong>must</strong> register a JVM shutdown hook for itself. Unless the
78+
* context gets closed early, all context instances will be automatically
79+
* closed on JVM shutdown. This allows for freeing external resources held by
80+
* beans within the context, e.g. temporary files.
8281
* @param locations the resource locations to use to load the application context
8382
* @return a new application context
8483
* @throws Exception if context loading failed
84+
* @deprecated as of Spring Framework 6.0, in favor of methods defined in the
85+
* {@link SmartContextLoader} SPI
8586
*/
87+
@Deprecated
8688
ApplicationContext loadContext(String... locations) throws Exception;
8789

8890
}

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.test.context;
1818

1919
import org.springframework.context.ApplicationContext;
20+
import org.springframework.lang.Nullable;
2021

2122
/**
2223
* Strategy interface for loading an {@link ApplicationContext application context}
@@ -123,4 +124,32 @@ public interface SmartContextLoader extends ContextLoader {
123124
*/
124125
ApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception;
125126

127+
/**
128+
* {@code SmartContextLoader} does not support deprecated {@link ContextLoader} methods.
129+
* Call {@link #processContextConfiguration(ContextConfigurationAttributes)} instead.
130+
* @throws UnsupportedOperationException in this implementation
131+
* @since 6.0
132+
*/
133+
@Override
134+
@SuppressWarnings("deprecation")
135+
default String[] processLocations(Class<?> clazz, @Nullable String... locations) {
136+
throw new UnsupportedOperationException("""
137+
SmartContextLoader does not support the ContextLoader SPI. \
138+
Call processContextConfiguration(ContextConfigurationAttributes) instead.""");
139+
}
140+
141+
/**
142+
* {@code SmartContextLoader} does not support deprecated {@link ContextLoader} methods.
143+
* <p>Call {@link #loadContext(MergedContextConfiguration)} instead.
144+
* @throws UnsupportedOperationException in this implementation
145+
* @since 6.0
146+
*/
147+
@Override
148+
@SuppressWarnings("deprecation")
149+
default ApplicationContext loadContext(String... locations) throws Exception {
150+
throw new UnsupportedOperationException("""
151+
SmartContextLoader does not support the ContextLoader SPI. \
152+
Call loadContext(MergedContextConfiguration) instead.""");
153+
}
154+
126155
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -85,6 +85,7 @@ protected ContextCache getContextCache() {
8585
* <p>Supports both the {@link SmartContextLoader} and {@link ContextLoader} SPIs.
8686
* @throws Exception if an error occurs while loading the application context
8787
*/
88+
@SuppressWarnings("deprecation")
8889
protected ApplicationContext loadContextInternal(MergedContextConfiguration mergedContextConfiguration)
8990
throws Exception {
9091

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -212,6 +212,7 @@ protected void customizeContext(ConfigurableApplicationContext context, MergedCo
212212
* @see #processContextConfiguration(ContextConfigurationAttributes)
213213
*/
214214
@Override
215+
@SuppressWarnings("deprecation")
215216
public final String[] processLocations(Class<?> clazz, String... locations) {
216217
return (ObjectUtils.isEmpty(locations) && isGenerateDefaultLocations()) ?
217218
generateDefaultLocations(clazz) : modifyLocations(clazz, locations);

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

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,7 +20,6 @@
2020
import org.apache.commons.logging.LogFactory;
2121

2222
import org.springframework.context.ApplicationContext;
23-
import org.springframework.lang.Nullable;
2423
import org.springframework.test.context.ContextConfiguration;
2524
import org.springframework.test.context.ContextConfigurationAttributes;
2625
import org.springframework.test.context.ContextLoader;
@@ -86,37 +85,6 @@ public abstract class AbstractDelegatingSmartContextLoader implements SmartConte
8685
protected abstract SmartContextLoader getAnnotationConfigLoader();
8786

8887

89-
// ContextLoader
90-
91-
/**
92-
* {@code AbstractDelegatingSmartContextLoader} does not support the
93-
* {@link ContextLoader#processLocations(Class, String...)} method. Call
94-
* {@link #processContextConfiguration(ContextConfigurationAttributes)} instead.
95-
* @throws UnsupportedOperationException in this implementation
96-
*/
97-
@Override
98-
public final String[] processLocations(Class<?> clazz, @Nullable String... locations) {
99-
throw new UnsupportedOperationException(
100-
"DelegatingSmartContextLoaders do not support the ContextLoader SPI. " +
101-
"Call processContextConfiguration(ContextConfigurationAttributes) instead.");
102-
}
103-
104-
/**
105-
* {@code AbstractDelegatingSmartContextLoader} does not support the
106-
* {@link ContextLoader#loadContext(String...) } method. Call
107-
* {@link #loadContext(MergedContextConfiguration)} instead.
108-
* @throws UnsupportedOperationException in this implementation
109-
*/
110-
@Override
111-
public final ApplicationContext loadContext(String... locations) throws Exception {
112-
throw new UnsupportedOperationException(
113-
"DelegatingSmartContextLoaders do not support the ContextLoader SPI. " +
114-
"Call loadContext(MergedContextConfiguration) instead.");
115-
}
116-
117-
118-
// SmartContextLoader
119-
12088
/**
12189
* Delegates to candidate {@code SmartContextLoaders} to process the supplied
12290
* {@link ContextConfigurationAttributes}.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@ protected void validateMergedContextConfiguration(MergedContextConfiguration mer
174174
* @see org.springframework.test.context.ContextLoader#loadContext
175175
* @see GenericApplicationContext
176176
* @see #loadContext(MergedContextConfiguration)
177+
* @deprecated as of Spring Framework 6.0, in favor of {@link #loadContext(MergedContextConfiguration)}
177178
*/
179+
@Deprecated
178180
@Override
179181
public final ConfigurableApplicationContext loadContext(String... locations) throws Exception {
180182
if (logger.isDebugEnabled()) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ private MergedContextConfiguration buildMergedContextConfiguration(Class<?> test
373373
classes.addAll(0, Arrays.asList(configAttributes.getClasses()));
374374
}
375375
else {
376+
@SuppressWarnings("deprecation")
376377
String[] processedLocations = contextLoader.processLocations(
377378
configAttributes.getDeclaringClass(), configAttributes.getLocations());
378379
locations.addAll(0, Arrays.asList(processedLocations));

spring-test/src/main/java/org/springframework/test/context/web/AbstractGenericWebContextLoader.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -263,6 +263,7 @@ protected void customizeContext(
263263
* @see org.springframework.test.context.ContextLoader#loadContext(java.lang.String[])
264264
*/
265265
@Override
266+
@SuppressWarnings("deprecation")
266267
public final ApplicationContext loadContext(String... locations) throws Exception {
267268
throw new UnsupportedOperationException(
268269
"AbstractGenericWebContextLoader does not support the loadContext(String... locations) method");

spring-test/src/test/java/org/springframework/test/context/support/CustomizedGenericXmlContextLoaderTests.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,20 +37,21 @@
3737
class CustomizedGenericXmlContextLoaderTests {
3838

3939
@Test
40+
@SuppressWarnings("deprecation")
4041
void customizeContext() throws Exception {
4142
StringBuilder builder = new StringBuilder();
4243
String expectedContents = "customizeContext() was called";
4344

44-
new GenericXmlContextLoader() {
45-
45+
GenericXmlContextLoader customLoader = new GenericXmlContextLoader() {
4646
@Override
4747
protected void customizeContext(GenericApplicationContext context) {
4848
assertThat(context.isActive()).as("The context should not yet have been refreshed.").isFalse();
4949
builder.append(expectedContents);
5050
}
51-
}.loadContext("classpath:/org/springframework/test/context/support/CustomizedGenericXmlContextLoaderTests-context.xml");
51+
};
52+
customLoader.loadContext("classpath:/org/springframework/test/context/support/CustomizedGenericXmlContextLoaderTests-context.xml");
5253

53-
assertThat(builder.toString()).as("customizeContext() should have been called.").isEqualTo(expectedContents);
54+
assertThat(builder).asString().as("customizeContext() should have been called.").isEqualTo(expectedContents);
5455
}
5556

5657
}

spring-test/src/test/java/org/springframework/test/context/support/GenericXmlContextLoaderResourceLocationsTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ void assertContextConfigurationLocations(Class<?> testClass, String[] expectedLo
5757
ContextConfiguration contextConfig = testClass.getAnnotation(ContextConfiguration.class);
5858
ContextLoader contextLoader = new GenericXmlContextLoader();
5959
String[] configuredLocations = (String[]) AnnotationUtils.getValue(contextConfig);
60+
@SuppressWarnings("deprecation")
6061
String[] processedLocations = contextLoader.processLocations(testClass, configuredLocations);
6162

6263
if (logger.isDebugEnabled()) {

0 commit comments

Comments
 (0)