Skip to content

Commit

Permalink
Fix Instant and String conversion with Date assertions
Browse files Browse the repository at this point in the history
This reverts the changes introduced with #3410.
  • Loading branch information
scordio committed May 9, 2024
1 parent 9b2c8f6 commit 755e614
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import static org.assertj.core.util.Lists.list;
import static org.assertj.core.util.Lists.newArrayList;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -161,7 +162,7 @@ public SELF isEqualTo(String dateAsString) {
* @throws AssertionError if actual {@code Date} and given {@link Instant} are not equal (after converting instant to a Date).
*/
public SELF isEqualTo(Instant instant) {
return isEqualTo(Date.from(instant));
return isEqualTo(dateFrom(instant));
}

/**
Expand Down Expand Up @@ -232,7 +233,7 @@ public SELF isEqualToIgnoringHours(String dateAsString) {
*/
@Deprecated
public SELF isEqualToIgnoringHours(Instant instant) {
return isEqualToIgnoringHours(Date.from(instant));
return isEqualToIgnoringHours(dateFrom(instant));
}

/**
Expand Down Expand Up @@ -332,7 +333,7 @@ public SELF isEqualToIgnoringMinutes(String dateAsString) {
*/
@Deprecated
public SELF isEqualToIgnoringMinutes(Instant instant) {
return isEqualToIgnoringMinutes(Date.from(instant));
return isEqualToIgnoringMinutes(dateFrom(instant));
}

/**
Expand Down Expand Up @@ -435,7 +436,7 @@ public SELF isEqualToIgnoringSeconds(String dateAsString) {
*/
@Deprecated
public SELF isEqualToIgnoringSeconds(Instant instant) {
return isEqualToIgnoringSeconds(Date.from(instant));
return isEqualToIgnoringSeconds(dateFrom(instant));
}

/**
Expand Down Expand Up @@ -536,7 +537,7 @@ public SELF isEqualToIgnoringMillis(String dateAsString) {
*/
@Deprecated
public SELF isEqualToIgnoringMillis(Instant instant) {
return isEqualToIgnoringMillis(Date.from(instant));
return isEqualToIgnoringMillis(dateFrom(instant));
}

/**
Expand Down Expand Up @@ -631,7 +632,7 @@ public SELF isNotEqualTo(String dateAsString) {
* @since 3.19.0
*/
public SELF isNotEqualTo(Instant instant) {
return isNotEqualTo(Date.from(instant));
return isNotEqualTo(dateFrom(instant));
}

/**
Expand Down Expand Up @@ -912,7 +913,7 @@ public SELF isBefore(Date other) {
* @since 3.19.0
*/
public SELF isBefore(Instant other) {
dates.assertIsBefore(info, actual, Date.from(other));
dates.assertIsBefore(info, actual, dateFrom(other));
return myself;
}

Expand Down Expand Up @@ -1037,7 +1038,7 @@ public SELF isBeforeOrEqualTo(Date other) {
* @since 3.19.0
*/
public SELF isBeforeOrEqualTo(Instant other) {
dates.assertIsBeforeOrEqualTo(info, actual, Date.from(other));
dates.assertIsBeforeOrEqualTo(info, actual, dateFrom(other));
return myself;
}

Expand Down Expand Up @@ -1189,7 +1190,7 @@ public SELF isAfter(Date other) {
* @since 3.19.0
*/
public SELF isAfter(Instant other) {
dates.assertIsAfter(info, actual, Date.from(other));
dates.assertIsAfter(info, actual, dateFrom(other));
return myself;
}

Expand Down Expand Up @@ -1313,7 +1314,7 @@ public SELF isAfterOrEqualTo(Date other) {
* @since 3.19.0
*/
public SELF isAfterOrEqualTo(Instant other) {
dates.assertIsAfterOrEqualTo(info, actual, Date.from(other));
dates.assertIsAfterOrEqualTo(info, actual, dateFrom(other));
return myself;
}

Expand Down Expand Up @@ -1514,7 +1515,7 @@ public SELF isBetween(String start, String end) {
* @since 3.19.0
*/
public SELF isBetween(Instant start, Instant end) {
return isBetween(Date.from(start), Date.from(end));
return isBetween(dateFrom(start), dateFrom(end));
}

/**
Expand Down Expand Up @@ -1625,7 +1626,7 @@ public SELF isBetween(String start, String end, boolean inclusiveStart, boolean
* @since 3.19.0
*/
public SELF isBetween(Instant start, Instant end, boolean inclusiveStart, boolean inclusiveEnd) {
dates.assertIsBetween(info, actual, Date.from(start), Date.from(end), inclusiveStart, inclusiveEnd);
dates.assertIsBetween(info, actual, dateFrom(start), dateFrom(end), inclusiveStart, inclusiveEnd);
return myself;
}

Expand Down Expand Up @@ -1688,7 +1689,7 @@ public SELF isNotBetween(Date start, Date end, boolean inclusiveStart, boolean i
* @since 3.19.0
*/
public SELF isNotBetween(Instant start, Instant end, boolean inclusiveStart, boolean inclusiveEnd) {
dates.assertIsNotBetween(info, actual, Date.from(start), Date.from(end), inclusiveStart, inclusiveEnd);
dates.assertIsNotBetween(info, actual, dateFrom(start), dateFrom(end), inclusiveStart, inclusiveEnd);
return myself;
}

Expand Down Expand Up @@ -1798,7 +1799,7 @@ public SELF isNotBetween(Date start, Date end) {
* @since 3.19.0
*/
public SELF isNotBetween(Instant start, Instant end) {
return isNotBetween(Date.from(start), Date.from(end), true, false);
return isNotBetween(dateFrom(start), dateFrom(end), true, false);
}

/**
Expand Down Expand Up @@ -2253,7 +2254,7 @@ public SELF isInSameYearAs(Date other) {
* @since 3.19.0
*/
public SELF isInSameYearAs(Instant other) {
dates.assertIsInSameYearAs(info, actual, Date.from(other));
dates.assertIsInSameYearAs(info, actual, dateFrom(other));
return myself;
}

Expand Down Expand Up @@ -2344,7 +2345,7 @@ public SELF isInSameMonthAs(Date other) {
* @since 3.19.0
*/
public SELF isInSameMonthAs(Instant other) {
dates.assertIsInSameMonthAs(info, actual, Date.from(other));
dates.assertIsInSameMonthAs(info, actual, dateFrom(other));
return myself;
}

Expand Down Expand Up @@ -2438,7 +2439,7 @@ public SELF isInSameDayAs(Date other) {
* @since 3.19.0
*/
public SELF isInSameDayAs(Instant other) {
dates.assertIsInSameDayAs(info, actual, Date.from(other));
dates.assertIsInSameDayAs(info, actual, dateFrom(other));
return myself;
}

Expand Down Expand Up @@ -2555,7 +2556,7 @@ public SELF isInSameHourWindowAs(Date other) {
* @since 3.19.0
*/
public SELF isInSameHourWindowAs(Instant other) {
dates.assertIsInSameHourWindowAs(info, actual, Date.from(other));
dates.assertIsInSameHourWindowAs(info, actual, dateFrom(other));
return myself;
}

Expand Down Expand Up @@ -2755,7 +2756,7 @@ public SELF isInSameMinuteWindowAs(Date other) {
* @since 3.19.0
*/
public SELF isInSameMinuteWindowAs(Instant other) {
dates.assertIsInSameMinuteWindowAs(info, actual, Date.from(other));
dates.assertIsInSameMinuteWindowAs(info, actual, dateFrom(other));
return myself;
}

Expand Down Expand Up @@ -2969,7 +2970,7 @@ public SELF isInSameSecondWindowAs(Date other) {
* @since 3.19.0
*/
public SELF isInSameSecondWindowAs(Instant other) {
dates.assertIsInSameSecondWindowAs(info, actual, Date.from(other));
dates.assertIsInSameSecondWindowAs(info, actual, dateFrom(other));
return myself;
}

Expand Down Expand Up @@ -3162,7 +3163,7 @@ public SELF isCloseTo(Date other, long deltaInMilliseconds) {
* @since 3.19.0
*/
public SELF isCloseTo(Instant other, long deltaInMilliseconds) {
dates.assertIsCloseTo(info, actual, Date.from(other), deltaInMilliseconds);
dates.assertIsCloseTo(info, actual, dateFrom(other), deltaInMilliseconds);
return myself;
}

Expand Down Expand Up @@ -3637,4 +3638,8 @@ public SELF usingDefaultComparator() {
return super.usingDefaultComparator();
}

private Date dateFrom(Instant instant) {
return actual instanceof Timestamp ? Timestamp.from(instant) : Date.from(instant);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
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 @@ -106,9 +105,6 @@ 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
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,101 @@
*/
package org.assertj.core.api.date;

import static org.mockito.Mockito.verify;
import static java.time.Instant.parse;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.BDDAssertions.then;
import static org.assertj.core.util.AssertionsUtil.expectAssertionError;
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS;
import static org.junit.jupiter.params.provider.Arguments.arguments;

import java.sql.Timestamp;
import java.time.Instant;
import java.util.Date;

import org.assertj.core.api.DateAssert;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.opentest4j.AssertionFailedError;

/**
* Tests for {@link DateAssert#isEqualTo(Date)} and {@link DateAssert#isEqualTo(String)}.
*
* @author Joel Costigliola
*/
class DateAssert_isEqualTo_Test extends AbstractDateAssertWithDateArg_Test {
class DateAssert_isEqualTo_Test {

@Override
protected DateAssert assertionInvocationWithDateArg() {
return assertions.isEqualTo(otherDate);
}
@Nested
@TestInstance(PER_CLASS)
class With_Object {

@Override
protected DateAssert assertionInvocationWithStringArg(String dateAsString) {
return assertions.isEqualTo(dateAsString);
}
@ParameterizedTest
@MethodSource
void should_pass(Date actual, Object expected) {
// WHEN/THEN
assertThat(actual).isEqualTo(expected);
}

Arguments[] should_pass() {
return new Arguments[] {
arguments(Date.from(parse("1970-01-01T00:00:00.000000001Z")), Timestamp.from(parse("1970-01-01T00:00:00.000000001Z")))
};
}

@ParameterizedTest
@MethodSource
void should_fail(Date actual, Object expected) {
// WHEN
AssertionError error = expectAssertionError(() -> assertThat(actual).isEqualTo(expected));
// THEN
then(error).isInstanceOf(AssertionFailedError.class);
}

Arguments[] should_fail() {
return new Arguments[] {
arguments(Timestamp.from(parse("1970-01-01T00:00:00.000000001Z")), Date.from(parse("1970-01-01T00:00:00.000000001Z")))
};
}

protected DateAssert assertionInvocationWithInstantArg(Instant instant) {
return assertions.isEqualTo(instant);
}

@Override
protected void verifyAssertionInvocation(Date date) {
verify(objects).assertEqual(getInfo(assertions), getActual(assertions), date);
@Nested
@TestInstance(PER_CLASS)
class With_Instant {

@ParameterizedTest
@MethodSource
void should_pass(Date actual, Instant expected) {
// WHEN/THEN
assertThat(actual).isEqualTo(expected);
}

Arguments[] should_pass() {
return new Arguments[] {
arguments(Date.from(parse("1970-01-01T00:00:00.000000001Z")), parse("1970-01-01T00:00:00.000000001Z")),
arguments(Timestamp.from(parse("1970-01-01T00:00:00.000000001Z")), parse("1970-01-01T00:00:00.000000001Z"))
};
}

}

@Override
protected DateAssert assertionInvocationWithInstantArg() {
return assertions.isEqualTo(otherDate.toInstant());
@Nested
@TestInstance(PER_CLASS)
class With_String {

@ParameterizedTest
@MethodSource
void should_pass(Date actual, String expected) {
// WHEN/THEN
assertThat(actual).isEqualTo(expected);
}

Arguments[] should_pass() {
return new Arguments[] {
arguments(Date.from(parse("1970-01-01T00:00:00.000000001Z")), "1970-01-01T00:00:00.000Z"),
// FIXME arguments(Timestamp.from(parse("1970-01-01T00:00:00.000000001Z")), "1970-01-01T00:00:00.000Z")
};
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
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 @@ -258,29 +255,6 @@ 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 755e614

Please sign in to comment.