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.spi.ILoggingEvent;
20 import ch.qos.logback.classic.spi.LoggingEvent;
21 import ch.qos.logback.classic.util.TestHelper;
22 import ch.qos.logback.core.CoreConstants;
23 import ch.qos.logback.core.util.EnvUtil;
24 import org.junit.jupiter.api.AfterEach;
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27
28 import java.io.BufferedReader;
29 import java.io.PrintWriter;
30 import java.io.StringReader;
31 import java.io.StringWriter;
32 import java.lang.reflect.InvocationTargetException;
33 import java.util.Arrays;
34 import java.util.List;
35
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.junit.jupiter.api.Assertions.assertEquals;
38 import static org.junit.jupiter.api.Assertions.assertNotNull;
39 import static org.junit.jupiter.api.Assertions.assertNull;
40 import static org.junit.jupiter.api.Assertions.assertTrue;
41
42 public class ThrowableProxyConverterTest {
43
44 LoggerContext lc = new LoggerContext();
45 ThrowableProxyConverter tpc = new ThrowableProxyConverter();
46 StringWriter sw = new StringWriter();
47 PrintWriter pw = new PrintWriter(sw);
48
49 @BeforeEach
50 public void setUp() throws Exception {
51 tpc.setContext(lc);
52 tpc.start();
53 }
54
55 @AfterEach
56 public void tearDown() throws Exception {
57 }
58
59 private ILoggingEvent createLoggingEvent(Throwable t) {
60 return new LoggingEvent(this.getClass().getName(), lc.getLogger(Logger.ROOT_LOGGER_NAME), Level.DEBUG,
61 "test message", t, null);
62 }
63
64 @Test
65 public void suppressed() throws InvocationTargetException, IllegalAccessException {
66 Exception ex = null;
67 try {
68 someMethod();
69 } catch (Exception e) {
70 Exception fooException = new Exception("Foo");
71 Exception barException = new Exception("Bar");
72 e.addSuppressed(fooException);
73 e.addSuppressed(barException);
74 ex = e;
75 }
76 verify(ex);
77 }
78
79 @Test
80 public void suppressedWithCause() throws InvocationTargetException, IllegalAccessException {
81 Exception ex = null;
82 try {
83 someMethod();
84 } catch (Exception e) {
85 ex = new Exception("Wrapper", e);
86 Exception fooException = new Exception("Foo");
87 Exception barException = new Exception("Bar");
88 e.addSuppressed(fooException);
89 e.addSuppressed(barException);
90 }
91 verify(ex);
92 }
93
94 @Test
95 public void suppressedWithSuppressed() throws Exception {
96 Exception ex = null;
97 try {
98 someMethod();
99 } catch (Exception e) {
100 ex = new Exception("Wrapper", e);
101 Exception fooException = new Exception("Foo");
102 Exception barException = new Exception("Bar");
103 barException.addSuppressed(fooException);
104 e.addSuppressed(barException);
105 }
106 verify(ex);
107 }
108
109 @Test
110 public void smoke() {
111 Exception t = new Exception("smoke");
112 verify(t);
113 }
114
115 @Test
116 public void nested() {
117 Throwable t = TestHelper.makeNestedException(1);
118 verify(t);
119 }
120
121 @Test
122 public void cyclicCause() {
123
124 if (!EnvUtil.isJDK16OrHigher())
125 return;
126 Exception e = new Exception("foo");
127 Exception e2 = new Exception(e);
128 e.initCause(e2);
129 verify(e);
130 }
131
132 @Test
133 public void cyclicSuppressed() {
134
135 if (!EnvUtil.isJDK16OrHigher())
136 return;
137
138 Exception e = new Exception("foo");
139 Exception e2 = new Exception(e);
140 e.addSuppressed(e2);
141 verify(e);
142 }
143
144 @Test
145 public void withArgumentOfOne() throws Exception {
146 final Throwable t = TestHelper.makeNestedException(0);
147 t.printStackTrace(pw);
148 final ILoggingEvent le = createLoggingEvent(t);
149
150 final List<String> optionList = Arrays.asList("1");
151 tpc.setOptionList(optionList);
152 tpc.start();
153
154 final String result = tpc.convert(le);
155
156 System.out.println(result);
157 final BufferedReader reader = new BufferedReader(new StringReader(result));
158 assertTrue(reader.readLine().contains(t.getMessage()));
159 assertNotNull(reader.readLine());
160 assertNull(reader.readLine(), "Unexpected line in stack trace");
161 }
162
163 @Test
164 public void withShortArgument() throws Exception {
165 final Throwable t = TestHelper.makeNestedException(0);
166 t.printStackTrace(pw);
167 final ILoggingEvent le = createLoggingEvent(t);
168
169 final List<String> options = Arrays.asList("short");
170 tpc.setOptionList(options);
171 tpc.start();
172
173 final String result = tpc.convert(le);
174
175 final BufferedReader reader = new BufferedReader(new StringReader(result));
176 assertTrue(reader.readLine().contains(t.getMessage()));
177 assertNotNull(reader.readLine());
178 assertNull(reader.readLine(), "Unexpected line in stack trace");
179 }
180
181 @Test
182 public void skipSelectedLine() throws Exception {
183 String nameOfContainingMethod = "skipSelectedLine";
184
185 final Throwable t = TestHelper.makeNestedException(0);
186 t.printStackTrace(pw);
187 final ILoggingEvent le = createLoggingEvent(t);
188 tpc.setOptionList(Arrays.asList("full", nameOfContainingMethod));
189 tpc.start();
190
191
192 final String result = tpc.convert(le);
193
194
195 assertThat(result).doesNotContain(nameOfContainingMethod);
196
197 }
198
199 @Test
200 public void skipMultipleLines() throws Exception {
201 String nameOfContainingMethod = "skipMultipleLines";
202
203 final Throwable t = TestHelper.makeNestedException(0);
204 t.printStackTrace(pw);
205 final ILoggingEvent le = createLoggingEvent(t);
206 tpc.setOptionList(Arrays.asList("full", nameOfContainingMethod, "junit"));
207 tpc.start();
208
209
210 final String result = tpc.convert(le);
211
212
213 assertThat(result).doesNotContain(nameOfContainingMethod).doesNotContain("junit");
214 }
215
216 @Test
217 public void shouldLimitTotalLinesExcludingSkipped() throws Exception {
218
219 final Throwable t = TestHelper.makeNestedException(0);
220 t.printStackTrace(pw);
221 final ILoggingEvent le = createLoggingEvent(t);
222 tpc.setOptionList(Arrays.asList("3", "shouldLimitTotalLinesExcludingSkipped"));
223 tpc.start();
224
225
226 final String result = tpc.convert(le);
227
228
229 String[] lines = result.split(CoreConstants.LINE_SEPARATOR);
230 assertThat(lines).hasSize(3 + 1);
231 }
232
233 void someMethod() throws Exception {
234 throw new Exception("someMethod");
235 }
236
237 void verify(Throwable t) {
238 t.printStackTrace(pw);
239
240 ILoggingEvent le = createLoggingEvent(t);
241 String result = tpc.convert(le);
242
243 result = result.replace("common frames omitted", "more");
244 assertEquals(sw.toString(), result);
245 }
246 }