|
| 1 | +package com.fasterxml.jackson.datatype.joda; |
| 2 | + |
| 3 | +import org.joda.time.DateTime; |
| 4 | +import org.joda.time.DateTimeZone; |
| 5 | + |
| 6 | +import com.fasterxml.jackson.annotation.JsonTypeInfo; |
| 7 | +import com.fasterxml.jackson.databind.*; |
| 8 | + |
| 9 | +// for [datatype-joda#44] |
| 10 | +public class TimeZoneTest extends JodaTestBase |
| 11 | +{ |
| 12 | + // November 3, 2013 at 1:00a is the fall back DST transition that year in much of the US. |
| 13 | + private static final int FALL_BACK_YEAR = 2013; |
| 14 | + |
| 15 | + private static final int FALL_BACK_MONTH = 11; |
| 16 | + |
| 17 | + private static final int FALL_BACK_DAY = 3; |
| 18 | + |
| 19 | + // The first one for America/Los_Angeles happens at 8:00 UTC. |
| 20 | + private static final int FIRST_FALL_BACK_HOUR = 8; |
| 21 | + |
| 22 | + // And the second one happens at 9:00 UTC |
| 23 | + private static final int SECOND_FALL_BACK_HOUR = 9; |
| 24 | + |
| 25 | + private static final DateTimeZone AMERICA_LOS_ANGELES = DateTimeZone.forID("America/Los_Angeles"); |
| 26 | + |
| 27 | + private final DateTime DATE_JAN_1_1970_UTC = new DateTime(0L, DateTimeZone.UTC); |
| 28 | + |
| 29 | + @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.WRAPPER_ARRAY, property = "@class") |
| 30 | + private static interface TypeInfoMixIn { |
| 31 | + } |
| 32 | + |
| 33 | + /* |
| 34 | + /********************************************************** |
| 35 | + /* Test methods |
| 36 | + /********************************************************** |
| 37 | + */ |
| 38 | + |
| 39 | + private final ObjectMapper MAPPER = jodaMapper(); |
| 40 | + |
| 41 | + public void testSimple() throws Exception |
| 42 | + { |
| 43 | + // First, no zone id included |
| 44 | + ObjectWriter w = MAPPER.writer() |
| 45 | + .without(SerializationFeature.WRITE_DATES_WITH_ZONE_ID); |
| 46 | + |
| 47 | + assertEquals("0", |
| 48 | + w.with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) |
| 49 | + .writeValueAsString(DATE_JAN_1_1970_UTC)); |
| 50 | + assertEquals(quote("1970-01-01T00:00:00.000Z"), |
| 51 | + w.without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) |
| 52 | + .writeValueAsString(DATE_JAN_1_1970_UTC)); |
| 53 | + |
| 54 | + // then with zone id |
| 55 | + |
| 56 | + w = w.with(SerializationFeature.WRITE_DATES_WITH_ZONE_ID); |
| 57 | + assertEquals(quote("0[UTC]"), |
| 58 | + w.with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) |
| 59 | + .writeValueAsString(DATE_JAN_1_1970_UTC)); |
| 60 | + assertEquals(quote("1970-01-01T00:00:00.000Z[UTC]"), |
| 61 | + w.without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) |
| 62 | + .writeValueAsString(DATE_JAN_1_1970_UTC)); |
| 63 | + } |
| 64 | + |
| 65 | + public void testRoundTrip() throws Exception |
| 66 | + { |
| 67 | + ObjectWriter w = MAPPER.writer() |
| 68 | + .with(SerializationFeature.WRITE_DATES_WITH_ZONE_ID); |
| 69 | + DateTime input = new DateTime(2014, 8, 24, 5, 17, 45, DateTimeZone.forID("America/Chicago")); // arbitrary |
| 70 | + |
| 71 | + // First as timestamp |
| 72 | + |
| 73 | + String json = w.with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) |
| 74 | + .writeValueAsString(input); |
| 75 | + DateTime result = MAPPER.readValue(json, DateTime.class); |
| 76 | + assertEquals("Actual timepoints differ", input.getMillis(), result.getMillis()); |
| 77 | + assertEquals("TimeZones differ", input, result); |
| 78 | + |
| 79 | + // then as regular tet |
| 80 | + json = w.without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) |
| 81 | + .writeValueAsString(input); |
| 82 | + result = MAPPER.readValue(json, DateTime.class); |
| 83 | + assertEquals("Actual timepoints differ", input.getMillis(), result.getMillis()); |
| 84 | + assertEquals("TimeZones differ", input, result); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Test that de/serializing an ambiguous time (e.g. a 'fall back' DST transition) works and preserves the proper |
| 89 | + * instants in time and time zones. |
| 90 | + */ |
| 91 | + public void testFallBackTransition() throws Exception |
| 92 | + { |
| 93 | + DateTime firstOneAmUtc = new DateTime(FALL_BACK_YEAR, FALL_BACK_MONTH, FALL_BACK_DAY, FIRST_FALL_BACK_HOUR, 0, 0, |
| 94 | + DateTimeZone.UTC); |
| 95 | + DateTime secondOneAmUtc = new DateTime(FALL_BACK_YEAR, FALL_BACK_MONTH, FALL_BACK_DAY, SECOND_FALL_BACK_HOUR, 0, 0, |
| 96 | + DateTimeZone.UTC); |
| 97 | + |
| 98 | + DateTime firstOneAm = new DateTime(firstOneAmUtc, AMERICA_LOS_ANGELES); |
| 99 | + DateTime secondOneAm = new DateTime(secondOneAmUtc, AMERICA_LOS_ANGELES); |
| 100 | + |
| 101 | + ObjectWriter w = MAPPER.writer() |
| 102 | + .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) |
| 103 | + .with(SerializationFeature.WRITE_DATES_WITH_ZONE_ID); |
| 104 | + |
| 105 | + String firstOneAmStr = w.writeValueAsString(firstOneAm); |
| 106 | + String secondOneAmStr = w.writeValueAsString(secondOneAm); |
| 107 | + |
| 108 | + DateTime firstRoundTrip = MAPPER.readValue(firstOneAmStr, DateTime.class); |
| 109 | + DateTime secondRoundTrip = MAPPER.readValue(secondOneAmStr, DateTime.class); |
| 110 | + |
| 111 | + assertEquals("Actual timepoints differ", firstOneAm.getMillis(), firstRoundTrip.getMillis()); |
| 112 | + assertEquals("TimeZones differ", firstOneAm, firstRoundTrip); |
| 113 | + |
| 114 | + assertEquals("Actual timepoints differ", secondOneAm.getMillis(), secondRoundTrip.getMillis()); |
| 115 | + assertEquals("TimeZones differ", secondOneAm, secondRoundTrip); |
| 116 | + } |
| 117 | + |
| 118 | + public void testSerializationWithTypeInfo() throws Exception |
| 119 | + { |
| 120 | + // but if re-configured to include the time zone |
| 121 | + ObjectMapper m = jodaMapper(); |
| 122 | + m.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); |
| 123 | + m.enable(SerializationFeature.WRITE_DATES_WITH_ZONE_ID); |
| 124 | + |
| 125 | + m.addMixIn(DateTime.class, TypeInfoMixIn.class); |
| 126 | + assertEquals("[\"org.joda.time.DateTime\",\"0[UTC]\"]", |
| 127 | + m.writeValueAsString(DATE_JAN_1_1970_UTC)); |
| 128 | + } |
| 129 | +} |
0 commit comments