Skip to content

Commit f4f508d

Browse files
committed
Revisit date-time tests for compatibility with JDK 9 build 72
Issue: SPR-13232
1 parent 48f330d commit f4f508d

File tree

9 files changed

+128
-201
lines changed

9 files changed

+128
-201
lines changed

spring-context/src/test/java/org/springframework/format/datetime/DateFormatterTests.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,16 @@ public void shouldPrintAndParseISODateTime() throws Exception {
149149

150150
@Test
151151
public void shouldSupportJodaStylePatterns() throws Exception {
152-
String[] chars = { "S", "M", "L", "F", "-" };
152+
String[] chars = { "S", "M", "-" };
153153
for (String d : chars) {
154154
for (String t : chars) {
155155
String style = d + t;
156156
if (!style.equals("--")) {
157157
Date date = getDate(2009, Calendar.JUNE, 10, 14, 23, 0, 0);
158158
if (t.equals("-")) {
159159
date = getDate(2009, Calendar.JUNE, 10);
160-
} else if (d.equals("-")) {
160+
}
161+
else if (d.equals("-")) {
161162
date = getDate(1970, Calendar.JANUARY, 1, 14, 23, 0, 0);
162163
}
163164
testJodaStylePatterns(style, Locale.US, date);
@@ -166,13 +167,11 @@ public void shouldSupportJodaStylePatterns() throws Exception {
166167
}
167168
}
168169

169-
private void testJodaStylePatterns(String style, Locale locale, Date date)
170-
throws Exception {
170+
private void testJodaStylePatterns(String style, Locale locale, Date date) throws Exception {
171171
DateFormatter formatter = new DateFormatter();
172172
formatter.setTimeZone(UTC);
173173
formatter.setStylePattern(style);
174-
DateTimeFormatter jodaFormatter = DateTimeFormat.forStyle(style).withLocale(
175-
locale).withZone(DateTimeZone.UTC);
174+
DateTimeFormatter jodaFormatter = DateTimeFormat.forStyle(style).withLocale(locale).withZone(DateTimeZone.UTC);
176175
String jodaPrinted = jodaFormatter.print(date.getTime());
177176
assertThat("Unable to print style pattern " + style,
178177
formatter.print(date, locale), is(equalTo(jodaPrinted)));
@@ -181,7 +180,7 @@ private void testJodaStylePatterns(String style, Locale locale, Date date)
181180
}
182181

183182
@Test
184-
public void shouldThrowOnUnsupportStylePattern() throws Exception {
183+
public void shouldThrowOnUnsupportedStylePattern() throws Exception {
185184
DateFormatter formatter = new DateFormatter();
186185
formatter.setStylePattern("OO");
187186
thown.expect(IllegalStateException.class);

spring-context/src/test/java/org/springframework/format/datetime/DateFormattingTests.java

Lines changed: 48 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,30 @@ public void tearDown() {
7676

7777

7878
@Test
79-
public void testBindDate() {
79+
public void testBindLong() {
8080
MutablePropertyValues propertyValues = new MutablePropertyValues();
81-
propertyValues.add("date", "10/31/09 12:00 PM");
81+
propertyValues.add("millis", "1256961600");
8282
binder.bind(propertyValues);
8383
assertEquals(0, binder.getBindingResult().getErrorCount());
84-
assertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("date"));
84+
assertEquals("1256961600", binder.getBindingResult().getFieldValue("millis"));
8585
}
8686

8787
@Test
88-
public void testBindDateArray() {
88+
public void testBindLongAnnotated() {
8989
MutablePropertyValues propertyValues = new MutablePropertyValues();
90-
propertyValues.add("date", new String[] {"10/31/09 12:00 PM"});
90+
propertyValues.add("millisAnnotated", "10/31/09");
9191
binder.bind(propertyValues);
9292
assertEquals(0, binder.getBindingResult().getErrorCount());
93+
assertEquals("10/31/09", binder.getBindingResult().getFieldValue("millisAnnotated"));
94+
}
95+
96+
@Test
97+
public void testBindCalendarAnnotated() {
98+
MutablePropertyValues propertyValues = new MutablePropertyValues();
99+
propertyValues.add("calendarAnnotated", "10/31/09");
100+
binder.bind(propertyValues);
101+
assertEquals(0, binder.getBindingResult().getErrorCount());
102+
assertEquals("10/31/09", binder.getBindingResult().getFieldValue("calendarAnnotated"));
93103
}
94104

95105
@Test
@@ -101,6 +111,14 @@ public void testBindDateAnnotated() {
101111
assertEquals("10/31/09", binder.getBindingResult().getFieldValue("dateAnnotated"));
102112
}
103113

114+
@Test
115+
public void testBindDateArray() {
116+
MutablePropertyValues propertyValues = new MutablePropertyValues();
117+
propertyValues.add("dateAnnotated", new String[]{"10/31/09 12:00 PM"});
118+
binder.bind(propertyValues);
119+
assertEquals(0, binder.getBindingResult().getErrorCount());
120+
}
121+
104122
@Test
105123
public void testBindDateAnnotatedWithError() {
106124
MutablePropertyValues propertyValues = new MutablePropertyValues();
@@ -122,39 +140,12 @@ public void testBindDateAnnotatedWithFallbackError() {
122140
}
123141

124142
@Test
125-
public void testBindCalendar() {
126-
MutablePropertyValues propertyValues = new MutablePropertyValues();
127-
propertyValues.add("calendar", "10/31/09 12:00 PM");
128-
binder.bind(propertyValues);
129-
assertEquals(0, binder.getBindingResult().getErrorCount());
130-
assertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("calendar"));
131-
}
132-
133-
@Test
134-
public void testBindCalendarAnnotated() {
135-
MutablePropertyValues propertyValues = new MutablePropertyValues();
136-
propertyValues.add("calendarAnnotated", "10/31/09");
137-
binder.bind(propertyValues);
138-
assertEquals(0, binder.getBindingResult().getErrorCount());
139-
assertEquals("10/31/09", binder.getBindingResult().getFieldValue("calendarAnnotated"));
140-
}
141-
142-
@Test
143-
public void testBindLong() {
144-
MutablePropertyValues propertyValues = new MutablePropertyValues();
145-
propertyValues.add("millis", "1256961600");
146-
binder.bind(propertyValues);
147-
assertEquals(0, binder.getBindingResult().getErrorCount());
148-
assertEquals("1256961600", binder.getBindingResult().getFieldValue("millis"));
149-
}
150-
151-
@Test
152-
public void testBindLongAnnotated() {
143+
public void testBindDateAnnotatedPattern() {
153144
MutablePropertyValues propertyValues = new MutablePropertyValues();
154-
propertyValues.add("millisAnnotated", "10/31/09");
145+
propertyValues.add("dateAnnotatedPattern", "10/31/09 1:05");
155146
binder.bind(propertyValues);
156147
assertEquals(0, binder.getBindingResult().getErrorCount());
157-
assertEquals("10/31/09", binder.getBindingResult().getFieldValue("millisAnnotated"));
148+
assertEquals("10/31/09 1:05", binder.getBindingResult().getFieldValue("dateAnnotatedPattern"));
158149
}
159150

160151
@Test
@@ -239,23 +230,17 @@ public void stringToDateWithGlobalFormat() throws Exception {
239230
@SuppressWarnings("unused")
240231
private static class SimpleDateBean {
241232

242-
@DateTimeFormat
243-
private Date date;
244-
245-
@DateTimeFormat(style="S-")
246-
private Date dateAnnotated;
233+
private Long millis;
247234

248-
@DateTimeFormat
249-
private Calendar calendar;
235+
private Long millisAnnotated;
250236

251237
@DateTimeFormat(style="S-")
252238
private Calendar calendarAnnotated;
253239

254-
private Long millis;
255-
256-
private Long millisAnnotated;
240+
@DateTimeFormat(style="S-")
241+
private Date dateAnnotated;
257242

258-
@DateTimeFormat(pattern="M/d/yy h:mm a")
243+
@DateTimeFormat(pattern="M/d/yy h:mm")
259244
private Date dateAnnotatedPattern;
260245

261246
@DateTimeFormat(iso=ISO.DATE)
@@ -269,28 +254,21 @@ private static class SimpleDateBean {
269254

270255
private final List<SimpleDateBean> children = new ArrayList<SimpleDateBean>();
271256

272-
public Date getDate() {
273-
return date;
274-
}
275-
276-
public void setDate(Date date) {
277-
this.date = date;
278-
}
279-
280-
public Date getDateAnnotated() {
281-
return dateAnnotated;
257+
public Long getMillis() {
258+
return millis;
282259
}
283260

284-
public void setDateAnnotated(Date dateAnnotated) {
285-
this.dateAnnotated = dateAnnotated;
261+
public void setMillis(Long millis) {
262+
this.millis = millis;
286263
}
287264

288-
public Calendar getCalendar() {
289-
return calendar;
265+
@DateTimeFormat(style="S-")
266+
public Long getMillisAnnotated() {
267+
return millisAnnotated;
290268
}
291269

292-
public void setCalendar(Calendar calendar) {
293-
this.calendar = calendar;
270+
public void setMillisAnnotated(@DateTimeFormat(style="S-") Long millisAnnotated) {
271+
this.millisAnnotated = millisAnnotated;
294272
}
295273

296274
public Calendar getCalendarAnnotated() {
@@ -301,21 +279,20 @@ public void setCalendarAnnotated(Calendar calendarAnnotated) {
301279
this.calendarAnnotated = calendarAnnotated;
302280
}
303281

304-
public Long getMillis() {
305-
return millis;
282+
public Date getDateAnnotated() {
283+
return dateAnnotated;
306284
}
307285

308-
public void setMillis(Long millis) {
309-
this.millis = millis;
286+
public void setDateAnnotated(Date dateAnnotated) {
287+
this.dateAnnotated = dateAnnotated;
310288
}
311289

312-
@DateTimeFormat(style="S-")
313-
public Long getMillisAnnotated() {
314-
return millisAnnotated;
290+
public Date getDateAnnotatedPattern() {
291+
return dateAnnotatedPattern;
315292
}
316293

317-
public void setMillisAnnotated(@DateTimeFormat(style="S-") Long millisAnnotated) {
318-
this.millisAnnotated = millisAnnotated;
294+
public void setDateAnnotatedPattern(Date dateAnnotatedPattern) {
295+
this.dateAnnotatedPattern = dateAnnotatedPattern;
319296
}
320297

321298
public Date getIsoDate() {

spring-context/src/test/java/org/springframework/format/datetime/joda/DateTimeFormatterFactoryTests.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -77,7 +77,9 @@ public void createDateTimeFormatterWithFallback() throws Exception {
7777
@Test
7878
public void createDateTimeFormatterInOrderOfPropertyPriority() throws Exception {
7979
factory.setStyle("SS");
80-
assertThat(applyLocale(factory.createDateTimeFormatter()).print(dateTime), is("10/21/09 12:10 PM"));
80+
String value = applyLocale(factory.createDateTimeFormatter()).print(dateTime);
81+
assertTrue(value.startsWith("10/21/09"));
82+
assertTrue(value.endsWith("12:10 PM"));
8183

8284
factory.setIso(ISO.DATE);
8385
assertThat(applyLocale(factory.createDateTimeFormatter()).print(dateTime), is("2009-10-21"));

0 commit comments

Comments
 (0)