From aac5dac5c4759a20b5d9a9dc1a5211dba69f091c Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Wed, 16 Aug 2017 11:08:19 +0200 Subject: [PATCH] Prepared JpaConverterTest for test-cases with other entity-classes --- .../jpa/converters/JPAConverterTest.java | 41 ++++++++++--------- 1 file changed, 21 insertions(+), 20 deletions(-) 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(); + } } } -- 2.20.1