From: Kai Moritz Date: Wed, 16 Aug 2017 09:08:19 +0000 (+0200) Subject: Prepared JpaConverterTest for test-cases with other entity-classes X-Git-Tag: jpa-converters-1.0.0~5 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=aac5dac5c4759a20b5d9a9dc1a5211dba69f091c;p=jpa-converters Prepared JpaConverterTest for test-cases with other entity-classes --- diff --git a/src/test/java/de/juplo/jpa/converters/JPAConverterTest.java b/src/test/java/de/juplo/jpa/converters/JPAConverterTest.java index c9f87cf..130aa7a 100644 --- a/src/test/java/de/juplo/jpa/converters/JPAConverterTest.java +++ b/src/test/java/de/juplo/jpa/converters/JPAConverterTest.java @@ -53,20 +53,19 @@ public class JPAConverterTest public void testOffsetDateTime() { EierlegendeWollmilchSau ews; - Long id; TimeConversions.setZoneOffsetStrategy(new FixedZoneOffsetStrategy(defaultOffset)); ews = new EierlegendeWollmilchSau(); ews.odt = odt; - id = persist(ews); - ews = retrieve(id); + persist(ews); + ews = retrieve(EierlegendeWollmilchSau.class, ews.id); assertEquals(odt, ews.odt); TimeConversions.setZoneOffsetStrategy(new FixedZoneOffsetStrategy(otherOffset)); ews = new EierlegendeWollmilchSau(); ews.odt = odt; - id = persist(ews); - ews = retrieve(id); + persist(ews); + ews = retrieve(EierlegendeWollmilchSau.class, ews.id); assertEquals(odt.toInstant(), ews.odt.toInstant()); } @@ -88,43 +87,45 @@ public class JPAConverterTest public void testZonedDateTime() { EierlegendeWollmilchSau ews; - Long id; TimeConversions.setZoneIdStrategy(new FixedZoneIdStrategy(defaultZone)); ews = new EierlegendeWollmilchSau(); ews.zdt = zdt; - id = persist(ews); - ews = retrieve(id); + persist(ews); + ews = retrieve(EierlegendeWollmilchSau.class, ews.id); assertEquals(zdt, ews.zdt); TimeConversions.setZoneIdStrategy(new FixedZoneIdStrategy(otherZone)); ews = new EierlegendeWollmilchSau(); ews.zdt = zdt; - id = persist(ews); - ews = retrieve(id); + persist(ews); + ews = retrieve(EierlegendeWollmilchSau.class, ews.id); assertEquals(zdt.toInstant(), ews.zdt.toInstant()); } - private Long persist(EierlegendeWollmilchSau ews) + private void persist(Object entity) { EntityTransaction tx = em.getTransaction(); tx.begin(); - em.persist(ews); - log.info("persisting {}", ews); + em.persist(entity); + log.info("persisting {}", entity); tx.commit(); em.clear(); - return ews.id; } - private EierlegendeWollmilchSau retrieve(Long id) + private T retrieve(Class entityClass, Object id) { - EierlegendeWollmilchSau ews; EntityTransaction tx = em.getTransaction(); tx.begin(); - ews = em.find(EierlegendeWollmilchSau.class, id); - tx.rollback(); - log.info("retrieved {}", ews); - return ews; + try + { + log.info("retrieving {} of class {}", id, entityClass); + return em.find(entityClass, id); + } + finally + { + tx.rollback(); + } } }