|
| 1 | +package com.fasterxml.jackson.datatype.jsr310.deser; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonFormat; |
| 4 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 5 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 6 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 7 | +import com.fasterxml.jackson.databind.ObjectReader; |
| 8 | +import com.fasterxml.jackson.databind.exc.MismatchedInputException; |
| 9 | +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; |
| 10 | +import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration; |
| 11 | +import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; |
| 12 | +import org.junit.Test; |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | +import java.time.Month; |
| 16 | +import java.time.format.DateTimeParseException; |
| 17 | +import java.time.temporal.TemporalAccessor; |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +import static org.junit.Assert.*; |
| 21 | + |
| 22 | +public class MonthDeserTest extends ModuleTestBase |
| 23 | +{ |
| 24 | + private final ObjectMapper MAPPER = newMapper(); |
| 25 | + private final ObjectReader READER = MAPPER.readerFor(Month.class); |
| 26 | + private final TypeReference<Map<String, Month>> MAP_TYPE_REF = new TypeReference<Map<String, Month>>() { }; |
| 27 | + |
| 28 | + static class Wrapper { |
| 29 | + @JsonFormat(pattern="MM") |
| 30 | + public Month value; |
| 31 | + |
| 32 | + public Wrapper(Month v) { value = v; } |
| 33 | + public Wrapper() { } |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testDeserializationAsString01() throws Exception |
| 38 | + { |
| 39 | + expectSuccess(Month.of(1), "'01'"); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void testDeserializationAsString02() throws Exception |
| 44 | + { |
| 45 | + expectSuccess(Month.of(1), "'JANUARY'"); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testBadDeserializationAsString01() throws Throwable |
| 50 | + { |
| 51 | + expectFailure("'notamonth'"); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testDeserialization01() throws Exception |
| 56 | + { |
| 57 | + assertEquals("The value is not correct.", Month.of(1), |
| 58 | + MAPPER.readValue("1", Month.class)); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testDeserialization02() throws Exception |
| 63 | + { |
| 64 | + assertEquals("The value is not correct.", Month.of(8), |
| 65 | + MAPPER.readValue("\"08\"", Month.class)); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testDeserializationWithTypeInfo01() throws Exception |
| 70 | + { |
| 71 | + final ObjectMapper mapper = new ObjectMapper() |
| 72 | + .registerModule(new JavaTimeModule()); |
| 73 | + mapper.addMixIn(TemporalAccessor.class, MockObjectConfiguration.class); |
| 74 | + |
| 75 | + Month month = Month.of(11); |
| 76 | + TemporalAccessor value = mapper.readValue("[\"" + Month.class.getName() + "\",\"11\"]", TemporalAccessor.class); |
| 77 | + assertEquals("The value is not correct.", month, value); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void testFormatAnnotation() throws Exception |
| 82 | + { |
| 83 | + final Wrapper input = new Wrapper(Month.of(12)); |
| 84 | + String json = MAPPER.writeValueAsString(input); |
| 85 | + assertEquals("{\"value\":\"12\"}", json); |
| 86 | + |
| 87 | + Wrapper output = MAPPER.readValue(json, Wrapper.class); |
| 88 | + assertEquals(input.value, output.value); |
| 89 | + } |
| 90 | + |
| 91 | + /* |
| 92 | + /********************************************************** |
| 93 | + /* Tests for empty string handling |
| 94 | + /********************************************************** |
| 95 | + */ |
| 96 | + |
| 97 | + // minor changes in 2.12 |
| 98 | + @Test |
| 99 | + public void testDeserializeFromEmptyString() throws Exception |
| 100 | + { |
| 101 | + final String key = "month"; |
| 102 | + |
| 103 | + // First: by default, lenient, so empty String fine |
| 104 | + final ObjectReader objectReader = MAPPER.readerFor(MAP_TYPE_REF); |
| 105 | + |
| 106 | + String doc = MAPPER.writeValueAsString(asMap(key, null)); |
| 107 | + Map<String, Month> actualMapFromNullStr = objectReader.readValue(doc); |
| 108 | + assertNull(actualMapFromNullStr.get(key)); |
| 109 | + |
| 110 | + doc = MAPPER.writeValueAsString(asMap(key, "")); |
| 111 | + assertNotNull(objectReader.readValue(doc)); |
| 112 | + |
| 113 | + // But can make strict: |
| 114 | + final ObjectMapper strictMapper = mapperBuilder().build(); |
| 115 | + strictMapper.configOverride(Month.class) |
| 116 | + .setFormat(JsonFormat.Value.forLeniency(false)); |
| 117 | + doc = strictMapper.writeValueAsString(asMap("date", "")); |
| 118 | + try { |
| 119 | + strictMapper.readerFor(MAP_TYPE_REF) |
| 120 | + .readValue(doc); |
| 121 | + fail("Should not pass"); |
| 122 | + } catch (MismatchedInputException e) { |
| 123 | + verifyException(e, "not allowed because 'strict' mode set for"); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + private void expectFailure(String aposJson) throws Throwable { |
| 128 | + try { |
| 129 | + read(aposJson); |
| 130 | + fail("expected DateTimeParseException"); |
| 131 | + } catch (JsonProcessingException e) { |
| 132 | + if (e.getCause() == null) { |
| 133 | + throw e; |
| 134 | + } |
| 135 | + if (!(e.getCause() instanceof DateTimeParseException)) { |
| 136 | + throw e.getCause(); |
| 137 | + } |
| 138 | + } catch (IOException e) { |
| 139 | + throw e; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private void expectSuccess(Object exp, String aposJson) throws IOException { |
| 144 | + final Month value = read(aposJson); |
| 145 | + notNull(value); |
| 146 | + expect(exp, value); |
| 147 | + } |
| 148 | + |
| 149 | + private Month read(final String aposJson) throws IOException { |
| 150 | + return READER.readValue(a2q(aposJson)); |
| 151 | + } |
| 152 | + |
| 153 | + private static void notNull(Object value) { |
| 154 | + assertNotNull("The value should not be null.", value); |
| 155 | + } |
| 156 | + |
| 157 | + private static void expect(Object exp, Object value) { |
| 158 | + assertEquals("The value is not correct.", exp, value); |
| 159 | + } |
| 160 | +} |
0 commit comments