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
This commit is contained in:
parent
22d5a23a54
commit
bbeed23f94
|
|
@ -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<MyDate, Long>() {
|
||||
public Long convert(MyDate source) {
|
||||
return source.getTime();
|
||||
}
|
||||
});
|
||||
formattingService.addConverter(new Converter<MyDate, Date>() {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<Integer> 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 {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue