1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.helpers;
15
16 import java.io.PrintWriter;
17 import java.io.StringWriter;
18
19 import org.junit.jupiter.api.AfterEach;
20 import org.junit.jupiter.api.BeforeEach;
21 import org.junit.jupiter.api.Test;
22
23 import ch.qos.logback.core.CoreConstants;
24
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26
27 public class ThrowableToStringArrayTest {
28
29 StringWriter sw = new StringWriter();
30 PrintWriter pw = new PrintWriter(sw);
31
32 @BeforeEach
33 public void setUp() throws Exception {
34 }
35
36 @AfterEach
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 }