1   package ch.qos.logback.classic.spi;
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 ThrowableToDataPointTest {
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      ThrowableDataPoint[] tdpArray = ThrowableToDataPointArray.convert(t);
31      StringBuilder sb = new StringBuilder();
32      for (ThrowableDataPoint tdp : tdpArray) {
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      
39      assertEquals(expected, result);
40    }
41    
42    @Test
43    public void smoke() {
44      Exception e = new Exception("smoke");
45      verify(e);
46    }
47  
48    @Test
49    public void nested() {
50      Exception w = null;
51      try {
52        someMethod();
53      } catch (Exception e) {
54        w = new Exception("wrapping", e);
55      }
56      verify(w);
57    }
58  
59    @Test
60    public void multiNested() {
61      Exception w = null;
62      try {
63        someOtherMethod();
64      } catch (Exception e) {
65        w = new Exception("wrapping", e);
66      }
67      verify(w);
68    }
69    
70    void someMethod() throws Exception {
71      throw new Exception("someMethod");
72    }
73  
74    void someOtherMethod() throws Exception {
75      try {
76        someMethod();
77      } catch (Exception e) {
78        throw new Exception("someOtherMethod", e);
79      }
80    }
81  }