-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
ThrowableProxyConverterTest.java
174 lines (149 loc) · 5.07 KB
/
ThrowableProxyConverterTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* Logback: the reliable, generic, fast and flexible logging framework.
* Copyright (C) 1999-2013, QOS.ch. All rights reserved.
*
* This program and the accompanying materials are dual-licensed under
* either the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation
*
* or (per the licensee's choosing)
*
* under the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation.
*/
package ch.qos.logback.classic.pattern;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.classic.util.TeztHelper;
import static ch.qos.logback.classic.util.TeztHelper.addSuppressed;
import static org.junit.Assert.*;
import static org.junit.Assume.assumeTrue;
public class ThrowableProxyConverterTest {
LoggerContext lc = new LoggerContext();
ThrowableProxyConverter tpc = new ThrowableProxyConverter();
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
@Before
public void setUp() throws Exception {
tpc.setContext(lc);
tpc.start();
}
@After
public void tearDown() throws Exception {
}
private ILoggingEvent createLoggingEvent(Throwable t) {
return new LoggingEvent(this.getClass().getName(), lc
.getLogger(Logger.ROOT_LOGGER_NAME), Level.DEBUG, "test message", t,
null);
}
@Test
public void suppressed() throws InvocationTargetException, IllegalAccessException
{
assumeTrue(TeztHelper.suppressedSupported()); // only execute on Java 7, would work anyway but doesn't make sense.
Exception ex = null;
try {
someMethod();
} catch (Exception e) {
Exception fooException = new Exception("Foo");
Exception barException = new Exception("Bar");
addSuppressed(e, fooException);
addSuppressed(e, barException);
ex = e;
}
verify(ex);
}
@Test
public void suppressedWithCause() throws InvocationTargetException, IllegalAccessException
{
assumeTrue(TeztHelper.suppressedSupported()); // only execute on Java 7, would work anyway but doesn't make sense.
Exception ex = null;
try {
someMethod();
} catch (Exception e) {
ex=new Exception("Wrapper", e);
Exception fooException = new Exception("Foo");
Exception barException = new Exception("Bar");
addSuppressed(ex, fooException);
addSuppressed(e, barException);
}
verify(ex);
}
@Test
public void suppressedWithSuppressed() throws Exception
{
assumeTrue(TeztHelper.suppressedSupported()); // only execute on Java 7, would work anyway but doesn't make sense.
Exception ex = null;
try {
someMethod();
} catch (Exception e) {
ex=new Exception("Wrapper", e);
Exception fooException = new Exception("Foo");
Exception barException = new Exception("Bar");
addSuppressed(barException, fooException);
addSuppressed(e, barException);
}
verify(ex);
}
@Test
public void smoke() {
Exception t = new Exception("smoke");
verify(t);
}
@Test
public void nested() {
Throwable t = TeztHelper.makeNestedException(1);
verify(t);
}
@Test
public void withArgumentOfOne() throws Exception {
final Throwable t = TeztHelper.makeNestedException(0);
t.printStackTrace(pw);
final ILoggingEvent le = createLoggingEvent(t);
final List<String> optionList = Arrays.asList("1");
tpc.setOptionList(optionList);
tpc.start();
final String result = tpc.convert(le);
final BufferedReader reader = new BufferedReader(new StringReader(result));
assertTrue(reader.readLine().contains(t.getMessage()));
assertNotNull(reader.readLine());
assertNull("Unexpected line in stack trace", reader.readLine());
}
@Test
public void withShortArgument() throws Exception {
final Throwable t = TeztHelper.makeNestedException(0);
t.printStackTrace(pw);
final ILoggingEvent le = createLoggingEvent(t);
final List<String> options = Arrays.asList("short");
tpc.setOptionList(options);
tpc.start();
final String result = tpc.convert(le);
final BufferedReader reader = new BufferedReader(new StringReader(result));
assertTrue(reader.readLine().contains(t.getMessage()));
assertNotNull(reader.readLine());
assertNull("Unexpected line in stack trace", reader.readLine());
}
void someMethod() throws Exception {
throw new Exception("someMethod");
}
void verify(Throwable t) {
t.printStackTrace(pw);
ILoggingEvent le = createLoggingEvent(t);
String result = tpc.convert(le);
System.out.println(result);
result = result.replace("common frames omitted", "more");
assertEquals(sw.toString(), result);
}
}