Skip to content

Commit

Permalink
Add a simple test to verify that case-insensitive handling works for …
Browse files Browse the repository at this point in the history
…regular cases (wrt #273)
  • Loading branch information
cowtowncoder committed Dec 6, 2017
1 parent 8de0cad commit b54627d
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;

public abstract class XmlTestBase
Expand Down Expand Up @@ -182,13 +183,17 @@ protected XmlTestBase() {
super();
}

protected static ObjectMapper newObjectMapper() {
return new XmlMapper();
}

protected XmlMapper xmlMapper(boolean useListWrapping)
{
JacksonXmlModule module = new JacksonXmlModule();
module.setDefaultUseWrapper(useListWrapping);
return new XmlMapper(module);
}

/*
/**********************************************************
/* Additional assertion methods
Expand Down Expand Up @@ -246,7 +251,8 @@ protected void verifyException(Throwable e, String... matches)
return;
}
}
fail("Expected an exception with one of substrings ("+Arrays.asList(matches)+"): got one with message \""+msg+"\"");
fail("Expected an exception with one of substrings ("+Arrays.asList(matches)+"): got one ("+
e.getClass().getName()+") with message \""+msg+"\"");
}

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.fasterxml.jackson.dataformat.xml.deser;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;

public class CaseInsensitiveDeserTest extends XmlTestBase
{
// [databind#1036]
static class BaseResponse {
public int errorCode;
public String debugMessage;
}

// [databind#1438]
static class InsensitiveCreator
{
int v;

@JsonCreator
public InsensitiveCreator(@JsonProperty("value") int v0) {
v = v0;
}
}

/*
/********************************************************
/* Test methods
/********************************************************
*/

private final ObjectMapper MAPPER = newObjectMapper();

private final ObjectMapper INSENSITIVE_MAPPER = newObjectMapper();
{
INSENSITIVE_MAPPER.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES);

}

// [databind#1036]
public void testCaseInsensitive1036() throws Exception
{
final String DOC =
"<BaseResponse><ErrorCode>2</ErrorCode><DebugMessage>Signature not valid!</DebugMessage></BaseResponse>";

// Ok with insensitive
BaseResponse response = INSENSITIVE_MAPPER.readValue(DOC, BaseResponse.class);
assertEquals(2, response.errorCode);
assertEquals("Signature not valid!", response.debugMessage);

// but not without
try {
MAPPER.readValue(DOC, BaseResponse.class);
fail("Should not pass");
} catch (UnrecognizedPropertyException e) {
verifyException(e, "ErrorCode");
}
}

// [databind#1438]
public void testCreatorWithInsensitive() throws Exception
{
final String DOC = aposToQuotes("<root><VALUE>3</VALUE></root>");
InsensitiveCreator bean = INSENSITIVE_MAPPER.readValue(DOC, InsensitiveCreator.class);
assertEquals(3, bean.v);
}
}

0 comments on commit b54627d

Please sign in to comment.