Browse Source

Polish ContextLoader support

pull/28922/head
Sam Brannen 4 years ago
parent
commit
855a3e5221
  1. 4
      spring-test/src/main/java/org/springframework/test/context/SmartContextLoader.java
  2. 3
      spring-test/src/main/java/org/springframework/test/context/support/AbstractDelegatingSmartContextLoader.java
  3. 5
      spring-test/src/main/java/org/springframework/test/context/support/AbstractGenericContextLoader.java
  4. 21
      spring-test/src/main/java/org/springframework/test/context/web/AbstractGenericWebContextLoader.java

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

@ -146,8 +146,8 @@ public interface SmartContextLoader extends ContextLoader { @@ -146,8 +146,8 @@ public interface SmartContextLoader extends ContextLoader {
* {@linkplain org.springframework.context.ConfigurableApplicationContext#refresh()
* refresh} the {@code ApplicationContext} or
* {@linkplain org.springframework.context.ConfigurableApplicationContext#registerShutdownHook()
* register a JVM shutdown hook} for it. Otherwise, this method should behave
* identical to {@link #loadContext(MergedContextConfiguration)}.
* register a JVM shutdown hook} for it. Otherwise, this method should implement
* behavior identical to {@link #loadContext(MergedContextConfiguration)}.
* <p>The default implementation throws an {@link UnsupportedOperationException}.
* Concrete implementations must therefore override this method in order to
* support AOT (ahead of time) processing.

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

@ -195,7 +195,6 @@ public abstract class AbstractDelegatingSmartContextLoader implements SmartConte @@ -195,7 +195,6 @@ public abstract class AbstractDelegatingSmartContextLoader implements SmartConte
* @throws IllegalArgumentException if the supplied merged configuration is {@code null}
* @throws IllegalStateException if neither candidate loader is capable of loading an
* {@code ApplicationContext} from the supplied merged context configuration
* @since 6.0
*/
@Override
public final ApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception {
@ -204,7 +203,7 @@ public abstract class AbstractDelegatingSmartContextLoader implements SmartConte @@ -204,7 +203,7 @@ public abstract class AbstractDelegatingSmartContextLoader implements SmartConte
/**
* Delegates to an appropriate candidate {@code SmartContextLoader} to load
* an {@link ApplicationContext}.
* an {@link ApplicationContext} for AOT processing.
* <p>Delegation is based on explicit knowledge of the implementations of the
* default loaders for {@linkplain #getXmlLoader() XML configuration files and
* Groovy scripts} and {@linkplain #getAnnotationConfigLoader() annotated classes}.

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

@ -113,8 +113,8 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader @@ -113,8 +113,8 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader
* {@linkplain org.springframework.context.ConfigurableApplicationContext#refresh()
* refresh} the {@code ApplicationContext} or
* {@linkplain org.springframework.context.ConfigurableApplicationContext#registerShutdownHook()
* register a JVM shutdown hook} for it. Otherwise, this method behaves
* identical to {@link #loadContext(MergedContextConfiguration)}.
* register a JVM shutdown hook} for it. Otherwise, this method implements
* behavior identical to {@link #loadContext(MergedContextConfiguration)}.
* @param mergedConfig the merged context configuration to use to load the
* application context
* @return a new application context
@ -136,7 +136,6 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader @@ -136,7 +136,6 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader
* @param refresh whether to refresh the {@code ApplicationContext} and register
* a JVM shutdown hook for it
* @return a new application context
* @since 6.0
*/
private final GenericApplicationContext loadContext(
MergedContextConfiguration mergedConfig, boolean refresh) throws Exception {

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

@ -113,8 +113,8 @@ public abstract class AbstractGenericWebContextLoader extends AbstractContextLoa @@ -113,8 +113,8 @@ public abstract class AbstractGenericWebContextLoader extends AbstractContextLoa
* {@linkplain org.springframework.context.ConfigurableApplicationContext#refresh()
* refresh} the {@code ApplicationContext} or
* {@linkplain org.springframework.context.ConfigurableApplicationContext#registerShutdownHook()
* register a JVM shutdown hook} for it. Otherwise, this method behaves
* identical to {@link #loadContext(MergedContextConfiguration)}.
* register a JVM shutdown hook} for it. Otherwise, this method implements
* behavior identical to {@link #loadContext(MergedContextConfiguration)}.
* @param mergedConfig the merged context configuration to use to load the
* application context
* @return a new web application context
@ -137,18 +137,18 @@ public abstract class AbstractGenericWebContextLoader extends AbstractContextLoa @@ -137,18 +137,18 @@ public abstract class AbstractGenericWebContextLoader extends AbstractContextLoa
* @param refresh whether to refresh the {@code ApplicationContext} and register
* a JVM shutdown hook for it
* @return a new web application context
* @since 6.0
* @see org.springframework.test.context.SmartContextLoader#loadContext(MergedContextConfiguration)
* @see org.springframework.test.context.SmartContextLoader#loadContextForAotProcessing(MergedContextConfiguration)
*/
private final GenericWebApplicationContext loadContext(
MergedContextConfiguration mergedConfig, boolean refresh) throws Exception {
Assert.isTrue(mergedConfig instanceof WebMergedContextConfiguration,
() -> String.format("Cannot load WebApplicationContext from non-web merged context configuration %s. " +
"Consider annotating your test class with @WebAppConfiguration.", mergedConfig));
WebMergedContextConfiguration webMergedConfig = (WebMergedContextConfiguration) mergedConfig;
if (!(mergedConfig instanceof WebMergedContextConfiguration webMergedConfig)) {
throw new IllegalArgumentException("""
Cannot load WebApplicationContext from non-web merged context configuration %s. \
Consider annotating your test class with @WebAppConfiguration."""
.formatted(mergedConfig));
}
if (logger.isDebugEnabled()) {
logger.debug(String.format("Loading WebApplicationContext for merged context configuration %s.",
@ -240,8 +240,9 @@ public abstract class AbstractGenericWebContextLoader extends AbstractContextLoa @@ -240,8 +240,9 @@ public abstract class AbstractGenericWebContextLoader extends AbstractContextLoa
ServletContext servletContext = null;
// Find the root WebApplicationContext
while (parent != null) {
if (parent instanceof WebApplicationContext && !(parent.getParent() instanceof WebApplicationContext)) {
servletContext = ((WebApplicationContext) parent).getServletContext();
if (parent instanceof WebApplicationContext parentWac &&
!(parent.getParent() instanceof WebApplicationContext)) {
servletContext = parentWac.getServletContext();
break;
}
parent = parent.getParent();

Loading…
Cancel
Save