Skip to content

Commit

Permalink
Fixed bug in null-handling for java.time converters
Browse files Browse the repository at this point in the history
  • Loading branch information
aaberg committed Jan 17, 2025
1 parent 537d73e commit 69bc00c
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 2 deletions.
3 changes: 3 additions & 0 deletions core/src/main/java/org/sql2o/converters/InstantConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
public class InstantConverter extends ConverterBase<Instant> {
@Override
public Instant convert(Object val) throws ConverterException {
if (val == null) {
return null;
}
if (val instanceof Timestamp) {
return ((Timestamp)val).toInstant();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
public class LocalDateConverter extends ConverterBase<LocalDate> {
@Override
public LocalDate convert(Object val) throws ConverterException {
if (val == null) {
return null;
}
if (val instanceof java.sql.Date) {
return ((java.sql.Date) val).toLocalDate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
public class LocalDateTimeConverter extends ConverterBase<LocalDateTime> {
@Override
public LocalDateTime convert(Object val) throws ConverterException {
if (val == null) {
return null;
}
if (val instanceof java.sql.Timestamp) {
return ((java.sql.Timestamp) val).toLocalDateTime();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ public class LocalTimeConverter extends ConverterBase<LocalTime> {

@Override
public LocalTime convert(Object val) throws ConverterException {
if (val == null) {
return null;
}
if (val instanceof java.sql.Time) {
return ((java.sql.Time) val).toLocalTime();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
public class OffsetDateTimeConverter extends ConverterBase<OffsetDateTime>{
@Override
public OffsetDateTime convert(Object val) throws ConverterException {
if (val == null) {
return null;
}
if (val instanceof OffsetDateTime) {
return (OffsetDateTime) val;
}
Expand Down
15 changes: 13 additions & 2 deletions core/src/test/java/org/sql2o/converters/InstantConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.*;

public class InstantConverterTest {

Expand Down Expand Up @@ -107,4 +106,16 @@ void insertAndFetch_usingInstantType_isSuccessfull(String dbName, String url, St
// Assert
assertEquals(instantToInsert, result);
}

@Test
void convert_null_returnsNull() throws ConverterException {
// Arrange
InstantConverter converter = new InstantConverter();

// Act
Instant result = converter.convert(null);

// Assert
assertNull(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ public void insertAndFetch_usingLocalDateType_isSuccessfull(String dbName, Strin
assertEquals(dateToInsert, pojo.getDate());
}

@Test
public void convert_null_returns_null() throws ConverterException {
// setup
final var converter = new LocalDateConverter();

// test
final var convertedDate = converter.convert(null);
assertNull(convertedDate);
}

private Sql2o setupDatabase(String url, String user, String pass) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ void convert_invalidTimeString_throwsException() {
assertThrows(ConverterException.class, () -> converter.convert("invalid"));
}

@Test
void convert_null_returns_null() throws ConverterException {
// setup
final var converter = new LocalDateTimeConverter();

// test
final var convertedTime = converter.convert(null);

// assert
assertNull(convertedTime);
}

@ParameterizedTest(name = "{0}")
@ArgumentsSource(TestDatabasesArgumentSourceProvider.class)
void insertAndFetch_usingLocalDateTimeType_isSuccessfull(String dbName, String url, String user, String pass) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ void insertAndFetch_usingLocalTimeConverter_isSuccessfull(String dbName, String
assertEquals(targetTime, pojo.getTime());
}

@Test
void convert_null_returns_null() throws ConverterException {
// setup
final var converter = new LocalTimeConverter();

// test
final var convertedTime = converter.convert(null);

// assert
assertNull(convertedTime);
}

static class LocalTimePojo {
private int id;
private LocalTime time;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,16 @@ void insertAndFetch_usingOffsetDateTimeWithGivenTimeZone_isSuccessfull(String db
// Assert
assertEquals(targetTime.toInstant(), resultTime.toInstant());
}

@Test
void convert_null_returnsNull() throws ConverterException {
// Arrange
final var converter = new OffsetDateTimeConverter();

// Act
final var convertedTime = converter.convert(null);

// Assert
assertNull(convertedTime);
}
}

0 comments on commit 69bc00c

Please sign in to comment.