From 09137fd66d2c583b2496c71774b3f6e9ff4f9933 Mon Sep 17 00:00:00 2001 From: Rico Schrage Date: Tue, 6 Apr 2021 16:04:57 +0200 Subject: [PATCH] Fixes #56: all util.Date (+ subclasses) objects won't be processed by the BeanFactory to make jackson apply the correct date-serializer. --- .../bean/ext/JavaTimeExtension.java | 4 +++- .../persistence/SamplerBeanFactoryTest.java | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/deepsampler-persistence/src/main/java/de/ppi/deepsampler/persistence/bean/ext/JavaTimeExtension.java b/deepsampler-persistence/src/main/java/de/ppi/deepsampler/persistence/bean/ext/JavaTimeExtension.java index bcfbe105..824e3166 100644 --- a/deepsampler-persistence/src/main/java/de/ppi/deepsampler/persistence/bean/ext/JavaTimeExtension.java +++ b/deepsampler-persistence/src/main/java/de/ppi/deepsampler/persistence/bean/ext/JavaTimeExtension.java @@ -7,12 +7,14 @@ import java.time.LocalDate; import java.time.LocalDateTime; +import java.util.Date; public class JavaTimeExtension extends StandardBeanFactoryExtension { @Override public boolean isProcessable(Class beanCls) { - return LocalDateTime.class.isAssignableFrom(beanCls) || LocalDate.class.isAssignableFrom(beanCls); + return LocalDateTime.class.isAssignableFrom(beanCls) || LocalDate.class.isAssignableFrom(beanCls) + || Date.class.isAssignableFrom(beanCls); } @Override diff --git a/deepsampler-persistence/src/test/java/de/ppi/deepsampler/persistence/SamplerBeanFactoryTest.java b/deepsampler-persistence/src/test/java/de/ppi/deepsampler/persistence/SamplerBeanFactoryTest.java index 2f08589d..84893a00 100644 --- a/deepsampler-persistence/src/test/java/de/ppi/deepsampler/persistence/SamplerBeanFactoryTest.java +++ b/deepsampler-persistence/src/test/java/de/ppi/deepsampler/persistence/SamplerBeanFactoryTest.java @@ -3,6 +3,7 @@ import de.ppi.deepsampler.persistence.model.PersistentBean; import org.junit.jupiter.api.Test; +import java.sql.Timestamp; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -28,6 +29,24 @@ void testImmutableCollectionBean() { assertEquals(bean.collectionOfStrings, persistentBean.getValue("0$collectionOfStrings")); } + @Test + void testTimestampSql() { + // GIVEN + Timestamp ts = new Timestamp(1L); + final TimestampBean timestampBean = new TimestampBean(); + timestampBean.timestamp = ts; + + // WHEN + PersistentBean persistentBean = SamplerBeanFactory.create().toBean(timestampBean); + + // THEN + assertEquals(ts, persistentBean.getValue("0$timestamp")); + } + + private static class TimestampBean { + Timestamp timestamp; + } + private static class CollectionBean { Collection collectionOfStrings; }