forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCompositeExceptionTest.java
373 lines (299 loc) · 13.6 KB
/
CompositeExceptionTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/**
* Copyright (c) 2016-present, RxJava Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.reactivex.rxjava3.exceptions;
import static org.junit.Assert.*;
import java.io.*;
import java.util.*;
import org.junit.Test;
import io.reactivex.rxjava3.core.RxJavaTest;
public class CompositeExceptionTest extends RxJavaTest {
private final Throwable ex1 = new Throwable("Ex1");
private final Throwable ex2 = new Throwable("Ex2", ex1);
private final Throwable ex3 = new Throwable("Ex3", ex2);
private CompositeException getNewCompositeExceptionWithEx123() {
List<Throwable> throwables = new ArrayList<Throwable>();
throwables.add(ex1);
throwables.add(ex2);
throwables.add(ex3);
return new CompositeException(throwables);
}
@Test
public void multipleWithSameCause() {
Throwable rootCause = new Throwable("RootCause");
Throwable e1 = new Throwable("1", rootCause);
Throwable e2 = new Throwable("2", rootCause);
Throwable e3 = new Throwable("3", rootCause);
CompositeException ce = new CompositeException(e1, e2, e3);
System.err.println("----------------------------- print composite stacktrace");
ce.printStackTrace();
assertEquals(3, ce.getExceptions().size());
assertNoCircularReferences(ce);
assertNotNull(getRootCause(ce));
System.err.println("----------------------------- print cause stacktrace");
ce.getCause().printStackTrace();
}
@Test
public void emptyErrors() {
try {
new CompositeException();
fail("CompositeException should fail if errors is empty");
} catch (IllegalArgumentException e) {
assertEquals("errors is empty", e.getMessage());
}
try {
new CompositeException(new ArrayList<Throwable>());
fail("CompositeException should fail if errors is empty");
} catch (IllegalArgumentException e) {
assertEquals("errors is empty", e.getMessage());
}
}
@Test
public void compositeExceptionFromParentThenChild() {
CompositeException cex = new CompositeException(ex1, ex2);
System.err.println("----------------------------- print composite stacktrace");
cex.printStackTrace();
assertEquals(2, cex.getExceptions().size());
assertNoCircularReferences(cex);
assertNotNull(getRootCause(cex));
System.err.println("----------------------------- print cause stacktrace");
cex.getCause().printStackTrace();
}
@Test
public void compositeExceptionFromChildThenParent() {
CompositeException cex = new CompositeException(ex2, ex1);
System.err.println("----------------------------- print composite stacktrace");
cex.printStackTrace();
assertEquals(2, cex.getExceptions().size());
assertNoCircularReferences(cex);
assertNotNull(getRootCause(cex));
System.err.println("----------------------------- print cause stacktrace");
cex.getCause().printStackTrace();
}
@Test
public void compositeExceptionFromChildAndComposite() {
CompositeException cex = new CompositeException(ex1, getNewCompositeExceptionWithEx123());
System.err.println("----------------------------- print composite stacktrace");
cex.printStackTrace();
assertEquals(3, cex.getExceptions().size());
assertNoCircularReferences(cex);
assertNotNull(getRootCause(cex));
System.err.println("----------------------------- print cause stacktrace");
cex.getCause().printStackTrace();
}
@Test
public void compositeExceptionFromCompositeAndChild() {
CompositeException cex = new CompositeException(getNewCompositeExceptionWithEx123(), ex1);
System.err.println("----------------------------- print composite stacktrace");
cex.printStackTrace();
assertEquals(3, cex.getExceptions().size());
assertNoCircularReferences(cex);
assertNotNull(getRootCause(cex));
System.err.println("----------------------------- print cause stacktrace");
cex.getCause().printStackTrace();
}
@Test
public void compositeExceptionFromTwoDuplicateComposites() {
List<Throwable> exs = new ArrayList<Throwable>();
exs.add(getNewCompositeExceptionWithEx123());
exs.add(getNewCompositeExceptionWithEx123());
CompositeException cex = new CompositeException(exs);
System.err.println("----------------------------- print composite stacktrace");
cex.printStackTrace();
assertEquals(3, cex.getExceptions().size());
assertNoCircularReferences(cex);
assertNotNull(getRootCause(cex));
System.err.println("----------------------------- print cause stacktrace");
cex.getCause().printStackTrace();
}
/**
* This hijacks the Throwable.printStackTrace() output and puts it in a string, where we can look for
* "CIRCULAR REFERENCE" (a String added by Throwable.printEnclosedStackTrace)
*/
private static void assertNoCircularReferences(Throwable ex) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(baos);
ex.printStackTrace(printStream);
assertFalse(baos.toString().contains("CIRCULAR REFERENCE"));
}
private static Throwable getRootCause(Throwable ex) {
Throwable root = ex.getCause();
if (root == null) {
return null;
} else {
while (true) {
if (root.getCause() == null) {
return root;
} else {
root = root.getCause();
}
}
}
}
@Test
public void nullCollection() {
CompositeException composite = new CompositeException((List<Throwable>)null);
composite.getCause();
composite.printStackTrace();
}
@Test
public void nullElement() {
CompositeException composite = new CompositeException(Collections.singletonList((Throwable) null));
composite.getCause();
composite.printStackTrace();
}
@Test
public void compositeExceptionWithUnsupportedInitCause() {
Throwable t = new Throwable() {
private static final long serialVersionUID = -3282577447436848385L;
@Override
public synchronized Throwable initCause(Throwable cause) {
throw new UnsupportedOperationException();
}
};
CompositeException cex = new CompositeException(t, ex1);
System.err.println("----------------------------- print composite stacktrace");
cex.printStackTrace();
assertEquals(2, cex.getExceptions().size());
assertNoCircularReferences(cex);
assertNotNull(getRootCause(cex));
System.err.println("----------------------------- print cause stacktrace");
cex.getCause().printStackTrace();
}
@Test
public void compositeExceptionWithNullInitCause() {
Throwable t = new Throwable("ThrowableWithNullInitCause") {
private static final long serialVersionUID = -7984762607894527888L;
@Override
public synchronized Throwable initCause(Throwable cause) {
return null;
}
};
CompositeException cex = new CompositeException(t, ex1);
System.err.println("----------------------------- print composite stacktrace");
cex.printStackTrace();
assertEquals(2, cex.getExceptions().size());
assertNoCircularReferences(cex);
assertNotNull(getRootCause(cex));
System.err.println("----------------------------- print cause stacktrace");
cex.getCause().printStackTrace();
}
@Test
public void messageCollection() {
CompositeException compositeException = new CompositeException(ex1, ex3);
assertEquals("2 exceptions occurred. ", compositeException.getMessage());
}
@Test
public void messageVarargs() {
CompositeException compositeException = new CompositeException(ex1, ex2, ex3);
assertEquals("3 exceptions occurred. ", compositeException.getMessage());
}
@Test
public void constructorWithNull() {
assertTrue(new CompositeException((Throwable[])null).getExceptions().get(0) instanceof NullPointerException);
assertTrue(new CompositeException((Iterable<Throwable>)null).getExceptions().get(0) instanceof NullPointerException);
assertTrue(new CompositeException(null, new TestException()).getExceptions().get(0) instanceof NullPointerException);
}
@Test
public void printStackTrace() {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
new CompositeException(new TestException()).printStackTrace(pw);
assertTrue(sw.toString().contains("TestException"));
}
@Test
public void badException() {
Throwable e = new BadException();
assertSame(e, new CompositeException(e).getCause().getCause());
assertSame(e, new CompositeException(new RuntimeException(e)).getCause().getCause().getCause());
}
@Test
public void exceptionOverview() {
CompositeException composite = new CompositeException(
new TestException("ex1"),
new TestException("ex2"),
new TestException("ex3", new TestException("ex4"))
);
String overview = composite.getCause().getMessage();
assertTrue(overview, overview.contains("Multiple exceptions (3)"));
assertTrue(overview, overview.contains("io.reactivex.rxjava3.exceptions.TestException: ex1"));
assertTrue(overview, overview.contains("io.reactivex.rxjava3.exceptions.TestException: ex2"));
assertTrue(overview, overview.contains("io.reactivex.rxjava3.exceptions.TestException: ex3"));
assertTrue(overview, overview.contains("io.reactivex.rxjava3.exceptions.TestException: ex4"));
assertTrue(overview, overview.contains("at io.reactivex.rxjava3.exceptions.CompositeExceptionTest.exceptionOverview"));
}
@Test
public void causeWithExceptionWithoutStacktrace() {
CompositeException composite = new CompositeException(
new TestException("ex1"),
new CompositeException.ExceptionOverview("example")
);
String overview = composite.getCause().getMessage();
assertTrue(overview, overview.contains("Multiple exceptions (2)"));
assertTrue(overview, overview.contains("io.reactivex.rxjava3.exceptions.TestException: ex1"));
assertTrue(overview, overview.contains("io.reactivex.rxjava3.exceptions.CompositeException.ExceptionOverview: example"));
assertEquals(overview, 2, overview.split("at\\s").length);
}
@Test
public void reoccurringException() {
TestException ex0 = new TestException("ex0");
TestException ex1 = new TestException("ex1", ex0);
CompositeException composite = new CompositeException(
ex1,
new TestException("ex2", ex1)
);
String overview = composite.getCause().getMessage();
System.err.println(overview);
assertTrue(overview, overview.contains("Multiple exceptions (2)"));
assertTrue(overview, overview.contains("io.reactivex.rxjava3.exceptions.TestException: ex0"));
assertTrue(overview, overview.contains("io.reactivex.rxjava3.exceptions.TestException: ex1"));
assertTrue(overview, overview.contains("io.reactivex.rxjava3.exceptions.TestException: ex2"));
assertTrue(overview, overview.contains("(cause not expanded again) io.reactivex.rxjava3.exceptions.TestException: ex0"));
assertEquals(overview, 5, overview.split("at\\s").length);
}
@Test
public void nestedMultilineMessage() {
TestException ex1 = new TestException("ex1");
TestException ex2 = new TestException("ex2");
CompositeException composite1 = new CompositeException(
ex1,
ex2
);
TestException ex3 = new TestException("ex3");
TestException ex4 = new TestException("ex4", composite1);
CompositeException composite2 = new CompositeException(
ex3,
ex4
);
String overview = composite2.getCause().getMessage();
System.err.println(overview);
assertTrue(overview, overview.contains(" Multiple exceptions (2)"));
assertTrue(overview, overview.contains(" |-- io.reactivex.rxjava3.exceptions.TestException: ex1"));
assertTrue(overview, overview.contains(" |-- io.reactivex.rxjava3.exceptions.TestException: ex2"));
}
@Test
public void singleExceptionIsTheCause() {
TestException ex = new TestException("ex1");
CompositeException composite = new CompositeException(ex);
assertSame(composite.getCause(), ex);
}
}
final class BadException extends Throwable {
private static final long serialVersionUID = 8999507293896399171L;
@Override
public synchronized Throwable getCause() {
return this;
}
}