mirror of
https://github.com/spring-projects/spring-framework
synced 2026-06-08 17:33:33 +00:00
SPR-8217 update MVC namespace to use HandlerMethod infrastructure
This commit is contained in:
+8
-8
@@ -33,8 +33,8 @@ import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
|
||||
import org.springframework.web.bind.support.WebArgumentResolver;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
import org.springframework.web.context.support.GenericWebApplicationContext;
|
||||
import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;
|
||||
import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMethodAdapter;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMethodExceptionResolver;
|
||||
|
||||
/**
|
||||
* Test fixture for the configuration in mvc-config-annotation-driven.xml.
|
||||
@@ -52,7 +52,7 @@ public class AnnotationDrivenBeanDefinitionParserTests {
|
||||
@Test
|
||||
public void testMessageCodesResolver() {
|
||||
loadBeanDefinitions("mvc-config-message-codes-resolver.xml");
|
||||
AnnotationMethodHandlerAdapter adapter = appContext.getBean(AnnotationMethodHandlerAdapter.class);
|
||||
RequestMappingHandlerMethodAdapter adapter = appContext.getBean(RequestMappingHandlerMethodAdapter.class);
|
||||
assertNotNull(adapter);
|
||||
Object initializer = new DirectFieldAccessor(adapter).getPropertyValue("webBindingInitializer");
|
||||
assertNotNull(initializer);
|
||||
@@ -64,21 +64,21 @@ public class AnnotationDrivenBeanDefinitionParserTests {
|
||||
@Test
|
||||
public void testMessageConverters() {
|
||||
loadBeanDefinitions("mvc-config-message-converters.xml");
|
||||
verifyMessageConverters(appContext.getBean(AnnotationMethodHandlerAdapter.class), true);
|
||||
verifyMessageConverters(appContext.getBean(AnnotationMethodHandlerExceptionResolver.class), true);
|
||||
verifyMessageConverters(appContext.getBean(RequestMappingHandlerMethodAdapter.class), true);
|
||||
verifyMessageConverters(appContext.getBean(RequestMappingHandlerMethodExceptionResolver.class), true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMessageConvertersWithoutDefaultRegistrations() {
|
||||
loadBeanDefinitions("mvc-config-message-converters-defaults-off.xml");
|
||||
verifyMessageConverters(appContext.getBean(AnnotationMethodHandlerAdapter.class), false);
|
||||
verifyMessageConverters(appContext.getBean(AnnotationMethodHandlerExceptionResolver.class), false);
|
||||
verifyMessageConverters(appContext.getBean(RequestMappingHandlerMethodAdapter.class), false);
|
||||
verifyMessageConverters(appContext.getBean(RequestMappingHandlerMethodExceptionResolver.class), false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArgumentResolvers() {
|
||||
loadBeanDefinitions("mvc-config-argument-resolvers.xml");
|
||||
AnnotationMethodHandlerAdapter adapter = appContext.getBean(AnnotationMethodHandlerAdapter.class);
|
||||
RequestMappingHandlerMethodAdapter adapter = appContext.getBean(RequestMappingHandlerMethodAdapter.class);
|
||||
assertNotNull(adapter);
|
||||
Object resolvers = new DirectFieldAccessor(adapter).getPropertyValue("customArgumentResolvers");
|
||||
assertNotNull(resolvers);
|
||||
|
||||
+2
-1
@@ -36,6 +36,7 @@ import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
||||
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
|
||||
import org.springframework.web.bind.support.WebArgumentResolver;
|
||||
import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMethodAdapter;
|
||||
|
||||
/**
|
||||
* Integration tests for the {@link MvcAnnotationDriven} feature specification.
|
||||
@@ -50,7 +51,7 @@ public class MvcAnnotationDrivenFeatureTests {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(MvcFeature.class, MvcBeans.class);
|
||||
ctx.refresh();
|
||||
AnnotationMethodHandlerAdapter adapter = ctx.getBean(AnnotationMethodHandlerAdapter.class);
|
||||
RequestMappingHandlerMethodAdapter adapter = ctx.getBean(RequestMappingHandlerMethodAdapter.class);
|
||||
assertNotNull(adapter);
|
||||
Object initializer = new DirectFieldAccessor(adapter).getPropertyValue("webBindingInitializer");
|
||||
assertNotNull(initializer);
|
||||
|
||||
+67
-55
@@ -16,15 +16,23 @@
|
||||
|
||||
package org.springframework.web.servlet.config;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
import javax.servlet.RequestDispatcher;
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.TypeMismatchException;
|
||||
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
@@ -47,6 +55,8 @@ import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.context.support.GenericWebApplicationContext;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.method.support.InvocableHandlerMethod;
|
||||
import org.springframework.web.servlet.HandlerExecutionChain;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
@@ -56,14 +66,12 @@ import org.springframework.web.servlet.handler.WebRequestHandlerInterceptorAdapt
|
||||
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
|
||||
import org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter;
|
||||
import org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter;
|
||||
import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;
|
||||
import org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMethodAdapter;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMethodMapping;
|
||||
import org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler;
|
||||
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
|
||||
import org.springframework.web.servlet.theme.ThemeChangeInterceptor;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Keith Donald
|
||||
* @author Arjen Poutsma
|
||||
@@ -72,12 +80,20 @@ import static org.junit.Assert.*;
|
||||
public class MvcNamespaceTests {
|
||||
|
||||
private GenericWebApplicationContext appContext;
|
||||
|
||||
private TestController handler;
|
||||
|
||||
private HandlerMethod handlerMethod;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
public void setUp() throws Exception {
|
||||
appContext = new GenericWebApplicationContext();
|
||||
appContext.setServletContext(new TestMockServletContext());
|
||||
LocaleContextHolder.setLocale(Locale.US);
|
||||
|
||||
handler = new TestController();
|
||||
Method method = TestController.class.getMethod("testBind", Date.class, TestBean.class, BindingResult.class);
|
||||
handlerMethod = new InvocableHandlerMethod(handler, method);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -87,13 +103,12 @@ public class MvcNamespaceTests {
|
||||
assertEquals(8, appContext.getBeanDefinitionCount());
|
||||
appContext.refresh();
|
||||
|
||||
DefaultAnnotationHandlerMapping mapping = appContext.getBean(DefaultAnnotationHandlerMapping.class);
|
||||
RequestMappingHandlerMethodMapping mapping = appContext.getBean(RequestMappingHandlerMethodMapping.class);
|
||||
assertNotNull(mapping);
|
||||
assertEquals(0, mapping.getOrder());
|
||||
TestController handler = new TestController();
|
||||
mapping.setDefaultHandler(handler);
|
||||
mapping.setDefaultHandler(handlerMethod);
|
||||
|
||||
AnnotationMethodHandlerAdapter adapter = appContext.getBean(AnnotationMethodHandlerAdapter.class);
|
||||
RequestMappingHandlerMethodAdapter adapter = appContext.getBean(RequestMappingHandlerMethodAdapter.class);
|
||||
assertNotNull(adapter);
|
||||
|
||||
HttpMessageConverter<?>[] messageConverters = adapter.getMessageConverters();
|
||||
@@ -105,18 +120,18 @@ public class MvcNamespaceTests {
|
||||
assertNotNull(appContext.getBean(Validator.class));
|
||||
|
||||
// default web binding initializer behavior test
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
|
||||
request.addParameter("date", "2009-10-31");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
HandlerExecutionChain chain = mapping.getHandler(request);
|
||||
assertEquals(2, chain.getInterceptors().length);
|
||||
assertTrue(chain.getInterceptors()[1] instanceof ConversionServiceExposingInterceptor);
|
||||
ConversionServiceExposingInterceptor interceptor = (ConversionServiceExposingInterceptor) chain.getInterceptors()[1];
|
||||
interceptor.preHandle(request, response, handler);
|
||||
assertEquals(1, chain.getInterceptors().length);
|
||||
assertTrue(chain.getInterceptors()[0] instanceof ConversionServiceExposingInterceptor);
|
||||
ConversionServiceExposingInterceptor interceptor = (ConversionServiceExposingInterceptor) chain.getInterceptors()[0];
|
||||
interceptor.preHandle(request, response, handlerMethod);
|
||||
assertSame(appContext.getBean(ConversionService.class), request.getAttribute(ConversionService.class.getName()));
|
||||
|
||||
adapter.handle(request, response, handler);
|
||||
adapter.handle(request, response, handlerMethod);
|
||||
assertTrue(handler.recordedValidationError);
|
||||
}
|
||||
|
||||
@@ -127,27 +142,26 @@ public class MvcNamespaceTests {
|
||||
assertEquals(8, appContext.getBeanDefinitionCount());
|
||||
appContext.refresh();
|
||||
|
||||
DefaultAnnotationHandlerMapping mapping = appContext.getBean(DefaultAnnotationHandlerMapping.class);
|
||||
RequestMappingHandlerMethodMapping mapping = appContext.getBean(RequestMappingHandlerMethodMapping.class);
|
||||
assertNotNull(mapping);
|
||||
TestController handler = new TestController();
|
||||
mapping.setDefaultHandler(handler);
|
||||
mapping.setDefaultHandler(handlerMethod);
|
||||
|
||||
// default web binding initializer behavior test
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
|
||||
request.setRequestURI("/accounts/12345");
|
||||
request.addParameter("date", "2009-10-31");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
HandlerExecutionChain chain = mapping.getHandler(request);
|
||||
assertEquals(2, chain.getInterceptors().length);
|
||||
assertTrue(chain.getInterceptors()[1] instanceof ConversionServiceExposingInterceptor);
|
||||
ConversionServiceExposingInterceptor interceptor = (ConversionServiceExposingInterceptor) chain.getInterceptors()[1];
|
||||
assertEquals(1, chain.getInterceptors().length);
|
||||
assertTrue(chain.getInterceptors()[0] instanceof ConversionServiceExposingInterceptor);
|
||||
ConversionServiceExposingInterceptor interceptor = (ConversionServiceExposingInterceptor) chain.getInterceptors()[0];
|
||||
interceptor.preHandle(request, response, handler);
|
||||
assertSame(appContext.getBean("conversionService"), request.getAttribute(ConversionService.class.getName()));
|
||||
|
||||
AnnotationMethodHandlerAdapter adapter = appContext.getBean(AnnotationMethodHandlerAdapter.class);
|
||||
RequestMappingHandlerMethodAdapter adapter = appContext.getBean(RequestMappingHandlerMethodAdapter.class);
|
||||
assertNotNull(adapter);
|
||||
adapter.handle(request, response, handler);
|
||||
adapter.handle(request, response, handlerMethod);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -157,16 +171,14 @@ public class MvcNamespaceTests {
|
||||
assertEquals(8, appContext.getBeanDefinitionCount());
|
||||
appContext.refresh();
|
||||
|
||||
AnnotationMethodHandlerAdapter adapter = appContext.getBean(AnnotationMethodHandlerAdapter.class);
|
||||
RequestMappingHandlerMethodAdapter adapter = appContext.getBean(RequestMappingHandlerMethodAdapter.class);
|
||||
assertNotNull(adapter);
|
||||
|
||||
TestController handler = new TestController();
|
||||
|
||||
// default web binding initializer behavior test
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.addParameter("date", "2009-10-31");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
adapter.handle(request, response, handler);
|
||||
adapter.handle(request, response, handlerMethod);
|
||||
|
||||
assertTrue(appContext.getBean(TestValidator.class).validatorInvoked);
|
||||
assertFalse(handler.recordedValidationError);
|
||||
@@ -179,30 +191,30 @@ public class MvcNamespaceTests {
|
||||
assertEquals(11, appContext.getBeanDefinitionCount());
|
||||
appContext.refresh();
|
||||
|
||||
DefaultAnnotationHandlerMapping mapping = appContext.getBean(DefaultAnnotationHandlerMapping.class);
|
||||
RequestMappingHandlerMethodMapping mapping = appContext.getBean(RequestMappingHandlerMethodMapping.class);
|
||||
assertNotNull(mapping);
|
||||
mapping.setDefaultHandler(new TestController());
|
||||
mapping.setDefaultHandler(handlerMethod);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
|
||||
request.setRequestURI("/accounts/12345");
|
||||
request.addParameter("locale", "en");
|
||||
request.addParameter("theme", "green");
|
||||
|
||||
HandlerExecutionChain chain = mapping.getHandler(request);
|
||||
assertEquals(4, chain.getInterceptors().length);
|
||||
assertTrue(chain.getInterceptors()[1] instanceof ConversionServiceExposingInterceptor);
|
||||
assertTrue(chain.getInterceptors()[2] instanceof LocaleChangeInterceptor);
|
||||
assertTrue(chain.getInterceptors()[3] instanceof ThemeChangeInterceptor);
|
||||
assertEquals(3, chain.getInterceptors().length);
|
||||
assertTrue(chain.getInterceptors()[0] instanceof ConversionServiceExposingInterceptor);
|
||||
assertTrue(chain.getInterceptors()[1] instanceof LocaleChangeInterceptor);
|
||||
assertTrue(chain.getInterceptors()[2] instanceof ThemeChangeInterceptor);
|
||||
|
||||
request.setRequestURI("/logged/accounts/12345");
|
||||
chain = mapping.getHandler(request);
|
||||
assertEquals(5, chain.getInterceptors().length);
|
||||
assertTrue(chain.getInterceptors()[4] instanceof WebRequestHandlerInterceptorAdapter);
|
||||
assertEquals(4, chain.getInterceptors().length);
|
||||
assertTrue(chain.getInterceptors()[3] instanceof WebRequestHandlerInterceptorAdapter);
|
||||
|
||||
request.setRequestURI("/foo/logged");
|
||||
chain = mapping.getHandler(request);
|
||||
assertEquals(5, chain.getInterceptors().length);
|
||||
assertTrue(chain.getInterceptors()[4] instanceof WebRequestHandlerInterceptorAdapter);
|
||||
assertEquals(4, chain.getInterceptors().length);
|
||||
assertTrue(chain.getInterceptors()[3] instanceof WebRequestHandlerInterceptorAdapter);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -314,20 +326,20 @@ public class MvcNamespaceTests {
|
||||
assertEquals(10, appContext.getBeanDefinitionCount());
|
||||
appContext.refresh();
|
||||
|
||||
DefaultAnnotationHandlerMapping mapping = appContext.getBean(DefaultAnnotationHandlerMapping.class);
|
||||
RequestMappingHandlerMethodMapping mapping = appContext.getBean(RequestMappingHandlerMethodMapping.class);
|
||||
assertNotNull(mapping);
|
||||
mapping.setDefaultHandler(new TestController());
|
||||
mapping.setDefaultHandler(handlerMethod);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
|
||||
|
||||
HandlerExecutionChain chain = mapping.getHandler(request);
|
||||
assertEquals(4, chain.getInterceptors().length);
|
||||
assertTrue(chain.getInterceptors()[1] instanceof ConversionServiceExposingInterceptor);
|
||||
assertTrue(chain.getInterceptors()[2] instanceof LocaleChangeInterceptor);
|
||||
assertTrue(chain.getInterceptors()[3] instanceof ThemeChangeInterceptor);
|
||||
LocaleChangeInterceptor interceptor = (LocaleChangeInterceptor) chain.getInterceptors()[2];
|
||||
assertEquals(3, chain.getInterceptors().length);
|
||||
assertTrue(chain.getInterceptors()[0] instanceof ConversionServiceExposingInterceptor);
|
||||
assertTrue(chain.getInterceptors()[1] instanceof LocaleChangeInterceptor);
|
||||
assertTrue(chain.getInterceptors()[2] instanceof ThemeChangeInterceptor);
|
||||
LocaleChangeInterceptor interceptor = (LocaleChangeInterceptor) chain.getInterceptors()[1];
|
||||
assertEquals("lang", interceptor.getParamName());
|
||||
ThemeChangeInterceptor interceptor2 = (ThemeChangeInterceptor) chain.getInterceptors()[3];
|
||||
ThemeChangeInterceptor interceptor2 = (ThemeChangeInterceptor) chain.getInterceptors()[2];
|
||||
assertEquals("style", interceptor2.getParamName());
|
||||
}
|
||||
|
||||
@@ -338,18 +350,18 @@ public class MvcNamespaceTests {
|
||||
assertEquals(12, appContext.getBeanDefinitionCount());
|
||||
appContext.refresh();
|
||||
|
||||
DefaultAnnotationHandlerMapping mapping = appContext.getBean(DefaultAnnotationHandlerMapping.class);
|
||||
RequestMappingHandlerMethodMapping mapping = appContext.getBean(RequestMappingHandlerMethodMapping.class);
|
||||
assertNotNull(mapping);
|
||||
mapping.setDefaultHandler(new TestController());
|
||||
mapping.setDefaultHandler(handlerMethod);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setMethod("GET");
|
||||
|
||||
HandlerExecutionChain chain = mapping.getHandler(request);
|
||||
assertEquals(4, chain.getInterceptors().length);
|
||||
assertTrue(chain.getInterceptors()[1] instanceof ConversionServiceExposingInterceptor);
|
||||
assertTrue(chain.getInterceptors()[2] instanceof LocaleChangeInterceptor);
|
||||
assertTrue(chain.getInterceptors()[3] instanceof ThemeChangeInterceptor);
|
||||
assertEquals(3, chain.getInterceptors().length);
|
||||
assertTrue(chain.getInterceptors()[0] instanceof ConversionServiceExposingInterceptor);
|
||||
assertTrue(chain.getInterceptors()[1] instanceof LocaleChangeInterceptor);
|
||||
assertTrue(chain.getInterceptors()[2] instanceof ThemeChangeInterceptor);
|
||||
|
||||
SimpleUrlHandlerMapping mapping2 = appContext.getBean(SimpleUrlHandlerMapping.class);
|
||||
assertNotNull(mapping2);
|
||||
|
||||
+1
-1
@@ -96,7 +96,7 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class RequestMappingHandlerAdapterIntegrationTests {
|
||||
public class RequestMappingHandlerMethodAdapterIntegrationTests {
|
||||
|
||||
private RequestMappingHandlerMethodAdapter handlerAdapter;
|
||||
|
||||
+2
-2
@@ -36,12 +36,12 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl
|
||||
* Test fixture for {@link RequestMappingHandlerMethodAdapter} unit tests.
|
||||
*
|
||||
* The tests in this class focus on {@link RequestMappingHandlerMethodAdapter} functionality exclusively.
|
||||
* Also see {@link RequestMappingHandlerAdapterIntegrationTests} for higher-level tests invoking
|
||||
* Also see {@link RequestMappingHandlerMethodAdapterIntegrationTests} for higher-level tests invoking
|
||||
* {@link Controller @Controller} methods.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class RequestMappingHandlerAdapterTests {
|
||||
public class RequestMappingHandlerMethodAdapterTests {
|
||||
|
||||
private RequestMappingHandlerMethodAdapter handlerAdapter;
|
||||
|
||||
Reference in New Issue
Block a user