Merge pull request #1687 from Marcono1234/iso-utils-test-fix
Fix ISO8601UtilsTest failing on systems with UTC+X
This commit is contained in:
commit
1e4b84c34f
@ -15,9 +15,25 @@ public class ISO8601UtilsTest {
|
|||||||
@Rule
|
@Rule
|
||||||
public final ExpectedException exception = ExpectedException.none();
|
public final ExpectedException exception = ExpectedException.none();
|
||||||
|
|
||||||
|
private static TimeZone utcTimeZone() {
|
||||||
|
return TimeZone.getTimeZone("UTC");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static GregorianCalendar createUtcCalendar() {
|
||||||
|
TimeZone utc = utcTimeZone();
|
||||||
|
GregorianCalendar calendar = new GregorianCalendar(utc);
|
||||||
|
// Calendar was created with current time, must clear it
|
||||||
|
calendar.clear();
|
||||||
|
return calendar;
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDateFormatString() {
|
public void testDateFormatString() {
|
||||||
Date date = new GregorianCalendar(2018, Calendar.JUNE, 25).getTime();
|
GregorianCalendar calendar = new GregorianCalendar(utcTimeZone(), Locale.US);
|
||||||
|
// Calendar was created with current time, must clear it
|
||||||
|
calendar.clear();
|
||||||
|
calendar.set(2018, Calendar.JUNE, 25);
|
||||||
|
Date date = calendar.getTime();
|
||||||
String dateStr = ISO8601Utils.format(date);
|
String dateStr = ISO8601Utils.format(date);
|
||||||
String expectedDate = "2018-06-25";
|
String expectedDate = "2018-06-25";
|
||||||
assertEquals(expectedDate, dateStr.substring(0, expectedDate.length()));
|
assertEquals(expectedDate, dateStr.substring(0, expectedDate.length()));
|
||||||
@ -51,51 +67,28 @@ public class ISO8601UtilsTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDateParseWithTimezone() throws ParseException {
|
public void testDateParseWithTimezone() throws ParseException {
|
||||||
TimeZone defaultTimeZone = TimeZone.getDefault();
|
String dateStr = "2018-06-25T00:00:00-03:00";
|
||||||
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
|
Date date = ISO8601Utils.parse(dateStr, new ParsePosition(0));
|
||||||
Locale defaultLocale = Locale.getDefault();
|
GregorianCalendar calendar = createUtcCalendar();
|
||||||
Locale.setDefault(Locale.US);
|
calendar.set(2018, Calendar.JUNE, 25, 3, 0);
|
||||||
try {
|
Date expectedDate = calendar.getTime();
|
||||||
String dateStr = "2018-06-25T00:00:00-03:00";
|
assertEquals(expectedDate, date);
|
||||||
Date date = ISO8601Utils.parse(dateStr, new ParsePosition(0));
|
|
||||||
Date expectedDate = new GregorianCalendar(2018, Calendar.JUNE, 25, 3, 0).getTime();
|
|
||||||
assertEquals(expectedDate, date);
|
|
||||||
} finally {
|
|
||||||
TimeZone.setDefault(defaultTimeZone);
|
|
||||||
Locale.setDefault(defaultLocale);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDateParseSpecialTimezone() throws ParseException {
|
public void testDateParseSpecialTimezone() throws ParseException {
|
||||||
TimeZone defaultTimeZone = TimeZone.getDefault();
|
String dateStr = "2018-06-25T00:02:00-02:58";
|
||||||
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
|
Date date = ISO8601Utils.parse(dateStr, new ParsePosition(0));
|
||||||
Locale defaultLocale = Locale.getDefault();
|
GregorianCalendar calendar = createUtcCalendar();
|
||||||
Locale.setDefault(Locale.US);
|
calendar.set(2018, Calendar.JUNE, 25, 3, 0);
|
||||||
try {
|
Date expectedDate = calendar.getTime();
|
||||||
String dateStr = "2018-06-25T00:02:00-02:58";
|
assertEquals(expectedDate, date);
|
||||||
Date date = ISO8601Utils.parse(dateStr, new ParsePosition(0));
|
|
||||||
Date expectedDate = new GregorianCalendar(2018, Calendar.JUNE, 25, 3, 0).getTime();
|
|
||||||
assertEquals(expectedDate, date);
|
|
||||||
} finally {
|
|
||||||
TimeZone.setDefault(defaultTimeZone);
|
|
||||||
Locale.setDefault(defaultLocale);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDateParseInvalidTime() throws ParseException {
|
public void testDateParseInvalidTime() throws ParseException {
|
||||||
TimeZone defaultTimeZone = TimeZone.getDefault();
|
String dateStr = "2018-06-25T61:60:62-03:00";
|
||||||
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
|
exception.expect(ParseException.class);
|
||||||
Locale defaultLocale = Locale.getDefault();
|
ISO8601Utils.parse(dateStr, new ParsePosition(0));
|
||||||
Locale.setDefault(Locale.US);
|
|
||||||
try {
|
|
||||||
String dateStr = "2018-06-25T61:60:62-03:00";
|
|
||||||
exception.expect(ParseException.class);
|
|
||||||
ISO8601Utils.parse(dateStr, new ParsePosition(0));
|
|
||||||
} finally {
|
|
||||||
TimeZone.setDefault(defaultTimeZone);
|
|
||||||
Locale.setDefault(defaultLocale);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user