|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2016 the original author or authors. |
|
|
|
* Copyright 2002-2018 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -63,14 +63,12 @@ public abstract class AbstractGenericWebContextLoader extends AbstractContextLoa |
|
|
|
protected static final Log logger = LogFactory.getLog(AbstractGenericWebContextLoader.class); |
|
|
|
protected static final Log logger = LogFactory.getLog(AbstractGenericWebContextLoader.class); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// --- SmartContextLoader -----------------------------------------------
|
|
|
|
// SmartContextLoader
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Load a Spring {@link WebApplicationContext} from the supplied |
|
|
|
* Load a Spring {@link WebApplicationContext} from the supplied |
|
|
|
* {@link MergedContextConfiguration}. |
|
|
|
* {@link MergedContextConfiguration}. |
|
|
|
* |
|
|
|
|
|
|
|
* <p>Implementation details: |
|
|
|
* <p>Implementation details: |
|
|
|
* |
|
|
|
|
|
|
|
* <ul> |
|
|
|
* <ul> |
|
|
|
* <li>Calls {@link #validateMergedContextConfiguration(WebMergedContextConfiguration)} |
|
|
|
* <li>Calls {@link #validateMergedContextConfiguration(WebMergedContextConfiguration)} |
|
|
|
* to allow subclasses to validate the supplied configuration before proceeding.</li> |
|
|
|
* to allow subclasses to validate the supplied configuration before proceeding.</li> |
|
|
|
@ -97,16 +95,15 @@ public abstract class AbstractGenericWebContextLoader extends AbstractContextLoa |
|
|
|
* <li>{@link ConfigurableApplicationContext#refresh Refreshes} the |
|
|
|
* <li>{@link ConfigurableApplicationContext#refresh Refreshes} the |
|
|
|
* context and registers a JVM shutdown hook for it.</li> |
|
|
|
* context and registers a JVM shutdown hook for it.</li> |
|
|
|
* </ul> |
|
|
|
* </ul> |
|
|
|
* |
|
|
|
|
|
|
|
* @return a new web application context |
|
|
|
* @return a new web application context |
|
|
|
* @see org.springframework.test.context.SmartContextLoader#loadContext(MergedContextConfiguration) |
|
|
|
* @see org.springframework.test.context.SmartContextLoader#loadContext(MergedContextConfiguration) |
|
|
|
* @see GenericWebApplicationContext |
|
|
|
* @see GenericWebApplicationContext |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public final ConfigurableApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception { |
|
|
|
public final ConfigurableApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception { |
|
|
|
Assert.isTrue(mergedConfig instanceof WebMergedContextConfiguration, () -> String.format( |
|
|
|
Assert.isTrue(mergedConfig instanceof WebMergedContextConfiguration, |
|
|
|
"Cannot load WebApplicationContext from non-web merged context configuration %s. " |
|
|
|
() -> String.format("Cannot load WebApplicationContext from non-web merged context configuration %s. " + |
|
|
|
+ "Consider annotating your test class with @WebAppConfiguration.", mergedConfig)); |
|
|
|
"Consider annotating your test class with @WebAppConfiguration.", mergedConfig)); |
|
|
|
|
|
|
|
|
|
|
|
WebMergedContextConfiguration webMergedConfig = (WebMergedContextConfiguration) mergedConfig; |
|
|
|
WebMergedContextConfiguration webMergedConfig = (WebMergedContextConfiguration) mergedConfig; |
|
|
|
|
|
|
|
|
|
|
|
@ -150,20 +147,15 @@ public abstract class AbstractGenericWebContextLoader extends AbstractContextLoa |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Configures web resources for the supplied web application context (WAC). |
|
|
|
* Configures web resources for the supplied web application context (WAC). |
|
|
|
* |
|
|
|
|
|
|
|
* <h4>Implementation Details</h4> |
|
|
|
* <h4>Implementation Details</h4> |
|
|
|
* |
|
|
|
|
|
|
|
* <p>If the supplied WAC has no parent or its parent is not a WAC, the |
|
|
|
* <p>If the supplied WAC has no parent or its parent is not a WAC, the |
|
|
|
* supplied WAC will be configured as the Root WAC (see "<em>Root WAC |
|
|
|
* supplied WAC will be configured as the Root WAC (see "<em>Root WAC |
|
|
|
* Configuration</em>" below). |
|
|
|
* Configuration</em>" below). |
|
|
|
* |
|
|
|
|
|
|
|
* <p>Otherwise the context hierarchy of the supplied WAC will be traversed |
|
|
|
* <p>Otherwise the context hierarchy of the supplied WAC will be traversed |
|
|
|
* to find the top-most WAC (i.e., the root); and the {@link ServletContext} |
|
|
|
* to find the top-most WAC (i.e., the root); and the {@link ServletContext} |
|
|
|
* of the Root WAC will be set as the {@code ServletContext} for the supplied |
|
|
|
* of the Root WAC will be set as the {@code ServletContext} for the supplied |
|
|
|
* WAC. |
|
|
|
* WAC. |
|
|
|
* |
|
|
|
|
|
|
|
* <h4>Root WAC Configuration</h4> |
|
|
|
* <h4>Root WAC Configuration</h4> |
|
|
|
* |
|
|
|
|
|
|
|
* <ul> |
|
|
|
* <ul> |
|
|
|
* <li>The resource base path is retrieved from the supplied |
|
|
|
* <li>The resource base path is retrieved from the supplied |
|
|
|
* {@code WebMergedContextConfiguration}.</li> |
|
|
|
* {@code WebMergedContextConfiguration}.</li> |
|
|
|
@ -178,24 +170,20 @@ public abstract class AbstractGenericWebContextLoader extends AbstractContextLoa |
|
|
|
* {@link WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE} key.</li> |
|
|
|
* {@link WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE} key.</li> |
|
|
|
* <li>Finally, the {@code MockServletContext} is set in the |
|
|
|
* <li>Finally, the {@code MockServletContext} is set in the |
|
|
|
* {@code WebApplicationContext}.</li> |
|
|
|
* {@code WebApplicationContext}.</li> |
|
|
|
* |
|
|
|
* @param context the web application context for which to configure the web resources |
|
|
|
* @param context the web application context for which to configure the web |
|
|
|
* @param webMergedConfig the merged context configuration to use to load the web application context |
|
|
|
* resources |
|
|
|
|
|
|
|
* @param webMergedConfig the merged context configuration to use to load the |
|
|
|
|
|
|
|
* web application context |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected void configureWebResources(GenericWebApplicationContext context, |
|
|
|
protected void configureWebResources(GenericWebApplicationContext context, |
|
|
|
WebMergedContextConfiguration webMergedConfig) { |
|
|
|
WebMergedContextConfiguration webMergedConfig) { |
|
|
|
|
|
|
|
|
|
|
|
ApplicationContext parent = context.getParent(); |
|
|
|
ApplicationContext parent = context.getParent(); |
|
|
|
|
|
|
|
|
|
|
|
// if the WAC has no parent or the parent is not a WAC, set the WAC as
|
|
|
|
// If the WebApplicationContext has no parent or the parent is not a WebApplicationContext,
|
|
|
|
// the Root WAC:
|
|
|
|
// set the current context as the root WebApplicationContext:
|
|
|
|
if (parent == null || (!(parent instanceof WebApplicationContext))) { |
|
|
|
if (parent == null || (!(parent instanceof WebApplicationContext))) { |
|
|
|
String resourceBasePath = webMergedConfig.getResourceBasePath(); |
|
|
|
String resourceBasePath = webMergedConfig.getResourceBasePath(); |
|
|
|
ResourceLoader resourceLoader = resourceBasePath.startsWith(ResourceLoader.CLASSPATH_URL_PREFIX) ? new DefaultResourceLoader() |
|
|
|
ResourceLoader resourceLoader = (resourceBasePath.startsWith(ResourceLoader.CLASSPATH_URL_PREFIX) ? |
|
|
|
: new FileSystemResourceLoader(); |
|
|
|
new DefaultResourceLoader() : new FileSystemResourceLoader()); |
|
|
|
|
|
|
|
|
|
|
|
ServletContext servletContext = new MockServletContext(resourceBasePath, resourceLoader); |
|
|
|
ServletContext servletContext = new MockServletContext(resourceBasePath, resourceLoader); |
|
|
|
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context); |
|
|
|
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context); |
|
|
|
context.setServletContext(servletContext); |
|
|
|
context.setServletContext(servletContext); |
|
|
|
@ -203,7 +191,7 @@ public abstract class AbstractGenericWebContextLoader extends AbstractContextLoa |
|
|
|
else { |
|
|
|
else { |
|
|
|
ServletContext servletContext = null; |
|
|
|
ServletContext servletContext = null; |
|
|
|
|
|
|
|
|
|
|
|
// find the Root WAC
|
|
|
|
// Find the root WebApplicationContext
|
|
|
|
while (parent != null) { |
|
|
|
while (parent != null) { |
|
|
|
if (parent instanceof WebApplicationContext && !(parent.getParent() instanceof WebApplicationContext)) { |
|
|
|
if (parent instanceof WebApplicationContext && !(parent.getParent() instanceof WebApplicationContext)) { |
|
|
|
servletContext = ((WebApplicationContext) parent).getServletContext(); |
|
|
|
servletContext = ((WebApplicationContext) parent).getServletContext(); |
|
|
|
@ -219,10 +207,8 @@ public abstract class AbstractGenericWebContextLoader extends AbstractContextLoa |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Customize the internal bean factory of the {@code WebApplicationContext} |
|
|
|
* Customize the internal bean factory of the {@code WebApplicationContext} |
|
|
|
* created by this context loader. |
|
|
|
* created by this context loader. |
|
|
|
* |
|
|
|
|
|
|
|
* <p>The default implementation is empty but can be overridden in subclasses |
|
|
|
* <p>The default implementation is empty but can be overridden in subclasses |
|
|
|
* to customize {@code DefaultListableBeanFactory}'s standard settings. |
|
|
|
* to customize {@code DefaultListableBeanFactory}'s standard settings. |
|
|
|
* |
|
|
|
|
|
|
|
* @param beanFactory the bean factory created by this context loader |
|
|
|
* @param beanFactory the bean factory created by this context loader |
|
|
|
* @param webMergedConfig the merged context configuration to use to load the |
|
|
|
* @param webMergedConfig the merged context configuration to use to load the |
|
|
|
* web application context |
|
|
|
* web application context |
|
|
|
@ -232,57 +218,55 @@ public abstract class AbstractGenericWebContextLoader extends AbstractContextLoa |
|
|
|
* @see DefaultListableBeanFactory#setAllowCircularReferences |
|
|
|
* @see DefaultListableBeanFactory#setAllowCircularReferences |
|
|
|
* @see DefaultListableBeanFactory#setAllowRawInjectionDespiteWrapping |
|
|
|
* @see DefaultListableBeanFactory#setAllowRawInjectionDespiteWrapping |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory, |
|
|
|
protected void customizeBeanFactory( |
|
|
|
WebMergedContextConfiguration webMergedConfig) { |
|
|
|
DefaultListableBeanFactory beanFactory, WebMergedContextConfiguration webMergedConfig) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Load bean definitions into the supplied {@link GenericWebApplicationContext context} |
|
|
|
* Load bean definitions into the supplied {@link GenericWebApplicationContext context} |
|
|
|
* from the locations or classes in the supplied {@code WebMergedContextConfiguration}. |
|
|
|
* from the locations or classes in the supplied {@code WebMergedContextConfiguration}. |
|
|
|
* |
|
|
|
|
|
|
|
* <p>Concrete subclasses must provide an appropriate implementation. |
|
|
|
* <p>Concrete subclasses must provide an appropriate implementation. |
|
|
|
* |
|
|
|
|
|
|
|
* @param context the context into which the bean definitions should be loaded |
|
|
|
* @param context the context into which the bean definitions should be loaded |
|
|
|
* @param webMergedConfig the merged context configuration to use to load the |
|
|
|
* @param webMergedConfig the merged context configuration to use to load the |
|
|
|
* web application context |
|
|
|
* web application context |
|
|
|
* @see #loadContext(MergedContextConfiguration) |
|
|
|
* @see #loadContext(MergedContextConfiguration) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected abstract void loadBeanDefinitions(GenericWebApplicationContext context, |
|
|
|
protected abstract void loadBeanDefinitions( |
|
|
|
WebMergedContextConfiguration webMergedConfig); |
|
|
|
GenericWebApplicationContext context, WebMergedContextConfiguration webMergedConfig); |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Customize the {@link GenericWebApplicationContext} created by this context |
|
|
|
* Customize the {@link GenericWebApplicationContext} created by this context |
|
|
|
* loader <i>after</i> bean definitions have been loaded into the context but |
|
|
|
* loader <i>after</i> bean definitions have been loaded into the context but |
|
|
|
* <i>before</i> the context is refreshed. |
|
|
|
* <i>before</i> the context is refreshed. |
|
|
|
* |
|
|
|
|
|
|
|
* <p>The default implementation simply delegates to |
|
|
|
* <p>The default implementation simply delegates to |
|
|
|
* {@link AbstractContextLoader#customizeContext(ConfigurableApplicationContext, MergedContextConfiguration)}. |
|
|
|
* {@link AbstractContextLoader#customizeContext(ConfigurableApplicationContext, MergedContextConfiguration)}. |
|
|
|
* |
|
|
|
|
|
|
|
* @param context the newly created web application context |
|
|
|
* @param context the newly created web application context |
|
|
|
* @param webMergedConfig the merged context configuration to use to load the |
|
|
|
* @param webMergedConfig the merged context configuration to use to load the |
|
|
|
* web application context |
|
|
|
* web application context |
|
|
|
* @see #loadContext(MergedContextConfiguration) |
|
|
|
* @see #loadContext(MergedContextConfiguration) |
|
|
|
* @see #customizeContext(ConfigurableApplicationContext, MergedContextConfiguration) |
|
|
|
* @see #customizeContext(ConfigurableApplicationContext, MergedContextConfiguration) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected void customizeContext(GenericWebApplicationContext context, WebMergedContextConfiguration webMergedConfig) { |
|
|
|
protected void customizeContext( |
|
|
|
|
|
|
|
GenericWebApplicationContext context, WebMergedContextConfiguration webMergedConfig) { |
|
|
|
|
|
|
|
|
|
|
|
super.customizeContext(context, webMergedConfig); |
|
|
|
super.customizeContext(context, webMergedConfig); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// --- ContextLoader -------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
// ContextLoader
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* {@code AbstractGenericWebContextLoader} should be used as a |
|
|
|
* {@code AbstractGenericWebContextLoader} should be used as a |
|
|
|
* {@link org.springframework.test.context.SmartContextLoader SmartContextLoader}, |
|
|
|
* {@link org.springframework.test.context.SmartContextLoader SmartContextLoader}, |
|
|
|
* not as a legacy {@link org.springframework.test.context.ContextLoader ContextLoader}. |
|
|
|
* not as a legacy {@link org.springframework.test.context.ContextLoader ContextLoader}. |
|
|
|
* Consequently, this method is not supported. |
|
|
|
* Consequently, this method is not supported. |
|
|
|
* |
|
|
|
|
|
|
|
* @see org.springframework.test.context.ContextLoader#loadContext(java.lang.String[]) |
|
|
|
* @see org.springframework.test.context.ContextLoader#loadContext(java.lang.String[]) |
|
|
|
* @throws UnsupportedOperationException |
|
|
|
* @throws UnsupportedOperationException in this implementation |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public final ApplicationContext loadContext(String... locations) throws Exception { |
|
|
|
public final ApplicationContext loadContext(String... locations) throws Exception { |
|
|
|
throw new UnsupportedOperationException( |
|
|
|
throw new UnsupportedOperationException( |
|
|
|
"AbstractGenericWebContextLoader does not support the loadContext(String... locations) method"); |
|
|
|
"AbstractGenericWebContextLoader does not support the loadContext(String... locations) method"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|