1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.classic.pattern;
15
16 import ch.qos.logback.classic.Level;
17 import ch.qos.logback.classic.Logger;
18 import ch.qos.logback.classic.LoggerContext;
19 import ch.qos.logback.classic.PatternLayout;
20 import ch.qos.logback.classic.spi.ILoggingEvent;
21 import ch.qos.logback.classic.spi.LoggingEvent;
22 import ch.qos.logback.core.util.EnvUtil;
23 import org.junit.jupiter.api.AfterEach;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26
27 import java.io.PrintWriter;
28 import java.io.StringWriter;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
31
32 import static org.assertj.core.api.Assertions.assertThat;
33 import static org.junit.jupiter.api.Assertions.assertEquals;
34
35 public class ExtendedThrowableProxyConverterTest {
36
37 LoggerContext lc = new LoggerContext();
38 ExtendedThrowableProxyConverter etpc = new ExtendedThrowableProxyConverter();
39 StringWriter sw = new StringWriter();
40 PrintWriter pw = new PrintWriter(sw);
41
42 @BeforeEach
43 public void setUp() throws Exception {
44 lc.setPackagingDataEnabled(true);
45 etpc.setContext(lc);
46 etpc.start();
47 }
48
49 @AfterEach
50 public void tearDown() throws Exception {
51 }
52
53 private ILoggingEvent createLoggingEvent(Throwable t) {
54 return new LoggingEvent(this.getClass().getName(), lc.getLogger(Logger.ROOT_LOGGER_NAME), Level.DEBUG,
55 "test message", t, null);
56 }
57
58 @Test
59 public void integration() {
60 PatternLayout pl = new PatternLayout();
61 pl.setContext(lc);
62 pl.setPattern("%m%n%xEx");
63 pl.start();
64 ILoggingEvent e = createLoggingEvent(new Exception("x"));
65 String res = pl.doLayout(e);
66
67
68 Pattern p = Pattern.compile("\\s*at .*?\\[.*?\\]");
69 Matcher m = p.matcher(res);
70 int i = 0;
71 while (m.find()) {
72 i++;
73 }
74 assertThat(i).isGreaterThan(5);
75 }
76
77 @Test
78 public void smoke() {
79 Exception t = new Exception("smoke");
80 verify(t);
81 }
82
83 @Test
84 public void nested() {
85 Throwable t = makeNestedException(1);
86 verify(t);
87 }
88
89 @Test
90 public void cyclicCause() {
91
92
93
94 if (!EnvUtil.isJDK16OrHigher())
95 return;
96
97 Exception e = new Exception("foo");
98 Exception e2 = new Exception(e);
99 e.initCause(e2);
100 verify(e);
101 }
102
103 @Test
104 public void cyclicSuppressed() {
105
106
107
108 if (!EnvUtil.isJDK16OrHigher())
109 return;
110
111 Exception e = new Exception("foo");
112 Exception e2 = new Exception(e);
113 e.addSuppressed(e2);
114 verify(e);
115 }
116
117 void verify(Throwable t) {
118 t.printStackTrace(pw);
119
120 ILoggingEvent le = createLoggingEvent(t);
121 String result = etpc.convert(le);
122 result = result.replace("common frames omitted", "more");
123
124 result = result.replaceAll(" ~?\\[(?!CIRCULAR).*\\]", "");
125 assertEquals(sw.toString(), result);
126 }
127
128 Throwable makeNestedException(int level) {
129 if (level == 0) {
130 return new Exception("nesting level=" + level);
131 }
132 Throwable cause = makeNestedException(level - 1);
133 return new Exception("nesting level =" + level, cause);
134 }
135 }