From: Kai Moritz Date: Tue, 15 Aug 2017 18:06:27 +0000 (+0200) Subject: Implemented some test-cases X-Git-Tag: jpa-converters-1.0.0~12 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=2aeda6423279d79a8bc34b8ceb510fd41cec4955;p=jpa-converters Implemented some test-cases --- diff --git a/pom.xml b/pom.xml index 226ef78..693b3ab 100755 --- a/pom.xml +++ b/pom.xml @@ -60,13 +60,14 @@ UTF-8 + 1.4.190 + 5.0.2.Final 1.0.0.Final 4.12 1.7.13 - @@ -90,6 +91,18 @@ ${junit.version} test + + org.hibernate + hibernate-entitymanager + ${hibernate.version} + test + + + com.h2database + h2 + ${h2.version} + test + diff --git a/src/test/java/de/juplo/jpa/converters/EierlegendeWollmilchSau.java b/src/test/java/de/juplo/jpa/converters/EierlegendeWollmilchSau.java new file mode 100644 index 0000000..c02dea6 --- /dev/null +++ b/src/test/java/de/juplo/jpa/converters/EierlegendeWollmilchSau.java @@ -0,0 +1,93 @@ +package de.juplo.jpa.converters; + +import java.time.OffsetDateTime; +import java.time.ZonedDateTime; +import javax.persistence.Access; +import javax.persistence.AccessType; +import javax.persistence.Convert; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * + * @author Kai Moritz + */ +@Entity +@Access(AccessType.PROPERTY) +public class EierlegendeWollmilchSau +{ + Logger log = LoggerFactory.getLogger(EierlegendeWollmilchSau.class); + + + Long id; + + OffsetDateTime odt; + ZonedDateTime zdt; + + + @Id + @GeneratedValue + public Long getId() + { + return id; + } + + public void setId(Long id) + { + this.id = id; + } + + + @Convert(converter = OffsetDateTimeConverter.class) + public OffsetDateTime getOdt() + { + log.trace("get OffsetDateTime: {}", odt); + return odt; + } + + public void setOdt(OffsetDateTime odt) + { + log.trace("set OffsetDateTime: {}", odt); + this.odt = odt; + } + + @Convert(converter = ZonedDateTimeConverter.class) + public ZonedDateTime getZdt() + { + log.trace("get ZonedDateTime: {}", zdt); + return zdt; + } + + public void setZdt(ZonedDateTime zdt) + { + log.trace("set ZonedDateTime: {}", zdt); + this.zdt = zdt; + } + + + @Override + public String toString() + { + boolean first = true; + StringBuilder builder = new StringBuilder(); + if (odt != null) + { + builder.append("OffsetDateTime: "); + builder.append(odt); + first = false; + } + if (zdt != null) + { + if (!first) + builder.append(", "); + builder.append("ZonedDateTime: "); + builder.append(zdt); + first = false; + } + return builder.toString(); + } +} diff --git a/src/test/java/de/juplo/jpa/converters/JPAConverterTest.java b/src/test/java/de/juplo/jpa/converters/JPAConverterTest.java new file mode 100644 index 0000000..38b84e3 --- /dev/null +++ b/src/test/java/de/juplo/jpa/converters/JPAConverterTest.java @@ -0,0 +1,104 @@ +package de.juplo.jpa.converters; + +import de.juplo.jpa.converters.TimeConversions.FixedZoneIdStrategy; +import de.juplo.jpa.converters.TimeConversions.FixedZoneOffsetStrategy; +import java.time.OffsetDateTime; +import java.time.ZoneId; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import javax.persistence.EntityManager; +import javax.persistence.EntityTransaction; +import javax.persistence.Persistence; +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * + * @author kai + */ +public class JPAConverterTest +{ + final static Logger log = LoggerFactory.getLogger(JPAConverterTest.class); + + + EntityManager em = + Persistence.createEntityManagerFactory("test").createEntityManager(); + + ZonedDateTime zdt = ZonedDateTime.now(); + OffsetDateTime odt = OffsetDateTime.now(); + + ZoneId defaultZone = ZoneId.systemDefault(); + ZoneId otherZone = defaultZone.getId().equals("America/New_York") ? ZoneId.of("Europe/Berlin") : ZoneId.of("America/New_York"); + + ZoneOffset defaultOffset = defaultZone.getRules().getOffset(odt.toLocalDateTime()); + ZoneOffset otherOffset = ZoneOffset.ofTotalSeconds(defaultOffset.getTotalSeconds() + 3 * 60 * 60); + + + @Test + public void testOffsetDateTime() + { + EierlegendeWollmilchSau ews; + Long id; + + TimeConversions.setZoneOffsetStrategy(new FixedZoneOffsetStrategy(defaultOffset)); + ews = new EierlegendeWollmilchSau(); + ews.odt = odt; + id = persist(ews); + ews = retrieve(id); + assertEquals(odt, ews.odt); + + TimeConversions.setZoneOffsetStrategy(new FixedZoneOffsetStrategy(otherOffset)); + ews = new EierlegendeWollmilchSau(); + ews.odt = odt; + id = persist(ews); + ews = retrieve(id); + assertEquals(odt.toInstant(), ews.odt.toInstant()); + } + + @Test + public void testZonedDateTime() + { + EierlegendeWollmilchSau ews; + Long id; + + TimeConversions.setZoneIdStrategy(new FixedZoneIdStrategy(defaultZone)); + ews = new EierlegendeWollmilchSau(); + ews.zdt = zdt; + id = persist(ews); + ews = retrieve(id); + assertEquals(zdt, ews.zdt); + + TimeConversions.setZoneIdStrategy(new FixedZoneIdStrategy(otherZone)); + ews = new EierlegendeWollmilchSau(); + ews.zdt = zdt; + id = persist(ews); + ews = retrieve(id); + assertEquals(zdt.toInstant(), ews.zdt.toInstant()); + } + + + private Long persist(EierlegendeWollmilchSau ews) + { + EntityTransaction tx = em.getTransaction(); + tx.begin(); + em.persist(ews); + log.info("persisting {}", ews); + tx.commit(); + em.clear(); + return ews.id; + } + + private EierlegendeWollmilchSau retrieve(Long id) + { + EierlegendeWollmilchSau ews; + EntityTransaction tx = em.getTransaction(); + tx.begin(); + ews = em.find(EierlegendeWollmilchSau.class, id); + tx.rollback(); + log.info("retrieved {}", ews); + return ews; + } +} diff --git a/src/test/java/de/juplo/jpa/converters/OffsetDateTimeConverterTest.java b/src/test/java/de/juplo/jpa/converters/OffsetDateTimeConverterTest.java new file mode 100644 index 0000000..36cb219 --- /dev/null +++ b/src/test/java/de/juplo/jpa/converters/OffsetDateTimeConverterTest.java @@ -0,0 +1,57 @@ +package de.juplo.jpa.converters; + +import de.juplo.jpa.converters.TimeConversions.FixedZoneOffsetStrategy; +import java.sql.Timestamp; +import java.time.OffsetDateTime; +import java.time.ZoneId; +import java.time.ZoneOffset; +import static org.junit.Assert.assertEquals; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * + * @author Kai Moritz + */ +public class OffsetDateTimeConverterTest +{ + Logger log = LoggerFactory.getLogger(OffsetDateTimeConverterTest.class); + + OffsetDateTimeConverter converter = new OffsetDateTimeConverter(); + OffsetDateTime odt = OffsetDateTime.now(); + ZoneOffset defaultOffset = ZoneId.systemDefault().getRules().getOffset(odt.toLocalDateTime()); + ZoneOffset otherOffset = ZoneOffset.ofTotalSeconds(defaultOffset.getTotalSeconds() + 3 * 60 * 60); + + + @Test + public void testConvertToDatabaseColumn() + { + Timestamp ts; + + TimeConversions.setZoneOffsetStrategy(new FixedZoneOffsetStrategy(defaultOffset)); + ts = converter.convertToDatabaseColumn(odt); + log.info("{}: {} -> {}", defaultOffset, odt, ts); + assertEquals(odt.toInstant(), ts.toInstant()); + + TimeConversions.setZoneOffsetStrategy(new FixedZoneOffsetStrategy(otherOffset)); + ts = converter.convertToDatabaseColumn(odt); + log.info("{}: {} -> {}", otherOffset, odt, ts); + assertEquals(odt.toInstant(), OffsetDateTime.of(ts.toLocalDateTime(), otherOffset).toInstant()); + } + + @Test + public void testRoundtrip() + { + Timestamp ts; + + TimeConversions.setZoneOffsetStrategy(new FixedZoneOffsetStrategy(defaultOffset)); + ts = converter.convertToDatabaseColumn(odt); + assertEquals(odt.toInstant(), converter.convertToEntityAttribute(ts).toInstant()); + + TimeConversions.setZoneOffsetStrategy(new FixedZoneOffsetStrategy(otherOffset)); + ts = converter.convertToDatabaseColumn(odt); + assertEquals(odt.toInstant(), converter.convertToEntityAttribute(ts).toInstant()); + } +} diff --git a/src/test/resources/META-INF/persistence.xml b/src/test/resources/META-INF/persistence.xml new file mode 100644 index 0000000..75ee68d --- /dev/null +++ b/src/test/resources/META-INF/persistence.xml @@ -0,0 +1,20 @@ + + + + + de.juplo.jpa.converters.OffsetDateTime + de.juplo.jpa.converters.ZonedDateTime + + + + + + + + + + + diff --git a/src/test/resources/log4j.properties b/src/test/resources/log4j.properties new file mode 100644 index 0000000..60ccba1 --- /dev/null +++ b/src/test/resources/log4j.properties @@ -0,0 +1,12 @@ +log4j.rootLogger=INFO,stdout + +log4j.logger.de.juplo=TRACE + +log4j.logger.org.hibernate=WARN +log4j.logger.org.hibernate.SQL=DEBUG +log4j.logger.org.hibernate.type=TRACE +log4j.logger.org.hibernate.type.BasicTypeRegistry=ERROR + +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c:%L - %m%n