提交 f4f508d8 编写于 作者: J Juergen Hoeller

Revisit date-time tests for compatibility with JDK 9 build 72

Issue: SPR-13232
上级 48f330dd
......@@ -149,7 +149,7 @@ public class DateFormatterTests {
@Test
public void shouldSupportJodaStylePatterns() throws Exception {
String[] chars = { "S", "M", "L", "F", "-" };
String[] chars = { "S", "M", "-" };
for (String d : chars) {
for (String t : chars) {
String style = d + t;
......@@ -157,7 +157,8 @@ public class DateFormatterTests {
Date date = getDate(2009, Calendar.JUNE, 10, 14, 23, 0, 0);
if (t.equals("-")) {
date = getDate(2009, Calendar.JUNE, 10);
} else if (d.equals("-")) {
}
else if (d.equals("-")) {
date = getDate(1970, Calendar.JANUARY, 1, 14, 23, 0, 0);
}
testJodaStylePatterns(style, Locale.US, date);
......@@ -166,13 +167,11 @@ public class DateFormatterTests {
}
}
private void testJodaStylePatterns(String style, Locale locale, Date date)
throws Exception {
private void testJodaStylePatterns(String style, Locale locale, Date date) throws Exception {
DateFormatter formatter = new DateFormatter();
formatter.setTimeZone(UTC);
formatter.setStylePattern(style);
DateTimeFormatter jodaFormatter = DateTimeFormat.forStyle(style).withLocale(
locale).withZone(DateTimeZone.UTC);
DateTimeFormatter jodaFormatter = DateTimeFormat.forStyle(style).withLocale(locale).withZone(DateTimeZone.UTC);
String jodaPrinted = jodaFormatter.print(date.getTime());
assertThat("Unable to print style pattern " + style,
formatter.print(date, locale), is(equalTo(jodaPrinted)));
......@@ -181,7 +180,7 @@ public class DateFormatterTests {
}
@Test
public void shouldThrowOnUnsupportStylePattern() throws Exception {
public void shouldThrowOnUnsupportedStylePattern() throws Exception {
DateFormatter formatter = new DateFormatter();
formatter.setStylePattern("OO");
thown.expect(IllegalStateException.class);
......
......@@ -76,20 +76,30 @@ public class DateFormattingTests {
@Test
public void testBindDate() {
public void testBindLong() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("date", "10/31/09 12:00 PM");
propertyValues.add("millis", "1256961600");
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("date"));
assertEquals("1256961600", binder.getBindingResult().getFieldValue("millis"));
}
@Test
public void testBindDateArray() {
public void testBindLongAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("date", new String[] {"10/31/09 12:00 PM"});
propertyValues.add("millisAnnotated", "10/31/09");
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("10/31/09", binder.getBindingResult().getFieldValue("millisAnnotated"));
}
@Test
public void testBindCalendarAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("calendarAnnotated", "10/31/09");
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("10/31/09", binder.getBindingResult().getFieldValue("calendarAnnotated"));
}
@Test
......@@ -101,6 +111,14 @@ public class DateFormattingTests {
assertEquals("10/31/09", binder.getBindingResult().getFieldValue("dateAnnotated"));
}
@Test
public void testBindDateArray() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateAnnotated", new String[]{"10/31/09 12:00 PM"});
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
}
@Test
public void testBindDateAnnotatedWithError() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
......@@ -122,39 +140,12 @@ public class DateFormattingTests {
}
@Test
public void testBindCalendar() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("calendar", "10/31/09 12:00 PM");
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("calendar"));
}
@Test
public void testBindCalendarAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("calendarAnnotated", "10/31/09");
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("10/31/09", binder.getBindingResult().getFieldValue("calendarAnnotated"));
}
@Test
public void testBindLong() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("millis", "1256961600");
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("1256961600", binder.getBindingResult().getFieldValue("millis"));
}
@Test
public void testBindLongAnnotated() {
public void testBindDateAnnotatedPattern() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("millisAnnotated", "10/31/09");
propertyValues.add("dateAnnotatedPattern", "10/31/09 1:05");
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("10/31/09", binder.getBindingResult().getFieldValue("millisAnnotated"));
assertEquals("10/31/09 1:05", binder.getBindingResult().getFieldValue("dateAnnotatedPattern"));
}
@Test
......@@ -239,23 +230,17 @@ public class DateFormattingTests {
@SuppressWarnings("unused")
private static class SimpleDateBean {
@DateTimeFormat
private Date date;
@DateTimeFormat(style="S-")
private Date dateAnnotated;
private Long millis;
@DateTimeFormat
private Calendar calendar;
private Long millisAnnotated;
@DateTimeFormat(style="S-")
private Calendar calendarAnnotated;
private Long millis;
private Long millisAnnotated;
@DateTimeFormat(style="S-")
private Date dateAnnotated;
@DateTimeFormat(pattern="M/d/yy h:mm a")
@DateTimeFormat(pattern="M/d/yy h:mm")
private Date dateAnnotatedPattern;
@DateTimeFormat(iso=ISO.DATE)
......@@ -269,28 +254,21 @@ public class DateFormattingTests {
private final List<SimpleDateBean> children = new ArrayList<SimpleDateBean>();
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Date getDateAnnotated() {
return dateAnnotated;
public Long getMillis() {
return millis;
}
public void setDateAnnotated(Date dateAnnotated) {
this.dateAnnotated = dateAnnotated;
public void setMillis(Long millis) {
this.millis = millis;
}
public Calendar getCalendar() {
return calendar;
@DateTimeFormat(style="S-")
public Long getMillisAnnotated() {
return millisAnnotated;
}
public void setCalendar(Calendar calendar) {
this.calendar = calendar;
public void setMillisAnnotated(@DateTimeFormat(style="S-") Long millisAnnotated) {
this.millisAnnotated = millisAnnotated;
}
public Calendar getCalendarAnnotated() {
......@@ -301,21 +279,20 @@ public class DateFormattingTests {
this.calendarAnnotated = calendarAnnotated;
}
public Long getMillis() {
return millis;
public Date getDateAnnotated() {
return dateAnnotated;
}
public void setMillis(Long millis) {
this.millis = millis;
public void setDateAnnotated(Date dateAnnotated) {
this.dateAnnotated = dateAnnotated;
}
@DateTimeFormat(style="S-")
public Long getMillisAnnotated() {
return millisAnnotated;
public Date getDateAnnotatedPattern() {
return dateAnnotatedPattern;
}
public void setMillisAnnotated(@DateTimeFormat(style="S-") Long millisAnnotated) {
this.millisAnnotated = millisAnnotated;
public void setDateAnnotatedPattern(Date dateAnnotatedPattern) {
this.dateAnnotatedPattern = dateAnnotatedPattern;
}
public Date getIsoDate() {
......
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2015 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.
......@@ -77,7 +77,9 @@ public class DateTimeFormatterFactoryTests {
@Test
public void createDateTimeFormatterInOrderOfPropertyPriority() throws Exception {
factory.setStyle("SS");
assertThat(applyLocale(factory.createDateTimeFormatter()).print(dateTime), is("10/21/09 12:10 PM"));
String value = applyLocale(factory.createDateTimeFormatter()).print(dateTime);
assertTrue(value.startsWith("10/21/09"));
assertTrue(value.endsWith("12:10 PM"));
factory.setIso(ISO.DATE);
assertThat(applyLocale(factory.createDateTimeFormatter()).print(dateTime), is("2009-10-21"));
......
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 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.
......@@ -28,7 +28,6 @@ import org.joda.time.Instant;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.joda.time.LocalTime;
import org.joda.time.MutableDateTime;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
......@@ -56,6 +55,7 @@ public class JodaTimeFormattingTests {
private DataBinder binder;
@Before
public void setUp() {
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
......@@ -85,6 +85,7 @@ public class JodaTimeFormattingTests {
JodaTimeContextHolder.setJodaTimeContext(null);
}
@Test
public void testJodaTimePatternsForStyle() {
System.out.println(org.joda.time.format.DateTimeFormat.patternForStyle("SS", LocaleContextHolder.getLocale()));
......@@ -226,28 +227,33 @@ public class JodaTimeFormattingTests {
@Test
public void testBindLocalDateTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateTime", "10/31/09 12:00 PM");
propertyValues.add("localDateTime", new LocalDateTime(2009, 10, 31, 12, 0));
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("localDateTime"));
String value = binder.getBindingResult().getFieldValue("localDateTime").toString();
assertTrue(value.startsWith("10/31/09"));
assertTrue(value.endsWith("12:00 PM"));
}
@Test
public void testBindLocalDateTimeAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateTimeAnnotated", "Oct 31, 2009 12:00 PM");
propertyValues.add("localDateTimeAnnotated", new LocalDateTime(2009, 10, 31, 12, 0));
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("Oct 31, 2009 12:00 PM", binder.getBindingResult().getFieldValue("localDateTimeAnnotated"));
String value = binder.getBindingResult().getFieldValue("localDateTimeAnnotated").toString();
assertTrue(value.startsWith("Oct 31, 2009"));
assertTrue(value.endsWith("12:00 PM"));
}
@Test
public void testBindDateTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateTime", "10/31/09 12:00 PM");
propertyValues.add("dateTime", new DateTime(2009, 10, 31, 12, 0));
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("dateTime"));
String value = binder.getBindingResult().getFieldValue("dateTime").toString();
assertTrue(value.startsWith("10/31/09"));
}
@Test
......@@ -256,10 +262,12 @@ public class JodaTimeFormattingTests {
registrar.setDateTimeStyle("MM");
setUp(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateTime", "Oct 31, 2009 12:00:00 PM");
propertyValues.add("localDateTime", new LocalDateTime(2009, 10, 31, 12, 0));
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("Oct 31, 2009 12:00:00 PM", binder.getBindingResult().getFieldValue("localDateTime"));
String value = binder.getBindingResult().getFieldValue("localDateTime").toString();
assertTrue(value.startsWith("Oct 31, 2009"));
assertTrue(value.endsWith("12:00:00 PM"));
}
@Test
......@@ -289,10 +297,11 @@ public class JodaTimeFormattingTests {
@Test
public void testBindDateTimeAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateTimeAnnotated", "Oct 31, 2009 12:00 PM");
propertyValues.add("dateTimeAnnotated", new DateTime(2009, 10, 31, 12, 0));
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("Oct 31, 2009 12:00 PM", binder.getBindingResult().getFieldValue("dateTimeAnnotated"));
String value = binder.getBindingResult().getFieldValue("dateTimeAnnotated").toString();
assertTrue(value.startsWith("Oct 31, 2009"));
}
@Test
......@@ -307,19 +316,11 @@ public class JodaTimeFormattingTests {
@Test
public void testBindDateTimeAnnotatedDefault() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("dateTimeAnnotatedDefault", "10/31/09 12:00 PM");
propertyValues.add("dateTimeAnnotatedDefault", new DateTime(2009, 10, 31, 12, 0));
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("dateTimeAnnotatedDefault"));
}
@Test
public void testBindDate() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("date", "10/31/09 12:00 PM");
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("date"));
String value = binder.getBindingResult().getFieldValue("dateTimeAnnotatedDefault").toString();
assertTrue(value.startsWith("10/31/09"));
}
@Test
......@@ -349,15 +350,6 @@ public class JodaTimeFormattingTests {
assertEquals("10/31/09", binder.getBindingResult().getFieldValue("dateAnnotated"));
}
@Test
public void testBindCalendar() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("calendar", "10/31/09 12:00 PM");
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("calendar"));
}
@Test
public void testBindCalendarAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
......@@ -412,15 +404,6 @@ public class JodaTimeFormattingTests {
assertEquals("2009-10-31T07:00:00.000-05:00", binder.getBindingResult().getFieldValue("isoDateTime"));
}
@Test
public void testBindInstant() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("instant", "10/31/09 12:00 PM");
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("instant"));
}
@Test
public void testBindInstantAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
......@@ -430,15 +413,6 @@ public class JodaTimeFormattingTests {
assertEquals("2009-10-31T07:00:00.000-05:00", binder.getBindingResult().getFieldValue("instantAnnotated"));
}
@Test
public void testBindMutableDateTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("mutableDateTime", "10/31/09 12:00 PM");
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("mutableDateTime"));
}
@Test
public void testBindMutableDateTimeAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
......@@ -512,9 +486,6 @@ public class JodaTimeFormattingTests {
@DateTimeFormat(style="S-")
private Date dateAnnotated;
@DateTimeFormat
private Calendar calendar;
@DateTimeFormat(style="S-")
private Calendar calendarAnnotated;
......@@ -537,13 +508,9 @@ public class JodaTimeFormattingTests {
@DateTimeFormat(iso=ISO.DATE_TIME)
private DateTime isoDateTime;
private Instant instant;
@DateTimeFormat(iso=ISO.DATE_TIME)
private Instant instantAnnotated;
private MutableDateTime mutableDateTime;
@DateTimeFormat(iso=ISO.DATE_TIME)
private Instant mutableDateTimeAnnotated;
......@@ -645,14 +612,6 @@ public class JodaTimeFormattingTests {
this.dateAnnotated = dateAnnotated;
}
public Calendar getCalendar() {
return calendar;
}
public void setCalendar(Calendar calendar) {
this.calendar = calendar;
}
public Calendar getCalendarAnnotated() {
return calendarAnnotated;
}
......@@ -702,14 +661,6 @@ public class JodaTimeFormattingTests {
this.isoDateTime = isoDateTime;
}
public Instant getInstant() {
return instant;
}
public void setInstant(Instant instant) {
this.instant = instant;
}
public Instant getInstantAnnotated() {
return instantAnnotated;
}
......@@ -718,14 +669,6 @@ public class JodaTimeFormattingTests {
this.instantAnnotated = instantAnnotated;
}
public MutableDateTime getMutableDateTime() {
return mutableDateTime;
}
public void setMutableDateTime(MutableDateTime mutableDateTime) {
this.mutableDateTime = mutableDateTime;
}
public Instant getMutableDateTimeAnnotated() {
return mutableDateTimeAnnotated;
}
......
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2015 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.
......@@ -78,7 +78,9 @@ public class DateTimeFormatterFactoryTests {
@Test
public void createDateTimeFormatterInOrderOfPropertyPriority() throws Exception {
factory.setStylePattern("SS");
assertThat(applyLocale(factory.createDateTimeFormatter()).format(dateTime), is("10/21/09 12:10 PM"));
String value = applyLocale(factory.createDateTimeFormatter()).format(dateTime);
assertTrue(value.startsWith("10/21/09"));
assertTrue(value.endsWith("12:10 PM"));
factory.setIso(ISO.DATE);
assertThat(applyLocale(factory.createDateTimeFormatter()).format(dateTime), is("2009-10-21"));
......
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 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.
......@@ -16,7 +16,6 @@
package org.springframework.format.datetime.standard;
import java.text.DateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
......@@ -25,10 +24,10 @@ import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import org.junit.After;
import org.junit.Before;
......@@ -55,6 +54,7 @@ public class DateTimeFormattingTests {
private DataBinder binder;
@Before
public void setUp() {
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
......@@ -84,6 +84,7 @@ public class DateTimeFormattingTests {
DateTimeContextHolder.setDateTimeContext(null);
}
@Test
public void testBindLocalDate() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
......@@ -235,19 +236,23 @@ public class DateTimeFormattingTests {
@Test
public void testBindLocalDateTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateTime", "10/31/09 12:00 PM");
propertyValues.add("localDateTime", LocalDateTime.of(2009, 10, 31, 12, 0));
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("localDateTime"));
String value = binder.getBindingResult().getFieldValue("localDateTime").toString();
assertTrue(value.startsWith("10/31/09"));
assertTrue(value.endsWith("12:00 PM"));
}
@Test
public void testBindLocalDateTimeAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateTimeAnnotated", "Oct 31, 2009 12:00:00 PM");
propertyValues.add("localDateTimeAnnotated", LocalDateTime.of(2009, 10, 31, 12, 0));
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("Oct 31, 2009 12:00:00 PM", binder.getBindingResult().getFieldValue("localDateTimeAnnotated"));
String value = binder.getBindingResult().getFieldValue("localDateTimeAnnotated").toString();
assertTrue(value.startsWith("Oct 31, 2009"));
assertTrue(value.endsWith("12:00:00 PM"));
}
@Test
......@@ -256,7 +261,9 @@ public class DateTimeFormattingTests {
propertyValues.add("localDateTime", new GregorianCalendar(2009, 9, 31, 12, 0));
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("localDateTime"));
String value = binder.getBindingResult().getFieldValue("localDateTime").toString();
assertTrue(value.startsWith("10/31/09"));
assertTrue(value.endsWith("12:00 PM"));
}
@Test
......@@ -265,10 +272,12 @@ public class DateTimeFormattingTests {
registrar.setDateTimeStyle(FormatStyle.MEDIUM);
setUp(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateTime", "Oct 31, 2009 12:00:00 PM");
propertyValues.add("localDateTime", LocalDateTime.of(2009, 10, 31, 12, 0));
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("Oct 31, 2009 12:00:00 PM", binder.getBindingResult().getFieldValue("localDateTime"));
String value = binder.getBindingResult().getFieldValue("localDateTime").toString();
assertTrue(value.startsWith("Oct 31, 2009"));
assertTrue(value.endsWith("12:00:00 PM"));
}
@Test
......@@ -318,13 +327,11 @@ public class DateTimeFormattingTests {
@Test
public void testBindInstantFromJavaUtilDate() throws Exception {
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.US);
df.setTimeZone(TimeZone.getTimeZone("GMT"));
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("instant", df.parse("10/31/09 12:00 PM"));
propertyValues.add("instant", new Date(109, 9, 31, 12, 0));
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertTrue(binder.getBindingResult().getFieldValue("instant").toString().startsWith("2009-10-31T12:00"));
assertTrue(binder.getBindingResult().getFieldValue("instant").toString().startsWith("2009-10-31"));
}
......
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2015 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.
......@@ -35,12 +35,12 @@ import org.springframework.format.FormatterRegistry;
import org.springframework.format.Parser;
import org.springframework.format.Printer;
import org.springframework.format.annotation.NumberFormat;
import org.springframework.format.annotation.NumberFormat.Style;
import static org.junit.Assert.*;
/**
* @author Rossen Stoyanchev
* @author Juergen Hoeller
*/
public class FormattingConversionServiceFactoryBeanTests {
......@@ -49,11 +49,11 @@ public class FormattingConversionServiceFactoryBeanTests {
FormattingConversionServiceFactoryBean factory = new FormattingConversionServiceFactoryBean();
factory.afterPropertiesSet();
FormattingConversionService fcs = factory.getObject();
TypeDescriptor descriptor = new TypeDescriptor(TestBean.class.getDeclaredField("percent"));
Object value = fcs.convert("5%", TypeDescriptor.valueOf(String.class), descriptor);
assertEquals(.05, value);
value = fcs.convert(.05, descriptor, TypeDescriptor.valueOf(String.class));
assertEquals("5%", value);
TypeDescriptor descriptor = new TypeDescriptor(TestBean.class.getDeclaredField("pattern"));
Object value = fcs.convert("15,00", TypeDescriptor.valueOf(String.class), descriptor);
assertEquals(15.0, value);
value = fcs.convert(15.0, descriptor, TypeDescriptor.valueOf(String.class));
assertEquals("15", value);
}
@Test
......@@ -62,9 +62,9 @@ public class FormattingConversionServiceFactoryBeanTests {
factory.setRegisterDefaultFormatters(false);
factory.afterPropertiesSet();
FormattingConversionService fcs = factory.getObject();
TypeDescriptor descriptor = new TypeDescriptor(TestBean.class.getDeclaredField("percent"));
TypeDescriptor descriptor = new TypeDescriptor(TestBean.class.getDeclaredField("pattern"));
try {
fcs.convert("5%", TypeDescriptor.valueOf(String.class), descriptor);
fcs.convert("15,00", TypeDescriptor.valueOf(String.class), descriptor);
fail("This format should not be parseable");
}
catch (ConversionFailedException ex) {
......@@ -128,10 +128,11 @@ public class FormattingConversionServiceFactoryBeanTests {
private @interface SpecialInt {
}
private static class TestBean {
@NumberFormat(style = Style.PERCENT)
private double percent;
@NumberFormat(pattern = "##,00")
private double pattern;
@SpecialInt
private int specialInt;
......@@ -143,9 +144,9 @@ public class FormattingConversionServiceFactoryBeanTests {
public void setSpecialInt(int field) {
this.specialInt = field;
}
}
private static class TestBeanFormatter implements Formatter<TestBean> {
@Override
......@@ -159,9 +160,9 @@ public class FormattingConversionServiceFactoryBeanTests {
object.setSpecialInt(Integer.parseInt(text));
return object;
}
}
private static class SpecialIntAnnotationFormatterFactory implements AnnotationFormatterFactory<SpecialInt> {
private final Set<Class<?>> fieldTypes = new HashSet<Class<?>>(1);
......@@ -196,13 +197,13 @@ public class FormattingConversionServiceFactoryBeanTests {
}
}
private static class TestFormatterRegistrar implements FormatterRegistrar {
@Override
public void registerFormatters(FormatterRegistry registry) {
registry.addFormatter(new TestBeanFormatter());
}
}
}
......@@ -40,18 +40,13 @@ import org.junit.Test;
/**
* Unit tests for {@link org.springframework.http.HttpHeaders}.
*
* @author Arjen Poutsma
* @author Sebastien Deleuze
*/
public class HttpHeadersTests {
private HttpHeaders headers;
@Before
public void setUp() {
headers = new HttpHeaders();
}
private final HttpHeaders headers = new HttpHeaders();
@Test
......@@ -67,7 +62,7 @@ public class HttpHeadersTests {
}
@Test // SPR-9655
public void acceptiPlanet() {
public void acceptIPlanet() {
headers.add("Accept", "text/html");
headers.add("Accept", "text/plain");
List<MediaType> expected = Arrays.asList(new MediaType("text", "html"), new MediaType("text", "plain"));
......@@ -171,7 +166,7 @@ public class HttpHeadersTests {
assertEquals("Invalid Date header", "Thu, 18 Dec 2008 10:20:00 GMT", headers.getFirst("date"));
// RFC 850
headers.set("Date", "Thursday, 18-Dec-08 11:20:00 CET");
headers.set("Date", "Thu, 18 Dec 2008 10:20:00 GMT");
assertEquals("Invalid Date header", date, headers.getDate());
}
......
......@@ -131,7 +131,8 @@ public class GsonFactoryBeanTests {
Date date = cal.getTime();
bean.setDate(date);
String result = gson.toJson(bean);
assertEquals("{\"date\":\"Jan 1, 2014 12:00:00 AM\"}", result);
assertTrue(result.startsWith("{\"date\":\"Jan 1, 2014"));
assertTrue(result.endsWith("12:00:00 AM\"}"));
}
@Test
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册