1 package de.juplo.jpa.converters;
3 import de.juplo.jpa.converters.TimeConversions.FixedZoneOffsetStrategy;
4 import java.sql.Timestamp;
5 import java.time.OffsetDateTime;
6 import java.time.ZoneId;
7 import java.time.ZoneOffset;
8 import static org.junit.Assert.assertEquals;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
18 public class OffsetDateTimeConverterTest
20 Logger log = LoggerFactory.getLogger(OffsetDateTimeConverterTest.class);
22 OffsetDateTimeConverter converter = new OffsetDateTimeConverter();
23 OffsetDateTime odt = OffsetDateTime.now();
24 ZoneOffset defaultOffset = ZoneId.systemDefault().getRules().getOffset(odt.toLocalDateTime());
25 ZoneOffset otherOffset = ZoneOffset.ofTotalSeconds(defaultOffset.getTotalSeconds() + 3 * 60 * 60);
29 public void testConvertToDatabaseColumn()
33 TimeConversions.setZoneOffsetStrategy(new FixedZoneOffsetStrategy(defaultOffset));
34 ts = converter.convertToDatabaseColumn(odt);
35 log.info("{}: {} -> {}", defaultOffset, odt, ts);
36 assertEquals(odt.toInstant(), ts.toInstant());
38 TimeConversions.setZoneOffsetStrategy(new FixedZoneOffsetStrategy(otherOffset));
39 ts = converter.convertToDatabaseColumn(odt);
40 log.info("{}: {} -> {}", otherOffset, odt, ts);
41 assertEquals(odt.toInstant(), OffsetDateTime.of(ts.toLocalDateTime(), otherOffset).toInstant());
45 public void testRoundtrip()
49 TimeConversions.setZoneOffsetStrategy(new FixedZoneOffsetStrategy(defaultOffset));
50 ts = converter.convertToDatabaseColumn(odt);
51 assertEquals(odt.toInstant(), converter.convertToEntityAttribute(ts).toInstant());
53 TimeConversions.setZoneOffsetStrategy(new FixedZoneOffsetStrategy(otherOffset));
54 ts = converter.convertToDatabaseColumn(odt);
55 assertEquals(odt.toInstant(), converter.convertToEntityAttribute(ts).toInstant());