From adc58110b477723dc226e659634abf4ce26ca114 Mon Sep 17 00:00:00 2001 From: "kexin.an" Date: Sat, 30 Mar 2024 19:17:45 +0800 Subject: [PATCH] Fix timestamp/instant isEqualTo issue Fix #3359 --- .../internal/StandardComparisonStrategy.java | 4 +++ .../instant/InstantAssert_isEqualTo_Test.java | 14 ++++++++++ ...ndardComparisonStrategy_areEqual_Test.java | 26 +++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/assertj-core/src/main/java/org/assertj/core/internal/StandardComparisonStrategy.java b/assertj-core/src/main/java/org/assertj/core/internal/StandardComparisonStrategy.java index 84f29f11e0..f78a07dcf4 100644 --- a/assertj-core/src/main/java/org/assertj/core/internal/StandardComparisonStrategy.java +++ b/assertj-core/src/main/java/org/assertj/core/internal/StandardComparisonStrategy.java @@ -15,6 +15,7 @@ import static org.assertj.core.util.Preconditions.checkArgument; import java.util.Collection; +import java.util.Date; import java.util.Iterator; import java.util.Set; import java.util.TreeSet; @@ -105,6 +106,9 @@ public boolean areEqual(Object actual, Object other) { return java.util.Arrays.deepEquals((Object[]) actual, (Object[]) other); } } + + // use compareTo over equals as it deals correctly with java.sql.Timestamp + if (actual instanceof Date && other instanceof Date) return ((Date) actual).compareTo((Date) other) == 0; return actual.equals(other); } diff --git a/assertj-core/src/test/java/org/assertj/core/api/instant/InstantAssert_isEqualTo_Test.java b/assertj-core/src/test/java/org/assertj/core/api/instant/InstantAssert_isEqualTo_Test.java index 342351f28d..143b207426 100644 --- a/assertj-core/src/test/java/org/assertj/core/api/instant/InstantAssert_isEqualTo_Test.java +++ b/assertj-core/src/test/java/org/assertj/core/api/instant/InstantAssert_isEqualTo_Test.java @@ -14,9 +14,11 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.assertj.core.api.BDDAssertions.then; import static org.assertj.core.test.ErrorMessagesForTest.shouldBeEqualMessage; import static org.assertj.core.util.AssertionsUtil.assertThatAssertionErrorIsThrownBy; +import java.sql.Timestamp; import java.time.Instant; import org.assertj.core.api.ThrowableAssert.ThrowingCallable; @@ -48,4 +50,16 @@ void should_fail_if_date_as_string_parameter_is_null() { .withMessage("The String representing the Instant to compare actual with should not be null"); } + // https://github.com/assertj/assertj/issues/3359 + @Test + void should_cover_issue_3359() { + // GIVEN + final long now = System.currentTimeMillis(); + final Timestamp timestamp = new Timestamp(now); + final Instant instant = Instant.ofEpochMilli(now); + // WHEN/THEN + then(timestamp).isEqualTo(instant); + then(timestamp).isAfterOrEqualTo(instant); + } + } diff --git a/assertj-core/src/test/java/org/assertj/core/internal/StandardComparisonStrategy_areEqual_Test.java b/assertj-core/src/test/java/org/assertj/core/internal/StandardComparisonStrategy_areEqual_Test.java index 1c9fa170a1..452ceb5428 100644 --- a/assertj-core/src/test/java/org/assertj/core/internal/StandardComparisonStrategy_areEqual_Test.java +++ b/assertj-core/src/test/java/org/assertj/core/internal/StandardComparisonStrategy_areEqual_Test.java @@ -16,6 +16,9 @@ import static org.assertj.core.api.BDDAssertions.then; import static org.junit.jupiter.params.provider.Arguments.arguments; +import java.sql.Timestamp; +import java.time.Instant; +import java.util.Date; import java.util.stream.Stream; import org.junit.jupiter.api.Test; @@ -255,6 +258,29 @@ void should_return_false_if_array_is_non_null_and_other_is_null(Object actual) { then(result).isFalse(); } + @Test + void should_return_true_if_date_and_timestamp_are_equal() { + // GIVEN + Instant now = Instant.now(); + Date actual = Date.from(now); + Timestamp other = Timestamp.from(now); + // WHEN + boolean result = underTest.areEqual(actual, other); + // THEN + then(result).isTrue(); + } + + @Test + void should_return_false_if_dates_are_not_equal() { + // GIVEN + Date actual = Date.from(Instant.parse("2024-03-30T00:00:00.00Z")); + Timestamp other = Timestamp.from(Instant.parse("2024-03-30T00:00:00.01Z")); + // WHEN + boolean result = underTest.areEqual(actual, other); + // THEN + then(result).isFalse(); + } + private static Stream arrays() { return Stream.of(argument(new Object[] { "Luke", "Yoda", "Leia" }), new byte[] { 1, 2, 3 },