Skip to content

Commit 72c89e5

Browse files
Banuelorignijoel-costigliola
authored andcommitted
Fix timestamp/instant isEqualTo issue
Fix assertj#3359 (cherry picked from commit adc5811)
1 parent 17f4aa6 commit 72c89e5

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

assertj-core/src/main/java/org/assertj/core/internal/StandardComparisonStrategy.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import static org.assertj.core.util.Preconditions.checkArgument;
1616

1717
import java.util.Collection;
18+
import java.util.Date;
1819
import java.util.Iterator;
1920
import java.util.Set;
2021
import java.util.TreeSet;
@@ -105,6 +106,9 @@ public boolean areEqual(Object actual, Object other) {
105106
return java.util.Arrays.deepEquals((Object[]) actual, (Object[]) other);
106107
}
107108
}
109+
110+
// use compareTo over equals as it deals correctly with java.sql.Timestamp
111+
if (actual instanceof Date && other instanceof Date) return ((Date) actual).compareTo((Date) other) == 0;
108112
return actual.equals(other);
109113
}
110114

assertj-core/src/test/java/org/assertj/core/api/instant/InstantAssert_isEqualTo_Test.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414

1515
import static org.assertj.core.api.Assertions.assertThat;
1616
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
17+
import static org.assertj.core.api.BDDAssertions.then;
1718
import static org.assertj.core.test.ErrorMessagesForTest.shouldBeEqualMessage;
1819
import static org.assertj.core.util.AssertionsUtil.assertThatAssertionErrorIsThrownBy;
1920

21+
import java.sql.Timestamp;
2022
import java.time.Instant;
2123

2224
import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
@@ -48,4 +50,16 @@ void should_fail_if_date_as_string_parameter_is_null() {
4850
.withMessage("The String representing the Instant to compare actual with should not be null");
4951
}
5052

53+
// https://github.com/assertj/assertj/issues/3359
54+
@Test
55+
void should_cover_issue_3359() {
56+
// GIVEN
57+
final long now = System.currentTimeMillis();
58+
final Timestamp timestamp = new Timestamp(now);
59+
final Instant instant = Instant.ofEpochMilli(now);
60+
// WHEN/THEN
61+
then(timestamp).isEqualTo(instant);
62+
then(timestamp).isAfterOrEqualTo(instant);
63+
}
64+
5165
}

assertj-core/src/test/java/org/assertj/core/internal/StandardComparisonStrategy_areEqual_Test.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
import static org.assertj.core.api.BDDAssertions.then;
1717
import static org.junit.jupiter.params.provider.Arguments.arguments;
1818

19+
import java.sql.Timestamp;
20+
import java.time.Instant;
21+
import java.util.Date;
1922
import java.util.stream.Stream;
2023

2124
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) {
255258
then(result).isFalse();
256259
}
257260

261+
@Test
262+
void should_return_true_if_date_and_timestamp_are_equal() {
263+
// GIVEN
264+
Instant now = Instant.now();
265+
Date actual = Date.from(now);
266+
Timestamp other = Timestamp.from(now);
267+
// WHEN
268+
boolean result = underTest.areEqual(actual, other);
269+
// THEN
270+
then(result).isTrue();
271+
}
272+
273+
@Test
274+
void should_return_false_if_dates_are_not_equal() {
275+
// GIVEN
276+
Date actual = Date.from(Instant.parse("2024-03-30T00:00:00.00Z"));
277+
Timestamp other = Timestamp.from(Instant.parse("2024-03-30T00:00:00.01Z"));
278+
// WHEN
279+
boolean result = underTest.areEqual(actual, other);
280+
// THEN
281+
then(result).isFalse();
282+
}
283+
258284
private static Stream<Object> arrays() {
259285
return Stream.of(argument(new Object[] { "Luke", "Yoda", "Leia" }),
260286
new byte[] { 1, 2, 3 },

0 commit comments

Comments
 (0)