|
| 1 | +package com.fasterxml.jackson.datatype.jsr310.failing; |
| 2 | + |
| 3 | +import java.time.Duration; |
| 4 | + |
| 5 | +import org.junit.Test; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.databind.*; |
| 8 | + |
| 9 | +import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; |
| 10 | + |
| 11 | +import static org.junit.Assert.*; |
| 12 | + |
| 13 | +public class DurationDeser337Test extends ModuleTestBase |
| 14 | +{ |
| 15 | + @Test |
| 16 | + public void test_default() throws Exception { |
| 17 | + ObjectMapper mapper = newMapper(); |
| 18 | + |
| 19 | + Duration duration = Duration.parse("PT-43.636S"); |
| 20 | + |
| 21 | + String ser = mapper.writeValueAsString(duration); |
| 22 | + |
| 23 | + assertEquals("-43.636000000", ser); |
| 24 | + |
| 25 | + Duration deser = mapper.readValue(ser, Duration.class); |
| 26 | + |
| 27 | + assertEquals(duration, deser); |
| 28 | + assertEquals(deser.toString(), "PT-43.636S"); |
| 29 | + } |
| 30 | + |
| 31 | + // Handling with WRITE_DURATIONS_AS_TIMESTAMPS enabled can't round-trip a value |
| 32 | + @Test |
| 33 | + public void test_with() throws Exception { |
| 34 | + ObjectMapper mapper = mapperBuilder() |
| 35 | + .configure(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS, true) |
| 36 | + .build(); |
| 37 | + |
| 38 | + Duration duration = Duration.parse("PT-43.636S"); |
| 39 | + |
| 40 | + String ser = mapper.writeValueAsString(duration); |
| 41 | + |
| 42 | + assertEquals("-43.636000000", ser); |
| 43 | + |
| 44 | + Duration deser = mapper.readValue(ser, Duration.class); |
| 45 | + |
| 46 | + assertEquals(duration, deser); |
| 47 | + assertEquals(deser.toString(), "PT-43.636S"); |
| 48 | + } |
| 49 | + |
| 50 | + // Handling with WRITE_DURATIONS_AS_TIMESTAMPS disabled works |
| 51 | + @Test |
| 52 | + public void test_without() throws Exception { |
| 53 | + ObjectMapper mapper = mapperBuilder() |
| 54 | + .configure(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS, false) |
| 55 | + .build(); |
| 56 | + |
| 57 | + Duration duration = Duration.parse("PT-43.636S"); |
| 58 | + |
| 59 | + String ser = mapper.writeValueAsString(duration); |
| 60 | + assertEquals(q("PT-43.636S"), ser); |
| 61 | + |
| 62 | + Duration deser = mapper.readValue(ser, Duration.class); |
| 63 | + assertEquals(duration, deser); |
| 64 | + } |
| 65 | +} |
0 commit comments