From b98706183ea9a0b2667ba8305a56a442b2226134 Mon Sep 17 00:00:00 2001 From: David Kyle Date: Thu, 21 Dec 2017 15:48:05 +0000 Subject: [PATCH] Fix Java 9 & 10 test failures comparing ZonedDateTimes Original commit: elastic/x-pack-elasticsearch@2767bb3f167f5a5825ab2ab5ae09bc92fea84692 --- .../xpack/ml/calendars/SpecialEvent.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/calendars/SpecialEvent.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/calendars/SpecialEvent.java index f7da0c1b948..38250dfb454 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/calendars/SpecialEvent.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/calendars/SpecialEvent.java @@ -173,8 +173,18 @@ public class SpecialEvent implements ToXContentObject, Writeable { } SpecialEvent other = (SpecialEvent) obj; - return description.equals(other.description) && startTime.isEqual(other.startTime) - && endTime.isEqual(other.endTime) && calendarId.equals(other.calendarId); + // In Java 8 the tests pass with ZonedDateTime.isEquals() or ZonedDateTime.toInstant.equals() + // but in Java 9 & 10 the same tests fail. + // Both isEquals() and toInstant.equals() work the same; convert to epoch seconds and + // compare seconds and nanos are equal. For some reason the nanos are different in Java 9 & 10. + // It's sufficient to compare just the epoch seconds for the purpose of establishing equality + // which only occurs in testing. + // Note ZonedDataTime.equals() fails because the time zone and date-time must be the same + // which isn't the case in tests where the time zone is randomised. + return description.equals(other.description) + && Objects.equals(startTime.toInstant().getEpochSecond(), other.startTime.toInstant().getEpochSecond()) + && Objects.equals(endTime.toInstant().getEpochSecond(), other.endTime.toInstant().getEpochSecond()) + && calendarId.equals(other.calendarId); } @Override