Tatu Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 1 | package com.fasterxml.jackson.databind.ser; |
| 2 | |
Tatu Saloranta | 9b7ffec | 2016-05-03 22:13:13 -0700 | [diff] [blame^] | 3 | import java.io.IOException; |
Tatu Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 4 | import java.util.*; |
| 5 | |
Tatu Saloranta | 7a69571 | 2016-05-03 21:52:42 -0700 | [diff] [blame] | 6 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
Tatu Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 7 | import com.fasterxml.jackson.databind.*; |
| 8 | |
| 9 | /** |
| 10 | * Unit tests for verifying that simple exceptions can be serialized. |
| 11 | */ |
| 12 | public class TestExceptionSerialization |
| 13 | extends BaseMapTest |
| 14 | { |
Tatu Saloranta | 7a69571 | 2016-05-03 21:52:42 -0700 | [diff] [blame] | 15 | @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 Saloranta | 9b7ffec | 2016-05-03 22:13:13 -0700 | [diff] [blame^] | 23 | protected ExceptionWithIgnoral() { } |
Tatu Saloranta | 7a69571 | 2016-05-03 21:52:42 -0700 | [diff] [blame] | 24 | public ExceptionWithIgnoral(String msg) { |
| 25 | super(msg); |
| 26 | } |
| 27 | } |
| 28 | |
Tatu Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 29 | /* |
Tatu Saloranta | f3bb342 | 2012-03-20 16:20:01 -0700 | [diff] [blame] | 30 | /********************************************************** |
| 31 | /* Tests |
| 32 | /********************************************************** |
Tatu Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 33 | */ |
| 34 | |
Tatu Saloranta | 7a69571 | 2016-05-03 21:52:42 -0700 | [diff] [blame] | 35 | private final ObjectMapper MAPPER = new ObjectMapper(); |
| 36 | |
Tatu Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 37 | public void testSimple() throws Exception |
| 38 | { |
Tatu Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 39 | String TEST = "test exception"; |
Tatu Saloranta | 7a69571 | 2016-05-03 21:52:42 -0700 | [diff] [blame] | 40 | Map<String,Object> result = writeAndMap(MAPPER, new Exception(TEST)); |
Tatu Saloranta | f3bb342 | 2012-03-20 16:20:01 -0700 | [diff] [blame] | 41 | // 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 Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 49 | 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 Saloranta | 7a69571 | 2016-05-03 21:52:42 -0700 | [diff] [blame] | 59 | |
| 60 | // for [databind#877] |
Tatu Saloranta | 9b7ffec | 2016-05-03 22:13:13 -0700 | [diff] [blame^] | 61 | @SuppressWarnings("unchecked") |
Tatu Saloranta | 7a69571 | 2016-05-03 21:52:42 -0700 | [diff] [blame] | 62 | public void testIgnorals() throws Exception |
| 63 | { |
Tatu Saloranta | 9b7ffec | 2016-05-03 22:13:13 -0700 | [diff] [blame^] | 64 | ExceptionWithIgnoral input = new ExceptionWithIgnoral("foobar"); |
| 65 | input.initCause(new IOException("surprise!")); |
| 66 | |
Tatu Saloranta | 7a69571 | 2016-05-03 21:52:42 -0700 | [diff] [blame] | 67 | // First, should ignore anything with class annotations |
| 68 | String json = MAPPER |
Tatu Saloranta | 9b7ffec | 2016-05-03 22:13:13 -0700 | [diff] [blame^] | 69 | .writerWithDefaultPrettyPrinter() |
| 70 | .writeValueAsString(input); |
Tatu Saloranta | 7a69571 | 2016-05-03 21:52:42 -0700 | [diff] [blame] | 71 | |
Tatu Saloranta | 7a69571 | 2016-05-03 21:52:42 -0700 | [diff] [blame] | 72 | 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 Saloranta | 9b7ffec | 2016-05-03 22:13:13 -0700 | [diff] [blame^] | 82 | 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 Saloranta | 7a69571 | 2016-05-03 21:52:42 -0700 | [diff] [blame] | 93 | } |
Tatu Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 94 | } |