|
| 1 | +package com.fasterxml.jackson.datatype.jsr310.deser; |
| 2 | + |
| 3 | +import java.time.Month; |
| 4 | +import java.time.temporal.TemporalAccessor; |
| 5 | + |
| 6 | +import org.junit.Test; |
| 7 | +import org.junit.function.ThrowingRunnable; |
| 8 | + |
| 9 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 10 | +import com.fasterxml.jackson.databind.ObjectReader; |
| 11 | +import com.fasterxml.jackson.databind.cfg.CoercionAction; |
| 12 | +import com.fasterxml.jackson.databind.cfg.CoercionInputShape; |
| 13 | +import com.fasterxml.jackson.databind.exc.InvalidFormatException; |
| 14 | +import com.fasterxml.jackson.databind.exc.MismatchedInputException; |
| 15 | +import com.fasterxml.jackson.databind.json.JsonMapper; |
| 16 | +import com.fasterxml.jackson.datatype.jsr310.JavaTimeFeature; |
| 17 | +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; |
| 18 | +import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration; |
| 19 | +import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; |
| 20 | + |
| 21 | +import static org.junit.Assert.*; |
| 22 | + |
| 23 | +public class OneBasedMonthDeserTest extends ModuleTestBase |
| 24 | +{ |
| 25 | + static class Wrapper { |
| 26 | + public Month value; |
| 27 | + |
| 28 | + public Wrapper(Month v) { value = v; } |
| 29 | + public Wrapper() { } |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void testDeserializationAsString01_oneBased() throws Exception |
| 34 | + { |
| 35 | + assertEquals(Month.JANUARY, readerForOneBased().readValue("\"01\"")); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testDeserializationAsString01_zeroBased() throws Exception |
| 40 | + { |
| 41 | + assertEquals(Month.FEBRUARY, readerForZeroBased().readValue("\"01\"")); |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testDeserializationAsString02_oneBased() throws Exception |
| 47 | + { |
| 48 | + assertEquals(Month.JANUARY, readerForOneBased().readValue("\"JANUARY\"")); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testDeserializationAsString02_zeroBased() throws Exception |
| 53 | + { |
| 54 | + assertEquals(Month.JANUARY, readerForZeroBased().readValue("\"JANUARY\"")); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testBadDeserializationAsString01_oneBased() { |
| 59 | + assertError( |
| 60 | + () -> readerForOneBased().readValue("\"notamonth\""), |
| 61 | + InvalidFormatException.class, |
| 62 | + "Cannot deserialize value of type `java.time.Month` from String \"notamonth\": not one of the values accepted for Enum class: [OCTOBER, SEPTEMBER, JUNE, MARCH, MAY, APRIL, JULY, JANUARY, FEBRUARY, DECEMBER, AUGUST, NOVEMBER]" |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + static void assertError(ThrowingRunnable codeToRun, Class<? extends Throwable> expectedException, String expectedMessage) { |
| 67 | + try { |
| 68 | + codeToRun.run(); |
| 69 | + fail(String.format("Expecting %s, but nothing was thrown!", expectedException.getName())); |
| 70 | + } catch (Throwable actualException) { |
| 71 | + if (!expectedException.isInstance(actualException)) { |
| 72 | + fail(String.format("Expecting exception of type %s, but %s was thrown instead", expectedException.getName(), actualException.getClass().getName())); |
| 73 | + } |
| 74 | + if (actualException.getMessage() == null || !actualException.getMessage().contains(expectedMessage)) { |
| 75 | + fail(String.format("Expecting exception with message containing:'%s', but the actual error message was:'%s'", expectedMessage, actualException.getMessage())); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testDeserialization01_zeroBased() throws Exception |
| 83 | + { |
| 84 | + assertEquals(Month.FEBRUARY, readerForZeroBased().readValue("1")); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testDeserialization01_oneBased() throws Exception |
| 89 | + { |
| 90 | + assertEquals(Month.JANUARY, readerForOneBased().readValue("1")); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void testDeserialization02_zeroBased() throws Exception |
| 95 | + { |
| 96 | + assertEquals(Month.SEPTEMBER, readerForZeroBased().readValue("\"08\"")); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void testDeserialization02_oneBased() throws Exception |
| 101 | + { |
| 102 | + assertEquals(Month.AUGUST, readerForOneBased().readValue("\"08\"")); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void testDeserializationWithTypeInfo01_oneBased() throws Exception |
| 107 | + { |
| 108 | + ObjectMapper MAPPER = new ObjectMapper() |
| 109 | + .registerModule(new JavaTimeModule().enable(JavaTimeFeature.ONE_BASED_MONTHS)); |
| 110 | + MAPPER.addMixIn(TemporalAccessor.class, MockObjectConfiguration.class); |
| 111 | + |
| 112 | + TemporalAccessor value = MAPPER.readValue("[\"java.time.Month\",11]", TemporalAccessor.class); |
| 113 | + assertEquals(Month.NOVEMBER, value); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void testDeserializationWithTypeInfo01_zeroBased() throws Exception |
| 118 | + { |
| 119 | + ObjectMapper MAPPER = new ObjectMapper(); |
| 120 | + MAPPER.addMixIn(TemporalAccessor.class, MockObjectConfiguration.class); |
| 121 | + |
| 122 | + TemporalAccessor value = MAPPER.readValue("[\"java.time.Month\",\"11\"]", TemporalAccessor.class); |
| 123 | + assertEquals(Month.DECEMBER, value); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testFormatAnnotation_zeroBased() throws Exception |
| 128 | + { |
| 129 | + Wrapper output = readerForZeroBased().readValue("{\"value\":\"11\"}", Wrapper.class); |
| 130 | + assertEquals(new Wrapper(Month.DECEMBER).value, output.value); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void testFormatAnnotation_oneBased() throws Exception |
| 135 | + { |
| 136 | + Wrapper output = readerForOneBased().readValue("{\"value\":\"11\"}", Wrapper.class); |
| 137 | + assertEquals(new Wrapper(Month.NOVEMBER).value, output.value); |
| 138 | + } |
| 139 | + |
| 140 | + /* |
| 141 | + /********************************************************** |
| 142 | + /* Tests for empty string handling |
| 143 | + /********************************************************** |
| 144 | + */ |
| 145 | + |
| 146 | + @Test |
| 147 | + public void testDeserializeFromEmptyString() throws Exception |
| 148 | + { |
| 149 | + final ObjectMapper mapper = newMapper(); |
| 150 | + |
| 151 | + // Nulls are handled in general way, not by deserializer so they are ok |
| 152 | + Month m = mapper.readerFor(Month.class).readValue(" null "); |
| 153 | + assertNull(m); |
| 154 | + |
| 155 | + // But coercion from empty String not enabled for Enums by default: |
| 156 | + try { |
| 157 | + mapper.readerFor(Month.class).readValue("\"\""); |
| 158 | + fail("Should not pass"); |
| 159 | + } catch (MismatchedInputException e) { |
| 160 | + verifyException(e, "Cannot coerce empty String"); |
| 161 | + } |
| 162 | + // But can allow coercion of empty String to, say, null |
| 163 | + ObjectMapper emptyStringMapper = mapperBuilder() |
| 164 | + .withCoercionConfig(Month.class, |
| 165 | + h -> h.setCoercion(CoercionInputShape.EmptyString, CoercionAction.AsNull)) |
| 166 | + .build(); |
| 167 | + m = emptyStringMapper.readerFor(Month.class).readValue("\"\""); |
| 168 | + assertNull(m); |
| 169 | + } |
| 170 | + |
| 171 | + private ObjectReader readerForZeroBased() { |
| 172 | + return JsonMapper.builder() |
| 173 | + .addModule(new JavaTimeModule() |
| 174 | + .disable(JavaTimeFeature.ONE_BASED_MONTHS)) |
| 175 | + .build() |
| 176 | + .readerFor(Month.class); |
| 177 | + } |
| 178 | + |
| 179 | + private ObjectReader readerForOneBased() { |
| 180 | + return JsonMapper.builder() |
| 181 | + .addModule(new JavaTimeModule().enable(JavaTimeFeature.ONE_BASED_MONTHS)) |
| 182 | + .build() |
| 183 | + .readerFor(Month.class); |
| 184 | + } |
| 185 | +} |
0 commit comments