Skip to content

Commit 2df3040

Browse files
rjernstspinscale
authored andcommitted
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 5cd352e commit 2df3040

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;
@@ -130,23 +131,37 @@ public void testLocales() {
130131
assertThat(DateFormatters.forPattern("strict_date_optional_time").getLocale(), is(Locale.ROOT));
131132
Locale locale = randomLocale(random());
132133
assertThat(DateFormatters.forPattern("strict_date_optional_time").withLocale(locale).getLocale(), is(locale));
133-
IllegalArgumentException e =
134-
expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_millis").withLocale(locale));
135-
assertThat(e.getMessage(), is("epoch_millis date formatter can only be in locale ROOT"));
136-
e = expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_second").withLocale(locale));
137-
assertThat(e.getMessage(), is("epoch_second date formatter can only be in locale ROOT"));
134+
if (locale.equals(Locale.ROOT)) {
135+
DateFormatter millisFormatter = DateFormatters.forPattern("epoch_millis");
136+
assertThat(millisFormatter.withLocale(locale), is(millisFormatter));
137+
DateFormatter secondFormatter = DateFormatters.forPattern("epoch_second");
138+
assertThat(secondFormatter.withLocale(locale), is(secondFormatter));
139+
} else {
140+
IllegalArgumentException e =
141+
expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_millis").withLocale(locale));
142+
assertThat(e.getMessage(), is("epoch_millis date formatter can only be in locale ROOT"));
143+
e = expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_second").withLocale(locale));
144+
assertThat(e.getMessage(), is("epoch_second date formatter can only be in locale ROOT"));
145+
}
138146
}
139147

140148
public void testTimeZones() {
141149
// zone is null by default due to different behaviours between java8 and above
142150
assertThat(DateFormatters.forPattern("strict_date_optional_time").getZone(), is(nullValue()));
143151
ZoneId zoneId = randomZone();
144152
assertThat(DateFormatters.forPattern("strict_date_optional_time").withZone(zoneId).getZone(), is(zoneId));
145-
IllegalArgumentException e =
146-
expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_millis").withZone(zoneId));
147-
assertThat(e.getMessage(), is("epoch_millis date formatter can only be in zone offset UTC"));
148-
e = expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_second").withZone(zoneId));
149-
assertThat(e.getMessage(), is("epoch_second date formatter can only be in zone offset UTC"));
153+
if (zoneId.equals(ZoneOffset.UTC)) {
154+
DateFormatter millisFormatter = DateFormatters.forPattern("epoch_millis");
155+
assertThat(millisFormatter.withZone(zoneId), is(millisFormatter));
156+
DateFormatter secondFormatter = DateFormatters.forPattern("epoch_second");
157+
assertThat(secondFormatter.withZone(zoneId), is(secondFormatter));
158+
} else {
159+
IllegalArgumentException e =
160+
expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_millis").withZone(zoneId));
161+
assertThat(e.getMessage(), is("epoch_millis date formatter can only be in zone offset UTC"));
162+
e = expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_second").withZone(zoneId));
163+
assertThat(e.getMessage(), is("epoch_second date formatter can only be in zone offset UTC"));
164+
}
150165
}
151166

152167
public void testEqualsAndHashcode() {

0 commit comments

Comments
 (0)