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.Test;
28
29 import static java.nio.charset.StandardCharsets.UTF_8;
30 import static org.hamcrest.MatcherAssert.assertThat;
31 import static org.hamcrest.Matchers.stringContainsInOrder;
32
33 public class MavenSimpleLoggerTest {
34
35 @Test
36 public void includesCauseAndSuppressedExceptionsWhenWritingThrowables() 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
51 List<String> expectedLines = Arrays.asList(
52 "java.lang.RuntimeException: top-level",
53 "Caused by: java.lang.IllegalArgumentException: cause",
54 " Suppressed: java.lang.IllegalStateException: suppressed",
55 " Suppressed: java.lang.IllegalArgumentException: suppressed suppressed",
56 " Caused by: java.lang.ArrayIndexOutOfBoundsException: suppressed suppressed cause",
57 " Caused by: java.util.NoSuchElementException: cause of suppressed");
58
59 assertThat(actual, stringContainsInOrder(expectedLines));
60 }
61 }