Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix date type compare issue #3410

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
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,10 @@ public boolean areEqual(Object actual, Object other) {
return java.util.Arrays.deepEquals((Object[]) actual, (Object[]) other);
}
}

if (actual instanceof Date && other instanceof Date) {
return ((Date) actual).compareTo((Date) other) == 0;
}
return actual.equals(other);
}

Expand Down
Original file line number Diff line number Diff line change
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_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_date_are_not_equal() {
// GIVEN
Date actual = Date.from(Instant.parse("2024-03-30T00:00:00.00Z"));
Timestamp other = Timestamp.from(Instant.parse("2024-04-01T00:00:00.00Z"));
// 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