1   package ch.qos.logback.core.helpers;
2   
3   import static org.junit.Assert.assertEquals;
4   
5   import java.io.PrintWriter;
6   import java.io.StringWriter;
7   
8   import org.junit.After;
9   import org.junit.Before;
10  import org.junit.Test;
11  
12  import ch.qos.logback.core.CoreConstants;
13  
14  public class ThrowableToStringArrayTest {
15  
16    StringWriter sw = new StringWriter();
17    PrintWriter pw = new PrintWriter(sw);
18  
19    @Before
20    public void setUp() throws Exception {
21    }
22  
23    @After
24    public void tearDown() throws Exception {
25    }
26  
27    public void verify(Throwable t) {
28      t.printStackTrace(pw);
29      
30      String[] sa = ThrowableToStringArray.convert(t);
31      StringBuilder sb = new StringBuilder();
32      for (String tdp : sa) {
33        sb.append(tdp.toString());
34        sb.append(CoreConstants.LINE_SEPARATOR);
35      }
36      String expected = sw.toString();
37      String result = sb.toString().replace("common frames omitted", "more");
38      assertEquals(expected, result);
39    }
40    
41    @Test
42    public void smoke() {
43      Exception e = new Exception("smoke");
44      verify(e);
45    }
46  
47    @Test
48    public void nested() {
49      Exception w = null;
50      try {
51        someMethod();
52      } catch (Exception e) {
53        w = new Exception("wrapping", e);
54      }
55      verify(w);
56    }
57  
58    @Test
59    public void multiNested() {
60      Exception w = null;
61      try {
62        someOtherMethod();
63      } catch (Exception e) {
64        w = new Exception("wrapping", e);
65      }
66      verify(w);
67    }
68    
69    void someMethod() throws Exception {
70      throw new Exception("someMethod");
71    }
72  
73    void someOtherMethod() throws Exception {
74      try {
75        someMethod();
76      } catch (Exception e) {
77        throw new Exception("someOtherMethod", e);
78      }
79    }
80  }