6
6
import com .fasterxml .jackson .annotation .*;
7
7
8
8
import com .fasterxml .jackson .databind .*;
9
+ import com .fasterxml .jackson .databind .json .JsonMapper ;
10
+ import com .fasterxml .jackson .databind .jsontype .BasicPolymorphicTypeValidator ;
11
+ import com .fasterxml .jackson .databind .jsontype .PolymorphicTypeValidator ;
9
12
10
13
/**
11
14
* Unit tests for verifying that simple exceptions can be deserialized.
@@ -20,7 +23,7 @@ static class MyException extends Exception
20
23
21
24
protected String myMessage ;
22
25
protected HashMap <String ,Object > stuff = new HashMap <String , Object >();
23
-
26
+
24
27
@ JsonCreator
25
28
MyException (@ JsonProperty ("message" ) String msg , @ JsonProperty ("value" ) int v )
26
29
{
@@ -30,7 +33,7 @@ static class MyException extends Exception
30
33
}
31
34
32
35
public int getValue () { return value ; }
33
-
36
+
34
37
public String getFoo () { return "bar" ; }
35
38
36
39
@ JsonAnySetter public void setter (String key , Object value )
@@ -52,7 +55,7 @@ static class MyNoArgException extends Exception
52
55
*/
53
56
54
57
private final ObjectMapper MAPPER = new ObjectMapper ();
55
-
58
+
56
59
public void testIOException () throws IOException
57
60
{
58
61
IOException ioe = new IOException ("TEST" );
@@ -96,9 +99,10 @@ public void testJDK7SuppressionProperty() throws IOException
96
99
Exception exc = MAPPER .readValue ("{\" suppressed\" :[]}" , IOException .class );
97
100
assertNotNull (exc );
98
101
}
99
-
102
+
100
103
// [databind#381]
101
- public void testSingleValueArrayDeserialization () throws Exception {
104
+ public void testSingleValueArrayDeserialization () throws Exception
105
+ {
102
106
final ObjectMapper mapper = new ObjectMapper ();
103
107
mapper .enable (DeserializationFeature .UNWRAP_SINGLE_VALUE_ARRAYS );
104
108
final IOException exp ;
@@ -108,13 +112,72 @@ public void testSingleValueArrayDeserialization() throws Exception {
108
112
exp = internal ;
109
113
}
110
114
final String value = "[" + mapper .writeValueAsString (exp ) + "]" ;
111
-
115
+
112
116
final IOException cloned = mapper .readValue (value , IOException .class );
113
- assertEquals (exp .getMessage (), cloned .getMessage ());
114
-
115
- assertEquals (exp .getStackTrace ().length , cloned .getStackTrace ().length );
116
- for (int i = 0 ; i < exp .getStackTrace ().length ; i ++) {
117
- _assertEquality (i , exp .getStackTrace ()[i ], cloned .getStackTrace ()[i ]);
117
+ assertEquals (exp .getMessage (), cloned .getMessage ());
118
+
119
+ _assertEquality (exp .getStackTrace (), cloned .getStackTrace ());
120
+ }
121
+
122
+ public void testExceptionCauseDeserialization () throws IOException
123
+ {
124
+ ObjectMapper mapper = new ObjectMapper ();
125
+
126
+ final IOException exp = new IOException ("the outer exception" , new Throwable ("the cause" ));
127
+
128
+ final String value = mapper .writeValueAsString (exp );
129
+ final IOException act = mapper .readValue (value , IOException .class );
130
+
131
+ assertNotNull (act .getCause ());
132
+ assertEquals (exp .getCause ().getMessage (), act .getCause ().getMessage ());
133
+ _assertEquality (exp .getCause ().getStackTrace (), act .getCause ().getStackTrace ());
134
+ }
135
+
136
+
137
+ public void testSuppressedGenericThrowableDeserialization () throws IOException
138
+ {
139
+ ObjectMapper mapper = new ObjectMapper ();
140
+
141
+ final IOException exp = new IOException ("the outer exception" );
142
+ exp .addSuppressed (new Throwable ("the suppressed exception" ));
143
+
144
+ final String value = mapper .writeValueAsString (exp );
145
+ final IOException act = mapper .readValue (value , IOException .class );
146
+
147
+ assertNotNull (act .getSuppressed ());
148
+ assertEquals (1 , act .getSuppressed ().length );
149
+ assertEquals (exp .getSuppressed ()[0 ].getMessage (), act .getSuppressed ()[0 ].getMessage ());
150
+ _assertEquality (exp .getSuppressed ()[0 ].getStackTrace (), act .getSuppressed ()[0 ].getStackTrace ());
151
+ }
152
+
153
+ public void testSuppressedTypedExceptionDeserialization () throws IOException
154
+ {
155
+ PolymorphicTypeValidator typeValidator = BasicPolymorphicTypeValidator .builder ()
156
+ .allowIfSubTypeIsArray ()
157
+ .allowIfSubType (Throwable .class )
158
+ .build ();
159
+
160
+ ObjectMapper mapper = JsonMapper .builder ()
161
+ .activateDefaultTyping (typeValidator , ObjectMapper .DefaultTyping .NON_FINAL )
162
+ .build ();
163
+
164
+ final IOException exp = new IOException ("the outer exception" );
165
+ exp .addSuppressed (new IllegalArgumentException ("the suppressed exception" ));
166
+
167
+ final String value = mapper .writeValueAsString (exp );
168
+ final IOException act = mapper .readValue (value , IOException .class );
169
+
170
+ assertNotNull (act .getSuppressed ());
171
+ assertEquals (1 , act .getSuppressed ().length );
172
+ assertEquals (IllegalArgumentException .class , act .getSuppressed ()[0 ].getClass ());
173
+ assertEquals (exp .getSuppressed ()[0 ].getMessage (), act .getSuppressed ()[0 ].getMessage ());
174
+ _assertEquality (exp .getSuppressed ()[0 ].getStackTrace (), act .getSuppressed ()[0 ].getStackTrace ());
175
+ }
176
+
177
+ private void _assertEquality (StackTraceElement [] exp , StackTraceElement [] act ) {
178
+ assertEquals (exp .length , act .length );
179
+ for (int i = 0 ; i < exp .length ; i ++) {
180
+ _assertEquality (i , exp [i ], act [i ]);
118
181
}
119
182
}
120
183
@@ -145,15 +208,15 @@ protected void _assertEquality(int ix, String prop,
145
208
public void testSingleValueArrayDeserializationException () throws Exception {
146
209
final ObjectMapper mapper = new ObjectMapper ();
147
210
mapper .disable (DeserializationFeature .UNWRAP_SINGLE_VALUE_ARRAYS );
148
-
211
+
149
212
final IOException exp ;
150
213
try {
151
214
throw new IOException ("testing" );
152
215
} catch (IOException internal ) {
153
216
exp = internal ;
154
217
}
155
218
final String value = "[" + mapper .writeValueAsString (exp ) + "]" ;
156
-
219
+
157
220
try {
158
221
mapper .readValue (value , IOException .class );
159
222
fail ("Exception not thrown when attempting to deserialize an IOException wrapped in a single value array with UNWRAP_SINGLE_VALUE_ARRAYS disabled" );
0 commit comments