AbstractGenericWebContextLoader.java 4.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright 2002-2012 the original author or authors.
 *
 * 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 23 24 25 26 27 28 29 30 31

import javax.servlet.ServletContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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;
32
import org.springframework.test.context.support.AbstractContextLoader;
33 34 35 36
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.GenericWebApplicationContext;

/**
S
Sam Brannen 已提交
37
 * TODO [SPR-9864] Document AbstractGenericWebContextLoader.
38 39 40 41 42 43 44 45 46 47 48 49
 *
 * @author Sam Brannen
 * @since 3.2
 */
public abstract class AbstractGenericWebContextLoader extends AbstractContextLoader {

	private static final Log logger = LogFactory.getLog(AbstractGenericWebContextLoader.class);


	// --- SmartContextLoader -----------------------------------------------

	/**
S
Sam Brannen 已提交
50
	 * TODO [SPR-9864] Document overridden loadContext(MergedContextConfiguration).
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
	 *
	 * @see org.springframework.test.context.SmartContextLoader#loadContext(org.springframework.test.context.MergedContextConfiguration)
	 */
	public final ConfigurableApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception {

		if (!(mergedConfig instanceof WebMergedContextConfiguration)) {
			throw new IllegalArgumentException(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 (logger.isDebugEnabled()) {
			logger.debug(String.format("Loading WebApplicationContext for merged context configuration %s.",
				webMergedConfig));
		}

		GenericWebApplicationContext context = new GenericWebApplicationContext();
		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;
	}

	/**
S
Sam Brannen 已提交
81
	 * TODO [SPR-9864] Document configureWebResources().
82 83 84 85 86 87 88 89 90 91 92 93 94 95
	 */
	protected void configureWebResources(GenericWebApplicationContext context,
			WebMergedContextConfiguration webMergedConfig) {

		String resourceBasePath = webMergedConfig.getResourceBasePath();
		ResourceLoader resourceLoader = resourceBasePath.startsWith(ResourceLoader.CLASSPATH_URL_PREFIX) ? new DefaultResourceLoader()
				: new FileSystemResourceLoader();

		ServletContext servletContext = new MockServletContext(resourceBasePath, resourceLoader);
		servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
		context.setServletContext(servletContext);
	}

	/**
S
Sam Brannen 已提交
96
	 * TODO [SPR-9864] Document customizeBeanFactory().
97 98 99 100 101 102
	 */
	protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory,
			WebMergedContextConfiguration webMergedConfig) {
	}

	/**
S
Sam Brannen 已提交
103
	 * TODO [SPR-9864] Document loadBeanDefinitions().
104 105 106 107 108
	 */
	protected abstract void loadBeanDefinitions(GenericWebApplicationContext context,
			WebMergedContextConfiguration webMergedConfig);

	/**
S
Sam Brannen 已提交
109
	 * TODO [SPR-9864] Document customizeContext().
110 111 112 113 114 115 116
	 */
	protected void customizeContext(GenericWebApplicationContext context, WebMergedContextConfiguration webMergedConfig) {
	}

	// --- ContextLoader -------------------------------------------------------

	/**
S
Sam Brannen 已提交
117
	 * TODO [SPR-9864] Document overridden loadContext(String...).
118 119 120 121 122 123 124 125 126
	 *
	 * @see org.springframework.test.context.ContextLoader#loadContext(java.lang.String[])
	 */
	public final ApplicationContext loadContext(String... locations) throws Exception {
		throw new UnsupportedOperationException(
			"AbstractGenericWebContextLoader does not support the loadContext(String... locations) method");
	}

}