AbstractGenericWebContextLoader.java 12.5 KB
Newer Older
1
/*
2
 * Copyright 2002-2018 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

17
package org.springframework.test.context.web;
18 19 20 21 22

import javax.servlet.ServletContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
23

24 25 26 27 28 29 30 31 32
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigUtils;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.FileSystemResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.context.MergedContextConfiguration;
33
import org.springframework.test.context.support.AbstractContextLoader;
34
import org.springframework.util.Assert;
35 36 37 38
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.GenericWebApplicationContext;

/**
39 40 41 42 43 44 45 46 47 48 49 50 51 52
 * Abstract, generic extension of {@link AbstractContextLoader} that loads a
 * {@link GenericWebApplicationContext}.
 *
 * <p>If instances of concrete subclasses are invoked via the
 * {@link org.springframework.test.context.SmartContextLoader SmartContextLoader}
 * SPI, the context will be loaded from the {@link MergedContextConfiguration}
 * provided to {@link #loadContext(MergedContextConfiguration)}. In such cases, a
 * {@code SmartContextLoader} will decide whether to load the context from
 * <em>locations</em> or <em>annotated classes</em>. Note that {@code
 * AbstractGenericWebContextLoader} does not support the {@code
 * loadContext(String... locations)} method from the legacy
 * {@link org.springframework.test.context.ContextLoader ContextLoader} SPI.
 *
 * <p>Concrete subclasses must provide an appropriate implementation of
C
Chris Beams 已提交
53
 * {@link #loadBeanDefinitions}.
54 55
 *
 * @author Sam Brannen
56
 * @author Phillip Webb
57
 * @since 3.2
58 59
 * @see #loadContext(MergedContextConfiguration)
 * @see #loadContext(String...)
60 61 62
 */
public abstract class AbstractGenericWebContextLoader extends AbstractContextLoader {

63
	protected static final Log logger = LogFactory.getLog(AbstractGenericWebContextLoader.class);
64 65


66
	// SmartContextLoader
67 68

	/**
69 70 71 72
	 * Load a Spring {@link WebApplicationContext} from the supplied
	 * {@link MergedContextConfiguration}.
	 * <p>Implementation details:
	 * <ul>
73 74
	 * <li>Calls {@link #validateMergedContextConfiguration(WebMergedContextConfiguration)}
	 * to allow subclasses to validate the supplied configuration before proceeding.</li>
75
	 * <li>Creates a {@link GenericWebApplicationContext} instance.</li>
76 77 78 79 80 81
	 * <li>If the supplied {@code MergedContextConfiguration} references a
	 * {@linkplain MergedContextConfiguration#getParent() parent configuration},
	 * the corresponding {@link MergedContextConfiguration#getParentApplicationContext()
	 * ApplicationContext} will be retrieved and
	 * {@linkplain GenericWebApplicationContext#setParent(ApplicationContext) set as the parent}
	 * for the context created by this method.</li>
C
Chris Beams 已提交
82
	 * <li>Delegates to {@link #configureWebResources} to create the
83
	 * {@link MockServletContext} and set it in the {@code WebApplicationContext}.</li>
C
Chris Beams 已提交
84
	 * <li>Calls {@link #prepareContext} to allow for customizing the context
85
	 * before bean definitions are loaded.</li>
C
Chris Beams 已提交
86
	 * <li>Calls {@link #customizeBeanFactory} to allow for customizing the
87
	 * context's {@code DefaultListableBeanFactory}.</li>
C
Chris Beams 已提交
88
	 * <li>Delegates to {@link #loadBeanDefinitions} to populate the context
89 90 91 92
	 * from the locations or classes in the supplied {@code MergedContextConfiguration}.</li>
	 * <li>Delegates to {@link AnnotationConfigUtils} for
	 * {@linkplain AnnotationConfigUtils#registerAnnotationConfigProcessors registering}
	 * annotation configuration processors.</li>
C
Chris Beams 已提交
93
	 * <li>Calls {@link #customizeContext} to allow for customizing the context
94 95 96 97 98 99 100
	 * before it is refreshed.</li>
	 * <li>{@link ConfigurableApplicationContext#refresh Refreshes} the
	 * context and registers a JVM shutdown hook for it.</li>
	 * </ul>
	 * @return a new web application context
	 * @see org.springframework.test.context.SmartContextLoader#loadContext(MergedContextConfiguration)
	 * @see GenericWebApplicationContext
101
	 */
102
	@Override
103
	public final ConfigurableApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception {
104 105 106
		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));
107

108 109 110 111 112 113 114
		WebMergedContextConfiguration webMergedConfig = (WebMergedContextConfiguration) mergedConfig;

		if (logger.isDebugEnabled()) {
			logger.debug(String.format("Loading WebApplicationContext for merged context configuration %s.",
				webMergedConfig));
		}

115 116
		validateMergedContextConfiguration(webMergedConfig);

117
		GenericWebApplicationContext context = new GenericWebApplicationContext();
118 119 120 121 122

		ApplicationContext parent = mergedConfig.getParentApplicationContext();
		if (parent != null) {
			context.setParent(parent);
		}
123 124 125 126 127 128 129 130 131 132 133
		configureWebResources(context, webMergedConfig);
		prepareContext(context, webMergedConfig);
		customizeBeanFactory(context.getDefaultListableBeanFactory(), webMergedConfig);
		loadBeanDefinitions(context, webMergedConfig);
		AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
		customizeContext(context, webMergedConfig);
		context.refresh();
		context.registerShutdownHook();
		return context;
	}

134 135 136 137 138 139 140 141 142 143 144 145 146 147
	/**
	 * Validate the supplied {@link WebMergedContextConfiguration} with respect to
	 * what this context loader supports.
	 * <p>The default implementation is a <em>no-op</em> but can be overridden by
	 * subclasses as appropriate.
	 * @param mergedConfig the merged configuration to validate
	 * @throws IllegalStateException if the supplied configuration is not valid
	 * for this context loader
	 * @since 4.0.4
	 */
	protected void validateMergedContextConfiguration(WebMergedContextConfiguration mergedConfig) {
		/* no-op */
	}

148
	/**
149 150 151 152 153 154 155 156 157 158
	 * Configures web resources for the supplied web application context (WAC).
	 * <h4>Implementation Details</h4>
	 * <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
	 * Configuration</em>" below).
	 * <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}
	 * of the Root WAC will be set as the {@code ServletContext} for the supplied
	 * WAC.
	 * <h4>Root WAC Configuration</h4>
159 160 161 162 163 164 165 166 167 168 169 170 171
	 * <ul>
	 * <li>The resource base path is retrieved from the supplied
	 * {@code WebMergedContextConfiguration}.</li>
	 * <li>A {@link ResourceLoader} is instantiated for the {@link MockServletContext}:
	 * if the resource base path is prefixed with "{@code classpath:}", a
	 * {@link DefaultResourceLoader} will be used; otherwise, a
	 * {@link FileSystemResourceLoader} will be used.</li>
	 * <li>A {@code MockServletContext} will be created using the resource base
	 * path and resource loader.</li>
	 * <li>The supplied {@link GenericWebApplicationContext} is then stored in
	 * the {@code MockServletContext} under the
	 * {@link WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE} key.</li>
	 * <li>Finally, the {@code MockServletContext} is set in the
172
	 * {@code WebApplicationContext}.</li>
173 174
	 * @param context the web application context for which to configure the web resources
	 * @param webMergedConfig the merged context configuration to use to load the web application context
175 176 177 178
	 */
	protected void configureWebResources(GenericWebApplicationContext context,
			WebMergedContextConfiguration webMergedConfig) {

179 180
		ApplicationContext parent = context.getParent();

181 182
		// If the WebApplicationContext has no parent or the parent is not a WebApplicationContext,
		// set the current context as the root WebApplicationContext:
183 184
		if (parent == null || (!(parent instanceof WebApplicationContext))) {
			String resourceBasePath = webMergedConfig.getResourceBasePath();
185 186
			ResourceLoader resourceLoader = (resourceBasePath.startsWith(ResourceLoader.CLASSPATH_URL_PREFIX) ?
					new DefaultResourceLoader() : new FileSystemResourceLoader());
187 188 189 190 191 192 193
			ServletContext servletContext = new MockServletContext(resourceBasePath, resourceLoader);
			servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
			context.setServletContext(servletContext);
		}
		else {
			ServletContext servletContext = null;

194
			// Find the root WebApplicationContext
195 196 197 198 199 200 201 202 203 204
			while (parent != null) {
				if (parent instanceof WebApplicationContext && !(parent.getParent() instanceof WebApplicationContext)) {
					servletContext = ((WebApplicationContext) parent).getServletContext();
					break;
				}
				parent = parent.getParent();
			}
			Assert.state(servletContext != null, "Failed to find Root WebApplicationContext in the context hierarchy");
			context.setServletContext(servletContext);
		}
205 206 207
	}

	/**
208 209 210
	 * Customize the internal bean factory of the {@code WebApplicationContext}
	 * created by this context loader.
	 * <p>The default implementation is empty but can be overridden in subclasses
211
	 * to customize {@code DefaultListableBeanFactory}'s standard settings.
212 213 214 215 216 217 218 219
	 * @param beanFactory the bean factory created by this context loader
	 * @param webMergedConfig the merged context configuration to use to load the
	 * web application context
	 * @see #loadContext(MergedContextConfiguration)
	 * @see DefaultListableBeanFactory#setAllowBeanDefinitionOverriding
	 * @see DefaultListableBeanFactory#setAllowEagerClassLoading
	 * @see DefaultListableBeanFactory#setAllowCircularReferences
	 * @see DefaultListableBeanFactory#setAllowRawInjectionDespiteWrapping
220
	 */
221 222
	protected void customizeBeanFactory(
			DefaultListableBeanFactory beanFactory, WebMergedContextConfiguration webMergedConfig) {
223 224 225
	}

	/**
226
	 * Load bean definitions into the supplied {@link GenericWebApplicationContext context}
227
	 * from the locations or classes in the supplied {@code WebMergedContextConfiguration}.
228 229 230 231 232
	 * <p>Concrete subclasses must provide an appropriate implementation.
	 * @param context the context into which the bean definitions should be loaded
	 * @param webMergedConfig the merged context configuration to use to load the
	 * web application context
	 * @see #loadContext(MergedContextConfiguration)
233
	 */
234 235
	protected abstract void loadBeanDefinitions(
			GenericWebApplicationContext context, WebMergedContextConfiguration webMergedConfig);
236 237

	/**
238 239 240
	 * Customize the {@link GenericWebApplicationContext} created by this context
	 * loader <i>after</i> bean definitions have been loaded into the context but
	 * <i>before</i> the context is refreshed.
241 242
	 * <p>The default implementation simply delegates to
	 * {@link AbstractContextLoader#customizeContext(ConfigurableApplicationContext, MergedContextConfiguration)}.
243 244 245 246
	 * @param context the newly created web application context
	 * @param webMergedConfig the merged context configuration to use to load the
	 * web application context
	 * @see #loadContext(MergedContextConfiguration)
247
	 * @see #customizeContext(ConfigurableApplicationContext, MergedContextConfiguration)
248
	 */
249 250 251
	protected void customizeContext(
			GenericWebApplicationContext context, WebMergedContextConfiguration webMergedConfig) {

252
		super.customizeContext(context, webMergedConfig);
253 254
	}

255 256

	// ContextLoader
257 258

	/**
259 260 261 262
	 * {@code AbstractGenericWebContextLoader} should be used as a
	 * {@link org.springframework.test.context.SmartContextLoader SmartContextLoader},
	 * not as a legacy {@link org.springframework.test.context.ContextLoader ContextLoader}.
	 * Consequently, this method is not supported.
263
	 * @see org.springframework.test.context.ContextLoader#loadContext(java.lang.String[])
264
	 * @throws UnsupportedOperationException in this implementation
265
	 */
266
	@Override
267 268
	public final ApplicationContext loadContext(String... locations) throws Exception {
		throw new UnsupportedOperationException(
269
				"AbstractGenericWebContextLoader does not support the loadContext(String... locations) method");
270 271 272
	}

}