|
| 1 | +package com.fasterxml.jackson.dataformat.xml.deser; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 4 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 5 | + |
| 6 | +import com.fasterxml.jackson.databind.*; |
| 7 | +import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; |
| 8 | +import com.fasterxml.jackson.dataformat.xml.XmlMapper; |
| 9 | +import com.fasterxml.jackson.dataformat.xml.XmlTestBase; |
| 10 | + |
| 11 | +public class CaseInsensitiveDeserTest extends XmlTestBase |
| 12 | +{ |
| 13 | + // [databind#1036] |
| 14 | + static class BaseResponse { |
| 15 | + public int errorCode; |
| 16 | + public String debugMessage; |
| 17 | + } |
| 18 | + |
| 19 | + // [databind#1438] |
| 20 | + static class InsensitiveCreator |
| 21 | + { |
| 22 | + int v; |
| 23 | + |
| 24 | + @JsonCreator |
| 25 | + public InsensitiveCreator(@JsonProperty("value") int v0) { |
| 26 | + v = v0; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + /* |
| 31 | + /******************************************************** |
| 32 | + /* Test methods |
| 33 | + /******************************************************** |
| 34 | + */ |
| 35 | + |
| 36 | + private final ObjectMapper MAPPER = newObjectMapper(); |
| 37 | + |
| 38 | + private final ObjectMapper INSENSITIVE_MAPPER = newObjectMapper(); |
| 39 | + { |
| 40 | + INSENSITIVE_MAPPER.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES); |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + // [databind#1036] |
| 45 | + public void testCaseInsensitive1036() throws Exception |
| 46 | + { |
| 47 | + final String DOC = |
| 48 | +"<BaseResponse><ErrorCode>2</ErrorCode><DebugMessage>Signature not valid!</DebugMessage></BaseResponse>"; |
| 49 | + |
| 50 | + // Ok with insensitive |
| 51 | + BaseResponse response = INSENSITIVE_MAPPER.readValue(DOC, BaseResponse.class); |
| 52 | + assertEquals(2, response.errorCode); |
| 53 | + assertEquals("Signature not valid!", response.debugMessage); |
| 54 | + |
| 55 | + // but not without |
| 56 | + try { |
| 57 | + MAPPER.readValue(DOC, BaseResponse.class); |
| 58 | + fail("Should not pass"); |
| 59 | + } catch (UnrecognizedPropertyException e) { |
| 60 | + verifyException(e, "ErrorCode"); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // [databind#1438] |
| 65 | + public void testCreatorWithInsensitive() throws Exception |
| 66 | + { |
| 67 | + final String DOC = aposToQuotes("<root><VALUE>3</VALUE></root>"); |
| 68 | + InsensitiveCreator bean = INSENSITIVE_MAPPER.readValue(DOC, InsensitiveCreator.class); |
| 69 | + assertEquals(3, bean.v); |
| 70 | + } |
| 71 | +} |
0 commit comments