diff --git a/src/main/java/org/springframework/data/auditing/CurrentDateTimeProvider.java b/src/main/java/org/springframework/data/auditing/CurrentDateTimeProvider.java index 06c7d6a2c..d31575806 100644 --- a/src/main/java/org/springframework/data/auditing/CurrentDateTimeProvider.java +++ b/src/main/java/org/springframework/data/auditing/CurrentDateTimeProvider.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-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. @@ -15,7 +15,7 @@ */ package org.springframework.data.auditing; -import java.time.ZonedDateTime; +import java.time.LocalDateTime; import java.time.temporal.TemporalAccessor; import java.util.Optional; @@ -25,6 +25,7 @@ import org.joda.time.DateTime; * Default {@link DateTimeProvider} simply creating new {@link DateTime} instances for each method call. * * @author Oliver Gierke + * @author Christoph Strobl * @since 1.5 */ public enum CurrentDateTimeProvider implements DateTimeProvider { @@ -37,6 +38,6 @@ public enum CurrentDateTimeProvider implements DateTimeProvider { */ @Override public Optional getNow() { - return Optional.of(ZonedDateTime.now()); + return Optional.of(LocalDateTime.now()); } } diff --git a/src/main/java/org/springframework/data/auditing/DefaultAuditableBeanWrapperFactory.java b/src/main/java/org/springframework/data/auditing/DefaultAuditableBeanWrapperFactory.java index 18149447a..3e19b442b 100644 --- a/src/main/java/org/springframework/data/auditing/DefaultAuditableBeanWrapperFactory.java +++ b/src/main/java/org/springframework/data/auditing/DefaultAuditableBeanWrapperFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2015 the original author or authors. + * Copyright 2012-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. @@ -38,6 +38,7 @@ import org.springframework.util.Assert; * A factory class to {@link AuditableBeanWrapper} instances. * * @author Oliver Gierke + * @author Christoph Strobl * @since 1.5 */ class DefaultAuditableBeanWrapperFactory implements AuditableBeanWrapperFactory { @@ -190,6 +191,11 @@ class DefaultAuditableBeanWrapperFactory implements AuditableBeanWrapperFactory if (conversionService.canConvert(Date.class, targetType)) { + if(!conversionService.canConvert(it.getClass(), Date.class)) { + throw new IllegalArgumentException(String.format("Cannot convert date type for member %s! From %s to java.util.Date to %s.", + source, it.getClass(), targetType)); + } + Date date = conversionService.convert(it, Date.class); return conversionService.convert(date, targetType); } diff --git a/src/test/java/org/springframework/data/auditing/DefaultAuditableBeanWrapperFactoryUnitTests.java b/src/test/java/org/springframework/data/auditing/DefaultAuditableBeanWrapperFactoryUnitTests.java index 6e0fd13fb..62f20fbf2 100755 --- a/src/test/java/org/springframework/data/auditing/DefaultAuditableBeanWrapperFactoryUnitTests.java +++ b/src/test/java/org/springframework/data/auditing/DefaultAuditableBeanWrapperFactoryUnitTests.java @@ -18,6 +18,7 @@ package org.springframework.data.auditing; import static org.assertj.core.api.Assertions.*; import java.time.Instant; +import java.time.ZonedDateTime; import java.util.Optional; import org.junit.Test; @@ -28,6 +29,7 @@ import org.springframework.data.auditing.DefaultAuditableBeanWrapperFactory.Refl * Unit tests for {@link DefaultAuditableBeanWrapperFactory}. * * @author Oliver Gierke + * @author Christoph Strobl * @since 1.5 */ public class DefaultAuditableBeanWrapperFactoryUnitTests { @@ -78,4 +80,17 @@ public class DefaultAuditableBeanWrapperFactoryUnitTests { }); } + + @Test(expected = IllegalArgumentException.class) // DATACMNS-867 + public void errorsWhenUnableToConvertDateViaIntermedeateJavaUtilDateConversion() { + + Jsr310ThreeTenBpAuditedUser user = new Jsr310ThreeTenBpAuditedUser(); + ZonedDateTime zonedDateTime = ZonedDateTime.now(); + + Optional wrapper = factory.getBeanWrapperFor(Optional.of(user)); + + assertThat(wrapper).hasValueSatisfying(it -> { + it.setLastModifiedDate(Optional.of(zonedDateTime)); + }); + } }