Skip to content

Commit 26f1d7f

Browse files
authored
Tests: Handle epoch date formatters edge cases (#34437)
This commit handles cases testing withLocale and withZone when the zone and locale in question is the same as the special base case. This can happen sometimes since the locale and zoneids are randomized.
1 parent 79c735a commit 26f1d7f

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

server/src/test/java/org/elasticsearch/common/time/DateFormattersTests.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import java.time.Instant;
2525
import java.time.ZoneId;
26+
import java.time.ZoneOffset;
2627
import java.time.format.DateTimeParseException;
2728
import java.time.temporal.TemporalAccessor;
2829
import java.util.Locale;
@@ -81,23 +82,37 @@ public void testLocales() {
8182
assertThat(DateFormatters.forPattern("strict_date_optional_time").getLocale(), is(Locale.ROOT));
8283
Locale locale = randomLocale(random());
8384
assertThat(DateFormatters.forPattern("strict_date_optional_time").withLocale(locale).getLocale(), is(locale));
84-
IllegalArgumentException e =
85-
expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_millis").withLocale(locale));
86-
assertThat(e.getMessage(), is("epoch_millis date formatter can only be in locale ROOT"));
87-
e = expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_second").withLocale(locale));
88-
assertThat(e.getMessage(), is("epoch_second date formatter can only be in locale ROOT"));
85+
if (locale.equals(Locale.ROOT)) {
86+
DateFormatter millisFormatter = DateFormatters.forPattern("epoch_millis");
87+
assertThat(millisFormatter.withLocale(locale), is(millisFormatter));
88+
DateFormatter secondFormatter = DateFormatters.forPattern("epoch_second");
89+
assertThat(secondFormatter.withLocale(locale), is(secondFormatter));
90+
} else {
91+
IllegalArgumentException e =
92+
expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_millis").withLocale(locale));
93+
assertThat(e.getMessage(), is("epoch_millis date formatter can only be in locale ROOT"));
94+
e = expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_second").withLocale(locale));
95+
assertThat(e.getMessage(), is("epoch_second date formatter can only be in locale ROOT"));
96+
}
8997
}
9098

9199
public void testTimeZones() {
92100
// zone is null by default due to different behaviours between java8 and above
93101
assertThat(DateFormatters.forPattern("strict_date_optional_time").getZone(), is(nullValue()));
94102
ZoneId zoneId = randomZone();
95103
assertThat(DateFormatters.forPattern("strict_date_optional_time").withZone(zoneId).getZone(), is(zoneId));
96-
IllegalArgumentException e =
97-
expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_millis").withZone(zoneId));
98-
assertThat(e.getMessage(), is("epoch_millis date formatter can only be in zone offset UTC"));
99-
e = expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_second").withZone(zoneId));
100-
assertThat(e.getMessage(), is("epoch_second date formatter can only be in zone offset UTC"));
104+
if (zoneId.equals(ZoneOffset.UTC)) {
105+
DateFormatter millisFormatter = DateFormatters.forPattern("epoch_millis");
106+
assertThat(millisFormatter.withZone(zoneId), is(millisFormatter));
107+
DateFormatter secondFormatter = DateFormatters.forPattern("epoch_second");
108+
assertThat(secondFormatter.withZone(zoneId), is(secondFormatter));
109+
} else {
110+
IllegalArgumentException e =
111+
expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_millis").withZone(zoneId));
112+
assertThat(e.getMessage(), is("epoch_millis date formatter can only be in zone offset UTC"));
113+
e = expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_second").withZone(zoneId));
114+
assertThat(e.getMessage(), is("epoch_second date formatter can only be in zone offset UTC"));
115+
}
101116
}
102117

103118
public void testEqualsAndHashcode() {

0 commit comments

Comments
 (0)