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.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  }