Fix DateUtilsTest to work reliably on Java 9. This closes #304.

This commit is contained in:
Stephen Colebourne 2017-11-04 08:41:28 -06:00 committed by Gary Gregory
parent 63f6be088d
commit fc409b5791
1 changed files with 212 additions and 197 deletions

View File

@ -104,6 +104,7 @@ public class DateUtilsTest {
date2 = dateTimeParser.parse("November 18, 2001 1:23:11.321");
defaultZone = TimeZone.getDefault();
zone = TimeZone.getTimeZone("MET");
try {
TimeZone.setDefault(zone);
dateTimeParser.setTimeZone(zone);
date3 = dateTimeParser.parse("March 30, 2003 05:30:45.000");
@ -112,8 +113,10 @@ public class DateUtilsTest {
date6 = dateTimeParser.parse("March 30, 2003 02:10:00.000");
date7 = dateTimeParser.parse("March 30, 2003 02:40:00.000");
date8 = dateTimeParser.parse("October 26, 2003 05:30:45.000");
} finally {
dateTimeParser.setTimeZone(defaultZone);
TimeZone.setDefault(defaultZone);
}
calAmPm1 = Calendar.getInstance();
calAmPm1.setTime(dateAmPm1);
calAmPm2 = Calendar.getInstance();
@ -126,6 +129,7 @@ public class DateUtilsTest {
cal1.setTime(date1);
cal2 = Calendar.getInstance();
cal2.setTime(date2);
try {
TimeZone.setDefault(zone);
cal3 = Calendar.getInstance();
cal3.setTime(date3);
@ -139,8 +143,10 @@ public class DateUtilsTest {
cal7.setTime(date7);
cal8 = Calendar.getInstance();
cal8.setTime(date8);
} finally {
TimeZone.setDefault(defaultZone);
}
}
//-----------------------------------------------------------------------
@Test
@ -885,6 +891,7 @@ public class DateUtilsTest {
// Fix for http://issues.apache.org/bugzilla/show_bug.cgi?id=25560 / LANG-13
// Test rounding across the beginning of daylight saving time
try {
TimeZone.setDefault(zone);
dateTimeParser.setTimeZone(zone);
assertEquals("round MET date across DST change-over",
@ -936,9 +943,11 @@ public class DateUtilsTest {
assertEquals("round MET date across DST change-over",
dateTimeParser.parse("March 30, 2003 04:00:00.000"),
DateUtils.round((Object) cal7, Calendar.HOUR_OF_DAY));
} finally {
TimeZone.setDefault(defaultZone);
dateTimeParser.setTimeZone(defaultZone);
}
}
/**
* Tests the Changes Made by LANG-346 to the DateUtils.modify() private method invoked
@ -948,8 +957,6 @@ public class DateUtilsTest {
*/
@Test
public void testRoundLang346() throws Exception {
TimeZone.setDefault(defaultZone);
dateTimeParser.setTimeZone(defaultZone);
final Calendar testCalendar = Calendar.getInstance();
testCalendar.set(2007, Calendar.JULY, 2, 8, 8, 50);
Date date = testCalendar.getTime();
@ -1163,6 +1170,7 @@ public class DateUtilsTest {
// Fix for http://issues.apache.org/bugzilla/show_bug.cgi?id=25560
// Test truncate across beginning of daylight saving time
try {
TimeZone.setDefault(zone);
dateTimeParser.setTimeZone(zone);
assertEquals("truncate MET date across DST change-over",
@ -1178,8 +1186,10 @@ public class DateUtilsTest {
assertEquals("truncate MET date across DST change-over",
dateTimeParser.parse("October 26, 2003 00:00:00.000"),
DateUtils.truncate((Object) cal8, Calendar.DATE));
} finally {
TimeZone.setDefault(defaultZone);
dateTimeParser.setTimeZone(defaultZone);
}
// Bug 31395, large dates
final Date endOfTime = new Date(Long.MAX_VALUE); // fyi: Sun Aug 17 07:12:55 CET 292278994 -- 807 millis
@ -1208,11 +1218,12 @@ public class DateUtilsTest {
*/
@Test
public void testTruncateLang59() throws Exception {
try {
// Set TimeZone to Mountain Time
final TimeZone MST_MDT = TimeZone.getTimeZone("MST7MDT");
TimeZone.setDefault(MST_MDT);
final DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z");
format.setTimeZone(MST_MDT);
final TimeZone denverZone = TimeZone.getTimeZone("America/Denver");
TimeZone.setDefault(denverZone);
final DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS XXX");
format.setTimeZone(denverZone);
final Date oct31_01MDT = new Date(1099206000000L);
@ -1221,11 +1232,11 @@ public class DateUtilsTest {
final Date oct31_01_02_03MDT = new Date(oct31_01_02MDT.getTime() + 3000L); // + 3 seconds
final Date oct31_01_02_03_04MDT = new Date(oct31_01_02_03MDT.getTime() + 4L); // + 4 milliseconds
assertEquals("Check 00:00:00.000", "2004-10-31 00:00:00.000 MDT", format.format(oct31MDT));
assertEquals("Check 01:00:00.000", "2004-10-31 01:00:00.000 MDT", format.format(oct31_01MDT));
assertEquals("Check 01:02:00.000", "2004-10-31 01:02:00.000 MDT", format.format(oct31_01_02MDT));
assertEquals("Check 01:02:03.000", "2004-10-31 01:02:03.000 MDT", format.format(oct31_01_02_03MDT));
assertEquals("Check 01:02:03.004", "2004-10-31 01:02:03.004 MDT", format.format(oct31_01_02_03_04MDT));
assertEquals("Check 00:00:00.000", "2004-10-31 00:00:00.000 -06:00", format.format(oct31MDT));
assertEquals("Check 01:00:00.000", "2004-10-31 01:00:00.000 -06:00", format.format(oct31_01MDT));
assertEquals("Check 01:02:00.000", "2004-10-31 01:02:00.000 -06:00", format.format(oct31_01_02MDT));
assertEquals("Check 01:02:03.000", "2004-10-31 01:02:03.000 -06:00", format.format(oct31_01_02_03MDT));
assertEquals("Check 01:02:03.004", "2004-10-31 01:02:03.004 -06:00", format.format(oct31_01_02_03_04MDT));
// ------- Demonstrate Problem -------
final Calendar gval = Calendar.getInstance();
@ -1252,7 +1263,6 @@ public class DateUtilsTest {
assertEquals("Truncate Calendar.DATE",
oct31MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.DATE));
// ---------- Test Round (down) ----------
assertEquals("Round Calendar.MILLISECOND",
oct31_01_02_03_04MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.MILLISECOND));
@ -1271,10 +1281,11 @@ public class DateUtilsTest {
assertEquals("Round Calendar.DATE",
oct31MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.DATE));
} finally {
// restore default time zone
TimeZone.setDefault(defaultZone);
}
}
// http://issues.apache.org/jira/browse/LANG-530
@SuppressWarnings("deprecation")
@ -1462,6 +1473,7 @@ public class DateUtilsTest {
// Fix for http://issues.apache.org/bugzilla/show_bug.cgi?id=25560
// Test ceiling across the beginning of daylight saving time
try {
TimeZone.setDefault(zone);
dateTimeParser.setTimeZone(zone);
@ -1514,8 +1526,11 @@ public class DateUtilsTest {
assertEquals("ceiling MET date across DST change-over",
dateTimeParser.parse("March 30, 2003 04:00:00.000"),
DateUtils.ceiling((Object) cal7, Calendar.HOUR_OF_DAY));
} finally {
TimeZone.setDefault(defaultZone);
dateTimeParser.setTimeZone(defaultZone);
}
// Bug 31395, large dates
final Date endOfTime = new Date(Long.MAX_VALUE); // fyi: Sun Aug 17 07:12:55 CET 292278994 -- 807 millis