diff --git a/spring-context/src/test/java/org/springframework/format/datetime/standard/DateTimeFormattingTests.java b/spring-context/src/test/java/org/springframework/format/datetime/standard/DateTimeFormattingTests.java index 75e6515a69c..fe2d60ca6be 100644 --- a/spring-context/src/test/java/org/springframework/format/datetime/standard/DateTimeFormattingTests.java +++ b/spring-context/src/test/java/org/springframework/format/datetime/standard/DateTimeFormattingTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -32,6 +32,7 @@ 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; @@ -60,12 +61,12 @@ public class DateTimeFormattingTests { @Before - public void setUp() { + public void setup() { DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar(); - setUp(registrar); + setup(registrar); } - private void setUp(DateTimeFormatterRegistrar registrar) { + private void setup(DateTimeFormatterRegistrar registrar) { conversionService = new FormattingConversionService(); DefaultConversionService.addDefaultConverters(conversionService); registrar.registerFormatters(conversionService); @@ -82,7 +83,7 @@ public class DateTimeFormattingTests { } @After - public void tearDown() { + public void cleanup() { LocaleContextHolder.setLocale(null); DateTimeContextHolder.setDateTimeContext(null); } @@ -98,10 +99,10 @@ public class DateTimeFormattingTests { } @Test - public void testBindLocalDateWithSpecificStyle() throws Exception { + public void testBindLocalDateWithSpecificStyle() { DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar(); registrar.setDateStyle(FormatStyle.LONG); - setUp(registrar); + setup(registrar); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("localDate", "October 31, 2009"); binder.bind(propertyValues); @@ -110,10 +111,10 @@ public class DateTimeFormattingTests { } @Test - public void testBindLocalDateWithSpecificFormatter() throws Exception { + public void testBindLocalDateWithSpecificFormatter() { DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar(); registrar.setDateFormatter(DateTimeFormatter.ofPattern("yyyyMMdd")); - setUp(registrar); + setup(registrar); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("localDate", "20091031"); binder.bind(propertyValues); @@ -177,7 +178,7 @@ public class DateTimeFormattingTests { } @Test - public void testBindLocalDateFromJavaUtilCalendar() throws Exception { + public void testBindLocalDateFromJavaUtilCalendar() { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("localDate", new GregorianCalendar(2009, 9, 31, 0, 0)); binder.bind(propertyValues); @@ -195,10 +196,10 @@ public class DateTimeFormattingTests { } @Test - public void testBindLocalTimeWithSpecificStyle() throws Exception { + public void testBindLocalTimeWithSpecificStyle() { DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar(); registrar.setTimeStyle(FormatStyle.MEDIUM); - setUp(registrar); + setup(registrar); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("localTime", "12:00:00 PM"); binder.bind(propertyValues); @@ -207,10 +208,10 @@ public class DateTimeFormattingTests { } @Test - public void testBindLocalTimeWithSpecificFormatter() throws Exception { + public void testBindLocalTimeWithSpecificFormatter() { DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar(); registrar.setTimeFormatter(DateTimeFormatter.ofPattern("HHmmss")); - setUp(registrar); + setup(registrar); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("localTime", "130000"); binder.bind(propertyValues); @@ -228,7 +229,7 @@ public class DateTimeFormattingTests { } @Test - public void testBindLocalTimeFromJavaUtilCalendar() throws Exception { + public void testBindLocalTimeFromJavaUtilCalendar() { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("localTime", new GregorianCalendar(1970, 0, 0, 12, 0)); binder.bind(propertyValues); @@ -259,7 +260,7 @@ public class DateTimeFormattingTests { } @Test - public void testBindLocalDateTimeFromJavaUtilCalendar() throws Exception { + public void testBindLocalDateTimeFromJavaUtilCalendar() { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("localDateTime", new GregorianCalendar(2009, 9, 31, 12, 0)); binder.bind(propertyValues); @@ -270,10 +271,10 @@ public class DateTimeFormattingTests { } @Test - public void testBindDateTimeWithSpecificStyle() throws Exception { + public void testBindDateTimeWithSpecificStyle() { DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar(); registrar.setDateTimeStyle(FormatStyle.MEDIUM); - setUp(registrar); + setup(registrar); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("localDateTime", LocalDateTime.of(2009, 10, 31, 12, 0)); binder.bind(propertyValues); @@ -356,12 +357,19 @@ public class DateTimeFormattingTests { @Test @SuppressWarnings("deprecation") - public void testBindInstantFromJavaUtilDate() throws Exception { - MutablePropertyValues propertyValues = new MutablePropertyValues(); - 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-31")); + public void testBindInstantFromJavaUtilDate() { + TimeZone defaultZone = TimeZone.getDefault(); + TimeZone.setDefault(TimeZone.getTimeZone("GMT")); + try { + MutablePropertyValues propertyValues = new MutablePropertyValues(); + 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-31")); + } + finally { + TimeZone.setDefault(defaultZone); + } } @Test