Skip to content

Commit

Permalink
Revisit date-time tests for compatibility with JDK 9 build 72
Browse files Browse the repository at this point in the history
Issue: SPR-13232
  • Loading branch information
jhoeller committed Jul 14, 2015
1 parent 48f330d commit f4f508d
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 201 deletions.
Expand Up @@ -149,15 +149,16 @@ public void shouldPrintAndParseISODateTime() throws Exception {

@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;
if (!style.equals("--")) {
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);
Expand All @@ -166,13 +167,11 @@ public void shouldSupportJodaStylePatterns() throws Exception {
}
}

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)));
Expand All @@ -181,7 +180,7 @@ private void testJodaStylePatterns(String style, Locale locale, Date date)
}

@Test
public void shouldThrowOnUnsupportStylePattern() throws Exception {
public void shouldThrowOnUnsupportedStylePattern() throws Exception {
DateFormatter formatter = new DateFormatter();
formatter.setStylePattern("OO");
thown.expect(IllegalStateException.class);
Expand Down
Expand Up @@ -76,20 +76,30 @@ public void tearDown() {


@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
Expand All @@ -101,6 +111,14 @@ public void testBindDateAnnotated() {
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();
Expand All @@ -122,39 +140,12 @@ public void testBindDateAnnotatedWithFallbackError() {
}

@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
Expand Down Expand Up @@ -239,23 +230,17 @@ public void stringToDateWithGlobalFormat() throws Exception {
@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)
Expand All @@ -269,28 +254,21 @@ private static class SimpleDateBean {

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() {
Expand All @@ -301,21 +279,20 @@ public void setCalendarAnnotated(Calendar calendarAnnotated) {
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() {
Expand Down
@@ -1,5 +1,5 @@
/*
* 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.
Expand Down Expand Up @@ -77,7 +77,9 @@ public void createDateTimeFormatterWithFallback() throws Exception {
@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"));
Expand Down

0 comments on commit f4f508d

Please sign in to comment.