Skip to content

Commit

Permalink
Test coverage: Add a dedicated test covering the serialization and de…
Browse files Browse the repository at this point in the history
…serialization of Throwable.getCause()
  • Loading branch information
klaasdellschaft committed Jun 18, 2021
1 parent c5fd40f commit 9b57715
Showing 1 changed file with 32 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ static class MyException extends Exception

protected String myMessage;
protected HashMap<String,Object> stuff = new HashMap<String, Object>();

@JsonCreator
MyException(@JsonProperty("message") String msg, @JsonProperty("value") int v)
{
Expand All @@ -30,7 +30,7 @@ static class MyException extends Exception
}

public int getValue() { return value; }

public String getFoo() { return "bar"; }

@JsonAnySetter public void setter(String key, Object value)
Expand All @@ -52,7 +52,7 @@ static class MyNoArgException extends Exception
*/

private final ObjectMapper MAPPER = new ObjectMapper();

public void testIOException() throws IOException
{
IOException ioe = new IOException("TEST");
Expand Down Expand Up @@ -96,9 +96,10 @@ public void testJDK7SuppressionProperty() throws IOException
Exception exc = MAPPER.readValue("{\"suppressed\":[]}", IOException.class);
assertNotNull(exc);
}

// [databind#381]
public void testSingleValueArrayDeserialization() throws Exception {
public void testSingleValueArrayDeserialization() throws Exception
{
final ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
final IOException exp;
Expand All @@ -108,13 +109,31 @@ public void testSingleValueArrayDeserialization() throws Exception {
exp = internal;
}
final String value = "[" + mapper.writeValueAsString(exp) + "]";

final IOException cloned = mapper.readValue(value, IOException.class);
assertEquals(exp.getMessage(), cloned.getMessage());

assertEquals(exp.getStackTrace().length, cloned.getStackTrace().length);
for (int i = 0; i < exp.getStackTrace().length; i ++) {
_assertEquality(i, exp.getStackTrace()[i], cloned.getStackTrace()[i]);
assertEquals(exp.getMessage(), cloned.getMessage());

_assertEquality(exp.getStackTrace(), cloned.getStackTrace());
}

public void testExceptionCauseDeserialization() throws IOException
{
ObjectMapper mapper = new ObjectMapper();

final IOException exp = new IOException("the outer exception", new Throwable("the cause"));

final String value = mapper.writeValueAsString(exp);
final IOException act = mapper.readValue(value, IOException.class);

assertNotNull(act.getCause());
assertEquals(exp.getCause().getMessage(), act.getCause().getMessage());
_assertEquality(exp.getCause().getStackTrace(), act.getCause().getStackTrace());
}

private void _assertEquality(StackTraceElement[] exp, StackTraceElement[] act) {
assertEquals(exp.length, act.length);
for (int i = 0; i < exp.length; i++) {
_assertEquality(i, exp[i], act[i]);
}
}

Expand Down Expand Up @@ -145,15 +164,15 @@ protected void _assertEquality(int ix, String prop,
public void testSingleValueArrayDeserializationException() throws Exception {
final ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);

final IOException exp;
try {
throw new IOException("testing");
} catch (IOException internal) {
exp = internal;
}
final String value = "[" + mapper.writeValueAsString(exp) + "]";

try {
mapper.readValue(value, IOException.class);
fail("Exception not thrown when attempting to deserialize an IOException wrapped in a single value array with UNWRAP_SINGLE_VALUE_ARRAYS disabled");
Expand Down

0 comments on commit 9b57715

Please sign in to comment.