From bbeed23f9460563c88246e6897be352a51151e63 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 13 Jun 2011 23:43:46 +0000 Subject: [PATCH] added further conversion tests (triggered by 3.0.6 backports) git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@4526 50f2f4bb-b051-0410-bef5-90022cba6387 --- .../FormattingConversionServiceTests.java | 50 ++++++++++++++++--- .../ServletAnnotationControllerTests.java | 40 +++++++++++++-- 2 files changed, 80 insertions(+), 10 deletions(-) diff --git a/org.springframework.context/src/test/java/org/springframework/format/support/FormattingConversionServiceTests.java b/org.springframework.context/src/test/java/org/springframework/format/support/FormattingConversionServiceTests.java index dbcec2671ec..11c7460fb83 100644 --- a/org.springframework.context/src/test/java/org/springframework/format/support/FormattingConversionServiceTests.java +++ b/org.springframework.context/src/test/java/org/springframework/format/support/FormattingConversionServiceTests.java @@ -16,9 +16,6 @@ package org.springframework.format.support; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; - import java.text.ParseException; import java.util.ArrayList; import java.util.Date; @@ -32,6 +29,7 @@ import org.joda.time.format.DateTimeFormat; import org.junit.After; import org.junit.Before; import org.junit.Test; + import org.springframework.beans.BeanUtils; import org.springframework.beans.ConfigurablePropertyAccessor; import org.springframework.beans.PropertyAccessorFactory; @@ -44,11 +42,14 @@ import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.format.Formatter; +import org.springframework.format.Printer; import org.springframework.format.datetime.joda.DateTimeParser; import org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory; import org.springframework.format.datetime.joda.ReadablePartialPrinter; import org.springframework.format.number.NumberFormatter; +import static org.junit.Assert.*; + /** * @author Keith Donald * @author Juergen Hoeller @@ -196,7 +197,8 @@ public class FormattingConversionServiceTests { @Test public void testParseNull() throws ParseException { formattingService.addFormatterForFieldType(Number.class, new NumberFormatter()); - assertNull(formattingService.convert(null, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class))); + assertNull(formattingService + .convert(null, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class))); } @Test @@ -225,12 +227,14 @@ public class FormattingConversionServiceTests { @Test public void testPrintNullDefault() throws ParseException { - assertEquals(null, formattingService.convert(null, TypeDescriptor.valueOf(Integer.class), TypeDescriptor.valueOf(String.class))); + assertEquals(null, formattingService + .convert(null, TypeDescriptor.valueOf(Integer.class), TypeDescriptor.valueOf(String.class))); } @Test public void testParseNullDefault() throws ParseException { - assertNull(formattingService.convert(null, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class))); + assertNull(formattingService + .convert(null, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class))); } @Test @@ -238,6 +242,29 @@ public class FormattingConversionServiceTests { assertNull(formattingService.convert("", TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class))); } + @Test + public void testFormatFieldForAnnotationWithSubclassAsFieldType() throws Exception { + formattingService.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory() { + public Printer getPrinter(org.springframework.format.annotation.DateTimeFormat annotation, Class fieldType) { + assertEquals(MyDate.class, fieldType); + return super.getPrinter(annotation, fieldType); + } + }); + formattingService.addConverter(new Converter() { + public Long convert(MyDate source) { + return source.getTime(); + } + }); + formattingService.addConverter(new Converter() { + public Date convert(MyDate source) { + return source; + } + }); + + formattingService.convert(new MyDate(), new TypeDescriptor(ModelWithSubclassField.class.getField("date")), + TypeDescriptor.valueOf(String.class)); + } + public static class Model { @@ -286,4 +313,15 @@ public class FormattingConversionServiceTests { } + + public static class MyDate extends Date { + } + + + private static class ModelWithSubclassField { + + @org.springframework.format.annotation.DateTimeFormat(style = "S-") + public MyDate date; + } + } diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/ServletAnnotationControllerTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/ServletAnnotationControllerTests.java index c3f486ec5e7..3f81ca2be8a 100644 --- a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/ServletAnnotationControllerTests.java +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/ServletAnnotationControllerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * 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. @@ -1817,7 +1817,7 @@ public class ServletAnnotationControllerTests { } @Test - public void parameterCsvAsStringArray() throws Exception { + public void parameterCsvAsIntegerArray() throws Exception { servlet = new DispatcherServlet() { @Override protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) { @@ -1844,6 +1844,34 @@ public class ServletAnnotationControllerTests { assertEquals("1-2", response.getContentAsString()); } + @Test + public void parameterCsvAsIntegerSet() throws Exception { + servlet = new DispatcherServlet() { + @Override + protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) { + GenericWebApplicationContext wac = new GenericWebApplicationContext(); + wac.registerBeanDefinition("controller", new RootBeanDefinition(CsvController.class)); + RootBeanDefinition csDef = new RootBeanDefinition(FormattingConversionServiceFactoryBean.class); + RootBeanDefinition wbiDef = new RootBeanDefinition(ConfigurableWebBindingInitializer.class); + wbiDef.getPropertyValues().add("conversionService", csDef); + RootBeanDefinition adapterDef = new RootBeanDefinition(AnnotationMethodHandlerAdapter.class); + adapterDef.getPropertyValues().add("webBindingInitializer", wbiDef); + wac.registerBeanDefinition("handlerAdapter", adapterDef); + wac.refresh(); + return wac; + } + }; + servlet.init(new MockServletConfig()); + + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setRequestURI("/integerSet"); + request.setMethod("POST"); + request.addParameter("content", "1,2"); + MockHttpServletResponse response = new MockHttpServletResponse(); + servlet.service(request, response); + assertEquals("1-2", response.getContentAsString()); + } + @Test public void testMatchWithoutMethodLevelPath() throws Exception { initServlet(NoPathGetAndM2PostController.class); @@ -3114,6 +3142,12 @@ public class ServletAnnotationControllerTests { public void processCsv(@RequestParam("content") Integer[] content, HttpServletResponse response) throws IOException { response.getWriter().write(StringUtils.arrayToDelimitedString(content, "-")); } + + @RequestMapping("/integerSet") + public void processCsv(@RequestParam("content") Set content, HttpServletResponse response) throws IOException { + assertTrue(content.iterator().next() instanceof Integer); + response.getWriter().write(StringUtils.collectionToDelimitedString(content, "-")); + } } @Controller @@ -3130,6 +3164,4 @@ public class ServletAnnotationControllerTests { } } - - }