|
1 | 1 | package com.fasterxml.jackson.datatype.jsr310.deser;
|
2 | 2 |
|
| 3 | +import com.fasterxml.jackson.annotation.JsonFormat; |
| 4 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 5 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 6 | +import com.fasterxml.jackson.databind.JsonMappingException; |
| 7 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 8 | +import com.fasterxml.jackson.databind.ObjectReader; |
| 9 | +import com.fasterxml.jackson.databind.exc.MismatchedInputException; |
| 10 | +import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration; |
| 11 | +import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; |
| 12 | +import org.junit.Test; |
| 13 | + |
3 | 14 | import java.math.BigInteger;
|
4 | 15 | import java.time.Duration;
|
| 16 | +import java.time.temporal.ChronoUnit; |
5 | 17 | import java.time.temporal.TemporalAmount;
|
6 | 18 | import java.util.Map;
|
7 | 19 |
|
8 |
| -import com.fasterxml.jackson.annotation.JsonFormat; |
9 |
| -import com.fasterxml.jackson.core.type.TypeReference; |
10 |
| -import org.junit.Test; |
11 |
| - |
12 | 20 | import static org.junit.Assert.assertEquals;
|
13 | 21 | import static org.junit.Assert.assertNotNull;
|
14 | 22 | import static org.junit.Assert.assertNull;
|
15 | 23 | import static org.junit.Assert.assertTrue;
|
16 | 24 | import static org.junit.Assert.fail;
|
17 | 25 |
|
18 |
| -import com.fasterxml.jackson.databind.DeserializationFeature; |
19 |
| -import com.fasterxml.jackson.databind.JsonMappingException; |
20 |
| -import com.fasterxml.jackson.databind.ObjectMapper; |
21 |
| -import com.fasterxml.jackson.databind.ObjectReader; |
22 |
| -import com.fasterxml.jackson.databind.exc.MismatchedInputException; |
23 |
| -import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration; |
24 |
| -import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; |
25 |
| - |
26 | 26 | public class DurationDeserTest extends ModuleTestBase
|
27 | 27 | {
|
28 | 28 | private final ObjectReader READER = newMapper().readerFor(Duration.class);
|
29 | 29 |
|
30 | 30 | private final TypeReference<Map<String, Duration>> MAP_TYPE_REF = new TypeReference<Map<String, Duration>>() { };
|
31 | 31 |
|
| 32 | + final static class Wrapper { |
| 33 | + public Duration value; |
| 34 | + |
| 35 | + public Wrapper() { } |
| 36 | + public Wrapper(Duration v) { value = v; } |
| 37 | + } |
| 38 | + |
| 39 | + |
32 | 40 | @Test
|
33 | 41 | public void testDeserializationAsFloat01() throws Exception
|
34 | 42 | {
|
@@ -420,4 +428,140 @@ public void testStrictDeserializeFromEmptyString() throws Exception {
|
420 | 428 | String valueFromEmptyStr = mapper.writeValueAsString(asMap(key, dateValAsEmptyStr));
|
421 | 429 | objectReader.readValue(valueFromEmptyStr);
|
422 | 430 | }
|
| 431 | + |
| 432 | + @Test |
| 433 | + public void shouldDeserializeInNanos_whenNanosUnitAsPattern_andValueIsInteger() throws Exception { |
| 434 | + ObjectMapper mapper = newMapper(); |
| 435 | + mapper.configOverride(Duration.class) |
| 436 | + .setFormat(JsonFormat.Value.forPattern("NANOS")); |
| 437 | + ObjectReader reader = mapper.readerFor(MAP_TYPE_REF); |
| 438 | + |
| 439 | + Wrapper wrapper = reader.readValue(wrapperPayload(25), Wrapper.class); |
| 440 | + |
| 441 | + assertEquals(Duration.ofNanos(25), wrapper.value); |
| 442 | + } |
| 443 | + |
| 444 | + @Test |
| 445 | + public void shouldDeserializeInMicros_whenMicrosUnitAsPattern_andValueIsInteger() throws Exception { |
| 446 | + ObjectMapper mapper = newMapper(); |
| 447 | + mapper.configOverride(Duration.class) |
| 448 | + .setFormat(JsonFormat.Value.forPattern("MICROS")); |
| 449 | + ObjectReader reader = mapper.readerFor(MAP_TYPE_REF); |
| 450 | + |
| 451 | + Wrapper wrapper = reader.readValue(wrapperPayload(25), Wrapper.class); |
| 452 | + |
| 453 | + assertEquals(Duration.of(25, ChronoUnit.MICROS), wrapper.value); |
| 454 | + } |
| 455 | + |
| 456 | + @Test |
| 457 | + public void shouldDeserializeInMillis_whenMillisUnitAsPattern_andValueIsInteger() throws Exception { |
| 458 | + ObjectMapper mapper = newMapper(); |
| 459 | + mapper.configOverride(Duration.class) |
| 460 | + .setFormat(JsonFormat.Value.forPattern("MILLIS")); |
| 461 | + ObjectReader reader = mapper.readerFor(MAP_TYPE_REF); |
| 462 | + |
| 463 | + Wrapper wrapper = reader.readValue(wrapperPayload(25), Wrapper.class); |
| 464 | + |
| 465 | + assertEquals(Duration.ofMillis(25), wrapper.value); |
| 466 | + } |
| 467 | + |
| 468 | + @Test |
| 469 | + public void shouldDeserializeInSeconds_whenSecondsUnitAsPattern_andValueIsInteger() throws Exception { |
| 470 | + ObjectMapper mapper = newMapper(); |
| 471 | + mapper.configOverride(Duration.class) |
| 472 | + .setFormat(JsonFormat.Value.forPattern("SECONDS")); |
| 473 | + ObjectReader reader = mapper.readerFor(MAP_TYPE_REF); |
| 474 | + |
| 475 | + Wrapper wrapper = reader.readValue(wrapperPayload(25), Wrapper.class); |
| 476 | + |
| 477 | + assertEquals(Duration.ofSeconds(25), wrapper.value); |
| 478 | + } |
| 479 | + |
| 480 | + @Test |
| 481 | + public void shouldDeserializeInMinutes_whenMinutesUnitAsPattern_andValueIsInteger() throws Exception { |
| 482 | + ObjectMapper mapper = newMapper(); |
| 483 | + mapper.configOverride(Duration.class) |
| 484 | + .setFormat(JsonFormat.Value.forPattern("MINUTES")); |
| 485 | + ObjectReader reader = mapper.readerFor(MAP_TYPE_REF); |
| 486 | + |
| 487 | + Wrapper wrapper = reader.readValue(wrapperPayload(25), Wrapper.class); |
| 488 | + |
| 489 | + assertEquals(Duration.ofMinutes(25), wrapper.value); |
| 490 | + } |
| 491 | + |
| 492 | + @Test |
| 493 | + public void shouldDeserializeInHours_whenHoursUnitAsPattern_andValueIsInteger() throws Exception { |
| 494 | + ObjectMapper mapper = newMapper(); |
| 495 | + mapper.configOverride(Duration.class) |
| 496 | + .setFormat(JsonFormat.Value.forPattern("HOURS")); |
| 497 | + ObjectReader reader = mapper.readerFor(MAP_TYPE_REF); |
| 498 | + |
| 499 | + Wrapper wrapper = reader.readValue(wrapperPayload(25), Wrapper.class); |
| 500 | + |
| 501 | + assertEquals(Duration.ofHours(25), wrapper.value); |
| 502 | + } |
| 503 | + |
| 504 | + @Test |
| 505 | + public void shouldDeserializeInHalfDays_whenHalfDaysUnitAsPattern_andValueIsInteger() throws Exception { |
| 506 | + ObjectMapper mapper = newMapper(); |
| 507 | + mapper.configOverride(Duration.class) |
| 508 | + .setFormat(JsonFormat.Value.forPattern("HALF_DAYS")); |
| 509 | + ObjectReader reader = mapper.readerFor(MAP_TYPE_REF); |
| 510 | + |
| 511 | + Wrapper wrapper = reader.readValue(wrapperPayload(25), Wrapper.class); |
| 512 | + |
| 513 | + assertEquals(Duration.of(25, ChronoUnit.HALF_DAYS), wrapper.value); |
| 514 | + } |
| 515 | + |
| 516 | + @Test |
| 517 | + public void shouldDeserializeInDays_whenDaysUnitAsPattern_andValueIsInteger() throws Exception { |
| 518 | + ObjectMapper mapper = newMapper(); |
| 519 | + mapper.configOverride(Duration.class) |
| 520 | + .setFormat(JsonFormat.Value.forPattern("DAYS")); |
| 521 | + ObjectReader reader = mapper.readerFor(MAP_TYPE_REF); |
| 522 | + |
| 523 | + Wrapper wrapper = reader.readValue(wrapperPayload(25), Wrapper.class); |
| 524 | + |
| 525 | + assertEquals(Duration.ofDays(25), wrapper.value); |
| 526 | + } |
| 527 | + |
| 528 | + @Test |
| 529 | + public void shouldIgnoreUnitPattern_whenValueIsFloat() throws Exception { |
| 530 | + ObjectMapper mapper = newMapper(); |
| 531 | + mapper.configOverride(Duration.class) |
| 532 | + .setFormat(JsonFormat.Value.forPattern("MINUTES")); |
| 533 | + ObjectReader reader = mapper.readerFor(MAP_TYPE_REF); |
| 534 | + |
| 535 | + Wrapper wrapper = reader.readValue(wrapperPayload(25.5), Wrapper.class); |
| 536 | + |
| 537 | + assertEquals(Duration.parse("PT25.5S"), wrapper.value); |
| 538 | + } |
| 539 | + |
| 540 | + @Test |
| 541 | + public void shouldIgnoreUnitPattern_whenValueIsString() throws Exception { |
| 542 | + ObjectMapper mapper = newMapper(); |
| 543 | + mapper.configOverride(Duration.class) |
| 544 | + .setFormat(JsonFormat.Value.forPattern("MINUTES")); |
| 545 | + ObjectReader reader = mapper.readerFor(MAP_TYPE_REF); |
| 546 | + |
| 547 | + Wrapper wrapper = reader.readValue("{\"value\":\"PT25S\"}", Wrapper.class); |
| 548 | + |
| 549 | + assertEquals(Duration.parse("PT25S"), wrapper.value); |
| 550 | + } |
| 551 | + |
| 552 | + @Test |
| 553 | + public void shouldIgnoreUnitPattern_whenUnitPatternDoesNotMatchExactly() throws Exception { |
| 554 | + ObjectMapper mapper = newMapper(); |
| 555 | + mapper.configOverride(Duration.class) |
| 556 | + .setFormat(JsonFormat.Value.forPattern("Nanos")); |
| 557 | + ObjectReader reader = mapper.readerFor(MAP_TYPE_REF); |
| 558 | + |
| 559 | + Wrapper wrapper = reader.readValue(wrapperPayload(25), Wrapper.class); |
| 560 | + |
| 561 | + assertEquals(Duration.ofSeconds(25), wrapper.value); |
| 562 | + } |
| 563 | + |
| 564 | + private String wrapperPayload(Number number) { |
| 565 | + return "{\"value\":" + number + "}"; |
| 566 | + } |
423 | 567 | }
|
0 commit comments