|
| 1 | +package com.fasterxml.jackson.datatype.jsr310.deser; |
| 2 | + |
| 3 | +import java.time.Instant; |
| 4 | +import java.util.Locale; |
| 5 | + |
| 6 | +import org.junit.Test; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.core.json.JsonReadFeature; |
| 9 | +import com.fasterxml.jackson.databind.ObjectReader; |
| 10 | +import com.fasterxml.jackson.databind.exc.InvalidFormatException; |
| 11 | +import com.fasterxml.jackson.databind.json.JsonMapper; |
| 12 | +import com.fasterxml.jackson.datatype.jsr310.JavaTimeFeature; |
| 13 | +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; |
| 14 | +import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; |
| 15 | + |
| 16 | +import static org.junit.Assert.assertEquals; |
| 17 | +import static org.junit.Assert.assertThrows; |
| 18 | + |
| 19 | +// [modules-java8#291] InstantDeserializer fails to parse negative numeric timestamp strings for |
| 20 | +// pre-1970 values. |
| 21 | +public class InstantDeser291Test |
| 22 | + extends ModuleTestBase |
| 23 | +{ |
| 24 | + private final JsonMapper MAPPER = JsonMapper.builder() |
| 25 | + .defaultLocale(Locale.ENGLISH) |
| 26 | + .addModule(new JavaTimeModule() |
| 27 | + .enable(JavaTimeFeature.ALWAYS_ALLOW_STRINGIFIED_DATE_TIMESTAMPS)) |
| 28 | + .build(); |
| 29 | + private final ObjectReader READER = MAPPER.readerFor(Instant.class); |
| 30 | + private final ObjectReader READER_ALLOW_LEADING_PLUS = READER |
| 31 | + .withFeatures(JsonReadFeature.ALLOW_LEADING_PLUS_SIGN_FOR_NUMBERS); |
| 32 | + |
| 33 | + private static final Instant INSTANT_3_SEC_AFTER_EPOC = Instant.ofEpochSecond(3); |
| 34 | + private static final Instant INSTANT_3_SEC_BEFORE_EPOC = Instant.ofEpochSecond(-3); |
| 35 | + |
| 36 | + private static final String STR_3_SEC = "\"3.000000000\""; |
| 37 | + private static final String STR_POSITIVE_3 = "\"+3.000000000\""; |
| 38 | + private static final String STR_NEGATIVE_3 = "\"-3.000000000\""; |
| 39 | + |
| 40 | + /** |
| 41 | + * Baseline that always succeeds, even before resolution of issue 291 |
| 42 | + * @throws Exception |
| 43 | + */ |
| 44 | + @Test |
| 45 | + public void testNormalNumericalString() throws Exception { |
| 46 | + assertEquals(INSTANT_3_SEC_AFTER_EPOC, READER.readValue(STR_3_SEC)); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Should succeed after issue 291 is resolved. |
| 51 | + * @throws Exception |
| 52 | + */ |
| 53 | + @Test |
| 54 | + public void testNegativeNumericalString() throws Exception { |
| 55 | + assertEquals(INSTANT_3_SEC_BEFORE_EPOC, READER.readValue(STR_NEGATIVE_3)); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * This should always fail since {@link JsonReadFeature#ALLOW_LEADING_PLUS_SIGN_FOR_NUMBERS} is |
| 60 | + * not enabled |
| 61 | + * @throws Exception |
| 62 | + */ |
| 63 | + @Test |
| 64 | + public void testPlusSignNumericalString() throws Exception { |
| 65 | + assertThrows(InvalidFormatException.class, () -> READER.readValue(STR_POSITIVE_3)); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Should succeed after issue 291 is resolved. Scenario where |
| 70 | + * {@link JsonReadFeature#ALLOW_LEADING_PLUS_SIGN_FOR_NUMBERS} is enabled |
| 71 | + * @throws Exception |
| 72 | + */ |
| 73 | + @Test |
| 74 | + public void testAllowedPlusSignNumericalString() throws Exception { |
| 75 | + assertEquals(INSTANT_3_SEC_AFTER_EPOC, READER_ALLOW_LEADING_PLUS.readValue(STR_POSITIVE_3)); |
| 76 | + } |
| 77 | +} |
0 commit comments