Skip to content

Commit

Permalink
Fix timestamp/instant isEqualTo issue
Browse files Browse the repository at this point in the history
Fix #3359
  • Loading branch information
Banuelorigni authored and joel-costigliola committed May 6, 2024
1 parent 714c136 commit adc5811
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

}
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Object> arrays() {
return Stream.of(argument(new Object[] { "Luke", "Yoda", "Leia" }),
new byte[] { 1, 2, 3 },
Expand Down

0 comments on commit adc5811

Please sign in to comment.