-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bit more work on #1313 to also support
@JsonFormat
way of per-prope…
…rty allowance (or not) of case-insensitivity
- Loading branch information
1 parent
15d4fe2
commit 5285e4a
Showing
8 changed files
with
223 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
src/test/java/com/fasterxml/jackson/databind/deser/jdk/EnumAltIdTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package com.fasterxml.jackson.databind.deser.jdk; | ||
|
||
import java.io.IOException; | ||
import java.util.EnumSet; | ||
|
||
import com.fasterxml.jackson.annotation.JsonFormat; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.BaseMapTest; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.ObjectReader; | ||
import com.fasterxml.jackson.databind.exc.InvalidFormatException; | ||
|
||
public class EnumAltIdTest extends BaseMapTest | ||
{ | ||
// [databind#1313] | ||
|
||
enum TestEnum { JACKSON, RULES, OK; } | ||
protected enum LowerCaseEnum { | ||
A, B, C; | ||
private LowerCaseEnum() { } | ||
@Override | ||
public String toString() { return name().toLowerCase(); } | ||
} | ||
|
||
protected static class EnumBean { | ||
@JsonFormat(with={ JsonFormat.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES }) | ||
public TestEnum value; | ||
} | ||
|
||
protected static class StrictCaseBean { | ||
@JsonFormat(without={ JsonFormat.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES }) | ||
public TestEnum value; | ||
} | ||
|
||
/* | ||
/********************************************************** | ||
/* Test methods, basic | ||
/********************************************************** | ||
*/ | ||
|
||
protected final ObjectMapper MAPPER = new ObjectMapper(); | ||
|
||
protected final ObjectReader READER_DEFAULT = MAPPER.reader(); | ||
protected final ObjectReader READER_IGNORE_CASE = MAPPER | ||
.reader(DeserializationFeature.READ_ENUMS_IGNORING_CASE); | ||
|
||
// Tests for [databind#1313], case-insensitive | ||
|
||
public void testFailWhenCaseSensitiveAndNameIsNotUpperCase() throws IOException { | ||
try { | ||
READER_DEFAULT.forType(TestEnum.class).readValue("\"Jackson\""); | ||
fail("InvalidFormatException expected"); | ||
} catch (InvalidFormatException e) { | ||
verifyException(e, "value not one of declared Enum instance names: [JACKSON, OK, RULES]"); | ||
} | ||
} | ||
|
||
public void testFailWhenCaseSensitiveAndToStringIsUpperCase() throws IOException { | ||
ObjectReader r = READER_DEFAULT.forType(LowerCaseEnum.class) | ||
.with(DeserializationFeature.READ_ENUMS_USING_TO_STRING); | ||
try { | ||
r.readValue("\"A\""); | ||
fail("InvalidFormatException expected"); | ||
} catch (InvalidFormatException e) { | ||
verifyException(e, "value not one of declared Enum instance names: [a, b, c]"); | ||
} | ||
} | ||
|
||
public void testEnumDesIgnoringCaseWithLowerCaseContent() throws IOException { | ||
assertEquals(TestEnum.JACKSON, | ||
READER_IGNORE_CASE.forType(TestEnum.class).readValue(quote("jackson"))); | ||
} | ||
|
||
public void testEnumDesIgnoringCaseWithUpperCaseToString() throws IOException { | ||
ObjectReader r = MAPPER.readerFor(LowerCaseEnum.class) | ||
.with(DeserializationFeature.READ_ENUMS_USING_TO_STRING, | ||
DeserializationFeature.READ_ENUMS_IGNORING_CASE); | ||
assertEquals(LowerCaseEnum.A, r.readValue("\"A\"")); | ||
} | ||
|
||
/* | ||
/********************************************************** | ||
/* Test methods, containers | ||
/********************************************************** | ||
*/ | ||
|
||
public void testIgnoreCaseInEnumList() throws Exception { | ||
TestEnum[] enums = READER_IGNORE_CASE.forType(TestEnum[].class) | ||
.readValue("[\"jackson\", \"rules\"]"); | ||
|
||
assertEquals(2, enums.length); | ||
assertEquals(TestEnum.JACKSON, enums[0]); | ||
assertEquals(TestEnum.RULES, enums[1]); | ||
} | ||
|
||
public void testIgnoreCaseInEnumSet() throws IOException { | ||
ObjectReader r = READER_IGNORE_CASE.forType(new TypeReference<EnumSet<TestEnum>>() { }); | ||
EnumSet<TestEnum> set = r.readValue("[\"jackson\"]"); | ||
assertEquals(1, set.size()); | ||
assertTrue(set.contains(TestEnum.JACKSON)); | ||
} | ||
|
||
/* | ||
/********************************************************** | ||
/* Test methods, property overrides | ||
/********************************************************** | ||
*/ | ||
|
||
public void testIgnoreCaseViaFormat() throws Exception | ||
{ | ||
final String JSON = aposToQuotes("{'value':'ok'}"); | ||
|
||
// should be able to allow on per-case basis: | ||
EnumBean pojo = READER_DEFAULT.forType(EnumBean.class) | ||
.readValue(JSON); | ||
assertEquals(TestEnum.OK, pojo.value); | ||
|
||
// including disabling acceptance | ||
try { | ||
READER_DEFAULT.forType(StrictCaseBean.class) | ||
.readValue(JSON); | ||
fail("Should not pass"); | ||
} catch (InvalidFormatException e) { | ||
verifyException(e, "value not one of declared Enum instance names: [JACKSON, OK, RULES]"); | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...n/databind/deser/EnumDefaultReadTest.java → ...tabind/deser/jdk/EnumDefaultReadTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.