diff --git a/src/main/java/org/springframework/data/jpa/convert/threeten/Jsr310JpaConverters.java b/src/main/java/org/springframework/data/jpa/convert/threeten/Jsr310JpaConverters.java index f8b58985a..fdb766d1d 100644 --- a/src/main/java/org/springframework/data/jpa/convert/threeten/Jsr310JpaConverters.java +++ b/src/main/java/org/springframework/data/jpa/convert/threeten/Jsr310JpaConverters.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2015 the original author or authors. + * Copyright 2014-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -52,12 +52,12 @@ public class Jsr310JpaConverters { @Override public Date convertToDatabaseColumn(LocalDate date) { - return LocalDateToDateConverter.INSTANCE.convert(date); + return date == null ? null : LocalDateToDateConverter.INSTANCE.convert(date); } @Override public LocalDate convertToEntityAttribute(Date date) { - return DateToLocalDateConverter.INSTANCE.convert(date); + return date == null ? null : DateToLocalDateConverter.INSTANCE.convert(date); } } @@ -66,12 +66,12 @@ public class Jsr310JpaConverters { @Override public Date convertToDatabaseColumn(LocalTime time) { - return LocalTimeToDateConverter.INSTANCE.convert(time); + return time == null ? null : LocalTimeToDateConverter.INSTANCE.convert(time); } @Override public LocalTime convertToEntityAttribute(Date date) { - return DateToLocalTimeConverter.INSTANCE.convert(date); + return date == null ? null : DateToLocalTimeConverter.INSTANCE.convert(date); } } @@ -80,12 +80,12 @@ public class Jsr310JpaConverters { @Override public Date convertToDatabaseColumn(LocalDateTime date) { - return LocalDateTimeToDateConverter.INSTANCE.convert(date); + return date == null ? null : LocalDateTimeToDateConverter.INSTANCE.convert(date); } @Override public LocalDateTime convertToEntityAttribute(Date date) { - return DateToLocalDateTimeConverter.INSTANCE.convert(date); + return date == null ? null : DateToLocalDateTimeConverter.INSTANCE.convert(date); } } @@ -94,12 +94,12 @@ public class Jsr310JpaConverters { @Override public Date convertToDatabaseColumn(Instant instant) { - return InstantToDateConverter.INSTANCE.convert(instant); + return instant == null ? null : InstantToDateConverter.INSTANCE.convert(instant); } @Override public Instant convertToEntityAttribute(Date date) { - return DateToInstantConverter.INSTANCE.convert(date); + return date == null ? null : DateToInstantConverter.INSTANCE.convert(date); } } @@ -107,12 +107,12 @@ public class Jsr310JpaConverters { public static class ZoneIdConverter implements AttributeConverter { public String convertToDatabaseColumn(ZoneId zoneId) { - return ZoneIdToStringConverter.INSTANCE.convert(zoneId); + return zoneId == null ? null : ZoneIdToStringConverter.INSTANCE.convert(zoneId); } @Override public ZoneId convertToEntityAttribute(String zoneId) { - return StringToZoneIdConverter.INSTANCE.convert(zoneId); + return zoneId == null ? null : StringToZoneIdConverter.INSTANCE.convert(zoneId); } } } diff --git a/src/test/java/org/springframework/data/jpa/convert/threeten/Jsr310JpaConvertersUnitTests.java b/src/test/java/org/springframework/data/jpa/convert/threeten/Jsr310JpaConvertersUnitTests.java new file mode 100644 index 000000000..09e6fa8f6 --- /dev/null +++ b/src/test/java/org/springframework/data/jpa/convert/threeten/Jsr310JpaConvertersUnitTests.java @@ -0,0 +1,56 @@ +/* + * Copyright 2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.jpa.convert.threeten; + +import static org.assertj.core.api.Assertions.*; + +import java.util.Arrays; + +import javax.persistence.AttributeConverter; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; +import org.junit.runners.Parameterized.Parameters; + +/** + * Unit tests for {@link Jsr310JpaConverters}. + * + * @author Oliver Gierke + */ +@RunWith(Parameterized.class) +public class Jsr310JpaConvertersUnitTests { + + @Parameters + public static Iterable data() { + + return Arrays.asList(new Jsr310JpaConverters.InstantConverter(), // + new Jsr310JpaConverters.LocalDateConverter(), // + new Jsr310JpaConverters.LocalDateTimeConverter(), // + new Jsr310JpaConverters.LocalTimeConverter(), // + new Jsr310JpaConverters.ZoneIdConverter()); + } + + public @Parameter AttributeConverter converter; + + @Test // DATAJPA- + public void convertersHandleNullValuesCorrectly() { + + assertThat(converter.convertToDatabaseColumn(null)).isNull(); + assertThat(converter.convertToEntityAttribute(null)).isNull(); + } +}