View Javadoc
1   /*
2    * Copyright 2002-2012 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 javax.servlet.ServletException;
20  
21  import org.junit.After;
22  
23  import org.springframework.beans.factory.support.RootBeanDefinition;
24  import org.springframework.context.ApplicationContextInitializer;
25  import org.springframework.mock.web.test.MockServletConfig;
26  import org.springframework.web.context.WebApplicationContext;
27  import org.springframework.web.context.support.GenericWebApplicationContext;
28  import org.springframework.web.servlet.DispatcherServlet;
29  import org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver;
30  import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;
31  
32  import static org.junit.Assert.*;
33  
34  /**
35   * Base class for tests using on the DispatcherServlet and HandlerMethod infrastructure classes:
36   * <ul>
37   * 	<li>RequestMappingHandlerMapping
38   * 	<li>RequestMappingHandlerAdapter
39   * 	<li>ExceptionHandlerExceptionResolver
40   * </ul>
41   *
42   * @author Rossen Stoyanchev
43   */
44  public abstract class AbstractServletHandlerMethodTests {
45  
46  	private DispatcherServlet servlet;
47  
48  	@After
49  	public void tearDown() {
50  		this.servlet = null;
51  	}
52  
53  	protected DispatcherServlet getServlet() {
54  		assertNotNull("DispatcherServlet not initialized", servlet);
55  		return servlet;
56  	}
57  
58  	/**
59  	 * Initialize a DispatcherServlet instance registering zero or more controller classes.
60  	 */
61  	protected WebApplicationContext initServletWithControllers(final Class<?>... controllerClasses)
62  			throws ServletException {
63  		return initServlet(null, controllerClasses);
64  	}
65  
66  	/**
67  	 * Initialize a DispatcherServlet instance registering zero or more controller classes
68  	 * and also providing additional bean definitions through a callback.
69  	 */
70  	@SuppressWarnings("serial")
71  	protected WebApplicationContext initServlet(
72  			final ApplicationContextInitializer<GenericWebApplicationContext> initializer,
73  			final Class<?>... controllerClasses) throws ServletException {
74  
75  		final GenericWebApplicationContext wac = new GenericWebApplicationContext();
76  
77  		servlet = new DispatcherServlet() {
78  			@Override
79  			protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) {
80  				for (Class<?> clazz : controllerClasses) {
81  					wac.registerBeanDefinition(clazz.getSimpleName(), new RootBeanDefinition(clazz));
82  				}
83  
84  				Class<?> mappingType = RequestMappingHandlerMapping.class;
85  				RootBeanDefinition beanDef = new RootBeanDefinition(mappingType);
86  				beanDef.getPropertyValues().add("removeSemicolonContent", "false");
87  				wac.registerBeanDefinition("handlerMapping", beanDef);
88  
89  				Class<?> adapterType = RequestMappingHandlerAdapter.class;
90  				wac.registerBeanDefinition("handlerAdapter", new RootBeanDefinition(adapterType));
91  
92  				Class<?> resolverType = ExceptionHandlerExceptionResolver.class;
93  				wac.registerBeanDefinition("requestMappingResolver", new RootBeanDefinition(resolverType));
94  
95  				resolverType = ResponseStatusExceptionResolver.class;
96  				wac.registerBeanDefinition("responseStatusResolver", new RootBeanDefinition(resolverType));
97  
98  				resolverType = DefaultHandlerExceptionResolver.class;
99  				wac.registerBeanDefinition("defaultResolver", new RootBeanDefinition(resolverType));
100 
101 				if (initializer != null) {
102 					initializer.initialize(wac);
103 				}
104 
105 				wac.refresh();
106 				return wac;
107 			}
108 		};
109 
110 		servlet.init(new MockServletConfig());
111 
112 		return wac;
113 	}
114 
115 }