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.
This commit is contained in:
Ryan Ernst 2018-10-15 12:18:18 -07:00 committed by GitHub
parent 79c735a04d
commit 26f1d7fc94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 25 additions and 10 deletions

View File

@ -23,6 +23,7 @@ import org.elasticsearch.test.ESTestCase;
import java.time.Instant; import java.time.Instant;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeParseException; import java.time.format.DateTimeParseException;
import java.time.temporal.TemporalAccessor; import java.time.temporal.TemporalAccessor;
import java.util.Locale; import java.util.Locale;
@ -81,11 +82,18 @@ public class DateFormattersTests extends ESTestCase {
assertThat(DateFormatters.forPattern("strict_date_optional_time").getLocale(), is(Locale.ROOT)); assertThat(DateFormatters.forPattern("strict_date_optional_time").getLocale(), is(Locale.ROOT));
Locale locale = randomLocale(random()); Locale locale = randomLocale(random());
assertThat(DateFormatters.forPattern("strict_date_optional_time").withLocale(locale).getLocale(), is(locale)); assertThat(DateFormatters.forPattern("strict_date_optional_time").withLocale(locale).getLocale(), is(locale));
IllegalArgumentException e = if (locale.equals(Locale.ROOT)) {
expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_millis").withLocale(locale)); DateFormatter millisFormatter = DateFormatters.forPattern("epoch_millis");
assertThat(e.getMessage(), is("epoch_millis date formatter can only be in locale ROOT")); assertThat(millisFormatter.withLocale(locale), is(millisFormatter));
e = expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_second").withLocale(locale)); DateFormatter secondFormatter = DateFormatters.forPattern("epoch_second");
assertThat(e.getMessage(), is("epoch_second date formatter can only be in locale ROOT")); assertThat(secondFormatter.withLocale(locale), is(secondFormatter));
} else {
IllegalArgumentException e =
expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_millis").withLocale(locale));
assertThat(e.getMessage(), is("epoch_millis date formatter can only be in locale ROOT"));
e = expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_second").withLocale(locale));
assertThat(e.getMessage(), is("epoch_second date formatter can only be in locale ROOT"));
}
} }
public void testTimeZones() { public void testTimeZones() {
@ -93,11 +101,18 @@ public class DateFormattersTests extends ESTestCase {
assertThat(DateFormatters.forPattern("strict_date_optional_time").getZone(), is(nullValue())); assertThat(DateFormatters.forPattern("strict_date_optional_time").getZone(), is(nullValue()));
ZoneId zoneId = randomZone(); ZoneId zoneId = randomZone();
assertThat(DateFormatters.forPattern("strict_date_optional_time").withZone(zoneId).getZone(), is(zoneId)); assertThat(DateFormatters.forPattern("strict_date_optional_time").withZone(zoneId).getZone(), is(zoneId));
IllegalArgumentException e = if (zoneId.equals(ZoneOffset.UTC)) {
expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_millis").withZone(zoneId)); DateFormatter millisFormatter = DateFormatters.forPattern("epoch_millis");
assertThat(e.getMessage(), is("epoch_millis date formatter can only be in zone offset UTC")); assertThat(millisFormatter.withZone(zoneId), is(millisFormatter));
e = expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_second").withZone(zoneId)); DateFormatter secondFormatter = DateFormatters.forPattern("epoch_second");
assertThat(e.getMessage(), is("epoch_second date formatter can only be in zone offset UTC")); assertThat(secondFormatter.withZone(zoneId), is(secondFormatter));
} else {
IllegalArgumentException e =
expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_millis").withZone(zoneId));
assertThat(e.getMessage(), is("epoch_millis date formatter can only be in zone offset UTC"));
e = expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_second").withZone(zoneId));
assertThat(e.getMessage(), is("epoch_second date formatter can only be in zone offset UTC"));
}
} }
public void testEqualsAndHashcode() { public void testEqualsAndHashcode() {