From 855a3e5221043b8bf5a695a223253f511387e16b Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Wed, 3 Aug 2022 18:37:54 +0300 Subject: [PATCH] Polish ContextLoader support --- .../test/context/SmartContextLoader.java | 4 ++-- .../AbstractDelegatingSmartContextLoader.java | 3 +-- .../support/AbstractGenericContextLoader.java | 5 ++--- .../web/AbstractGenericWebContextLoader.java | 21 ++++++++++--------- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/SmartContextLoader.java b/spring-test/src/main/java/org/springframework/test/context/SmartContextLoader.java index 58e0be9e4ca..9dc234f2f52 100644 --- a/spring-test/src/main/java/org/springframework/test/context/SmartContextLoader.java +++ b/spring-test/src/main/java/org/springframework/test/context/SmartContextLoader.java @@ -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)}. *

The default implementation throws an {@link UnsupportedOperationException}. * Concrete implementations must therefore override this method in order to * support AOT (ahead of time) processing. diff --git a/spring-test/src/main/java/org/springframework/test/context/support/AbstractDelegatingSmartContextLoader.java b/spring-test/src/main/java/org/springframework/test/context/support/AbstractDelegatingSmartContextLoader.java index d0388a110bf..817550d8530 100644 --- a/spring-test/src/main/java/org/springframework/test/context/support/AbstractDelegatingSmartContextLoader.java +++ b/spring-test/src/main/java/org/springframework/test/context/support/AbstractDelegatingSmartContextLoader.java @@ -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 /** * Delegates to an appropriate candidate {@code SmartContextLoader} to load - * an {@link ApplicationContext}. + * an {@link ApplicationContext} for AOT processing. *

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}. diff --git a/spring-test/src/main/java/org/springframework/test/context/support/AbstractGenericContextLoader.java b/spring-test/src/main/java/org/springframework/test/context/support/AbstractGenericContextLoader.java index 49eef6d2942..805d55eb368 100644 --- a/spring-test/src/main/java/org/springframework/test/context/support/AbstractGenericContextLoader.java +++ b/spring-test/src/main/java/org/springframework/test/context/support/AbstractGenericContextLoader.java @@ -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 * @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 { diff --git a/spring-test/src/main/java/org/springframework/test/context/web/AbstractGenericWebContextLoader.java b/spring-test/src/main/java/org/springframework/test/context/web/AbstractGenericWebContextLoader.java index c83aa6432c6..0b4e7db1708 100644 --- a/spring-test/src/main/java/org/springframework/test/context/web/AbstractGenericWebContextLoader.java +++ b/spring-test/src/main/java/org/springframework/test/context/web/AbstractGenericWebContextLoader.java @@ -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 * @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 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();