1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.lang3.exception;
18
19 import static org.apache.commons.lang3.LangAssertions.assertNullPointerException;
20 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.junit.jupiter.api.Assertions.assertFalse;
23 import static org.junit.jupiter.api.Assertions.assertNotEquals;
24 import static org.junit.jupiter.api.Assertions.assertNotNull;
25 import static org.junit.jupiter.api.Assertions.assertNotSame;
26 import static org.junit.jupiter.api.Assertions.assertNull;
27 import static org.junit.jupiter.api.Assertions.assertSame;
28 import static org.junit.jupiter.api.Assertions.assertThrows;
29 import static org.junit.jupiter.api.Assertions.assertTrue;
30 import static org.junit.jupiter.api.Assertions.fail;
31
32 import java.io.ByteArrayOutputStream;
33 import java.io.IOException;
34 import java.io.PrintStream;
35 import java.io.PrintWriter;
36 import java.io.StringWriter;
37 import java.lang.reflect.Constructor;
38 import java.lang.reflect.Modifier;
39 import java.util.ArrayList;
40 import java.util.Arrays;
41 import java.util.List;
42 import java.util.stream.Collectors;
43
44 import org.apache.commons.lang3.AbstractLangTest;
45 import org.apache.commons.lang3.test.NotVisibleExceptionFactory;
46 import org.junit.jupiter.api.AfterEach;
47 import org.junit.jupiter.api.BeforeEach;
48 import org.junit.jupiter.api.Test;
49
50
51
52
53 class ExceptionUtilsTest extends AbstractLangTest {
54
55 private static final class CountingException extends Exception {
56 private static final long serialVersionUID = 1L;
57
58 private int causeCalls;
59
60 CountingException(final Throwable cause) {
61 super(null, cause, false, false);
62 }
63
64 @Override
65 public boolean equals(final Object obj) {
66 throw new AssertionError("Chain walking must not invoke equals");
67 }
68
69 @Override
70 public synchronized Throwable getCause() {
71 causeCalls++;
72 return super.getCause();
73 }
74
75 @Override
76 public int hashCode() {
77 throw new AssertionError("Chain walking must not invoke hashCode");
78 }
79 }
80
81 private static final class EqualException extends Exception {
82 private static final long serialVersionUID = 1L;
83
84 @Override
85 public boolean equals(final Object obj) {
86 return obj instanceof EqualException;
87 }
88
89 @Override
90 public int hashCode() {
91 return 1;
92 }
93 }
94
95
96
97
98
99
100 private static final class ExceptionWithCause extends Exception {
101 private static final long serialVersionUID = 1L;
102
103 private Throwable cause;
104
105 ExceptionWithCause(final String str, final Throwable cause) {
106 super(str);
107 setCause(cause);
108 }
109
110 ExceptionWithCause(final Throwable cause) {
111 setCause(cause);
112 }
113
114 @Override
115 public synchronized Throwable getCause() {
116 return cause;
117 }
118
119 public void setCause(final Throwable cause) {
120 this.cause = cause;
121 }
122 }
123
124
125
126
127
128
129 private static final class ExceptionWithoutCause extends Exception {
130 private static final long serialVersionUID = 1L;
131
132 @SuppressWarnings("unused")
133 public void getTargetException() {
134
135 }
136 }
137
138
139
140 private static final class NestableException extends Exception {
141 private static final long serialVersionUID = 1L;
142
143 @SuppressWarnings("unused")
144 NestableException() {
145 }
146
147 NestableException(final Throwable t) {
148 super(t);
149 }
150 }
151
152 public static class TestThrowable extends Throwable {
153 private static final long serialVersionUID = 1L;
154 }
155
156 private static RuntimeException newRuntimeException(final String message, final Throwable cause) {
157 return new RuntimeException(message, cause);
158 }
159
160 private static int redeclareCheckedException() {
161 return throwsCheckedException();
162 }
163
164 private static int throwsCheckedException() {
165 try {
166 throw new IOException();
167 } catch (final Exception e) {
168 ExceptionUtils.asRuntimeException(e);
169 return -1;
170 }
171 }
172
173 private ExceptionWithCause cyclicCause;
174
175 private Throwable jdkNoCause;
176
177 private NestableException nested;
178
179 private Throwable notVisibleException;
180
181 private Throwable withCause;
182
183 private Throwable withoutCause;
184
185 private Throwable createExceptionWithCause() {
186 try {
187 try {
188 throw new ExceptionWithCause(createExceptionWithoutCause());
189 } catch (final Throwable t) {
190 throw new ExceptionWithCause(t);
191 }
192 } catch (final Throwable t) {
193 return t;
194 }
195 }
196
197 private Throwable createExceptionWithoutCause() {
198 try {
199 throw new ExceptionWithoutCause();
200 } catch (final Throwable t) {
201 return t;
202 }
203 }
204
205 @BeforeEach
206 public void setUp() {
207 withoutCause = createExceptionWithoutCause();
208 nested = new NestableException(withoutCause);
209 withCause = new ExceptionWithCause(nested);
210 jdkNoCause = new NullPointerException();
211 final ExceptionWithCause a = new ExceptionWithCause(null);
212 final ExceptionWithCause b = new ExceptionWithCause(a);
213 a.setCause(b);
214 cyclicCause = new ExceptionWithCause(a);
215 notVisibleException = NotVisibleExceptionFactory.createException(withoutCause);
216 }
217
218 @AfterEach
219 public void tearDown() {
220 withoutCause = null;
221 nested = null;
222 withCause = null;
223 jdkNoCause = null;
224 cyclicCause = null;
225 notVisibleException = null;
226 }
227
228 @Test
229 void test_getMessage_Throwable() {
230 Throwable th = null;
231 assertEquals("", ExceptionUtils.getMessage(th));
232
233 th = new IllegalArgumentException("Base");
234 assertEquals("IllegalArgumentException: Base", ExceptionUtils.getMessage(th));
235
236 th = new ExceptionWithCause("Wrapper", th);
237 assertEquals("ExceptionUtilsTest.ExceptionWithCause: Wrapper", ExceptionUtils.getMessage(th));
238 }
239
240 @Test
241 void test_getRootCauseMessage_Throwable() {
242 Throwable th = null;
243 assertEquals("", ExceptionUtils.getRootCauseMessage(th));
244
245 th = new IllegalArgumentException("Base");
246 assertEquals("IllegalArgumentException: Base", ExceptionUtils.getRootCauseMessage(th));
247
248 th = new ExceptionWithCause("Wrapper", th);
249 assertEquals("IllegalArgumentException: Base", ExceptionUtils.getRootCauseMessage(th));
250 }
251
252 @Test
253 void testAsRuntimeException() {
254 final Exception expected = new InterruptedException();
255 assertSame(expected, assertThrows(Exception.class, () -> ExceptionUtils.asRuntimeException(expected)));
256 assertNotSame(expected, assertThrows(Exception.class, () -> ExceptionUtils.asRuntimeException(new InterruptedException())));
257
258 assertThrows(expected.getClass(), () -> {
259 @SuppressWarnings("unused")
260 final Object retVal = ExceptionUtils.asRuntimeException(expected);
261 });
262
263 assertThrows(expected.getClass(), () -> {
264 @SuppressWarnings("unused")
265 final RuntimeException retVal = ExceptionUtils.asRuntimeException(expected);
266 });
267
268 assertThrows(expected.getClass(), () -> {
269 @SuppressWarnings("unused")
270 final IllegalStateException retVal = ExceptionUtils.asRuntimeException(expected);
271 });
272 }
273
274 @Test
275 void testCatchTechniques() {
276 IOException ioe = assertThrows(IOException.class, ExceptionUtilsTest::throwsCheckedException);
277 assertEquals(1, ExceptionUtils.getThrowableCount(ioe));
278
279 ioe = assertThrows(IOException.class, ExceptionUtilsTest::redeclareCheckedException);
280 assertEquals(1, ExceptionUtils.getThrowableCount(ioe));
281 }
282
283 @Test
284 void testConstructor() {
285 assertNotNull(new ExceptionUtils());
286 final Constructor<?>[] cons = ExceptionUtils.class.getDeclaredConstructors();
287 assertEquals(1, cons.length);
288 assertTrue(Modifier.isPublic(cons[0].getModifiers()));
289 assertTrue(Modifier.isPublic(ExceptionUtils.class.getModifiers()));
290 assertFalse(Modifier.isFinal(ExceptionUtils.class.getModifiers()));
291 }
292
293 @Test
294 void testForEach_jdkNoCause() {
295 final List<Throwable> throwables = new ArrayList<>();
296 ExceptionUtils.forEach(jdkNoCause, throwables::add);
297 assertEquals(1, throwables.size());
298 assertSame(jdkNoCause, throwables.get(0));
299 }
300
301 @Test
302 void testForEach_nested() {
303 final List<Throwable> throwables = new ArrayList<>();
304 ExceptionUtils.forEach(nested, throwables::add);
305 assertEquals(2, throwables.size());
306 assertSame(nested, throwables.get(0));
307 assertSame(withoutCause, throwables.get(1));
308 }
309
310 @Test
311 void testForEach_null() {
312 final List<Throwable> throwables = new ArrayList<>();
313 ExceptionUtils.forEach(null, throwables::add);
314 assertEquals(0, throwables.size());
315 }
316
317 @Test
318 void testForEach_recursiveCause() {
319 final List<Throwable> throwables = new ArrayList<>();
320 ExceptionUtils.forEach(cyclicCause, throwables::add);
321 assertEquals(3, throwables.size());
322 assertSame(cyclicCause, throwables.get(0));
323 assertSame(cyclicCause.getCause(), throwables.get(1));
324 assertSame(cyclicCause.getCause().getCause(), throwables.get(2));
325 }
326
327 @Test
328 void testForEach_withCause() {
329 final List<Throwable> throwables = new ArrayList<>();
330 ExceptionUtils.forEach(withCause, throwables::add);
331 assertEquals(3, throwables.size());
332 assertSame(withCause, throwables.get(0));
333 assertSame(nested, throwables.get(1));
334 assertSame(withoutCause, throwables.get(2));
335 }
336
337 @Test
338 void testForEach_withoutCause() {
339 final List<Throwable> throwables = new ArrayList<>();
340 ExceptionUtils.forEach(withoutCause, throwables::add);
341 assertEquals(1, throwables.size());
342 assertSame(withoutCause, throwables.get(0));
343 }
344
345 @SuppressWarnings("deprecation")
346 @Test
347 void testGetCause_Throwable() {
348 assertSame(null, ExceptionUtils.getCause(null));
349 assertSame(null, ExceptionUtils.getCause(withoutCause));
350 assertSame(withoutCause, ExceptionUtils.getCause(nested));
351 assertSame(nested, ExceptionUtils.getCause(withCause));
352 assertSame(null, ExceptionUtils.getCause(jdkNoCause));
353 assertSame(cyclicCause.getCause(), ExceptionUtils.getCause(cyclicCause));
354 assertSame(cyclicCause.getCause().getCause(), ExceptionUtils.getCause(cyclicCause.getCause()));
355 assertSame(cyclicCause.getCause(), ExceptionUtils.getCause(cyclicCause.getCause().getCause()));
356 assertSame(withoutCause, ExceptionUtils.getCause(notVisibleException));
357 }
358
359 @SuppressWarnings("deprecation")
360 @Test
361 void testGetCause_ThrowableArray() {
362 assertSame(null, ExceptionUtils.getCause(null, null));
363 assertSame(null, ExceptionUtils.getCause(null, new String[0]));
364
365
366 assertSame(nested, ExceptionUtils.getCause(withCause, null));
367 assertSame(null, ExceptionUtils.getCause(withCause, new String[0]));
368 assertSame(null, ExceptionUtils.getCause(withCause, new String[]{null}));
369 assertSame(nested, ExceptionUtils.getCause(withCause, new String[]{"getCause"}));
370
371
372 assertSame(null, ExceptionUtils.getCause(withoutCause, null));
373 assertSame(null, ExceptionUtils.getCause(withoutCause, new String[0]));
374 assertSame(null, ExceptionUtils.getCause(withoutCause, new String[]{null}));
375 assertSame(null, ExceptionUtils.getCause(withoutCause, new String[]{"getCause"}));
376 assertSame(null, ExceptionUtils.getCause(withoutCause, new String[]{"getTargetException"}));
377 }
378
379 @Test
380 void testGetRootCause_Throwable() {
381 assertSame(null, ExceptionUtils.getRootCause(null));
382 assertSame(withoutCause, ExceptionUtils.getRootCause(withoutCause));
383 assertSame(withoutCause, ExceptionUtils.getRootCause(nested));
384 assertSame(withoutCause, ExceptionUtils.getRootCause(withCause));
385 assertSame(jdkNoCause, ExceptionUtils.getRootCause(jdkNoCause));
386 assertSame(cyclicCause.getCause().getCause(), ExceptionUtils.getRootCause(cyclicCause));
387 }
388
389 @Test
390 void testGetRootCauseStackTrace_Throwable() {
391 assertEquals(0, ExceptionUtils.getRootCauseStackTrace(null).length);
392
393 final Throwable cause = createExceptionWithCause();
394 String[] stackTrace = ExceptionUtils.getRootCauseStackTrace(cause);
395 boolean match = false;
396 for (final String element : stackTrace) {
397 if (element.startsWith(ExceptionUtils.WRAPPED_MARKER)) {
398 match = true;
399 break;
400 }
401 }
402 assertTrue(match);
403
404 stackTrace = ExceptionUtils.getRootCauseStackTrace(withoutCause);
405 match = false;
406 for (final String element : stackTrace) {
407 if (element.startsWith(ExceptionUtils.WRAPPED_MARKER)) {
408 match = true;
409 break;
410 }
411 }
412 assertFalse(match);
413 }
414
415 @Test
416 void testGetRootCauseStackTraceList_Throwable() {
417 assertEquals(0, ExceptionUtils.getRootCauseStackTraceList(null).size());
418
419 final Throwable cause = createExceptionWithCause();
420 List<String> stackTrace = ExceptionUtils.getRootCauseStackTraceList(cause);
421 boolean match = false;
422 for (final String element : stackTrace) {
423 if (element.startsWith(ExceptionUtils.WRAPPED_MARKER)) {
424 match = true;
425 break;
426 }
427 }
428 assertTrue(match);
429
430 stackTrace = ExceptionUtils.getRootCauseStackTraceList(withoutCause);
431 match = false;
432 for (final String element : stackTrace) {
433 if (element.startsWith(ExceptionUtils.WRAPPED_MARKER)) {
434 match = true;
435 break;
436 }
437 }
438 assertFalse(match);
439 }
440
441 @Test
442 void testGetRootCauseStackTraceListComparesElements() {
443 final StackTraceElement causeFrame = new StackTraceElement("Type", "call", "Type.java", -1);
444 final StackTraceElement wrapperFrame = new StackTraceElement("Type", "call", "Type.java", -3);
445 assertEquals(causeFrame.toString(), wrapperFrame.toString());
446 assertNotEquals(causeFrame, wrapperFrame);
447 final Throwable cause = new Throwable("cause");
448 final Throwable wrapper = new Throwable("wrapper", cause);
449 cause.setStackTrace(new StackTraceElement[] {causeFrame});
450 wrapper.setStackTrace(new StackTraceElement[] {wrapperFrame});
451 assertEquals(Arrays.asList(cause.toString(), "\tat " + causeFrame, ExceptionUtils.WRAPPED_MARKER + wrapper, "\tat " + wrapperFrame),
452 ExceptionUtils.getRootCauseStackTraceList(wrapper));
453 }
454
455 @Test
456 void testGetRootCauseStackTraceListEmptyFrames() {
457 final Throwable cause = new Throwable("cause");
458 final Throwable wrapper = new Throwable("wrapper", cause);
459 cause.setStackTrace(new StackTraceElement[0]);
460 wrapper.setStackTrace(new StackTraceElement[0]);
461 assertEquals(Arrays.asList(cause.toString(), ExceptionUtils.WRAPPED_MARKER + wrapper),
462 ExceptionUtils.getRootCauseStackTraceList(wrapper));
463 }
464
465 @Test
466 void testGetRootCauseStackTraceListFrameShapedMessage() {
467 final Throwable throwable = new Throwable("denied\n\tat forged.Type.call(Type.java:42)\nmore text");
468 final StackTraceElement frame = new StackTraceElement("real.Type", "call", "Type.java", 7);
469 throwable.setStackTrace(new StackTraceElement[] {frame});
470 assertEquals(Arrays.asList(throwable.toString(), "\tat " + frame), ExceptionUtils.getRootCauseStackTraceList(throwable));
471 }
472
473 @Test
474 void testGetRootCauseStackTraceListIgnoresCustomPrintStackTrace() {
475 final Throwable throwable = new Throwable("message") {
476 private static final long serialVersionUID = 1L;
477
478 @Override
479 public void printStackTrace(final PrintWriter writer) {
480 throw new AssertionError("Must use structured stack frames");
481 }
482 };
483 final StackTraceElement frame = new StackTraceElement("real.Type", "call", null, -2);
484 throwable.setStackTrace(new StackTraceElement[] {frame});
485 assertEquals(Arrays.asList(throwable.toString(), "\tat " + frame), ExceptionUtils.getRootCauseStackTraceList(throwable));
486 }
487
488 @Test
489 void testGetRootCauseStackTraceListSharedSuffix() {
490 final StackTraceElement shared = new StackTraceElement("Shared", "call", "Shared.java", 1);
491 final StackTraceElement causeOnly = new StackTraceElement("Cause", "call", "Cause.java", 2);
492 final StackTraceElement wrapperOnly = new StackTraceElement("Wrapper", "call", "Wrapper.java", 3);
493 final Throwable cause = new Throwable("cause");
494 final Throwable wrapper = new Throwable("wrapper", cause);
495 cause.setStackTrace(new StackTraceElement[] {shared, causeOnly, shared});
496 wrapper.setStackTrace(new StackTraceElement[] {shared, wrapperOnly, shared});
497 assertEquals(Arrays.asList(cause.toString(), "\tat " + shared, "\tat " + causeOnly,
498 ExceptionUtils.WRAPPED_MARKER + wrapper, "\tat " + shared, "\tat " + wrapperOnly, "\tat " + shared),
499 ExceptionUtils.getRootCauseStackTraceList(wrapper));
500 assertEquals(3, cause.getStackTrace().length);
501 assertEquals(3, wrapper.getStackTrace().length);
502 }
503
504
505
506
507
508
509 @Test
510 void testGetRootCauseStackTraceMessageLinesNotMistakenForFrames() {
511 final Throwable t = new IllegalArgumentException(
512 "denied" + System.lineSeparator() + " attack detected" + System.lineSeparator() + "at your request, more text");
513 final String[] stackTrace = ExceptionUtils.getRootCauseStackTrace(t);
514
515 boolean sawRealFrame = false;
516 for (int i = 1; i < stackTrace.length; i++) {
517 final String element = stackTrace[i];
518 if (element.contains("attack detected") || element.contains("at your request")) {
519 fail("message text classified as a stack frame: " + element);
520 }
521 if (element.contains(getClass().getSimpleName())) {
522 sawRealFrame = true;
523 }
524 }
525
526 assertTrue(sawRealFrame, "real frames were suppressed");
527 }
528
529 @Test
530
531 void testgetStackFramesHappyPath() {
532 final String[] actual = ExceptionUtils.getStackFrames(new Throwable() {
533 private static final long serialVersionUID = 1L;
534
535
536 @Override
537 public void printStackTrace(final PrintWriter s) {
538 s.write("org.apache.commons.lang3.exception.ExceptionUtilsTest$1\n" +
539 "\tat org.apache.commons.lang3.exception.ExceptionUtilsTest.testgetStackFramesGappyPath(ExceptionUtilsTest.java:706)\n" +
540 "\tat java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n" +
541 "\tat com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)\n" +
542 "\tat com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)\n");
543 }
544 });
545
546 assertArrayEquals(new String[]{
547 "org.apache.commons.lang3.exception.ExceptionUtilsTest$1",
548 "\tat org.apache.commons.lang3.exception.ExceptionUtilsTest.testgetStackFramesGappyPath(ExceptionUtilsTest.java:706)",
549 "\tat java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)",
550 "\tat com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)",
551 "\tat com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)"
552 }, actual);
553 }
554
555 @Test
556
557 void testgetStackFramesNullArg() {
558 final String[] actual = ExceptionUtils.getStackFrames((Throwable) null);
559 assertEquals(0, actual.length);
560 }
561
562 @Test
563 void testGetThrowableCount_Throwable() {
564 assertEquals(0, ExceptionUtils.getThrowableCount(null));
565 assertEquals(1, ExceptionUtils.getThrowableCount(withoutCause));
566 assertEquals(2, ExceptionUtils.getThrowableCount(nested));
567 assertEquals(3, ExceptionUtils.getThrowableCount(withCause));
568 assertEquals(1, ExceptionUtils.getThrowableCount(jdkNoCause));
569 assertEquals(3, ExceptionUtils.getThrowableCount(cyclicCause));
570 }
571
572 @Test
573 void testGetThrowableList_Throwable_jdkNoCause() {
574 final List<?> throwables = ExceptionUtils.getThrowableList(jdkNoCause);
575 assertEquals(1, throwables.size());
576 assertSame(jdkNoCause, throwables.get(0));
577 }
578
579 @Test
580 void testGetThrowableList_Throwable_nested() {
581 final List<?> throwables = ExceptionUtils.getThrowableList(nested);
582 assertEquals(2, throwables.size());
583 assertSame(nested, throwables.get(0));
584 assertSame(withoutCause, throwables.get(1));
585 }
586
587 @Test
588 void testGetThrowableList_Throwable_null() {
589 final List<?> throwables = ExceptionUtils.getThrowableList(null);
590 assertEquals(0, throwables.size());
591 }
592
593 @Test
594 void testGetThrowableList_Throwable_recursiveCause() {
595 final List<?> throwables = ExceptionUtils.getThrowableList(cyclicCause);
596 assertEquals(3, throwables.size());
597 assertSame(cyclicCause, throwables.get(0));
598 assertSame(cyclicCause.getCause(), throwables.get(1));
599 assertSame(cyclicCause.getCause().getCause(), throwables.get(2));
600 }
601
602 @Test
603 void testGetThrowableList_Throwable_withCause() {
604 final List<?> throwables = ExceptionUtils.getThrowableList(withCause);
605 assertEquals(3, throwables.size());
606 assertSame(withCause, throwables.get(0));
607 assertSame(nested, throwables.get(1));
608 assertSame(withoutCause, throwables.get(2));
609 }
610
611 @Test
612 void testGetThrowableList_Throwable_withoutCause() {
613 final List<?> throwables = ExceptionUtils.getThrowableList(withoutCause);
614 assertEquals(1, throwables.size());
615 assertSame(withoutCause, throwables.get(0));
616 }
617
618 @Test
619 void testGetThrowableListDeepChain() {
620 final CountingException[] chain = new CountingException[10_000];
621 for (int i = chain.length - 1; i >= 0; i--) {
622 chain[i] = new CountingException(i + 1 < chain.length ? chain[i + 1] : null);
623 }
624 final List<Throwable> throwables = ExceptionUtils.getThrowableList(chain[0]);
625 assertEquals(chain.length, throwables.size());
626 for (int i = 0; i < chain.length; i++) {
627 assertSame(chain[i], throwables.get(i));
628 assertEquals(1, chain[i].causeCalls);
629 }
630 assertEquals(chain.length, ExceptionUtils.getThrowableCount(chain[0]));
631 assertSame(chain[chain.length - 1], ExceptionUtils.getRootCause(chain[0]));
632 final Throwable[] array = ExceptionUtils.getThrowables(chain[0]);
633 final Throwable[] stream = ExceptionUtils.stream(chain[0]).toArray(Throwable[]::new);
634 assertEquals(chain.length, array.length);
635 assertEquals(chain.length, stream.length);
636 for (int i = 0; i < chain.length; i++) {
637 assertSame(chain[i], array[i]);
638 assertSame(chain[i], stream[i]);
639 }
640 }
641
642 @Test
643 void testGetThrowableListEqualExceptions() {
644 final EqualException first = new EqualException();
645 final EqualException second = new EqualException();
646 final EqualException third = new EqualException();
647 first.initCause(second);
648 second.initCause(third);
649 final List<Throwable> throwables = ExceptionUtils.getThrowableList(first);
650 assertEquals(3, throwables.size());
651 assertSame(first, throwables.get(0));
652 assertSame(second, throwables.get(1));
653 assertSame(third, throwables.get(2));
654 third.initCause(second);
655 final List<Throwable> cyclic = ExceptionUtils.getThrowableList(first);
656 assertEquals(3, cyclic.size());
657 assertSame(first, cyclic.get(0));
658 assertSame(second, cyclic.get(1));
659 assertSame(third, cyclic.get(2));
660 }
661
662 @Test
663 void testGetThrowableListSelfCause() {
664 final ExceptionWithCause exception = new ExceptionWithCause(null);
665 exception.setCause(exception);
666 final List<Throwable> throwables = ExceptionUtils.getThrowableList(exception);
667 assertEquals(1, throwables.size());
668 assertSame(exception, throwables.get(0));
669 }
670
671 @Test
672 void testGetThrowables_Throwable_jdkNoCause() {
673 final Throwable[] throwables = ExceptionUtils.getThrowables(jdkNoCause);
674 assertEquals(1, throwables.length);
675 assertSame(jdkNoCause, throwables[0]);
676 }
677
678 @Test
679 void testGetThrowables_Throwable_nested() {
680 final Throwable[] throwables = ExceptionUtils.getThrowables(nested);
681 assertEquals(2, throwables.length);
682 assertSame(nested, throwables[0]);
683 assertSame(withoutCause, throwables[1]);
684 }
685
686 @Test
687 void testGetThrowables_Throwable_null() {
688 assertEquals(0, ExceptionUtils.getThrowables(null).length);
689 }
690
691 @Test
692 void testGetThrowables_Throwable_recursiveCause() {
693 final Throwable[] throwables = ExceptionUtils.getThrowables(cyclicCause);
694 assertEquals(3, throwables.length);
695 assertSame(cyclicCause, throwables[0]);
696 assertSame(cyclicCause.getCause(), throwables[1]);
697 assertSame(cyclicCause.getCause().getCause(), throwables[2]);
698 }
699
700 @Test
701 void testGetThrowables_Throwable_withCause() {
702 final Throwable[] throwables = ExceptionUtils.getThrowables(withCause);
703 assertEquals(3, throwables.length);
704 assertSame(withCause, throwables[0]);
705 assertSame(nested, throwables[1]);
706 assertSame(withoutCause, throwables[2]);
707 }
708
709 @Test
710 void testGetThrowables_Throwable_withoutCause() {
711 final Throwable[] throwables = ExceptionUtils.getThrowables(withoutCause);
712 assertEquals(1, throwables.length);
713 assertSame(withoutCause, throwables[0]);
714 }
715
716 @Test
717 void testIndexOf_ThrowableClass() {
718 assertEquals(-1, ExceptionUtils.indexOfThrowable(null, null));
719 assertEquals(-1, ExceptionUtils.indexOfThrowable(null, NestableException.class));
720
721 assertEquals(-1, ExceptionUtils.indexOfThrowable(withoutCause, null));
722 assertEquals(-1, ExceptionUtils.indexOfThrowable(withoutCause, ExceptionWithCause.class));
723 assertEquals(-1, ExceptionUtils.indexOfThrowable(withoutCause, NestableException.class));
724 assertEquals(0, ExceptionUtils.indexOfThrowable(withoutCause, ExceptionWithoutCause.class));
725
726 assertEquals(-1, ExceptionUtils.indexOfThrowable(nested, null));
727 assertEquals(-1, ExceptionUtils.indexOfThrowable(nested, ExceptionWithCause.class));
728 assertEquals(0, ExceptionUtils.indexOfThrowable(nested, NestableException.class));
729 assertEquals(1, ExceptionUtils.indexOfThrowable(nested, ExceptionWithoutCause.class));
730
731 assertEquals(-1, ExceptionUtils.indexOfThrowable(withCause, null));
732 assertEquals(0, ExceptionUtils.indexOfThrowable(withCause, ExceptionWithCause.class));
733 assertEquals(1, ExceptionUtils.indexOfThrowable(withCause, NestableException.class));
734 assertEquals(2, ExceptionUtils.indexOfThrowable(withCause, ExceptionWithoutCause.class));
735
736 assertEquals(-1, ExceptionUtils.indexOfThrowable(withCause, Exception.class));
737 assertEquals(-1, ExceptionUtils.indexOfThrowable(withCause, Throwable.class));
738 }
739
740 @Test
741 void testIndexOf_ThrowableClassInt() {
742 assertEquals(-1, ExceptionUtils.indexOfThrowable(null, null, 0));
743 assertEquals(-1, ExceptionUtils.indexOfThrowable(null, NestableException.class, 0));
744
745 assertEquals(-1, ExceptionUtils.indexOfThrowable(withoutCause, null));
746 assertEquals(-1, ExceptionUtils.indexOfThrowable(withoutCause, ExceptionWithCause.class, 0));
747 assertEquals(-1, ExceptionUtils.indexOfThrowable(withoutCause, NestableException.class, 0));
748 assertEquals(0, ExceptionUtils.indexOfThrowable(withoutCause, ExceptionWithoutCause.class, 0));
749
750 assertEquals(-1, ExceptionUtils.indexOfThrowable(nested, null, 0));
751 assertEquals(-1, ExceptionUtils.indexOfThrowable(nested, ExceptionWithCause.class, 0));
752 assertEquals(0, ExceptionUtils.indexOfThrowable(nested, NestableException.class, 0));
753 assertEquals(1, ExceptionUtils.indexOfThrowable(nested, ExceptionWithoutCause.class, 0));
754
755 assertEquals(-1, ExceptionUtils.indexOfThrowable(withCause, null));
756 assertEquals(0, ExceptionUtils.indexOfThrowable(withCause, ExceptionWithCause.class, 0));
757 assertEquals(1, ExceptionUtils.indexOfThrowable(withCause, NestableException.class, 0));
758 assertEquals(2, ExceptionUtils.indexOfThrowable(withCause, ExceptionWithoutCause.class, 0));
759
760 assertEquals(0, ExceptionUtils.indexOfThrowable(withCause, ExceptionWithCause.class, -1));
761 assertEquals(0, ExceptionUtils.indexOfThrowable(withCause, ExceptionWithCause.class, 0));
762 assertEquals(-1, ExceptionUtils.indexOfThrowable(withCause, ExceptionWithCause.class, 1));
763 assertEquals(-1, ExceptionUtils.indexOfThrowable(withCause, ExceptionWithCause.class, 9));
764
765 assertEquals(-1, ExceptionUtils.indexOfThrowable(withCause, Exception.class, 0));
766 assertEquals(-1, ExceptionUtils.indexOfThrowable(withCause, Throwable.class, 0));
767 }
768
769 @Test
770 void testIndexOfType_ThrowableClass() {
771 assertEquals(-1, ExceptionUtils.indexOfType(null, null));
772 assertEquals(-1, ExceptionUtils.indexOfType(null, NestableException.class));
773
774 assertEquals(-1, ExceptionUtils.indexOfType(withoutCause, null));
775 assertEquals(-1, ExceptionUtils.indexOfType(withoutCause, ExceptionWithCause.class));
776 assertEquals(-1, ExceptionUtils.indexOfType(withoutCause, NestableException.class));
777 assertEquals(0, ExceptionUtils.indexOfType(withoutCause, ExceptionWithoutCause.class));
778
779 assertEquals(-1, ExceptionUtils.indexOfType(nested, null));
780 assertEquals(-1, ExceptionUtils.indexOfType(nested, ExceptionWithCause.class));
781 assertEquals(0, ExceptionUtils.indexOfType(nested, NestableException.class));
782 assertEquals(1, ExceptionUtils.indexOfType(nested, ExceptionWithoutCause.class));
783
784 assertEquals(-1, ExceptionUtils.indexOfType(withCause, null));
785 assertEquals(0, ExceptionUtils.indexOfType(withCause, ExceptionWithCause.class));
786 assertEquals(1, ExceptionUtils.indexOfType(withCause, NestableException.class));
787 assertEquals(2, ExceptionUtils.indexOfType(withCause, ExceptionWithoutCause.class));
788
789 assertEquals(0, ExceptionUtils.indexOfType(withCause, Exception.class));
790 assertEquals(0, ExceptionUtils.indexOfType(withCause, Throwable.class));
791 }
792
793 @Test
794 void testIndexOfType_ThrowableClassInt() {
795 assertEquals(-1, ExceptionUtils.indexOfType(null, null, 0));
796 assertEquals(-1, ExceptionUtils.indexOfType(null, NestableException.class, 0));
797
798 assertEquals(-1, ExceptionUtils.indexOfType(withoutCause, null));
799 assertEquals(-1, ExceptionUtils.indexOfType(withoutCause, ExceptionWithCause.class, 0));
800 assertEquals(-1, ExceptionUtils.indexOfType(withoutCause, NestableException.class, 0));
801 assertEquals(0, ExceptionUtils.indexOfType(withoutCause, ExceptionWithoutCause.class, 0));
802
803 assertEquals(-1, ExceptionUtils.indexOfType(nested, null, 0));
804 assertEquals(-1, ExceptionUtils.indexOfType(nested, ExceptionWithCause.class, 0));
805 assertEquals(0, ExceptionUtils.indexOfType(nested, NestableException.class, 0));
806 assertEquals(1, ExceptionUtils.indexOfType(nested, ExceptionWithoutCause.class, 0));
807
808 assertEquals(-1, ExceptionUtils.indexOfType(withCause, null));
809 assertEquals(0, ExceptionUtils.indexOfType(withCause, ExceptionWithCause.class, 0));
810 assertEquals(1, ExceptionUtils.indexOfType(withCause, NestableException.class, 0));
811 assertEquals(2, ExceptionUtils.indexOfType(withCause, ExceptionWithoutCause.class, 0));
812
813 assertEquals(0, ExceptionUtils.indexOfType(withCause, ExceptionWithCause.class, -1));
814 assertEquals(0, ExceptionUtils.indexOfType(withCause, ExceptionWithCause.class, 0));
815 assertEquals(-1, ExceptionUtils.indexOfType(withCause, ExceptionWithCause.class, 1));
816 assertEquals(-1, ExceptionUtils.indexOfType(withCause, ExceptionWithCause.class, 9));
817
818 assertEquals(0, ExceptionUtils.indexOfType(withCause, Exception.class, 0));
819 assertEquals(0, ExceptionUtils.indexOfType(withCause, Throwable.class, 0));
820 }
821
822 @Test
823 void testIsChecked_checked() {
824 assertTrue(ExceptionUtils.isChecked(new IOException()));
825 }
826
827 @Test
828 void testIsChecked_error() {
829 assertFalse(ExceptionUtils.isChecked(new StackOverflowError()));
830 }
831
832 @Test
833 void testIsChecked_null() {
834 assertFalse(ExceptionUtils.isChecked(null));
835 }
836
837 @Test
838 void testIsChecked_unchecked() {
839 assertFalse(ExceptionUtils.isChecked(new IllegalArgumentException()));
840 }
841
842 @Test
843 void testIsCheckedCustomThrowable() {
844 assertTrue(ExceptionUtils.isChecked(new TestThrowable()));
845 }
846
847 @Test
848 void testIsUnchecked_checked() {
849 assertFalse(ExceptionUtils.isUnchecked(new IOException()));
850 }
851
852 @Test
853 void testIsUnchecked_error() {
854 assertTrue(ExceptionUtils.isUnchecked(new StackOverflowError()));
855 }
856
857 @Test
858 void testIsUnchecked_null() {
859 assertFalse(ExceptionUtils.isUnchecked(null));
860 }
861
862 @Test
863 void testIsUnchecked_unchecked() {
864 assertTrue(ExceptionUtils.isUnchecked(new IllegalArgumentException()));
865 }
866
867 @Test
868 void testIsUnCheckedCustomThrowable() {
869 assertFalse(ExceptionUtils.isUnchecked(new TestThrowable()));
870 }
871
872 @Test
873 void testPrintRootCauseStackTrace_Throwable() {
874 ExceptionUtils.printRootCauseStackTrace(null);
875
876
877 }
878
879 @Test
880 void testPrintRootCauseStackTrace_ThrowableStream() {
881 ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
882 ExceptionUtils.printRootCauseStackTrace(null, (PrintStream) null);
883 ExceptionUtils.printRootCauseStackTrace(null, new PrintStream(out));
884 assertEquals(0, out.toString().length());
885 assertNullPointerException(() -> ExceptionUtils.printRootCauseStackTrace(withCause, (PrintStream) null));
886 out = new ByteArrayOutputStream(1024);
887 final Throwable cause = createExceptionWithCause();
888 ExceptionUtils.printRootCauseStackTrace(cause, new PrintStream(out));
889 String stackTrace = out.toString();
890 assertTrue(stackTrace.contains(ExceptionUtils.WRAPPED_MARKER));
891 out = new ByteArrayOutputStream(1024);
892 ExceptionUtils.printRootCauseStackTrace(withoutCause, new PrintStream(out));
893 stackTrace = out.toString();
894 assertFalse(stackTrace.contains(ExceptionUtils.WRAPPED_MARKER));
895 }
896
897 @Test
898 void testPrintRootCauseStackTrace_ThrowableWriter() {
899 StringWriter writer = new StringWriter(1024);
900 ExceptionUtils.printRootCauseStackTrace(null, (PrintWriter) null);
901 ExceptionUtils.printRootCauseStackTrace(null, new PrintWriter(writer));
902 assertEquals(0, writer.getBuffer().length());
903 assertNullPointerException(() -> ExceptionUtils.printRootCauseStackTrace(withCause, (PrintWriter) null));
904 writer = new StringWriter(1024);
905 final Throwable cause = createExceptionWithCause();
906 ExceptionUtils.printRootCauseStackTrace(cause, new PrintWriter(writer));
907 String stackTrace = writer.toString();
908 assertTrue(stackTrace.contains(ExceptionUtils.WRAPPED_MARKER));
909 writer = new StringWriter(1024);
910 ExceptionUtils.printRootCauseStackTrace(withoutCause, new PrintWriter(writer));
911 stackTrace = writer.toString();
912 assertFalse(stackTrace.contains(ExceptionUtils.WRAPPED_MARKER));
913 }
914
915 @Test
916 void testRemoveCommonFrames_ListList() {
917 assertNullPointerException(() -> ExceptionUtils.removeCommonFrames(null, null));
918 }
919
920 @Test
921 void testRemoveCommonFramesStopsAtFirstDifference() {
922 final List<String> causeFrames = new ArrayList<>(Arrays.asList("\tat a.Factory.create(Factory.java:1)", "\tat a.B.one(B.java:2)",
923 "\tat a.B.main(B.java:9)"));
924 final List<String> wrapperFrames = Arrays.asList("\tat a.Factory.create(Factory.java:1)", "\tat a.B.two(B.java:5)",
925 "\tat a.B.main(B.java:9)");
926 final List<String> expectedWrapperFrames = new ArrayList<>(wrapperFrames);
927 ExceptionUtils.removeCommonFrames(causeFrames, wrapperFrames);
928 assertEquals(Arrays.asList("\tat a.Factory.create(Factory.java:1)", "\tat a.B.one(B.java:2)"), causeFrames);
929 assertEquals(expectedWrapperFrames, wrapperFrames);
930 }
931
932 @Test
933 void testRemoveCommonFramesStopsAtImmediateDifference() {
934 final List<String> causeFrames = new ArrayList<>(Arrays.asList("\tat a.Factory.create(Factory.java:1)", "\tat a.B.one(B.java:2)"));
935 final List<String> wrapperFrames = new ArrayList<>(Arrays.asList("\tat a.Factory.create(Factory.java:1)", "\tat a.B.two(B.java:5)"));
936 final List<String> expectedCauseFrames = new ArrayList<>(causeFrames);
937 final List<String> expectedWrapperFrames = new ArrayList<>(wrapperFrames);
938 ExceptionUtils.removeCommonFrames(causeFrames, wrapperFrames);
939 assertEquals(expectedCauseFrames, causeFrames);
940 assertEquals(expectedWrapperFrames, wrapperFrames);
941 }
942
943 @Test
944 void testRethrow() {
945 final Exception expected = new InterruptedException();
946
947 assertThrows(expected.getClass(), () -> {
948 @SuppressWarnings("unused")
949 final Object retVal = ExceptionUtils.rethrow(expected);
950 });
951
952 assertThrows(expected.getClass(), () -> {
953 @SuppressWarnings("unused")
954 final String retVal = ExceptionUtils.rethrow(expected);
955 });
956
957 assertThrows(expected.getClass(), () -> {
958 @SuppressWarnings("unused")
959 final int retVal = ExceptionUtils.rethrow(expected);
960 });
961
962 assertSame(expected, assertThrows(expected.getClass(), () -> ExceptionUtils.rethrow(expected)));
963 assertNotSame(expected, assertThrows(expected.getClass(), () -> ExceptionUtils.rethrow(new InterruptedException())));
964 }
965
966 @Test
967 void testRootCauseStackTraceKeepsSharedFactoryFrame() {
968 final RuntimeException cause = newRuntimeException("cause", null);
969 final RuntimeException wrapper = newRuntimeException("wrapper", cause);
970 final List<String> frames = ExceptionUtils.getRootCauseStackTraceList(wrapper);
971 assertEquals(cause.toString(), frames.get(0));
972 assertTrue(frames.get(1).contains("newRuntimeException"), frames.get(1));
973 }
974
975 @Test
976 void testStream_jdkNoCause() {
977 assertEquals(1, ExceptionUtils.stream(jdkNoCause).count());
978 assertSame(jdkNoCause, ExceptionUtils.stream(jdkNoCause).toArray()[0]);
979 }
980
981 @Test
982 void testStream_nested() {
983 assertEquals(2, ExceptionUtils.stream(nested).count());
984 final Object[] array = ExceptionUtils.stream(nested).toArray();
985 assertSame(nested, array[0]);
986 assertSame(withoutCause, array[1]);
987 }
988
989 @Test
990 void testStream_null() {
991 assertEquals(0, ExceptionUtils.stream(null).count());
992 }
993
994 @Test
995 void testStream_recursiveCause() {
996 final List<?> throwables = ExceptionUtils.stream(cyclicCause).collect(Collectors.toList());
997 assertEquals(3, throwables.size());
998 assertSame(cyclicCause, throwables.get(0));
999 assertSame(cyclicCause.getCause(), throwables.get(1));
1000 assertSame(cyclicCause.getCause().getCause(), throwables.get(2));
1001 }
1002
1003 @Test
1004 void testStream_withCause() {
1005 final List<?> throwables = ExceptionUtils.stream(withCause).collect(Collectors.toList());
1006 assertEquals(3, throwables.size());
1007 assertSame(withCause, throwables.get(0));
1008 assertSame(nested, throwables.get(1));
1009 assertSame(withoutCause, throwables.get(2));
1010 }
1011
1012 @Test
1013 void testStream_withoutCause() {
1014 final List<?> throwables = ExceptionUtils.stream(withoutCause).collect(Collectors.toList());
1015 assertEquals(1, throwables.size());
1016 assertSame(withoutCause, throwables.get(0));
1017 }
1018
1019 @Test
1020 void testThrowableOf_ThrowableClass() {
1021 assertNull(ExceptionUtils.throwableOfThrowable(null, null));
1022 assertNull(ExceptionUtils.throwableOfThrowable(null, NestableException.class));
1023
1024 assertNull(ExceptionUtils.throwableOfThrowable(withoutCause, null));
1025 assertNull(ExceptionUtils.throwableOfThrowable(withoutCause, ExceptionWithCause.class));
1026 assertNull(ExceptionUtils.throwableOfThrowable(withoutCause, NestableException.class));
1027 assertEquals(withoutCause, ExceptionUtils.throwableOfThrowable(withoutCause, ExceptionWithoutCause.class));
1028
1029 assertNull(ExceptionUtils.throwableOfThrowable(nested, null));
1030 assertNull(ExceptionUtils.throwableOfThrowable(nested, ExceptionWithCause.class));
1031 assertEquals(nested, ExceptionUtils.throwableOfThrowable(nested, NestableException.class));
1032 assertEquals(nested.getCause(), ExceptionUtils.throwableOfThrowable(nested, ExceptionWithoutCause.class));
1033
1034 assertNull(ExceptionUtils.throwableOfThrowable(withCause, null));
1035 assertEquals(withCause, ExceptionUtils.throwableOfThrowable(withCause, ExceptionWithCause.class));
1036 assertEquals(withCause.getCause(), ExceptionUtils.throwableOfThrowable(withCause, NestableException.class));
1037 assertEquals(withCause.getCause().getCause(), ExceptionUtils.throwableOfThrowable(withCause, ExceptionWithoutCause.class));
1038
1039 assertNull(ExceptionUtils.throwableOfThrowable(withCause, Exception.class));
1040 assertNull(ExceptionUtils.throwableOfThrowable(withCause, Throwable.class));
1041 }
1042
1043 @Test
1044 void testThrowableOf_ThrowableClassInt() {
1045 assertNull(ExceptionUtils.throwableOfThrowable(null, null, 0));
1046 assertNull(ExceptionUtils.throwableOfThrowable(null, NestableException.class, 0));
1047
1048 assertNull(ExceptionUtils.throwableOfThrowable(withoutCause, null));
1049 assertNull(ExceptionUtils.throwableOfThrowable(withoutCause, ExceptionWithCause.class, 0));
1050 assertNull(ExceptionUtils.throwableOfThrowable(withoutCause, NestableException.class, 0));
1051 assertEquals(withoutCause, ExceptionUtils.throwableOfThrowable(withoutCause, ExceptionWithoutCause.class, 0));
1052
1053 assertNull(ExceptionUtils.throwableOfThrowable(nested, null, 0));
1054 assertNull(ExceptionUtils.throwableOfThrowable(nested, ExceptionWithCause.class, 0));
1055 assertEquals(nested, ExceptionUtils.throwableOfThrowable(nested, NestableException.class, 0));
1056 assertEquals(nested.getCause(), ExceptionUtils.throwableOfThrowable(nested, ExceptionWithoutCause.class, 0));
1057
1058 assertNull(ExceptionUtils.throwableOfThrowable(withCause, null));
1059 assertEquals(withCause, ExceptionUtils.throwableOfThrowable(withCause, ExceptionWithCause.class, 0));
1060 assertEquals(withCause.getCause(), ExceptionUtils.throwableOfThrowable(withCause, NestableException.class, 0));
1061 assertEquals(withCause.getCause().getCause(), ExceptionUtils.throwableOfThrowable(withCause, ExceptionWithoutCause.class, 0));
1062
1063 assertEquals(withCause, ExceptionUtils.throwableOfThrowable(withCause, ExceptionWithCause.class, -1));
1064 assertEquals(withCause, ExceptionUtils.throwableOfThrowable(withCause, ExceptionWithCause.class, 0));
1065 assertNull(ExceptionUtils.throwableOfThrowable(withCause, ExceptionWithCause.class, 1));
1066 assertNull(ExceptionUtils.throwableOfThrowable(withCause, ExceptionWithCause.class, 9));
1067
1068 assertNull(ExceptionUtils.throwableOfThrowable(withCause, Exception.class, 0));
1069 assertNull(ExceptionUtils.throwableOfThrowable(withCause, Throwable.class, 0));
1070 }
1071
1072 @Test
1073 void testThrowableOfType_ThrowableClass() {
1074 assertNull(ExceptionUtils.throwableOfType(null, null));
1075 assertNull(ExceptionUtils.throwableOfType(null, NestableException.class));
1076
1077 assertNull(ExceptionUtils.throwableOfType(withoutCause, null));
1078 assertNull(ExceptionUtils.throwableOfType(withoutCause, ExceptionWithCause.class));
1079 assertNull(ExceptionUtils.throwableOfType(withoutCause, NestableException.class));
1080 assertEquals(withoutCause, ExceptionUtils.throwableOfType(withoutCause, ExceptionWithoutCause.class));
1081
1082 assertNull(ExceptionUtils.throwableOfType(nested, null));
1083 assertNull(ExceptionUtils.throwableOfType(nested, ExceptionWithCause.class));
1084 assertEquals(nested, ExceptionUtils.throwableOfType(nested, NestableException.class));
1085 assertEquals(nested.getCause(), ExceptionUtils.throwableOfType(nested, ExceptionWithoutCause.class));
1086
1087 assertNull(ExceptionUtils.throwableOfType(withCause, null));
1088 assertEquals(withCause, ExceptionUtils.throwableOfType(withCause, ExceptionWithCause.class));
1089 assertEquals(withCause.getCause(), ExceptionUtils.throwableOfType(withCause, NestableException.class));
1090 assertEquals(withCause.getCause().getCause(), ExceptionUtils.throwableOfType(withCause, ExceptionWithoutCause.class));
1091
1092 assertEquals(withCause, ExceptionUtils.throwableOfType(withCause, Exception.class));
1093 assertEquals(withCause, ExceptionUtils.throwableOfType(withCause, Throwable.class));
1094 }
1095
1096 @Test
1097 void testThrowableOfType_ThrowableClassInt() {
1098 assertNull(ExceptionUtils.throwableOfType(null, null, 0));
1099 assertNull(ExceptionUtils.throwableOfType(null, NestableException.class, 0));
1100
1101 assertNull(ExceptionUtils.throwableOfType(withoutCause, null));
1102 assertNull(ExceptionUtils.throwableOfType(withoutCause, ExceptionWithCause.class, 0));
1103 assertNull(ExceptionUtils.throwableOfType(withoutCause, NestableException.class, 0));
1104 assertEquals(withoutCause, ExceptionUtils.throwableOfType(withoutCause, ExceptionWithoutCause.class, 0));
1105
1106 assertNull(ExceptionUtils.throwableOfType(nested, null, 0));
1107 assertNull(ExceptionUtils.throwableOfType(nested, ExceptionWithCause.class, 0));
1108 assertEquals(nested, ExceptionUtils.throwableOfType(nested, NestableException.class, 0));
1109 assertEquals(nested.getCause(), ExceptionUtils.throwableOfType(nested, ExceptionWithoutCause.class, 0));
1110
1111 assertNull(ExceptionUtils.throwableOfType(withCause, null));
1112 assertEquals(withCause, ExceptionUtils.throwableOfType(withCause, ExceptionWithCause.class, 0));
1113 assertEquals(withCause.getCause(), ExceptionUtils.throwableOfType(withCause, NestableException.class, 0));
1114 assertEquals(withCause.getCause().getCause(), ExceptionUtils.throwableOfType(withCause, ExceptionWithoutCause.class, 0));
1115
1116 assertEquals(withCause, ExceptionUtils.throwableOfType(withCause, ExceptionWithCause.class, -1));
1117 assertEquals(withCause, ExceptionUtils.throwableOfType(withCause, ExceptionWithCause.class, 0));
1118 assertNull(ExceptionUtils.throwableOfType(withCause, ExceptionWithCause.class, 1));
1119 assertNull(ExceptionUtils.throwableOfType(withCause, ExceptionWithCause.class, 9));
1120
1121 assertEquals(withCause, ExceptionUtils.throwableOfType(withCause, Exception.class, 0));
1122 assertEquals(withCause, ExceptionUtils.throwableOfType(withCause, Throwable.class, 0));
1123 }
1124
1125 @Test
1126 void testWrapAndUnwrapCheckedException() {
1127 final Throwable t = assertThrows(Throwable.class, () -> ExceptionUtils.wrapAndThrow(new IOException()));
1128 assertTrue(ExceptionUtils.hasCause(t, IOException.class));
1129 }
1130
1131 @Test
1132 void testWrapAndUnwrapError() {
1133 final Throwable t = assertThrows(Throwable.class, () -> ExceptionUtils.wrapAndThrow(new OutOfMemoryError()));
1134 assertTrue(ExceptionUtils.hasCause(t, Error.class));
1135 }
1136
1137 @Test
1138 void testWrapAndUnwrapRuntimeException() {
1139 final Throwable t = assertThrows(Throwable.class, () -> ExceptionUtils.wrapAndThrow(new IllegalArgumentException()));
1140 assertTrue(ExceptionUtils.hasCause(t, RuntimeException.class));
1141 }
1142
1143 @Test
1144 void testWrapAndUnwrapThrowable() {
1145 final Throwable t = assertThrows(Throwable.class, () -> ExceptionUtils.wrapAndThrow(new TestThrowable()));
1146 assertTrue(ExceptionUtils.hasCause(t, TestThrowable.class));
1147 }
1148 }