From b2a19d0a1b13f0b66f756f0cfe5426e46a0d7787 Mon Sep 17 00:00:00 2001 From: Scott Leberknight <174812+sleberknight@users.noreply.github.com> Date: Sat, 7 Oct 2023 08:57:35 -0400 Subject: [PATCH 1/2] Add methods to KiwiJdbc to convert java.sql.Date to LocalDate Add two localDateOrNullFromDate methods, one that accepts a ResultSet and colum name, and one that accepts a java.sql.Date. They are both annotated with the Checker Nullable annotation to indicate null can be returned, and also that the java.sql.Date argument can be null as well. Closes #1044 --- .../java/org/kiwiproject/jdbc/KiwiJdbc.java | 26 ++++++++++ .../org/kiwiproject/jdbc/KiwiJdbcTest.java | 48 ++++++++++++++++++- 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/kiwiproject/jdbc/KiwiJdbc.java b/src/main/java/org/kiwiproject/jdbc/KiwiJdbc.java index 36e35c31..87a92ae3 100644 --- a/src/main/java/org/kiwiproject/jdbc/KiwiJdbc.java +++ b/src/main/java/org/kiwiproject/jdbc/KiwiJdbc.java @@ -4,6 +4,7 @@ import static org.kiwiproject.base.KiwiPreconditions.checkArgumentNotNull; import lombok.experimental.UtilityClass; +import org.checkerframework.checker.nullness.qual.Nullable; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -11,6 +12,7 @@ import java.sql.Timestamp; import java.sql.Types; import java.time.Instant; +import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZoneOffset; @@ -118,6 +120,30 @@ public static LocalDateTime localDateTimeFromTimestamp(Timestamp timestamp) { return isNull(timestamp) ? null : timestamp.toLocalDateTime(); } + /** + * Returns a {@link LocalDate} from the specified {@link java.sql.Date} column in the given {@link ResultSet}. + * + * @param rs the ResultSet + * @param columnName the date column name + * @return the converted LocalDate or {@code null} if the column was {@code NULL} + * @throws SQLException if there is problem getting the date + */ + @Nullable + public static LocalDate localDateOrNullFromDate(ResultSet rs, String columnName) throws SQLException { + return localDateOrNullFromDate(rs.getDate(columnName)); + } + + /** + * Returns a {@link LocalDateTime} from the given {@link java.sql.Date}. + * + * @param date the date to convert + * @return the converted LocalDate or {@code null} if the date is {@code null} + */ + @Nullable + public static LocalDate localDateOrNullFromDate(java.sql.@Nullable Date date) { + return isNull(date) ? null : date.toLocalDate(); + } + /** * Returns a {@link ZonedDateTime} in UTC from the specified column in the {@link ResultSet}. * diff --git a/src/test/java/org/kiwiproject/jdbc/KiwiJdbcTest.java b/src/test/java/org/kiwiproject/jdbc/KiwiJdbcTest.java index 6b14a0f2..7124e403 100644 --- a/src/test/java/org/kiwiproject/jdbc/KiwiJdbcTest.java +++ b/src/test/java/org/kiwiproject/jdbc/KiwiJdbcTest.java @@ -2,6 +2,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; @@ -19,7 +20,9 @@ import java.sql.Timestamp; import java.sql.Types; import java.time.Instant; +import java.time.LocalDate; import java.time.LocalDateTime; +import java.time.Month; import java.time.ZoneId; import java.time.ZoneOffset; import java.time.ZonedDateTime; @@ -194,6 +197,49 @@ void shouldReturnNull_WhenResultSet_ReturnsNullTimestamp() throws SQLException { } } + @Nested + class LocalDateFromJavaSqlDate { + + @Test + void shouldConvertFromDate() { + var originalDate = LocalDate.of(2023, Month.APRIL, 1); + var date = java.sql.Date.valueOf(originalDate); + + assertThat(KiwiJdbc.localDateOrNullFromDate(date)).isEqualTo(originalDate); + } + + @Test + void shouldReturnNull_WhenGivenNullDate() { + assertThat(KiwiJdbc.localDateOrNullFromDate(null)).isNull(); + } + + @Test + void shouldConvertFromResultSet() throws SQLException { + var originalDate = LocalDate.of(2023, Month.SEPTEMBER, 8); + var date = java.sql.Date.valueOf(originalDate); + + var resultSet = newMockResultSet(); + when(resultSet.getDate(anyString())).thenReturn(date); + + assertThat(KiwiJdbc.localDateOrNullFromDate(resultSet, "date_of_birth")) + .isEqualTo(originalDate); + + verify(resultSet).getDate("date_of_birth"); + verifyNoMoreInteractions(resultSet); + } + + @Test + void shouldReturnNull_WhenResultSet_ReturnsNullDate() throws SQLException { + var resultSet = newMockResultSet(); + when(resultSet.getDate(anyString())).thenReturn(null); + + assertThat(KiwiJdbc.localDateOrNullFromDate(resultSet, "expiration_date")).isNull(); + + verify(resultSet).getDate("expiration_date"); + verifyNoMoreInteractions(resultSet); + } + } + @Nested class UtcZonedDateTimeFromTimestamp { @@ -638,4 +684,4 @@ private static ResultSet newMockResultSet() { private PreparedStatement newMockPreparedStatement() { return mock(PreparedStatement.class); } -} \ No newline at end of file +} From 23c0bde094e7cdb00e2d10b0bb52f5ee8a4da164 Mon Sep 17 00:00:00 2001 From: Scott Leberknight <174812+sleberknight@users.noreply.github.com> Date: Sat, 7 Oct 2023 14:32:50 -0400 Subject: [PATCH 2/2] Rename localDateOrNullFromDate to localDateFromDateOrNull --- src/main/java/org/kiwiproject/jdbc/KiwiJdbc.java | 6 +++--- src/test/java/org/kiwiproject/jdbc/KiwiJdbcTest.java | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/kiwiproject/jdbc/KiwiJdbc.java b/src/main/java/org/kiwiproject/jdbc/KiwiJdbc.java index 87a92ae3..f947e0af 100644 --- a/src/main/java/org/kiwiproject/jdbc/KiwiJdbc.java +++ b/src/main/java/org/kiwiproject/jdbc/KiwiJdbc.java @@ -129,8 +129,8 @@ public static LocalDateTime localDateTimeFromTimestamp(Timestamp timestamp) { * @throws SQLException if there is problem getting the date */ @Nullable - public static LocalDate localDateOrNullFromDate(ResultSet rs, String columnName) throws SQLException { - return localDateOrNullFromDate(rs.getDate(columnName)); + public static LocalDate localDateFromDateOrNull(ResultSet rs, String columnName) throws SQLException { + return localDateFromDateOrNull(rs.getDate(columnName)); } /** @@ -140,7 +140,7 @@ public static LocalDate localDateOrNullFromDate(ResultSet rs, String columnName) * @return the converted LocalDate or {@code null} if the date is {@code null} */ @Nullable - public static LocalDate localDateOrNullFromDate(java.sql.@Nullable Date date) { + public static LocalDate localDateFromDateOrNull(java.sql.@Nullable Date date) { return isNull(date) ? null : date.toLocalDate(); } diff --git a/src/test/java/org/kiwiproject/jdbc/KiwiJdbcTest.java b/src/test/java/org/kiwiproject/jdbc/KiwiJdbcTest.java index 7124e403..4e03ca8b 100644 --- a/src/test/java/org/kiwiproject/jdbc/KiwiJdbcTest.java +++ b/src/test/java/org/kiwiproject/jdbc/KiwiJdbcTest.java @@ -198,19 +198,19 @@ void shouldReturnNull_WhenResultSet_ReturnsNullTimestamp() throws SQLException { } @Nested - class LocalDateFromJavaSqlDate { + class LocalDateFromDateOrNull { @Test void shouldConvertFromDate() { var originalDate = LocalDate.of(2023, Month.APRIL, 1); var date = java.sql.Date.valueOf(originalDate); - assertThat(KiwiJdbc.localDateOrNullFromDate(date)).isEqualTo(originalDate); + assertThat(KiwiJdbc.localDateFromDateOrNull(date)).isEqualTo(originalDate); } @Test void shouldReturnNull_WhenGivenNullDate() { - assertThat(KiwiJdbc.localDateOrNullFromDate(null)).isNull(); + assertThat(KiwiJdbc.localDateFromDateOrNull(null)).isNull(); } @Test @@ -221,7 +221,7 @@ void shouldConvertFromResultSet() throws SQLException { var resultSet = newMockResultSet(); when(resultSet.getDate(anyString())).thenReturn(date); - assertThat(KiwiJdbc.localDateOrNullFromDate(resultSet, "date_of_birth")) + assertThat(KiwiJdbc.localDateFromDateOrNull(resultSet, "date_of_birth")) .isEqualTo(originalDate); verify(resultSet).getDate("date_of_birth"); @@ -233,7 +233,7 @@ void shouldReturnNull_WhenResultSet_ReturnsNullDate() throws SQLException { var resultSet = newMockResultSet(); when(resultSet.getDate(anyString())).thenReturn(null); - assertThat(KiwiJdbc.localDateOrNullFromDate(resultSet, "expiration_date")).isNull(); + assertThat(KiwiJdbc.localDateFromDateOrNull(resultSet, "expiration_date")).isNull(); verify(resultSet).getDate("expiration_date"); verifyNoMoreInteractions(resultSet);