mvc:annotation-driven exposes default Validator and ConversionService as top-level beans (SPR-6377)
This commit is contained in:
@@ -1,24 +1,38 @@
|
||||
package org.springframework.web.servlet.config;
|
||||
/*
|
||||
* Copyright 2002-2009 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.
|
||||
*/
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
package org.springframework.web.servlet.config;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.core.convert.ConversionFailedException;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
import org.springframework.format.annotation.DateTimeFormat.ISO;
|
||||
import org.springframework.format.support.FormattingConversionServiceFactoryBean;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
@@ -26,33 +40,41 @@ import org.springframework.stereotype.Controller;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.Errors;
|
||||
import org.springframework.validation.Validator;
|
||||
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.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;
|
||||
import org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping;
|
||||
|
||||
/**
|
||||
* @author Keith Donald
|
||||
*/
|
||||
public class MvcNamespaceTests {
|
||||
|
||||
private GenericWebApplicationContext container;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
container = new GenericWebApplicationContext();
|
||||
container.setServletContext(new MockServletContext());
|
||||
LocaleContextHolder.setLocale(Locale.US);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testDefaultConfig() throws Exception {
|
||||
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(container);
|
||||
reader.loadBeanDefinitions(new ClassPathResource("mvc-config.xml", getClass()));
|
||||
assertEquals(2, container.getBeanDefinitionCount());
|
||||
assertEquals(4, container.getBeanDefinitionCount());
|
||||
DefaultAnnotationHandlerMapping mapping = container.getBean(DefaultAnnotationHandlerMapping.class);
|
||||
assertNotNull(mapping);
|
||||
assertEquals(0, mapping.getOrder());
|
||||
AnnotationMethodHandlerAdapter adapter = container.getBean(AnnotationMethodHandlerAdapter.class);
|
||||
assertNotNull(adapter);
|
||||
assertNotNull(container.getBean(FormattingConversionServiceFactoryBean.class));
|
||||
assertNotNull(container.getBean(ConversionService.class));
|
||||
assertNotNull(container.getBean(LocalValidatorFactoryBean.class));
|
||||
assertNotNull(container.getBean(Validator.class));
|
||||
|
||||
TestController handler = new TestController();
|
||||
|
||||
@@ -68,12 +90,13 @@ public class MvcNamespaceTests {
|
||||
public void testCustomConversionService() throws Exception {
|
||||
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(container);
|
||||
reader.loadBeanDefinitions(new ClassPathResource("mvc-config-custom-conversion-service.xml", getClass()));
|
||||
assertEquals(3, container.getBeanDefinitionCount());
|
||||
assertEquals(4, container.getBeanDefinitionCount());
|
||||
DefaultAnnotationHandlerMapping mapping = container.getBean(DefaultAnnotationHandlerMapping.class);
|
||||
assertNotNull(mapping);
|
||||
assertEquals(0, mapping.getOrder());
|
||||
AnnotationMethodHandlerAdapter adapter = container.getBean(AnnotationMethodHandlerAdapter.class);
|
||||
assertNotNull(adapter);
|
||||
assertNotNull(container.getBean(LocalValidatorFactoryBean.class));
|
||||
|
||||
TestController handler = new TestController();
|
||||
|
||||
@@ -88,12 +111,13 @@ public class MvcNamespaceTests {
|
||||
public void testCustomValidator() throws Exception {
|
||||
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(container);
|
||||
reader.loadBeanDefinitions(new ClassPathResource("mvc-config-custom-validator.xml", getClass()));
|
||||
assertEquals(3, container.getBeanDefinitionCount());
|
||||
assertEquals(4, container.getBeanDefinitionCount());
|
||||
DefaultAnnotationHandlerMapping mapping = container.getBean(DefaultAnnotationHandlerMapping.class);
|
||||
assertNotNull(mapping);
|
||||
assertEquals(0, mapping.getOrder());
|
||||
AnnotationMethodHandlerAdapter adapter = container.getBean(AnnotationMethodHandlerAdapter.class);
|
||||
assertNotNull(adapter);
|
||||
assertNotNull(container.getBean(FormattingConversionServiceFactoryBean.class));
|
||||
|
||||
TestController handler = new TestController();
|
||||
|
||||
@@ -150,4 +174,5 @@ public class MvcNamespaceTests {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user