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()
* 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.

View File

@ -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.
* <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}.

View File

@ -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 {

View File

@ -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();