blob: 07fc70a6b33ec922baba8ee47dbec1d9f831c8e4 [file] [log] [blame]
Tatu Salorantaab4f17f2016-10-06 19:27:32 -07001package com.fasterxml.jackson.databind.deser.exc;
Tatu Salorantaa63c2032011-12-24 00:26:53 -08002
Tatu Saloranta9b7ffec2016-05-03 22:13:13 -07003import java.io.IOException;
Tatu Salorantaa63c2032011-12-24 00:26:53 -08004import java.util.*;
5
Tatu Saloranta7a695712016-05-03 21:52:42 -07006import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
Tatu Salorantaa63c2032011-12-24 00:26:53 -08007import com.fasterxml.jackson.databind.*;
8
9/**
10 * Unit tests for verifying that simple exceptions can be serialized.
11 */
12public class TestExceptionSerialization
13 extends BaseMapTest
14{
Tatu Saloranta7a695712016-05-03 21:52:42 -070015 @SuppressWarnings("serial")
16 @JsonIgnoreProperties({ "bogus1" })
17 static class ExceptionWithIgnoral extends RuntimeException
18 {
19 public int bogus1 = 3;
20
21 public int bogus2 = 5;
22
Tatu Saloranta9b7ffec2016-05-03 22:13:13 -070023 protected ExceptionWithIgnoral() { }
Tatu Saloranta7a695712016-05-03 21:52:42 -070024 public ExceptionWithIgnoral(String msg) {
25 super(msg);
26 }
27 }
28
Tatu Salorantaccd72122016-09-12 22:56:46 -070029 // [databind#1368]
30 static class NoSerdeConstructor {
31 private String strVal;
32 public String getVal() { return strVal; }
33 public NoSerdeConstructor( String strVal ) {
34 this.strVal = strVal;
35 }
36 }
37
Tatu Salorantaa63c2032011-12-24 00:26:53 -080038 /*
Tatu Salorantaf3bb3422012-03-20 16:20:01 -070039 /**********************************************************
40 /* Tests
41 /**********************************************************
Tatu Salorantaa63c2032011-12-24 00:26:53 -080042 */
43
Tatu Saloranta7a695712016-05-03 21:52:42 -070044 private final ObjectMapper MAPPER = new ObjectMapper();
45
Tatu Salorantaa63c2032011-12-24 00:26:53 -080046 public void testSimple() throws Exception
47 {
Tatu Salorantaa63c2032011-12-24 00:26:53 -080048 String TEST = "test exception";
Tatu Saloranta7a695712016-05-03 21:52:42 -070049 Map<String,Object> result = writeAndMap(MAPPER, new Exception(TEST));
Tatu Salorantaf3bb3422012-03-20 16:20:01 -070050 // JDK 7 has introduced a new property 'suppressed' to Throwable
51 Object ob = result.get("suppressed");
52 if (ob != null) {
53 assertEquals(5, result.size());
54 } else {
55 assertEquals(4, result.size());
56 }
57
Tatu Salorantaa63c2032011-12-24 00:26:53 -080058 assertEquals(TEST, result.get("message"));
59 assertNull(result.get("cause"));
60 assertEquals(TEST, result.get("localizedMessage"));
61
62 // hmmh. what should we get for stack traces?
63 Object traces = result.get("stackTrace");
64 if (!(traces instanceof List<?>)) {
65 fail("Expected a List for exception member 'stackTrace', got: "+traces);
66 }
67 }
Tatu Saloranta7a695712016-05-03 21:52:42 -070068
69 // for [databind#877]
Tatu Saloranta9b7ffec2016-05-03 22:13:13 -070070 @SuppressWarnings("unchecked")
Tatu Saloranta7a695712016-05-03 21:52:42 -070071 public void testIgnorals() throws Exception
72 {
Tatu Saloranta9b7ffec2016-05-03 22:13:13 -070073 ExceptionWithIgnoral input = new ExceptionWithIgnoral("foobar");
74 input.initCause(new IOException("surprise!"));
75
Tatu Saloranta7a695712016-05-03 21:52:42 -070076 // First, should ignore anything with class annotations
77 String json = MAPPER
Tatu Saloranta9b7ffec2016-05-03 22:13:13 -070078 .writerWithDefaultPrettyPrinter()
79 .writeValueAsString(input);
Tatu Saloranta7a695712016-05-03 21:52:42 -070080
Tatu Saloranta7a695712016-05-03 21:52:42 -070081 Map<String,Object> result = MAPPER.readValue(json, Map.class);
82 assertEquals("foobar", result.get("message"));
83
84 assertNull(result.get("bogus1"));
85 assertNotNull(result.get("bogus2"));
86
87 // and then also remova second property with config overrides
88 ObjectMapper mapper = new ObjectMapper();
89 mapper.configOverride(ExceptionWithIgnoral.class)
90 .setIgnorals(JsonIgnoreProperties.Value.forIgnoredProperties("bogus2"));
Tatu Saloranta9b7ffec2016-05-03 22:13:13 -070091 String json2 = mapper
92 .writeValueAsString(new ExceptionWithIgnoral("foobar"));
93
94 Map<String,Object> result2 = mapper.readValue(json2, Map.class);
95 assertNull(result2.get("bogus1"));
96 assertNull(result2.get("bogus2"));
97
98 // and try to deserialize as well
99 ExceptionWithIgnoral output = mapper.readValue(json2, ExceptionWithIgnoral.class);
100 assertNotNull(output);
101 assertEquals("foobar", output.getMessage());
Tatu Saloranta7a695712016-05-03 21:52:42 -0700102 }
Tatu Salorantaccd72122016-09-12 22:56:46 -0700103
104 // [databind#1368]
105 public void testJsonMappingExceptionSerialization() throws IOException {
106 Exception e = null;
107 // cant deserialize due to unexpected constructor
108 try {
109 MAPPER.readValue( "{ \"val\": \"foo\" }", NoSerdeConstructor.class );
110 fail("Should not pass");
111 } catch (JsonMappingException e0) {
112 verifyException(e0, "no suitable constructor");
113 e = e0;
114 }
115 // but should be able to serialize new exception we got
116 String json = MAPPER.writeValueAsString(e);
117 JsonNode root = MAPPER.readTree(json);
118 String msg = root.path("message").asText();
119 String MATCH = "no suitable constructor";
120 if (!msg.contains(MATCH)) {
121 fail("Exception should contain '"+MATCH+"', does not: '"+msg+"'");
122 }
123 }
Tatu Salorantaa63c2032011-12-24 00:26:53 -0800124}