1 package de.juplo.jpa.converters;
3 import de.juplo.jpa.converters.TimeConversions.FixedZoneIdStrategy;
4 import de.juplo.jpa.converters.TimeConversions.FixedZoneOffsetStrategy;
5 import java.time.OffsetDateTime;
6 import java.time.ZoneId;
7 import java.time.ZoneOffset;
8 import java.time.ZonedDateTime;
9 import javax.persistence.EntityManager;
10 import javax.persistence.EntityTransaction;
11 import javax.persistence.Persistence;
12 import org.junit.Test;
13 import static org.junit.Assert.assertEquals;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
22 public class JPAConverterTest
24 final static Logger log = LoggerFactory.getLogger(JPAConverterTest.class);
29 ZonedDateTime zdt = ZonedDateTime.now();
30 OffsetDateTime odt = OffsetDateTime.now();
32 ZoneId defaultZone = ZoneId.systemDefault();
33 ZoneId otherZone = defaultZone.getId().equals("America/New_York") ? ZoneId.of("Europe/Berlin") : ZoneId.of("America/New_York");
35 ZoneOffset defaultOffset = defaultZone.getRules().getOffset(odt.toLocalDateTime());
36 ZoneOffset otherOffset = ZoneOffset.ofTotalSeconds(defaultOffset.getTotalSeconds() + 3 * 60 * 60);
40 public void testOffsetDateTimeAnnotated()
42 em = Persistence.createEntityManagerFactory("annotated").createEntityManager();
46 public void testOffsetDateTime()
48 EierlegendeWollmilchSau ews;
51 TimeConversions.setZoneOffsetStrategy(new FixedZoneOffsetStrategy(defaultOffset));
52 ews = new EierlegendeWollmilchSau();
56 assertEquals(odt, ews.odt);
58 TimeConversions.setZoneOffsetStrategy(new FixedZoneOffsetStrategy(otherOffset));
59 ews = new EierlegendeWollmilchSau();
63 assertEquals(odt.toInstant(), ews.odt.toInstant());
67 public void testZonedDateTimeAnnotated()
69 em = Persistence.createEntityManagerFactory("annotated").createEntityManager();
73 public void testZonedDateTime()
75 EierlegendeWollmilchSau ews;
78 TimeConversions.setZoneIdStrategy(new FixedZoneIdStrategy(defaultZone));
79 ews = new EierlegendeWollmilchSau();
83 assertEquals(zdt, ews.zdt);
85 TimeConversions.setZoneIdStrategy(new FixedZoneIdStrategy(otherZone));
86 ews = new EierlegendeWollmilchSau();
90 assertEquals(zdt.toInstant(), ews.zdt.toInstant());
94 private Long persist(EierlegendeWollmilchSau ews)
96 EntityTransaction tx = em.getTransaction();
99 log.info("persisting {}", ews);
105 private EierlegendeWollmilchSau retrieve(Long id)
107 EierlegendeWollmilchSau ews;
108 EntityTransaction tx = em.getTransaction();
110 ews = em.find(EierlegendeWollmilchSau.class, id);
112 log.info("retrieved {}", ews);