From a725717261c347366e5bc36d95ade9b3ec0109a9 Mon Sep 17 00:00:00 2001 From: Keith Donald Date: Wed, 11 Nov 2009 15:43:57 +0000 Subject: [PATCH] tests for custom conversion service / validator --- .../web/servlet/config/MvcNamespaceTests.java | 94 ++++++++++++++++++- .../mvc-config-custom-conversion-service.xml | 12 +++ .../config/mvc-config-custom-validator.xml | 12 +++ 3 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 org.springframework.web.servlet/src/test/resources/org/springframework/web/servlet/config/mvc-config-custom-conversion-service.xml create mode 100644 org.springframework.web.servlet/src/test/resources/org/springframework/web/servlet/config/mvc-config-custom-validator.xml diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java index 68db7132d2..e4f2fda92d 100644 --- a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java @@ -1,13 +1,19 @@ 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.assertTrue; import java.util.Date; import java.util.Locale; +import javax.validation.Valid; +import javax.validation.constraints.NotNull; + import org.junit.Before; import org.junit.Test; +import org.springframework.beans.ConversionNotSupportedException; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.core.io.ClassPathResource; @@ -17,6 +23,9 @@ import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.mock.web.MockServletContext; import org.springframework.stereotype.Controller; +import org.springframework.validation.BindingResult; +import org.springframework.validation.Errors; +import org.springframework.validation.Validator; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.context.support.GenericWebApplicationContext; @@ -52,14 +61,93 @@ public class MvcNamespaceTests { request.addParameter("date", "2009-10-31"); MockHttpServletResponse response = new MockHttpServletResponse(); adapter.handle(request, response, handler); + assertTrue(handler.recordedValidationError); } - + + @Test(expected=ConversionNotSupportedException.class) + 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()); + DefaultAnnotationHandlerMapping mapping = container.getBean(DefaultAnnotationHandlerMapping.class); + assertNotNull(mapping); + assertEquals(0, mapping.getOrder()); + AnnotationMethodHandlerAdapter adapter = container.getBean(AnnotationMethodHandlerAdapter.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); + } + + @Test + public void testCustomValidator() throws Exception { + XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(container); + reader.loadBeanDefinitions(new ClassPathResource("mvc-config-custom-validator.xml", getClass())); + assertEquals(3, container.getBeanDefinitionCount()); + DefaultAnnotationHandlerMapping mapping = container.getBean(DefaultAnnotationHandlerMapping.class); + assertNotNull(mapping); + assertEquals(0, mapping.getOrder()); + AnnotationMethodHandlerAdapter adapter = container.getBean(AnnotationMethodHandlerAdapter.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); + + assertTrue(container.getBean(TestValidator.class).validatorInvoked); + assertFalse(handler.recordedValidationError); + } + @Controller public static class TestController { + private boolean recordedValidationError; + @RequestMapping - public void testBind(@RequestParam @DateTimeFormat(iso=ISO.DATE) Date date) { - + public void testBind(@RequestParam @DateTimeFormat(iso=ISO.DATE) Date date, @Valid TestBean bean, BindingResult result) { + if (result.getErrorCount() == 1) { + this.recordedValidationError = true; + } else { + this.recordedValidationError = false; + } } } + + public static class TestValidator implements Validator { + + boolean validatorInvoked; + + public boolean supports(Class clazz) { + return true; + } + + public void validate(Object target, Errors errors) { + this.validatorInvoked = true; + } + + } + + private static class TestBean { + + @NotNull + private String field; + + public String getField() { + return field; + } + + public void setField(String field) { + this.field = field; + } + + } } diff --git a/org.springframework.web.servlet/src/test/resources/org/springframework/web/servlet/config/mvc-config-custom-conversion-service.xml b/org.springframework.web.servlet/src/test/resources/org/springframework/web/servlet/config/mvc-config-custom-conversion-service.xml new file mode 100644 index 0000000000..334c20d7f9 --- /dev/null +++ b/org.springframework.web.servlet/src/test/resources/org/springframework/web/servlet/config/mvc-config-custom-conversion-service.xml @@ -0,0 +1,12 @@ + + + + + + + + diff --git a/org.springframework.web.servlet/src/test/resources/org/springframework/web/servlet/config/mvc-config-custom-validator.xml b/org.springframework.web.servlet/src/test/resources/org/springframework/web/servlet/config/mvc-config-custom-validator.xml new file mode 100644 index 0000000000..31e46473e3 --- /dev/null +++ b/org.springframework.web.servlet/src/test/resources/org/springframework/web/servlet/config/mvc-config-custom-validator.xml @@ -0,0 +1,12 @@ + + + + + + + +