Polish ContextLoader support

This commit is contained in:
Sam Brannen 2022-08-03 18:37:54 +03:00
parent e3f4d810f6
commit 855a3e5221
4 changed files with 16 additions and 17 deletions

View File

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

View File

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

View File

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

View File

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