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