|
| 1 | +package com.fasterxml.jackson.datatype.jsr310.deser.key; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 4 | + |
| 5 | +import org.junit.Test; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 8 | +import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; |
| 9 | + |
| 10 | +import java.time.ZonedDateTime; |
| 11 | +import java.util.Map; |
| 12 | + |
| 13 | +import static org.junit.Assert.assertEquals; |
| 14 | +import static org.junit.Assume.*; |
| 15 | + |
| 16 | +// for [modules-java8#306] |
| 17 | +public class ZonedDateTimeKeyDeserializerTest |
| 18 | + extends ModuleTestBase |
| 19 | +{ |
| 20 | + private final ObjectMapper MAPPER = newMapper(); |
| 21 | + private final TypeReference<Map<ZonedDateTime, String>> MAP_TYPE_REF |
| 22 | + = new TypeReference<Map<ZonedDateTime, String>>() {}; |
| 23 | + |
| 24 | + @Test |
| 25 | + public void Instant_style_can_be_deserialized() throws Exception { |
| 26 | + Map<ZonedDateTime, String> map = MAPPER.readValue(getMap("2015-07-24T12:23:34.184Z"), |
| 27 | + MAP_TYPE_REF); |
| 28 | + Map.Entry<ZonedDateTime, String> entry = map.entrySet().iterator().next(); |
| 29 | + assertEquals("2015-07-24T12:23:34.184Z", entry.getKey().toString()); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void ZonedDateTime_with_zone_name_can_be_deserialized() throws Exception { |
| 34 | + Map<ZonedDateTime, String> map = MAPPER.readValue(getMap("2015-07-24T12:23:34.184Z[UTC]"), |
| 35 | + MAP_TYPE_REF); |
| 36 | + Map.Entry<ZonedDateTime, String> entry = map.entrySet().iterator().next(); |
| 37 | + assertEquals("2015-07-24T12:23:34.184Z[UTC]", entry.getKey().toString()); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void ZonedDateTime_with_place_name_can_be_deserialized() throws Exception { |
| 42 | + assumeFalse(System.getProperty("java.version").startsWith("1.8")); |
| 43 | + |
| 44 | + Map<ZonedDateTime, String> map = MAPPER.readValue(getMap("2015-07-24T12:23:34.184Z[Europe/London]"), |
| 45 | + MAP_TYPE_REF); |
| 46 | + Map.Entry<ZonedDateTime, String> entry = map.entrySet().iterator().next(); |
| 47 | + assertEquals("2015-07-24T13:23:34.184+01:00[Europe/London]", entry.getKey().toString()); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void ZonedDateTime_with_place_name_can_be_deserialized_Java_8() throws Exception { |
| 52 | + // Java 8 parses this format incorrectly due to https://bugs.openjdk.org/browse/JDK-8066982 |
| 53 | + assumeTrue(System.getProperty("java.version").startsWith("1.8")); |
| 54 | + |
| 55 | + Map<ZonedDateTime, String> map = MAPPER.readValue(getMap("2015-07-24T12:23:34.184Z[Europe/London]"), |
| 56 | + MAP_TYPE_REF); |
| 57 | + Map.Entry<ZonedDateTime, String> entry = map.entrySet().iterator().next(); |
| 58 | + assertEquals("2015-07-24T12:23:34.184+01:00[Europe/London]", entry.getKey().toString()); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void ZonedDateTime_with_offset_can_be_deserialized() throws Exception { |
| 63 | + Map<ZonedDateTime, String> map = MAPPER.readValue(getMap("2015-07-24T12:23:34.184+02:00"), |
| 64 | + MAP_TYPE_REF); |
| 65 | + Map.Entry<ZonedDateTime, String> entry = map.entrySet().iterator().next(); |
| 66 | + assertEquals("2015-07-24T12:23:34.184+02:00", entry.getKey().toString()); |
| 67 | + } |
| 68 | + |
| 69 | + private static String getMap(String input) { |
| 70 | + return "{\"" + input + "\": \"This is a string\"}"; |
| 71 | + } |
| 72 | +} |
0 commit comments