Skip to content

Commit 8181c02

Browse files
committed
Enforce GMT timezone for testBindInstantFromJavaUtilDate
Issue: SPR-16534 (cherry picked from commit 7d89de0)
1 parent d4af8c8 commit 8181c02

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

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

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -32,6 +32,7 @@
3232
import java.util.GregorianCalendar;
3333
import java.util.List;
3434
import java.util.Locale;
35+
import java.util.TimeZone;
3536

3637
import org.junit.After;
3738
import org.junit.Before;
@@ -60,12 +61,12 @@ public class DateTimeFormattingTests {
6061

6162

6263
@Before
63-
public void setUp() {
64+
public void setup() {
6465
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
65-
setUp(registrar);
66+
setup(registrar);
6667
}
6768

68-
private void setUp(DateTimeFormatterRegistrar registrar) {
69+
private void setup(DateTimeFormatterRegistrar registrar) {
6970
conversionService = new FormattingConversionService();
7071
DefaultConversionService.addDefaultConverters(conversionService);
7172
registrar.registerFormatters(conversionService);
@@ -82,7 +83,7 @@ private void setUp(DateTimeFormatterRegistrar registrar) {
8283
}
8384

8485
@After
85-
public void tearDown() {
86+
public void cleanup() {
8687
LocaleContextHolder.setLocale(null);
8788
DateTimeContextHolder.setDateTimeContext(null);
8889
}
@@ -98,10 +99,10 @@ public void testBindLocalDate() {
9899
}
99100

100101
@Test
101-
public void testBindLocalDateWithSpecificStyle() throws Exception {
102+
public void testBindLocalDateWithSpecificStyle() {
102103
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
103104
registrar.setDateStyle(FormatStyle.LONG);
104-
setUp(registrar);
105+
setup(registrar);
105106
MutablePropertyValues propertyValues = new MutablePropertyValues();
106107
propertyValues.add("localDate", "October 31, 2009");
107108
binder.bind(propertyValues);
@@ -110,10 +111,10 @@ public void testBindLocalDateWithSpecificStyle() throws Exception {
110111
}
111112

112113
@Test
113-
public void testBindLocalDateWithSpecificFormatter() throws Exception {
114+
public void testBindLocalDateWithSpecificFormatter() {
114115
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
115116
registrar.setDateFormatter(DateTimeFormatter.ofPattern("yyyyMMdd"));
116-
setUp(registrar);
117+
setup(registrar);
117118
MutablePropertyValues propertyValues = new MutablePropertyValues();
118119
propertyValues.add("localDate", "20091031");
119120
binder.bind(propertyValues);
@@ -177,7 +178,7 @@ public void testBindLocalDateAnnotatedWithDirectFieldAccessAndError() {
177178
}
178179

179180
@Test
180-
public void testBindLocalDateFromJavaUtilCalendar() throws Exception {
181+
public void testBindLocalDateFromJavaUtilCalendar() {
181182
MutablePropertyValues propertyValues = new MutablePropertyValues();
182183
propertyValues.add("localDate", new GregorianCalendar(2009, 9, 31, 0, 0));
183184
binder.bind(propertyValues);
@@ -195,10 +196,10 @@ public void testBindLocalTime() {
195196
}
196197

197198
@Test
198-
public void testBindLocalTimeWithSpecificStyle() throws Exception {
199+
public void testBindLocalTimeWithSpecificStyle() {
199200
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
200201
registrar.setTimeStyle(FormatStyle.MEDIUM);
201-
setUp(registrar);
202+
setup(registrar);
202203
MutablePropertyValues propertyValues = new MutablePropertyValues();
203204
propertyValues.add("localTime", "12:00:00 PM");
204205
binder.bind(propertyValues);
@@ -207,10 +208,10 @@ public void testBindLocalTimeWithSpecificStyle() throws Exception {
207208
}
208209

209210
@Test
210-
public void testBindLocalTimeWithSpecificFormatter() throws Exception {
211+
public void testBindLocalTimeWithSpecificFormatter() {
211212
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
212213
registrar.setTimeFormatter(DateTimeFormatter.ofPattern("HHmmss"));
213-
setUp(registrar);
214+
setup(registrar);
214215
MutablePropertyValues propertyValues = new MutablePropertyValues();
215216
propertyValues.add("localTime", "130000");
216217
binder.bind(propertyValues);
@@ -228,7 +229,7 @@ public void testBindLocalTimeAnnotated() {
228229
}
229230

230231
@Test
231-
public void testBindLocalTimeFromJavaUtilCalendar() throws Exception {
232+
public void testBindLocalTimeFromJavaUtilCalendar() {
232233
MutablePropertyValues propertyValues = new MutablePropertyValues();
233234
propertyValues.add("localTime", new GregorianCalendar(1970, 0, 0, 12, 0));
234235
binder.bind(propertyValues);
@@ -259,7 +260,7 @@ public void testBindLocalDateTimeAnnotated() {
259260
}
260261

261262
@Test
262-
public void testBindLocalDateTimeFromJavaUtilCalendar() throws Exception {
263+
public void testBindLocalDateTimeFromJavaUtilCalendar() {
263264
MutablePropertyValues propertyValues = new MutablePropertyValues();
264265
propertyValues.add("localDateTime", new GregorianCalendar(2009, 9, 31, 12, 0));
265266
binder.bind(propertyValues);
@@ -270,10 +271,10 @@ public void testBindLocalDateTimeFromJavaUtilCalendar() throws Exception {
270271
}
271272

272273
@Test
273-
public void testBindDateTimeWithSpecificStyle() throws Exception {
274+
public void testBindDateTimeWithSpecificStyle() {
274275
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
275276
registrar.setDateTimeStyle(FormatStyle.MEDIUM);
276-
setUp(registrar);
277+
setup(registrar);
277278
MutablePropertyValues propertyValues = new MutablePropertyValues();
278279
propertyValues.add("localDateTime", LocalDateTime.of(2009, 10, 31, 12, 0));
279280
binder.bind(propertyValues);
@@ -356,12 +357,19 @@ public void testBindInstant() {
356357

357358
@Test
358359
@SuppressWarnings("deprecation")
359-
public void testBindInstantFromJavaUtilDate() throws Exception {
360-
MutablePropertyValues propertyValues = new MutablePropertyValues();
361-
propertyValues.add("instant", new Date(109, 9, 31, 12, 0));
362-
binder.bind(propertyValues);
363-
assertEquals(0, binder.getBindingResult().getErrorCount());
364-
assertTrue(binder.getBindingResult().getFieldValue("instant").toString().startsWith("2009-10-31"));
360+
public void testBindInstantFromJavaUtilDate() {
361+
TimeZone defaultZone = TimeZone.getDefault();
362+
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
363+
try {
364+
MutablePropertyValues propertyValues = new MutablePropertyValues();
365+
propertyValues.add("instant", new Date(109, 9, 31, 12, 0));
366+
binder.bind(propertyValues);
367+
assertEquals(0, binder.getBindingResult().getErrorCount());
368+
assertTrue(binder.getBindingResult().getFieldValue("instant").toString().startsWith("2009-10-31"));
369+
}
370+
finally {
371+
TimeZone.setDefault(defaultZone);
372+
}
365373
}
366374

367375
@Test

0 commit comments

Comments
 (0)