1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.slf4j.impl;
20
21 import java.io.ByteArrayOutputStream;
22 import java.io.PrintStream;
23 import java.util.Arrays;
24 import java.util.List;
25 import java.util.NoSuchElementException;
26
27 import org.junit.jupiter.api.Test;
28 import org.junit.jupiter.api.TestInfo;
29
30 import static java.nio.charset.StandardCharsets.UTF_8;
31 import static org.junit.jupiter.api.Assertions.assertLinesMatch;
32
33 class MavenSimpleLoggerTest {
34
35 @Test
36 void includesCauseAndSuppressedExceptionsWhenWritingThrowables(TestInfo testInfo) throws Exception {
37 Exception causeOfSuppressed = new NoSuchElementException("cause of suppressed");
38 Exception suppressed = new IllegalStateException("suppressed", causeOfSuppressed);
39 suppressed.addSuppressed(new IllegalArgumentException(
40 "suppressed suppressed", new ArrayIndexOutOfBoundsException("suppressed suppressed cause")));
41 Exception cause = new IllegalArgumentException("cause");
42 cause.addSuppressed(suppressed);
43 Exception throwable = new RuntimeException("top-level", cause);
44
45 ByteArrayOutputStream output = new ByteArrayOutputStream();
46
47 new MavenSimpleLogger("logger").writeThrowable(throwable, new PrintStream(output));
48
49 String actual = output.toString(UTF_8.name());
50 List<String> actualLines = Arrays.asList(actual.split(System.lineSeparator()));
51
52 Class<?> testClass = testInfo.getTestClass().get();
53 String testMethodName = testInfo.getTestMethod().get().getName();
54 String testClassStackTraceLinePattern = "at " + testClass.getName() + "." + testMethodName + "\\("
55 + testClass.getSimpleName() + ".java:\\d+\\)";
56 List<String> expectedLines = Arrays.asList(
57 "java.lang.RuntimeException: top-level",
58 " " + testClassStackTraceLinePattern,
59 ">> stacktrace >>",
60 "Caused by: java.lang.IllegalArgumentException: cause",
61 " " + testClassStackTraceLinePattern,
62 ">> stacktrace >>",
63 " Suppressed: java.lang.IllegalStateException: suppressed",
64 " " + testClassStackTraceLinePattern,
65 ">> stacktrace >>",
66 " Suppressed: java.lang.IllegalArgumentException: suppressed suppressed",
67 " " + testClassStackTraceLinePattern,
68 ">> stacktrace >>",
69 " Caused by: java.lang.ArrayIndexOutOfBoundsException: suppressed suppressed cause",
70 " " + testClassStackTraceLinePattern,
71 ">> stacktrace >>",
72 " Caused by: java.util.NoSuchElementException: cause of suppressed",
73 " " + testClassStackTraceLinePattern,
74 ">> stacktrace >>");
75
76 assertLinesMatch(expectedLines, actualLines);
77 }
78 }