ServletTestExecutionListener.java 5.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * 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.
 */

package org.springframework.test.context.web;

import javax.servlet.ServletContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
S
Sam Brannen 已提交
23

24 25 26 27 28 29 30
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.context.TestContext;
S
Sam Brannen 已提交
31
import org.springframework.test.context.TestExecutionListener;
32 33 34 35 36
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletWebRequest;

/**
S
Sam Brannen 已提交
37
 * TODO [SPR-9864] Document ServletTestExecutionListener.
38 39 40 41
 *
 * @author Sam Brannen
 * @since 3.2
 */
S
Sam Brannen 已提交
42
public class ServletTestExecutionListener implements TestExecutionListener {
43

S
Sam Brannen 已提交
44
	private static final Log logger = LogFactory.getLog(ServletTestExecutionListener.class);
45 46


S
Sam Brannen 已提交
47 48 49 50 51 52 53 54 55 56
	/**
	 * The default implementation is <em>empty</em>. Can be overridden by
	 * subclasses as necessary.
	 *
	 * @see org.springframework.test.context.TestExecutionListener#beforeTestClass(TestContext)
	 */
	public void beforeTestClass(TestContext testContext) throws Exception {
		/* no-op */
	}

57
	/**
S
Sam Brannen 已提交
58
	 * TODO [SPR-9864] Document overridden prepareTestInstance().
59
	 *
S
Sam Brannen 已提交
60
	 * @see org.springframework.test.context.TestExecutionListener#prepareTestInstance(TestContext)
61 62 63 64 65 66
	 */
	public void prepareTestInstance(TestContext testContext) throws Exception {
		setUpRequestContextIfNecessary(testContext);
	}

	/**
S
Sam Brannen 已提交
67
	 * TODO [SPR-9864] Document overridden beforeTestMethod().
68
	 *
S
Sam Brannen 已提交
69
	 * @see org.springframework.test.context.TestExecutionListener#beforeTestMethod(TestContext)
70 71 72 73 74
	 */
	public void beforeTestMethod(TestContext testContext) throws Exception {
		setUpRequestContextIfNecessary(testContext);
	}

S
Sam Brannen 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
	/**
	 * TODO [SPR-9864] Document overridden afterTestMethod().
	 *
	 * @see org.springframework.test.context.TestExecutionListener#afterTestMethod(TestContext)
	 */
	public void afterTestMethod(TestContext testContext) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug(String.format("Resetting RequestContextHolder for test context %s.", testContext));
		}
		RequestContextHolder.resetRequestAttributes();
	}

	/**
	 * The default implementation is <em>empty</em>. Can be overridden by
	 * subclasses as necessary.
	 *
	 * @see org.springframework.test.context.TestExecutionListener#afterTestClass(TestContext)
	 */
	public void afterTestClass(TestContext testContext) throws Exception {
		/* no-op */
	}

97
	/**
S
Sam Brannen 已提交
98
	 * TODO [SPR-9864] Document setUpRequestContext().
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
	 *
	 * @param testContext
	 * @param servletContext
	 */
	private void setUpRequestContextIfNecessary(TestContext testContext) {

		ApplicationContext context = testContext.getApplicationContext();

		if (context instanceof WebApplicationContext) {
			WebApplicationContext wac = (WebApplicationContext) context;
			ServletContext servletContext = wac.getServletContext();
			if (!(servletContext instanceof MockServletContext)) {
				throw new IllegalStateException(String.format(
					"The WebApplicationContext for test context %s must be configured with a MockServletContext.",
					testContext));
			}

			if (logger.isDebugEnabled()) {
				logger.debug(String.format(
					"Setting up MockHttpServletRequest, MockHttpServletResponse, ServletWebRequest, and RequestContextHolder for test context %s.",
					testContext));
			}

			if (RequestContextHolder.getRequestAttributes() == null) {
				MockServletContext mockServletContext = (MockServletContext) servletContext;
				MockHttpServletRequest request = new MockHttpServletRequest(mockServletContext);
				MockHttpServletResponse response = new MockHttpServletResponse();
				ServletWebRequest servletWebRequest = new ServletWebRequest(request, response);

				RequestContextHolder.setRequestAttributes(servletWebRequest);

				if (wac instanceof ConfigurableApplicationContext) {
					ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) wac;
					ConfigurableListableBeanFactory bf = configurableApplicationContext.getBeanFactory();
					bf.registerResolvableDependency(MockHttpServletResponse.class, response);
					bf.registerResolvableDependency(ServletWebRequest.class, servletWebRequest);
				}
			}
		}
	}

}