|
11 | 11 | import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
12 | 12 | import com.fasterxml.jackson.databind.deser.std.FromStringDeserializer;
|
13 | 13 | import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
| 14 | +import com.fasterxml.jackson.databind.exc.InvalidFormatException; |
14 | 15 | import com.fasterxml.jackson.databind.module.SimpleModule;
|
15 | 16 |
|
16 | 17 | @SuppressWarnings("serial")
|
@@ -510,4 +511,56 @@ public void testExceptionFromCustomEnumKeyDeserializer() {
|
510 | 511 | assertTrue(e.getMessage().contains("Undefined AnEnum"));
|
511 | 512 | }
|
512 | 513 | }
|
| 514 | + |
| 515 | + public void testFailWhenCaseSensitiveAndNameIsNotUpperCase() throws IOException { |
| 516 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 517 | + |
| 518 | + try { |
| 519 | + objectMapper.readValue("\"Jackson\"", TestEnum.class); |
| 520 | + fail("InvalidFormatException expected"); |
| 521 | + } catch (InvalidFormatException e) { |
| 522 | + assertTrue(e.getMessage().contains("value not one of declared Enum instance names: [JACKSON, OK, RULES]")); |
| 523 | + } |
| 524 | + } |
| 525 | + |
| 526 | + public void testFailWhenCaseSensitiveAndToStringIsUpperCase() throws IOException { |
| 527 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 528 | + objectMapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING); |
| 529 | + |
| 530 | + try { |
| 531 | + objectMapper.readValue("\"A\"", LowerCaseEnum.class); |
| 532 | + fail("InvalidFormatException expected"); |
| 533 | + } catch (InvalidFormatException e) { |
| 534 | + assertTrue(e.getMessage().contains("value not one of declared Enum instance names: [a, b, c]")); |
| 535 | + } |
| 536 | + } |
| 537 | + |
| 538 | + public void testEnumDesIgnoringCaseWithLowerCaseContent() throws IOException { |
| 539 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 540 | + objectMapper.enable(DeserializationFeature.READ_ENUMS_IGNORING_CASE); |
| 541 | + |
| 542 | + TestEnum testEnum = objectMapper.readValue("\"jackson\"", TestEnum.class); |
| 543 | + |
| 544 | + assertEquals(TestEnum.JACKSON, testEnum); |
| 545 | + } |
| 546 | + |
| 547 | + public void testEnumDesIgnoringCaseWithUpperCaseToString() throws IOException { |
| 548 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 549 | + objectMapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING); |
| 550 | + objectMapper.enable(DeserializationFeature.READ_ENUMS_IGNORING_CASE); |
| 551 | + |
| 552 | + LowerCaseEnum lowerCaseEnum = objectMapper.readValue("\"A\"", LowerCaseEnum.class); |
| 553 | + |
| 554 | + assertEquals(LowerCaseEnum.A, lowerCaseEnum); |
| 555 | + } |
| 556 | + |
| 557 | + public void testIgnoreCaseInEnumList() throws Exception { |
| 558 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 559 | + objectMapper.enable(DeserializationFeature.READ_ENUMS_IGNORING_CASE); |
| 560 | + |
| 561 | + TestEnum[] testEnum = objectMapper.readValue("[\"jackson\", \"rules\"]", TestEnum[].class); |
| 562 | + |
| 563 | + assertEquals(TestEnum.JACKSON, testEnum[0]); |
| 564 | + assertEquals(TestEnum.RULES, testEnum[1]); |
| 565 | + } |
513 | 566 | }
|
0 commit comments