View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.helpers;
15  
16  import static org.junit.Assert.assertEquals;
17  
18  import java.io.PrintWriter;
19  import java.io.StringWriter;
20  
21  import org.junit.After;
22  import org.junit.Before;
23  import org.junit.Test;
24  
25  import ch.qos.logback.core.CoreConstants;
26  
27  public class ThrowableToStringArrayTest {
28  
29      StringWriter sw = new StringWriter();
30      PrintWriter pw = new PrintWriter(sw);
31  
32      @Before
33      public void setUp() throws Exception {
34      }
35  
36      @After
37      public void tearDown() throws Exception {
38      }
39  
40      public void verify(Throwable t) {
41          t.printStackTrace(pw);
42  
43          String[] sa = ThrowableToStringArray.convert(t);
44          StringBuilder sb = new StringBuilder();
45          for (String tdp : sa) {
46              sb.append(tdp);
47              sb.append(CoreConstants.LINE_SEPARATOR);
48          }
49          String expected = sw.toString();
50          String result = sb.toString().replace("common frames omitted", "more");
51          assertEquals(expected, result);
52      }
53  
54      @Test
55      public void smoke() {
56          Exception e = new Exception("smoke");
57          verify(e);
58      }
59  
60      @Test
61      public void nested() {
62          Exception w = null;
63          try {
64              someMethod();
65          } catch (Exception e) {
66              w = new Exception("wrapping", e);
67          }
68          verify(w);
69      }
70  
71      @Test
72      public void multiNested() {
73          Exception w = null;
74          try {
75              someOtherMethod();
76          } catch (Exception e) {
77              w = new Exception("wrapping", e);
78          }
79          verify(w);
80      }
81  
82      void someMethod() throws Exception {
83          throw new Exception("someMethod");
84      }
85  
86      void someOtherMethod() throws Exception {
87          try {
88              someMethod();
89          } catch (Exception e) {
90              throw new Exception("someOtherMethod", e);
91          }
92      }
93  }