Introduce @EnableMvcConfiguration

This commit is contained in:
Chris Beams
2011-05-06 19:11:19 +00:00
parent 01e5120a26
commit 446dfdbff2
21 changed files with 1947 additions and 62 deletions
@@ -0,0 +1,102 @@
/*
* Copyright 2002-2011 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.web.servlet.config.annotation;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import javax.servlet.RequestDispatcher;
import org.junit.Before;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockRequestDispatcher;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
import org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler;
/**
* Test fixture with a {@link DefaultServletHandlerConfigurer}.
*
* @author Rossen Stoyanchev
*/
public class DefaultServletHandlerConfigurerTests {
private DefaultServletHandlerConfigurer configurer;
private DispatchingMockServletContext servletContext;
private MockHttpServletResponse response;
@Before
public void setUp() {
response = new MockHttpServletResponse();
servletContext = new DispatchingMockServletContext();
configurer = new DefaultServletHandlerConfigurer(servletContext);
}
@Test
public void notEnabled() {
assertTrue(configurer.getHandlerMapping().getUrlMap().isEmpty());
}
@Test
public void enable() throws Exception {
configurer.enable();
SimpleUrlHandlerMapping handlerMapping = configurer.getHandlerMapping();
DefaultServletHttpRequestHandler handler = (DefaultServletHttpRequestHandler) handlerMapping.getUrlMap().get("/**");
assertNotNull(handler);
assertEquals(Integer.MAX_VALUE, handlerMapping.getOrder());
handler.handleRequest(new MockHttpServletRequest(), response);
String expected = "default";
assertEquals("The ServletContext was not called with the default servlet name", expected, servletContext.url);
assertEquals("The request was not forwarded", expected, response.getForwardedUrl());
}
@Test
public void enableWithServletName() throws Exception {
configurer.enable("defaultServlet");
SimpleUrlHandlerMapping handlerMapping = configurer.getHandlerMapping();
DefaultServletHttpRequestHandler handler = (DefaultServletHttpRequestHandler) handlerMapping.getUrlMap().get("/**");
assertNotNull(handler);
assertEquals(Integer.MAX_VALUE, handlerMapping.getOrder());
handler.handleRequest(new MockHttpServletRequest(), response);
String expected = "defaultServlet";
assertEquals("The ServletContext was not called with the default servlet name", expected, servletContext.url);
assertEquals("The request was not forwarded", expected, response.getForwardedUrl());
}
private static class DispatchingMockServletContext extends MockServletContext {
private String url;
@Override
public RequestDispatcher getNamedDispatcher(String url) {
this.url = url;
return new MockRequestDispatcher(url);
}
}
}
@@ -0,0 +1,168 @@
/*
* Copyright 2002-2011 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.web.servlet.config.annotation;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.ui.ModelMap;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.context.request.WebRequestInterceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.handler.WebRequestHandlerInterceptorAdapter;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.theme.ThemeChangeInterceptor;
/**
* Test fixture with a {@link InterceptorConfigurer}, two {@link HandlerInterceptor}s and two
* {@link WebRequestInterceptor}s.
*
* @author Rossen Stoyanchev
*/
public class InterceptorConfigurerTests {
private InterceptorConfigurer configurer;
private final HandlerInterceptor interceptor1 = new LocaleChangeInterceptor();
private final HandlerInterceptor interceptor2 = new ThemeChangeInterceptor();
private TestWebRequestInterceptor webRequestInterceptor1;
private TestWebRequestInterceptor webRequestInterceptor2;
private final MockHttpServletRequest request = new MockHttpServletRequest();
private final MockHttpServletResponse response = new MockHttpServletResponse();
@Before
public void setUp() {
configurer = new InterceptorConfigurer();
webRequestInterceptor1 = new TestWebRequestInterceptor();
webRequestInterceptor2 = new TestWebRequestInterceptor();
}
@Test
public void addInterceptor() {
configurer.addInterceptor(interceptor1);
HandlerInterceptor[] interceptors = getInterceptorsForPath(null);
assertArrayEquals(new HandlerInterceptor[] {interceptor1}, interceptors);
}
@Test
public void addInterceptors() {
configurer.addInterceptors(interceptor1, interceptor2);
HandlerInterceptor[] interceptors = getInterceptorsForPath(null);
assertArrayEquals(new HandlerInterceptor[] {interceptor1, interceptor2}, interceptors);
}
@Test
public void mapInterceptor() {
configurer.mapInterceptor(new String[] {"/path1"}, interceptor1);
configurer.mapInterceptor(new String[] {"/path2"}, interceptor2);
assertArrayEquals(new HandlerInterceptor[] {interceptor1}, getInterceptorsForPath("/path1"));
assertArrayEquals(new HandlerInterceptor[] {interceptor2}, getInterceptorsForPath("/path2"));
}
@Test
public void mapInterceptors() {
configurer.mapInterceptors(new String[] {"/path1"}, interceptor1, interceptor2);
assertArrayEquals(new HandlerInterceptor[] {interceptor1, interceptor2}, getInterceptorsForPath("/path1"));
assertArrayEquals(new HandlerInterceptor[] {}, getInterceptorsForPath("/path2"));
}
@Test
public void addWebRequestInterceptor() throws Exception {
configurer.addInterceptor(webRequestInterceptor1);
HandlerInterceptor[] interceptors = getInterceptorsForPath(null);
assertEquals(1, interceptors.length);
verifyAdaptedInterceptor(interceptors[0], webRequestInterceptor1);
}
@Test
public void addWebRequestInterceptors() throws Exception {
configurer.addInterceptors(webRequestInterceptor1, webRequestInterceptor2);
HandlerInterceptor[] interceptors = getInterceptorsForPath(null);
assertEquals(2, interceptors.length);
verifyAdaptedInterceptor(interceptors[0], webRequestInterceptor1);
verifyAdaptedInterceptor(interceptors[1], webRequestInterceptor2);
}
@Test
public void mapWebRequestInterceptor() throws Exception {
configurer.mapInterceptor(new String[] {"/path1"}, webRequestInterceptor1);
configurer.mapInterceptor(new String[] {"/path2"}, webRequestInterceptor2);
HandlerInterceptor[] interceptors = getInterceptorsForPath("/path1");
assertEquals(1, interceptors.length);
verifyAdaptedInterceptor(interceptors[0], webRequestInterceptor1);
interceptors = getInterceptorsForPath("/path2");
assertEquals(1, interceptors.length);
verifyAdaptedInterceptor(interceptors[0], webRequestInterceptor2);
}
@Test
public void mapWebRequestInterceptor2() throws Exception {
configurer.mapInterceptors(new String[] {"/path1"}, webRequestInterceptor1, webRequestInterceptor2);
HandlerInterceptor[] interceptors = getInterceptorsForPath("/path1");
assertEquals(2, interceptors.length);
verifyAdaptedInterceptor(interceptors[0], webRequestInterceptor1);
verifyAdaptedInterceptor(interceptors[1], webRequestInterceptor2);
assertEquals(0, getInterceptorsForPath("/path2").length);
}
private HandlerInterceptor[] getInterceptorsForPath(String lookupPath) {
return configurer.getMappedInterceptors().getInterceptors(lookupPath, new AntPathMatcher());
}
private void verifyAdaptedInterceptor(HandlerInterceptor interceptor, TestWebRequestInterceptor webInterceptor)
throws Exception {
assertTrue(interceptor instanceof WebRequestHandlerInterceptorAdapter);
interceptor.preHandle(request, response, null);
assertTrue(webInterceptor.preHandleInvoked);
}
private static class TestWebRequestInterceptor implements WebRequestInterceptor {
private boolean preHandleInvoked = false;
public void preHandle(WebRequest request) throws Exception {
preHandleInvoked = true;
}
public void postHandle(WebRequest request, ModelMap model) throws Exception {
}
public void afterCompletion(WebRequest request, Exception ex) throws Exception {
}
}
}
@@ -0,0 +1,132 @@
/*
* Copyright 2002-2011 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.web.servlet.config.annotation;
import static org.easymock.EasyMock.capture;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.List;
import org.easymock.Capture;
import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;
import org.springframework.format.support.FormattingConversionService;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver;
import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMethodAdapter;
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;
/**
* A test fixture with an {@link MvcConfiguration} and a mock {@link MvcConfigurer} for verifying delegation.
*
* @author Rossen Stoyanchev
*/
public class MvcConfigurationTests {
private MvcConfiguration mvcConfiguration;
private MvcConfigurer configurer;
@Before
public void setUp() {
configurer = EasyMock.createMock(MvcConfigurer.class);
mvcConfiguration = new MvcConfiguration();
mvcConfiguration.setConfigurers(Arrays.asList(configurer));
}
@Test
public void annotationHandlerAdapter() {
Capture<FormattingConversionService> conversionService = new Capture<FormattingConversionService>();
Capture<List<HandlerMethodArgumentResolver>> resolvers = new Capture<List<HandlerMethodArgumentResolver>>();
Capture<List<HandlerMethodReturnValueHandler>> handlers = new Capture<List<HandlerMethodReturnValueHandler>>();
Capture<List<HttpMessageConverter<?>>> converters = new Capture<List<HttpMessageConverter<?>>>();
expect(configurer.getValidator()).andReturn(null);
configurer.registerFormatters(capture(conversionService));
configurer.addCustomArgumentResolvers(capture(resolvers));
configurer.addCustomReturnValueHandlers(capture(handlers));
configurer.configureMessageConverters(capture(converters));
replay(configurer);
RequestMappingHandlerMethodAdapter adapter = mvcConfiguration.requestMappingHandlerAdapter();
ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) adapter.getWebBindingInitializer();
assertSame(conversionService.getValue(), initializer.getConversionService());
assertTrue(initializer.getValidator() instanceof LocalValidatorFactoryBean);
assertEquals(0, resolvers.getValue().size());
assertEquals(0, handlers.getValue().size());
assertTrue(converters.getValue().size() > 0);
assertEquals(converters.getValue(), adapter.getMessageConverters());
verify(configurer);
}
@Test
public void getCustomValidator() {
expect(configurer.getValidator()).andReturn(new LocalValidatorFactoryBean());
replay(configurer);
mvcConfiguration.validator();
verify(configurer);
}
@Test
public void configureValidator() {
expect(configurer.getValidator()).andReturn(null);
replay(configurer);
mvcConfiguration.validator();
verify(configurer);
}
@Test
public void handlerExceptionResolver() throws Exception {
Capture<List<HttpMessageConverter<?>>> converters = new Capture<List<HttpMessageConverter<?>>>();
Capture<List<HandlerExceptionResolver>> exceptionResolvers = new Capture<List<HandlerExceptionResolver>>();
configurer.configureMessageConverters(capture(converters));
configurer.configureHandlerExceptionResolvers(capture(exceptionResolvers));
replay(configurer);
mvcConfiguration.handlerExceptionResolver();
assertEquals(3, exceptionResolvers.getValue().size());
assertTrue(exceptionResolvers.getValue().get(0) instanceof ExceptionHandlerExceptionResolver);
assertTrue(exceptionResolvers.getValue().get(1) instanceof ResponseStatusExceptionResolver);
assertTrue(exceptionResolvers.getValue().get(2) instanceof DefaultHandlerExceptionResolver);
assertTrue(converters.getValue().size() > 0);
verify(configurer);
}
}
@@ -0,0 +1,89 @@
/*
* Copyright 2002-2011 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.web.servlet.config.annotation;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.support.GenericWebApplicationContext;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
/**
* Test fixture with a {@link ResourceConfigurer}.
*
* @author Rossen Stoyanchev
*/
public class ResourceConfigurerTests {
private ResourceConfigurer configurer;
private MockHttpServletResponse response;
@Before
public void setUp() {
configurer = new ResourceConfigurer(new GenericWebApplicationContext(), new MockServletContext());
configurer.addPathMapping("/resources/**");
configurer.addResourceLocation("classpath:org/springframework/web/servlet/config/annotation/");
response = new MockHttpServletResponse();
}
@Test
public void noMappings() throws Exception {
configurer = new ResourceConfigurer(new GenericWebApplicationContext(), new MockServletContext());
assertTrue(configurer.getHandlerMapping().getUrlMap().isEmpty());
}
@Test
public void mapPathToLocation() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("GET");
request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "/testStylesheet.css");
ResourceHttpRequestHandler handler = getResourceHandler("/resources/**");
handler.handleRequest(request, response);
assertEquals("test stylesheet content", response.getContentAsString());
}
@Test
public void cachePeriod() {
assertEquals(-1, getResourceHandler("/resources/**").getCacheSeconds());
configurer.setCachePeriod(0);
assertEquals(0, getResourceHandler("/resources/**").getCacheSeconds());
}
@Test
public void order() {
assertEquals(Integer.MAX_VALUE -1, configurer.getHandlerMapping().getOrder());
configurer.setOrder(0);
assertEquals(0, configurer.getHandlerMapping().getOrder());
}
private ResourceHttpRequestHandler getResourceHandler(String pathPattern) {
return (ResourceHttpRequestHandler) configurer.getHandlerMapping().getUrlMap().get(pathPattern);
}
}
@@ -0,0 +1,75 @@
/*
* Copyright 2002-2011 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.web.servlet.config.annotation;
import static org.junit.Assert.*;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
import org.springframework.web.servlet.mvc.ParameterizableViewController;
/**
* Test fixture with a {@link ViewControllerConfigurer}.
*
* @author Rossen Stoyanchev
*/
public class ViewControllerConfigurerTests {
private ViewControllerConfigurer configurer;
@Before
public void setUp() {
configurer = new ViewControllerConfigurer();
}
@Test
public void noMappings() throws Exception {
Map<String, ?> urlMap = configurer.getHandlerMapping().getUrlMap();
assertTrue(urlMap.isEmpty());
}
@Test
public void mapViewName() {
configurer.mapViewName("/path", "viewName");
Map<String, ?> urlMap = configurer.getHandlerMapping().getUrlMap();
ParameterizableViewController controller = (ParameterizableViewController) urlMap.get("/path");
assertNotNull(controller);
assertEquals("viewName", controller.getViewName());
}
@Test
public void mapViewNameByConvention() {
configurer.mapViewNameByConvention("/path");
Map<String, ?> urlMap = configurer.getHandlerMapping().getUrlMap();
ParameterizableViewController controller = (ParameterizableViewController) urlMap.get("/path");
assertNotNull(controller);
assertNull(controller.getViewName());
}
@Test
public void order() {
SimpleUrlHandlerMapping handlerMapping = configurer.getHandlerMapping();
assertEquals(1, handlerMapping.getOrder());
configurer.setOrder(2);
handlerMapping = configurer.getHandlerMapping();
assertEquals(2, handlerMapping.getOrder());
}
}