Skip to content

Commit 72548f7

Browse files
committed
Improve documentation of SmartContextLoader contracts
1 parent ee33aa6 commit 72548f7

File tree

1 file changed

+48
-34
lines changed

1 file changed

+48
-34
lines changed

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

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@
2020
import org.springframework.lang.Nullable;
2121

2222
/**
23-
* Strategy interface for loading an {@link ApplicationContext application context}
24-
* for an integration test managed by the Spring TestContext Framework.
23+
* Strategy interface for loading an {@link ApplicationContext} for an integration
24+
* test managed by the Spring TestContext Framework.
2525
*
2626
* <p>The {@code SmartContextLoader} SPI supersedes the {@link ContextLoader} SPI
2727
* introduced in Spring 2.5: a {@code SmartContextLoader} can choose to process
28-
* either resource locations or annotated classes. Furthermore, a
29-
* {@code SmartContextLoader} can set active bean definition profiles in the
30-
* context that it loads (see {@link MergedContextConfiguration#getActiveProfiles()}
31-
* and {@link #loadContext(MergedContextConfiguration)}).
28+
* resource locations, annotated classes, or a combination of both. Furthermore, a
29+
* {@code SmartContextLoader} can configure the context that it
30+
* {@linkplain #loadContext(MergedContextConfiguration) loads} based on any
31+
* properties available in the provided {@link MergedContextConfiguration}. For
32+
* example, active bean definition profiles can be configured for the context
33+
* based on {@link MergedContextConfiguration#getActiveProfiles()}.
3234
*
3335
* <p>See the Javadoc for {@link ContextConfiguration @ContextConfiguration}
3436
* for a definition of <em>annotated class</em>.
@@ -45,11 +47,8 @@
4547
* hierarchy of the root test class and then supplied to
4648
* {@link #loadContext(MergedContextConfiguration) loadContext()}.
4749
*
48-
* <p>Even though {@code SmartContextLoader} extends {@code ContextLoader},
49-
* clients should favor {@code SmartContextLoader}-specific methods over those
50-
* defined in {@code ContextLoader}, particularly because a
51-
* {@code SmartContextLoader} may choose not to support methods defined in the
52-
* {@code ContextLoader} SPI.
50+
* <p>NOTE: As of Spring Framework 6.0, {@code SmartContextLoader} no longer
51+
* supports methods defined in the {@code ContextLoader} SPI.
5352
*
5453
* <p>Concrete implementations must provide a {@code public} no-args constructor.
5554
*
@@ -58,43 +57,45 @@
5857
* <li>{@link org.springframework.test.context.support.DelegatingSmartContextLoader DelegatingSmartContextLoader}</li>
5958
* <li>{@link org.springframework.test.context.support.AnnotationConfigContextLoader AnnotationConfigContextLoader}</li>
6059
* <li>{@link org.springframework.test.context.support.GenericXmlContextLoader GenericXmlContextLoader}</li>
60+
* <li>{@link org.springframework.test.context.support.GenericGroovyXmlContextLoader GenericGroovyXmlContextLoader}</li>
6161
* <li>{@link org.springframework.test.context.web.WebDelegatingSmartContextLoader WebDelegatingSmartContextLoader}</li>
6262
* <li>{@link org.springframework.test.context.web.AnnotationConfigWebContextLoader AnnotationConfigWebContextLoader}</li>
6363
* <li>{@link org.springframework.test.context.web.GenericXmlWebContextLoader GenericXmlWebContextLoader}</li>
64+
* <li>{@link org.springframework.test.context.web.GenericGroovyXmlWebContextLoader GenericGroovyXmlWebContextLoader}</li>
6465
* </ul>
6566
*
6667
* @author Sam Brannen
6768
* @since 3.1
6869
* @see ContextConfiguration
6970
* @see ActiveProfiles
71+
* @see TestPropertySource
7072
* @see ContextConfigurationAttributes
7173
* @see MergedContextConfiguration
7274
*/
7375
public interface SmartContextLoader extends ContextLoader {
7476

7577
/**
76-
* Processes the {@link ContextConfigurationAttributes} for a given test class.
78+
* Process the {@link ContextConfigurationAttributes} for a given test class.
7779
* <p>Concrete implementations may choose to <em>modify</em> the {@code locations}
78-
* or {@code classes} in the supplied {@link ContextConfigurationAttributes},
80+
* or {@code classes} in the supplied {@code ContextConfigurationAttributes},
7981
* <em>generate</em> default configuration locations, or <em>detect</em>
8082
* default configuration classes if the supplied values are {@code null}
8183
* or empty.
82-
* <p><b>Note</b>: in contrast to a standard {@code ContextLoader}, a
83-
* {@code SmartContextLoader} <b>must</b> <em>preemptively</em> verify that
84-
* a generated or detected default actually exists before setting the corresponding
85-
* {@code locations} or {@code classes} property in the supplied
86-
* {@link ContextConfigurationAttributes}. Consequently, leaving the
87-
* {@code locations} or {@code classes} property empty signals that
88-
* this {@code SmartContextLoader} was not able to generate or detect defaults.
84+
* <p><b>Note</b>: a {@code SmartContextLoader} must <em>preemptively</em>
85+
* verify that a generated or detected default actually exists before setting
86+
* the corresponding {@code locations} or {@code classes} property in the
87+
* supplied {@code ContextConfigurationAttributes}. Consequently, leaving the
88+
* {@code locations} or {@code classes} property empty signals that this
89+
* {@code SmartContextLoader} was not able to generate or detect defaults.
8990
* @param configAttributes the context configuration attributes to process
9091
*/
9192
void processContextConfiguration(ContextConfigurationAttributes configAttributes);
9293

9394
/**
94-
* Loads a new {@link ApplicationContext context} based on the supplied
95+
* Load a new {@linkplain ApplicationContext context} based on the supplied
9596
* {@link MergedContextConfiguration merged context configuration},
96-
* configures the context, and finally returns the context in a fully
97-
* <em>refreshed</em> state.
97+
* configure the context, and return the context in a fully <em>refreshed</em>
98+
* state.
9899
* <p>Concrete implementations should register annotation configuration
99100
* processors with bean factories of
100101
* {@link ApplicationContext application contexts} loaded by this
@@ -103,22 +104,35 @@ public interface SmartContextLoader extends ContextLoader {
103104
* {@link org.springframework.beans.factory.annotation.Autowired @Autowired},
104105
* {@link jakarta.annotation.Resource @Resource}, and
105106
* {@link jakarta.inject.Inject @Inject}. In addition, concrete implementations
106-
* should set the active bean definition profiles in the context's
107-
* {@link org.springframework.core.env.Environment Environment}.
108-
* <p>Any {@code ApplicationContext} loaded by a
109-
* {@code SmartContextLoader} <strong>must</strong> register a JVM
110-
* shutdown hook for itself. Unless the context gets closed early, all context
111-
* instances will be automatically closed on JVM shutdown. This allows for
112-
* freeing of external resources held by beans within the context (e.g.,
113-
* temporary files).
107+
* should perform the following actions.
108+
* <ul>
109+
* <li>Set the parent {@code ApplicationContext} if appropriate (see
110+
* {@link MergedContextConfiguration#getParent()}).</li>
111+
* <li>Set the active bean definition profiles in the context's
112+
* {@link org.springframework.core.env.Environment Environment} (see
113+
* {@link MergedContextConfiguration#getActiveProfiles()}).</li>
114+
* <li>Add test {@link org.springframework.core.env.PropertySource PropertySources}
115+
* to the {@code Environment} (see
116+
* {@link MergedContextConfiguration#getPropertySourceLocations()},
117+
* {@link MergedContextConfiguration#getPropertySourceProperties()}, and
118+
* {@link org.springframework.test.context.support.TestPropertySourceUtils
119+
* TestPropertySourceUtils}).</li>
120+
* <li>Invoke {@link org.springframework.context.ApplicationContextInitializer
121+
* ApplicationContextInitializers} (see
122+
* {@link MergedContextConfiguration#getContextInitializerClasses()}).</li>
123+
* <li>Invoke {@link ContextCustomizer ContextCustomizers} (see
124+
* {@link MergedContextConfiguration#getContextCustomizers()}).</li>
125+
* <li>Register a JVM shutdown hook for the {@link ApplicationContext}. Unless
126+
* the context gets closed early, all context instances will be automatically
127+
* closed on JVM shutdown. This allows for freeing of external resources held
128+
* by beans within the context &mdash; for example, temporary files.</li>
129+
* </ul>
114130
* @param mergedConfig the merged context configuration to use to load the
115131
* application context
116132
* @return a new application context
117133
* @throws Exception if context loading failed
118134
* @see #processContextConfiguration(ContextConfigurationAttributes)
119-
* @see org.springframework.context.annotation.AnnotationConfigUtils
120-
* #registerAnnotationConfigProcessors(org.springframework.beans.factory.support.BeanDefinitionRegistry)
121-
* @see MergedContextConfiguration#getActiveProfiles()
135+
* @see org.springframework.context.annotation.AnnotationConfigUtils#registerAnnotationConfigProcessors(org.springframework.beans.factory.support.BeanDefinitionRegistry)
122136
* @see org.springframework.context.ConfigurableApplicationContext#getEnvironment()
123137
*/
124138
ApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception;

0 commit comments

Comments
 (0)