View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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  }