|
16 | 16 |
|
17 | 17 | package com.fasterxml.jackson.datatype.jsr310.ser; |
18 | 18 |
|
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertNotNull; |
| 21 | +import static org.junit.Assert.assertTrue; |
| 22 | + |
19 | 23 | import java.time.Instant; |
20 | 24 | import java.time.ZoneId; |
21 | 25 | import java.time.ZonedDateTime; |
|
26 | 30 | import java.util.Locale; |
27 | 31 | import java.util.TimeZone; |
28 | 32 |
|
| 33 | +import org.junit.Test; |
| 34 | + |
29 | 35 | import com.fasterxml.jackson.annotation.JsonFormat; |
30 | 36 | import com.fasterxml.jackson.databind.DeserializationFeature; |
31 | 37 | import com.fasterxml.jackson.databind.ObjectMapper; |
32 | 38 | import com.fasterxml.jackson.databind.ObjectReader; |
33 | 39 | import com.fasterxml.jackson.databind.SerializationFeature; |
| 40 | +import com.fasterxml.jackson.databind.module.SimpleModule; |
34 | 41 | import com.fasterxml.jackson.datatype.jsr310.DecimalUtils; |
35 | 42 | import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration; |
36 | 43 | import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; |
37 | 44 |
|
38 | | -import org.junit.Test; |
39 | | - |
40 | | -import static org.junit.Assert.assertEquals; |
41 | | -import static org.junit.Assert.assertNotNull; |
42 | | -import static org.junit.Assert.assertTrue; |
43 | | - |
44 | 45 | public class ZonedDateTimeSerTest |
45 | 46 | extends ModuleTestBase |
46 | 47 | { |
| 48 | + private static final DateTimeFormatter FORMATTER_WITHOUT_ZONEID = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"); |
| 49 | + |
47 | 50 | private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ISO_OFFSET_DATE_TIME; |
48 | 51 |
|
49 | 52 | private static final ZoneId Z1 = ZoneId.of("America/Chicago"); |
@@ -277,6 +280,70 @@ public void testSerializationAsStringWithZoneIdOn() throws Exception { |
277 | 280 | assertEquals("The value is incorrect.", "\"" + DateTimeFormatter.ISO_ZONED_DATE_TIME.format(date) + "\"", value); |
278 | 281 | } |
279 | 282 |
|
| 283 | + @Test |
| 284 | + public void testSerializationAsStringWithDefaultTimeZoneAndContextTimeZoneOnAndACustomFormatter() throws Exception { |
| 285 | + ZonedDateTime date = ZonedDateTime.now(Z3); |
| 286 | + // With a custom DateTimeFormatter without a ZoneId. |
| 287 | + String value = newMapperBuilder().addModule( |
| 288 | + new SimpleModule().addSerializer(new ZonedDateTimeSerializer(FORMATTER_WITHOUT_ZONEID))) |
| 289 | + .build() |
| 290 | + .writer() |
| 291 | + .with(TimeZone.getTimeZone(Z2)) |
| 292 | + .without(SerializationFeature.WRITE_DATES_WITH_ZONE_ID) |
| 293 | + .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) |
| 294 | + .with(SerializationFeature.WRITE_DATES_WITH_CONTEXT_TIME_ZONE) |
| 295 | + .writeValueAsString(date); |
| 296 | + |
| 297 | + // We expect to have the date written with the datetime of ZoneId Z2 |
| 298 | + assertEquals("The value is incorrect", "\"" + date.withZoneSameInstant(Z2).format(FORMATTER_WITHOUT_ZONEID) + "\"", value); |
| 299 | + } |
| 300 | + |
| 301 | + @Test |
| 302 | + public void testSerializationAsStringWithDefaultTimeZoneAndContextTimeZoneOffAndACustomFormatter() throws Exception { |
| 303 | + ZonedDateTime date = ZonedDateTime.now(Z3); |
| 304 | + // With a custom DateTimeFormatter without a Zone. |
| 305 | + String value = newMapperBuilder().addModule( |
| 306 | + new SimpleModule().addSerializer(new ZonedDateTimeSerializer(FORMATTER_WITHOUT_ZONEID))) |
| 307 | + .build() |
| 308 | + .writer() |
| 309 | + .with(TimeZone.getTimeZone(Z2)) |
| 310 | + .without(SerializationFeature.WRITE_DATES_WITH_ZONE_ID) |
| 311 | + .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) |
| 312 | + .without(SerializationFeature.WRITE_DATES_WITH_CONTEXT_TIME_ZONE) |
| 313 | + .writeValueAsString(date); |
| 314 | + |
| 315 | + // We expect to have the date written with the datetime of ZoneId Z3 |
| 316 | + assertEquals("The value is incorrect", "\"" + date.format(FORMATTER_WITHOUT_ZONEID) + "\"", value); |
| 317 | + } |
| 318 | + |
| 319 | + @Test |
| 320 | + public void testSerializationAsStringWithDefaultTimeZoneAndContextTimeZoneOn() throws Exception { |
| 321 | + ZonedDateTime date = ZonedDateTime.now(Z3); |
| 322 | + String value = MAPPER.writer() |
| 323 | + .with(TimeZone.getTimeZone(Z2)) |
| 324 | + .without(SerializationFeature.WRITE_DATES_WITH_ZONE_ID) |
| 325 | + .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) |
| 326 | + .with(SerializationFeature.WRITE_DATES_WITH_CONTEXT_TIME_ZONE) |
| 327 | + .writeValueAsString(date); |
| 328 | + |
| 329 | + // We expect to have the date written with the ZoneId Z2 |
| 330 | + assertEquals("The value is incorrect", "\"" + DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(date.withZoneSameInstant(Z2)) + "\"", value); |
| 331 | + } |
| 332 | + |
| 333 | + @Test |
| 334 | + public void testSerializationAsStringWithDefaultTimeZoneAndContextTimeZoneOff() throws Exception { |
| 335 | + ZonedDateTime date = ZonedDateTime.now(Z3); |
| 336 | + String value = MAPPER.writer() |
| 337 | + .with(TimeZone.getTimeZone(Z2)) |
| 338 | + .without(SerializationFeature.WRITE_DATES_WITH_ZONE_ID) |
| 339 | + .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) |
| 340 | + .without(SerializationFeature.WRITE_DATES_WITH_CONTEXT_TIME_ZONE) |
| 341 | + .writeValueAsString(date); |
| 342 | + |
| 343 | + // We expect to have the date written with the ZoneId Z3 |
| 344 | + assertEquals("The value is incorrect", "\"" + DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(date) + "\"", value); |
| 345 | + } |
| 346 | + |
280 | 347 | @Test |
281 | 348 | public void testSerializationWithTypeInfo01() throws Exception |
282 | 349 | { |
|
0 commit comments