blob: 111c67159ba57e5242075c403c1872d5cc94356c [file] [log] [blame]
Tatu Salorantaa63c2032011-12-24 00:26:53 -08001package com.fasterxml.jackson.databind.ser;
2
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 Salorantaa63c2032011-12-24 00:26:53 -080029 /*
Tatu Salorantaf3bb3422012-03-20 16:20:01 -070030 /**********************************************************
31 /* Tests
32 /**********************************************************
Tatu Salorantaa63c2032011-12-24 00:26:53 -080033 */
34
Tatu Saloranta7a695712016-05-03 21:52:42 -070035 private final ObjectMapper MAPPER = new ObjectMapper();
36
Tatu Salorantaa63c2032011-12-24 00:26:53 -080037 public void testSimple() throws Exception
38 {
Tatu Salorantaa63c2032011-12-24 00:26:53 -080039 String TEST = "test exception";
Tatu Saloranta7a695712016-05-03 21:52:42 -070040 Map<String,Object> result = writeAndMap(MAPPER, new Exception(TEST));
Tatu Salorantaf3bb3422012-03-20 16:20:01 -070041 // JDK 7 has introduced a new property 'suppressed' to Throwable
42 Object ob = result.get("suppressed");
43 if (ob != null) {
44 assertEquals(5, result.size());
45 } else {
46 assertEquals(4, result.size());
47 }
48
Tatu Salorantaa63c2032011-12-24 00:26:53 -080049 assertEquals(TEST, result.get("message"));
50 assertNull(result.get("cause"));
51 assertEquals(TEST, result.get("localizedMessage"));
52
53 // hmmh. what should we get for stack traces?
54 Object traces = result.get("stackTrace");
55 if (!(traces instanceof List<?>)) {
56 fail("Expected a List for exception member 'stackTrace', got: "+traces);
57 }
58 }
Tatu Saloranta7a695712016-05-03 21:52:42 -070059
60 // for [databind#877]
Tatu Saloranta9b7ffec2016-05-03 22:13:13 -070061 @SuppressWarnings("unchecked")
Tatu Saloranta7a695712016-05-03 21:52:42 -070062 public void testIgnorals() throws Exception
63 {
Tatu Saloranta9b7ffec2016-05-03 22:13:13 -070064 ExceptionWithIgnoral input = new ExceptionWithIgnoral("foobar");
65 input.initCause(new IOException("surprise!"));
66
Tatu Saloranta7a695712016-05-03 21:52:42 -070067 // First, should ignore anything with class annotations
68 String json = MAPPER
Tatu Saloranta9b7ffec2016-05-03 22:13:13 -070069 .writerWithDefaultPrettyPrinter()
70 .writeValueAsString(input);
Tatu Saloranta7a695712016-05-03 21:52:42 -070071
Tatu Saloranta7a695712016-05-03 21:52:42 -070072 Map<String,Object> result = MAPPER.readValue(json, Map.class);
73 assertEquals("foobar", result.get("message"));
74
75 assertNull(result.get("bogus1"));
76 assertNotNull(result.get("bogus2"));
77
78 // and then also remova second property with config overrides
79 ObjectMapper mapper = new ObjectMapper();
80 mapper.configOverride(ExceptionWithIgnoral.class)
81 .setIgnorals(JsonIgnoreProperties.Value.forIgnoredProperties("bogus2"));
Tatu Saloranta9b7ffec2016-05-03 22:13:13 -070082 String json2 = mapper
83 .writeValueAsString(new ExceptionWithIgnoral("foobar"));
84
85 Map<String,Object> result2 = mapper.readValue(json2, Map.class);
86 assertNull(result2.get("bogus1"));
87 assertNull(result2.get("bogus2"));
88
89 // and try to deserialize as well
90 ExceptionWithIgnoral output = mapper.readValue(json2, ExceptionWithIgnoral.class);
91 assertNotNull(output);
92 assertEquals("foobar", output.getMessage());
Tatu Saloranta7a695712016-05-03 21:52:42 -070093 }
Tatu Salorantaa63c2032011-12-24 00:26:53 -080094}