1 /*
2  * Copyright 2002-2011 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package org.springframework.web.servlet.mvc.method.annotation;
18 
19 import static junit.framework.Assert.assertNotNull;
20 
21 import javax.servlet.ServletException;
22 
23 import org.junit.After;
24 import org.springframework.beans.factory.support.RootBeanDefinition;
25 import org.springframework.context.ApplicationContextInitializer;
26 import org.springframework.mock.web.MockServletConfig;
27 import org.springframework.web.context.WebApplicationContext;
28 import org.springframework.web.context.support.GenericWebApplicationContext;
29 import org.springframework.web.servlet.DispatcherServlet;
30 import org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver;
31 import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;
32 
33 /**
34  * Base class for tests using on the DispatcherServlet and HandlerMethod infrastructure classes:
35  * <ul>
36  * 	<li>RequestMappingHandlerMapping
37  * 	<li>RequestMappingHandlerAdapter
38  * 	<li>ExceptionHandlerExceptionResolver
39  * </ul>
40  *
41  * @author Rossen Stoyanchev
42  */
43 public class AbstractServletHandlerMethodTests {
44 
45 	private DispatcherServlet servlet;
46 
47 	@After
tearDown()48 	public void tearDown() {
49 		this.servlet = null;
50 	}
51 
getServlet()52 	protected DispatcherServlet getServlet() {
53 		assertNotNull("DispatcherServlet not initialized", servlet);
54 		return servlet;
55 	}
56 
57 	/**
58 	 * Initialize a DispatcherServlet instance registering zero or more controller classes.
59 	 */
initServletWithControllers(final Class<?>... controllerClasses)60 	protected WebApplicationContext initServletWithControllers(final Class<?>... controllerClasses)
61 			throws ServletException {
62 		return initServlet(null, controllerClasses);
63 	}
64 
65 	/**
66 	 * Initialize a DispatcherServlet instance registering zero or more controller classes
67 	 * and also providing additional bean definitions through a callback.
68 	 */
69 	@SuppressWarnings("serial")
initServlet( final ApplicationContextInitializer<GenericWebApplicationContext> initializer, final Class<?>... controllerClasses)70 	protected WebApplicationContext initServlet(
71 			final ApplicationContextInitializer<GenericWebApplicationContext> initializer,
72 			final Class<?>... controllerClasses) throws ServletException {
73 
74 		final GenericWebApplicationContext wac = new GenericWebApplicationContext();
75 
76 		servlet = new DispatcherServlet() {
77 			@Override
78 			protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) {
79 				for (Class<?> clazz : controllerClasses) {
80 					wac.registerBeanDefinition(clazz.getSimpleName(), new RootBeanDefinition(clazz));
81 				}
82 
83 				Class<?> mappingType = RequestMappingHandlerMapping.class;
84 				wac.registerBeanDefinition("handlerMapping", new RootBeanDefinition(mappingType));
85 
86 				Class<?> adapterType = RequestMappingHandlerAdapter.class;
87 				wac.registerBeanDefinition("handlerAdapter", new RootBeanDefinition(adapterType));
88 
89 				Class<?> resolverType = ExceptionHandlerExceptionResolver.class;
90 				wac.registerBeanDefinition("requestMappingResolver", new RootBeanDefinition(resolverType));
91 
92 				resolverType = ResponseStatusExceptionResolver.class;
93 				wac.registerBeanDefinition("responseStatusResolver", new RootBeanDefinition(resolverType));
94 
95 				resolverType = DefaultHandlerExceptionResolver.class;
96 				wac.registerBeanDefinition("defaultResolver", new RootBeanDefinition(resolverType));
97 
98 				if (initializer != null) {
99 					initializer.initialize(wac);
100 				}
101 
102 				wac.refresh();
103 				return wac;
104 			}
105 		};
106 
107 		servlet.init(new MockServletConfig());
108 
109 		return wac;
110 	}
111 
112 }
113