From 9eeb352e1188b8398a952fc9819a5173d44f8fe0 Mon Sep 17 00:00:00 2001 From: Joel Costigliola <382613+joel-costigliola@users.noreply.github.com> Date: Mon, 20 May 2024 16:55:03 +0200 Subject: [PATCH] Refactor tests covering date string conversion --- ...string_based_date_representation_Test.java | 122 ++++++++++-------- 1 file changed, 69 insertions(+), 53 deletions(-) diff --git a/assertj-core/src/test/java/org/assertj/core/api/date/DateAssert_with_string_based_date_representation_Test.java b/assertj-core/src/test/java/org/assertj/core/api/date/DateAssert_with_string_based_date_representation_Test.java index 190cc78cf4..f74ea2d3e5 100644 --- a/assertj-core/src/test/java/org/assertj/core/api/date/DateAssert_with_string_based_date_representation_Test.java +++ b/assertj-core/src/test/java/org/assertj/core/api/date/DateAssert_with_string_based_date_representation_Test.java @@ -15,17 +15,22 @@ import static java.lang.String.format; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.assertj.core.api.Assertions.catchThrowable; import static org.assertj.core.api.Assertions.registerCustomDateFormat; import static org.assertj.core.api.Assertions.useDefaultDateFormatsOnly; +import static org.assertj.core.api.BDDAssertions.then; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; import static org.assertj.core.util.DateUtil.parseDatetime; import static org.assertj.core.util.DateUtil.parseDatetimeWithMs; import java.sql.Timestamp; -import java.text.ParseException; import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.OffsetDateTime; +import java.time.ZoneId; +import java.time.ZoneOffset; import java.util.Date; -import java.util.TimeZone; +import java.util.concurrent.TimeUnit; import org.assertj.core.api.DateAssertBaseTest; import org.assertj.core.util.DateUtil; @@ -47,7 +52,6 @@ public void tearDown() { @Test void date_assertion_using_default_date_string_representation() { - // datetime with ms is supported final Date date1timeWithMS = parseDatetimeWithMs("2003-04-26T03:01:02.999"); assertThat(date1timeWithMS).isEqualTo("2003-04-26T03:01:02.999"); @@ -56,52 +60,68 @@ void date_assertion_using_default_date_string_representation() { assertThat(datetime).isEqualTo("2003-04-26T03:01:02.000"); assertThat(datetime).isEqualTo("2003-04-26T03:01:02"); // date is supported - final Date date = DateUtil.parse("2003-04-26"); + Date date = DateUtil.parse("2003-04-26"); assertThat(date).isEqualTo("2003-04-26"); assertThat(date).isEqualTo("2003-04-26T00:00:00"); assertThat(date).isEqualTo("2003-04-26T00:00:00.000"); + + // TODO add test with nanos } @Test - void date_assertion_should_support_timestamp_string_representation() throws ParseException { - Date date = DateUtil.newTimestampDateFormat().parse("2015-05-08 11:30:00.560"); - String timestampAsString = DateUtil.newTimestampDateFormat().format(new Timestamp(date.getTime())); - - assertThat(date).isEqualTo(timestampAsString); + void date_assertion_should_support_local_date_string_representation() { + // GIVEN + LocalDate localDate = LocalDate.of(2003, 4, 26); + ZoneId systemDefault = ZoneId.systemDefault(); + Date date = Date.from(localDate.atStartOfDay(systemDefault).toInstant()); + // WHEN/THEN + then(date).isEqualTo("2003-04-26"); } @Test - void date_assertion_should_support_date_with_utc_time_zone_string_representation() throws ParseException { - SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); - isoFormat.setTimeZone(TimeZone.getTimeZone("UTC")); - Date date = isoFormat.parse("2003-04-26T00:00:00"); - - assertThat(date).isEqualTo("2003-04-26T00:00:00+00:00"); + void date_assertion_should_support_timestamp_string_representation() { + // GIVEN + Date date = new Date(Timestamp.valueOf("2003-04-26 13:01:02.999").getTime()); + // WHEN/THEN + then(date).isEqualTo("2003-04-26 13:01:02.999"); } @Test - void date_assertion_should_support_date_with_ms_and_utc_time_zone_string_representation() throws ParseException { + void date_assertion_should_support_date_with_iso_offset_datetime_string_representation() { // GIVEN - SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); - isoFormat.setTimeZone(TimeZone.getTimeZone("UTC")); - // WHEN - Date date = isoFormat.parse("2003-04-26T00:00:00.123"); - // THEN - assertThat(date).isEqualTo("2003-04-26T00:00:00.123+00:00"); + OffsetDateTime offsetDateTime = OffsetDateTime.of(2003, 4, 26, 13, 1, 2, 0, ZoneOffset.of("+01:00")); + Date date = Date.from(offsetDateTime.toInstant()); + // WHEN/THEN + then(date).isEqualTo("2003-04-26T13:01:02+01:00"); } @Test - void date_assertion_should_support_date_with_utc_time_zone_in_different_time_zone_string_representation() throws ParseException { - SimpleDateFormat isoDateFormatUtc = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); - isoDateFormatUtc.setTimeZone(TimeZone.getTimeZone("UTC")); - - SimpleDateFormat isoDateFormatNewYork = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX"); - isoDateFormatNewYork.setTimeZone(TimeZone.getTimeZone("America/New_York")); + void date_assertion_should_support_date_with_iso_offset_datetime_string_representation_with_millis() { + // GIVEN + int nanos = (int) TimeUnit.MILLISECONDS.toNanos(999); + OffsetDateTime isoOffsetDateTime = OffsetDateTime.of(2003, 4, 26, 13, 1, 2, nanos, ZoneOffset.of("+02:00")); + Date date = Date.from(isoOffsetDateTime.toInstant()); + // WHEN/THEN + then(date).isEqualTo("2003-04-26T13:01:02.999+02:00"); + } - Date date = isoDateFormatUtc.parse("2003-04-26T00:00:00"); - String newYorkDate = isoDateFormatNewYork.format(date); + @Test + void date_assertion_should_support_date_with_iso_local_datetime_string_representation_with_millis() { + // GIVEN + int nanos = (int) TimeUnit.MILLISECONDS.toNanos(999); + LocalDateTime localDateTimeWithMillis = LocalDateTime.of(2003, 4, 26, 13, 1, 2, nanos); + Date date = Date.from(localDateTimeWithMillis.atZone(ZoneId.systemDefault()).toInstant()); + // WHEN/THEN + then(date).isEqualTo("2003-04-26T13:01:02.999"); + } - assertThat(date).isEqualTo(newYorkDate); + @Test + void date_assertion_should_support_date_with_iso_local_datetime_string_representation() { + // GIVEN + LocalDateTime localDateTimeWithMillis = LocalDateTime.of(2003, 4, 26, 13, 1, 2, 0); + Date date = Date.from(localDateTimeWithMillis.atZone(ZoneId.systemDefault()).toInstant()); + // WHEN/THEN + then(date).isEqualTo("2003-04-26T13:01:02"); } @Test @@ -131,18 +151,17 @@ void should_fail_if_given_date_string_representation_cant_be_parsed_with_any_cus registerCustomDateFormat("yyyy/MM/dd'T'HH:mm:ss"); // registering again has no effect registerCustomDateFormat("yyyy/MM/dd'T'HH:mm:ss"); - assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThat(date).withDateFormat("yyyy/MM/dd") - .isEqualTo("2003 04 26")) - .withMessage(format("Failed to parse 2003 04 26 with any of these date formats:%n" - + - " [yyyy/MM/dd'T'HH:mm:ss,%n" + - " yyyy/MM/dd,%n" + - " yyyy-MM-dd'T'HH:mm:ss.SSSX,%n" + - " yyyy-MM-dd'T'HH:mm:ss.SSS,%n" + - " yyyy-MM-dd HH:mm:ss.SSS,%n" + - " yyyy-MM-dd'T'HH:mm:ssX,%n" + - " yyyy-MM-dd'T'HH:mm:ss,%n" + - " yyyy-MM-dd]")); + AssertionError error = expectAssertionError(() -> assertThat(date).withDateFormat("yyyy/MM/dd").isEqualTo("2003 04 26")); + assertThat(error).hasMessage(format("Failed to parse 2003 04 26 with any of these date formats:%n" + + + " [yyyy/MM/dd'T'HH:mm:ss,%n" + + " yyyy/MM/dd,%n" + + " yyyy-MM-dd'T'HH:mm:ss.SSSX,%n" + + " yyyy-MM-dd'T'HH:mm:ss.SSS,%n" + + " yyyy-MM-dd HH:mm:ss.SSS,%n" + + " yyyy-MM-dd'T'HH:mm:ssX,%n" + + " yyyy-MM-dd'T'HH:mm:ss,%n" + + " yyyy-MM-dd]")); } @Test @@ -165,11 +184,10 @@ void use_custom_date_formats_set_from_Assertions_entry_point() { // WHEN // fail : the registered format does not match the given date - Throwable error = catchThrowable(() -> assertThat(date).isEqualTo("2003/04/26")); + AssertionError error = expectAssertionError(() -> assertThat(date).isEqualTo("2003/04/26")); // THEN - assertThat(error).isInstanceOf(AssertionError.class) - .hasMessage(format("Failed to parse 2003/04/26 with any of these date formats:%n" + + assertThat(error).hasMessage(format("Failed to parse 2003/04/26 with any of these date formats:%n" + " [yyyy/MM/dd'T'HH:mm:ss,%n" + " yyyy-MM-dd'T'HH:mm:ss.SSSX,%n" + " yyyy-MM-dd'T'HH:mm:ss.SSS,%n" + @@ -201,11 +219,10 @@ void use_custom_date_formats_first_then_defaults_to_parse_a_date() { // WHEN // date with a custom format : failure since the default formats don't match. - Throwable error = catchThrowable(() -> assertThat(date).isEqualTo("2003/04/26")); + AssertionError error = expectAssertionError(() -> assertThat(date).isEqualTo("2003/04/26")); // THEN - assertThat(error).isInstanceOf(AssertionError.class) - .hasMessage(format("Failed to parse 2003/04/26 with any of these date formats:%n" + + assertThat(error).hasMessage(format("Failed to parse 2003/04/26 with any of these date formats:%n" + " [yyyy-MM-dd'T'HH:mm:ss.SSSX,%n" + " yyyy-MM-dd'T'HH:mm:ss.SSS,%n" + " yyyy-MM-dd HH:mm:ss.SSS,%n" + @@ -222,11 +239,10 @@ void use_custom_date_formats_first_then_defaults_to_parse_a_date() { // WHEN // but if not format at all matches, it fails. - error = catchThrowable(() -> assertThat(date).isEqualTo("2003 04 26")); + error = expectAssertionError(() -> assertThat(date).isEqualTo("2003 04 26")); // THEN - assertThat(error).isInstanceOf(AssertionError.class) - .hasMessage(format("Failed to parse 2003 04 26 with any of these date formats:%n" + + assertThat(error).hasMessage(format("Failed to parse 2003 04 26 with any of these date formats:%n" + " [yyyy/MM/dd,%n" + " yyyy-MM-dd'T'HH:mm:ss.SSSX,%n" + " yyyy-MM-dd'T'HH:mm:ss.SSS,%n" +