|
| 1 | +package com.fasterxml.jackson.databind.deser.jdk; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.EnumSet; |
| 5 | + |
| 6 | +import com.fasterxml.jackson.annotation.JsonFormat; |
| 7 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 8 | +import com.fasterxml.jackson.databind.BaseMapTest; |
| 9 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 10 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 11 | +import com.fasterxml.jackson.databind.ObjectReader; |
| 12 | +import com.fasterxml.jackson.databind.exc.InvalidFormatException; |
| 13 | + |
| 14 | +public class EnumAltIdTest extends BaseMapTest |
| 15 | +{ |
| 16 | + // [databind#1313] |
| 17 | + |
| 18 | + enum TestEnum { JACKSON, RULES, OK; } |
| 19 | + protected enum LowerCaseEnum { |
| 20 | + A, B, C; |
| 21 | + private LowerCaseEnum() { } |
| 22 | + @Override |
| 23 | + public String toString() { return name().toLowerCase(); } |
| 24 | + } |
| 25 | + |
| 26 | + protected static class EnumBean { |
| 27 | + @JsonFormat(with={ JsonFormat.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES }) |
| 28 | + public TestEnum value; |
| 29 | + } |
| 30 | + |
| 31 | + protected static class StrictCaseBean { |
| 32 | + @JsonFormat(without={ JsonFormat.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES }) |
| 33 | + public TestEnum value; |
| 34 | + } |
| 35 | + |
| 36 | + /* |
| 37 | + /********************************************************** |
| 38 | + /* Test methods, basic |
| 39 | + /********************************************************** |
| 40 | + */ |
| 41 | + |
| 42 | + protected final ObjectMapper MAPPER = new ObjectMapper(); |
| 43 | + |
| 44 | + protected final ObjectReader READER_DEFAULT = MAPPER.reader(); |
| 45 | + protected final ObjectReader READER_IGNORE_CASE = MAPPER |
| 46 | + .reader(DeserializationFeature.READ_ENUMS_IGNORING_CASE); |
| 47 | + |
| 48 | + // Tests for [databind#1313], case-insensitive |
| 49 | + |
| 50 | + public void testFailWhenCaseSensitiveAndNameIsNotUpperCase() throws IOException { |
| 51 | + try { |
| 52 | + READER_DEFAULT.forType(TestEnum.class).readValue("\"Jackson\""); |
| 53 | + fail("InvalidFormatException expected"); |
| 54 | + } catch (InvalidFormatException e) { |
| 55 | + verifyException(e, "value not one of declared Enum instance names: [JACKSON, OK, RULES]"); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public void testFailWhenCaseSensitiveAndToStringIsUpperCase() throws IOException { |
| 60 | + ObjectReader r = READER_DEFAULT.forType(LowerCaseEnum.class) |
| 61 | + .with(DeserializationFeature.READ_ENUMS_USING_TO_STRING); |
| 62 | + try { |
| 63 | + r.readValue("\"A\""); |
| 64 | + fail("InvalidFormatException expected"); |
| 65 | + } catch (InvalidFormatException e) { |
| 66 | + verifyException(e, "value not one of declared Enum instance names: [a, b, c]"); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public void testEnumDesIgnoringCaseWithLowerCaseContent() throws IOException { |
| 71 | + assertEquals(TestEnum.JACKSON, |
| 72 | + READER_IGNORE_CASE.forType(TestEnum.class).readValue(quote("jackson"))); |
| 73 | + } |
| 74 | + |
| 75 | + public void testEnumDesIgnoringCaseWithUpperCaseToString() throws IOException { |
| 76 | + ObjectReader r = MAPPER.readerFor(LowerCaseEnum.class) |
| 77 | + .with(DeserializationFeature.READ_ENUMS_USING_TO_STRING, |
| 78 | + DeserializationFeature.READ_ENUMS_IGNORING_CASE); |
| 79 | + assertEquals(LowerCaseEnum.A, r.readValue("\"A\"")); |
| 80 | + } |
| 81 | + |
| 82 | + /* |
| 83 | + /********************************************************** |
| 84 | + /* Test methods, containers |
| 85 | + /********************************************************** |
| 86 | + */ |
| 87 | + |
| 88 | + public void testIgnoreCaseInEnumList() throws Exception { |
| 89 | + TestEnum[] enums = READER_IGNORE_CASE.forType(TestEnum[].class) |
| 90 | + .readValue("[\"jackson\", \"rules\"]"); |
| 91 | + |
| 92 | + assertEquals(2, enums.length); |
| 93 | + assertEquals(TestEnum.JACKSON, enums[0]); |
| 94 | + assertEquals(TestEnum.RULES, enums[1]); |
| 95 | + } |
| 96 | + |
| 97 | + public void testIgnoreCaseInEnumSet() throws IOException { |
| 98 | + ObjectReader r = READER_IGNORE_CASE.forType(new TypeReference<EnumSet<TestEnum>>() { }); |
| 99 | + EnumSet<TestEnum> set = r.readValue("[\"jackson\"]"); |
| 100 | + assertEquals(1, set.size()); |
| 101 | + assertTrue(set.contains(TestEnum.JACKSON)); |
| 102 | + } |
| 103 | + |
| 104 | + /* |
| 105 | + /********************************************************** |
| 106 | + /* Test methods, property overrides |
| 107 | + /********************************************************** |
| 108 | + */ |
| 109 | + |
| 110 | + public void testIgnoreCaseViaFormat() throws Exception |
| 111 | + { |
| 112 | + final String JSON = aposToQuotes("{'value':'ok'}"); |
| 113 | + |
| 114 | + // should be able to allow on per-case basis: |
| 115 | + EnumBean pojo = READER_DEFAULT.forType(EnumBean.class) |
| 116 | + .readValue(JSON); |
| 117 | + assertEquals(TestEnum.OK, pojo.value); |
| 118 | + |
| 119 | + // including disabling acceptance |
| 120 | + try { |
| 121 | + READER_DEFAULT.forType(StrictCaseBean.class) |
| 122 | + .readValue(JSON); |
| 123 | + fail("Should not pass"); |
| 124 | + } catch (InvalidFormatException e) { |
| 125 | + verifyException(e, "value not one of declared Enum instance names: [JACKSON, OK, RULES]"); |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments