Skip to content

Commit 1e4b84c

Browse files
Merge pull request #1687 from Marcono1234/iso-utils-test-fix
Fix ISO8601UtilsTest failing on systems with UTC+X
2 parents 829a514 + 9171715 commit 1e4b84c

File tree

1 file changed

+32
-39
lines changed

1 file changed

+32
-39
lines changed

gson/src/test/java/com/google/gson/internal/bind/util/ISO8601UtilsTest.java

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,25 @@ public class ISO8601UtilsTest {
1515
@Rule
1616
public final ExpectedException exception = ExpectedException.none();
1717

18+
private static TimeZone utcTimeZone() {
19+
return TimeZone.getTimeZone("UTC");
20+
}
21+
22+
private static GregorianCalendar createUtcCalendar() {
23+
TimeZone utc = utcTimeZone();
24+
GregorianCalendar calendar = new GregorianCalendar(utc);
25+
// Calendar was created with current time, must clear it
26+
calendar.clear();
27+
return calendar;
28+
}
29+
1830
@Test
1931
public void testDateFormatString() {
20-
Date date = new GregorianCalendar(2018, Calendar.JUNE, 25).getTime();
32+
GregorianCalendar calendar = new GregorianCalendar(utcTimeZone(), Locale.US);
33+
// Calendar was created with current time, must clear it
34+
calendar.clear();
35+
calendar.set(2018, Calendar.JUNE, 25);
36+
Date date = calendar.getTime();
2137
String dateStr = ISO8601Utils.format(date);
2238
String expectedDate = "2018-06-25";
2339
assertEquals(expectedDate, dateStr.substring(0, expectedDate.length()));
@@ -51,51 +67,28 @@ public void testDateParseWithDefaultTimezone() throws ParseException {
5167

5268
@Test
5369
public void testDateParseWithTimezone() throws ParseException {
54-
TimeZone defaultTimeZone = TimeZone.getDefault();
55-
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
56-
Locale defaultLocale = Locale.getDefault();
57-
Locale.setDefault(Locale.US);
58-
try {
59-
String dateStr = "2018-06-25T00:00:00-03:00";
60-
Date date = ISO8601Utils.parse(dateStr, new ParsePosition(0));
61-
Date expectedDate = new GregorianCalendar(2018, Calendar.JUNE, 25, 3, 0).getTime();
62-
assertEquals(expectedDate, date);
63-
} finally {
64-
TimeZone.setDefault(defaultTimeZone);
65-
Locale.setDefault(defaultLocale);
66-
}
70+
String dateStr = "2018-06-25T00:00:00-03:00";
71+
Date date = ISO8601Utils.parse(dateStr, new ParsePosition(0));
72+
GregorianCalendar calendar = createUtcCalendar();
73+
calendar.set(2018, Calendar.JUNE, 25, 3, 0);
74+
Date expectedDate = calendar.getTime();
75+
assertEquals(expectedDate, date);
6776
}
6877

6978
@Test
7079
public void testDateParseSpecialTimezone() throws ParseException {
71-
TimeZone defaultTimeZone = TimeZone.getDefault();
72-
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
73-
Locale defaultLocale = Locale.getDefault();
74-
Locale.setDefault(Locale.US);
75-
try {
76-
String dateStr = "2018-06-25T00:02:00-02:58";
77-
Date date = ISO8601Utils.parse(dateStr, new ParsePosition(0));
78-
Date expectedDate = new GregorianCalendar(2018, Calendar.JUNE, 25, 3, 0).getTime();
79-
assertEquals(expectedDate, date);
80-
} finally {
81-
TimeZone.setDefault(defaultTimeZone);
82-
Locale.setDefault(defaultLocale);
83-
}
80+
String dateStr = "2018-06-25T00:02:00-02:58";
81+
Date date = ISO8601Utils.parse(dateStr, new ParsePosition(0));
82+
GregorianCalendar calendar = createUtcCalendar();
83+
calendar.set(2018, Calendar.JUNE, 25, 3, 0);
84+
Date expectedDate = calendar.getTime();
85+
assertEquals(expectedDate, date);
8486
}
8587

8688
@Test
8789
public void testDateParseInvalidTime() throws ParseException {
88-
TimeZone defaultTimeZone = TimeZone.getDefault();
89-
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
90-
Locale defaultLocale = Locale.getDefault();
91-
Locale.setDefault(Locale.US);
92-
try {
93-
String dateStr = "2018-06-25T61:60:62-03:00";
94-
exception.expect(ParseException.class);
95-
ISO8601Utils.parse(dateStr, new ParsePosition(0));
96-
} finally {
97-
TimeZone.setDefault(defaultTimeZone);
98-
Locale.setDefault(defaultLocale);
99-
}
90+
String dateStr = "2018-06-25T61:60:62-03:00";
91+
exception.expect(ParseException.class);
92+
ISO8601Utils.parse(dateStr, new ParsePosition(0));
10093
}
10194
}

0 commit comments

Comments
 (0)