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();
47 public void testOffsetDateTimeXml()
49 em = Persistence.createEntityManagerFactory("xml").createEntityManager();
53 public void testOffsetDateTime()
55 EierlegendeWollmilchSau ews;
58 TimeConversions.setZoneOffsetStrategy(new FixedZoneOffsetStrategy(defaultOffset));
59 ews = new EierlegendeWollmilchSau();
63 assertEquals(odt, ews.odt);
65 TimeConversions.setZoneOffsetStrategy(new FixedZoneOffsetStrategy(otherOffset));
66 ews = new EierlegendeWollmilchSau();
70 assertEquals(odt.toInstant(), ews.odt.toInstant());
75 public void testZonedDateTimeAnnotated()
77 em = Persistence.createEntityManagerFactory("annotated").createEntityManager();
82 public void testZonedDateTimeXml()
84 em = Persistence.createEntityManagerFactory("xml").createEntityManager();
88 public void testZonedDateTime()
90 EierlegendeWollmilchSau ews;
93 TimeConversions.setZoneIdStrategy(new FixedZoneIdStrategy(defaultZone));
94 ews = new EierlegendeWollmilchSau();
98 assertEquals(zdt, ews.zdt);
100 TimeConversions.setZoneIdStrategy(new FixedZoneIdStrategy(otherZone));
101 ews = new EierlegendeWollmilchSau();
105 assertEquals(zdt.toInstant(), ews.zdt.toInstant());
109 private Long persist(EierlegendeWollmilchSau ews)
111 EntityTransaction tx = em.getTransaction();
114 log.info("persisting {}", ews);
120 private EierlegendeWollmilchSau retrieve(Long id)
122 EierlegendeWollmilchSau ews;
123 EntityTransaction tx = em.getTransaction();
125 ews = em.find(EierlegendeWollmilchSau.class, id);
127 log.info("retrieved {}", ews);