|
| 1 | +package com.fasterxml.jackson.datatype.jsr310.failing; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import com.fasterxml.jackson.databind.SerializationFeature; |
| 6 | +import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; |
| 7 | +import org.junit.Test; |
| 8 | + |
| 9 | +import java.time.ZoneId; |
| 10 | +import java.time.ZoneOffset; |
| 11 | +import java.time.ZonedDateTime; |
| 12 | + |
| 13 | +import static org.junit.Assert.assertEquals; |
| 14 | + |
| 15 | +/** |
| 16 | + * Test case for https://github.com/FasterXML/jackson-modules-java8/issues/244 |
| 17 | + */ |
| 18 | +public class ZonedDateTimeIssue244Test extends ModuleTestBase |
| 19 | +{ |
| 20 | + private final ObjectMapper MAPPER = newMapper() |
| 21 | + .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); |
| 22 | + |
| 23 | + @Test |
| 24 | + public void zoneIdUTC() throws JsonProcessingException |
| 25 | + { |
| 26 | + assertSerializeAndSerialize(ZonedDateTime.now(ZoneId.of("UTC"))); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void zoneOffsetUTC() throws JsonProcessingException |
| 31 | + { |
| 32 | + assertSerializeAndSerialize(ZonedDateTime.now(ZoneOffset.UTC)); // fails! |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void zoneOffsetNonUTC() throws JsonProcessingException |
| 37 | + { |
| 38 | + assertSerializeAndSerialize(ZonedDateTime.now(ZoneOffset.ofHours(-7))); // fails! |
| 39 | + } |
| 40 | + |
| 41 | + private void assertSerializeAndSerialize(final ZonedDateTime date) throws JsonProcessingException |
| 42 | + { |
| 43 | + final Example example1 = new Example(date); |
| 44 | + final String json = MAPPER.writeValueAsString(example1); |
| 45 | + final Example example2 = MAPPER.readValue(json, Example.class); |
| 46 | + |
| 47 | + assertEquals(example1.getDate(), example2.getDate()); |
| 48 | + } |
| 49 | + |
| 50 | + static class Example |
| 51 | + { |
| 52 | + private ZonedDateTime date; |
| 53 | + |
| 54 | + public Example() |
| 55 | + { |
| 56 | + } |
| 57 | + |
| 58 | + public Example(final ZonedDateTime date) |
| 59 | + { |
| 60 | + this.date = date; |
| 61 | + } |
| 62 | + |
| 63 | + public ZonedDateTime getDate() |
| 64 | + { |
| 65 | + return date; |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments