1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.log4j.spi;
18
19 import junit.framework.TestCase;
20
21 import java.io.PrintWriter;
22
23
24
25
26
27 public class ThrowableInformationTest extends TestCase {
28
29
30
31
32
33 public ThrowableInformationTest(final String name) {
34 super(name);
35 }
36
37
38
39
40
41 private static final class OverriddenThrowable extends Throwable {
42 private static final long serialVersionUID = 1L;
43
44
45
46 public OverriddenThrowable() {
47 }
48
49
50
51
52
53
54 public void printStackTrace(final PrintWriter s) {
55 s.print((Object) "print(Object)");
56 s.print("print(char[])".toCharArray());
57 s.print("print(String)");
58 s.println((Object) "println(Object)");
59 s.println("println(char[])".toCharArray());
60 s.println("println(String)");
61 s.write("write(char[])".toCharArray());
62 s.write("write(char[], int, int)".toCharArray(), 2, 8);
63 s.write("write(String, int, int)", 2, 8);
64 }
65 }
66
67
68
69
70
71 public void testOverriddenBehavior() {
72 ThrowableInformation ti = new ThrowableInformation(new OverriddenThrowable());
73 String[] rep = ti.getThrowableStrRep();
74 assertEquals(4, rep.length);
75 assertEquals("print(Object)print(char[])print(String)println(Object)", rep[0]);
76 assertEquals("println(char[])", rep[1]);
77 assertEquals("println(String)", rep[2]);
78 assertEquals("write(char[])ite(charite(Stri", rep[3]);
79 }
80
81
82
83
84
85 private static final class NotOverriddenThrowable extends Throwable {
86 private static final long serialVersionUID = 1L;
87
88
89
90 public NotOverriddenThrowable() {
91 }
92
93
94
95
96
97
98 public void printStackTrace(final PrintWriter s) {
99 s.print(true);
100 s.print('a');
101 s.print(1);
102 s.print(2L);
103 s.print(Float.MAX_VALUE);
104 s.print(Double.MIN_VALUE);
105 s.println(true);
106 s.println('a');
107 s.println(1);
108 s.println(2L);
109 s.println(Float.MAX_VALUE);
110 s.println(Double.MIN_VALUE);
111 s.write('C');
112 }
113 }
114
115
116
117
118
119 public void testNotOverriddenBehavior() {
120 ThrowableInformation ti = new ThrowableInformation(new NotOverriddenThrowable());
121 String[] rep = ti.getThrowableStrRep();
122 assertEquals(7, rep.length);
123 StringBuffer buf = new StringBuffer(String.valueOf(true));
124 buf.append('a');
125 buf.append(String.valueOf(1));
126 buf.append(String.valueOf(2L));
127 buf.append(String.valueOf(Float.MAX_VALUE));
128 buf.append(String.valueOf(Double.MIN_VALUE));
129 buf.append(String.valueOf(true));
130 assertEquals(buf.toString(), rep[0]);
131 assertEquals("a", rep[1]);
132 assertEquals(String.valueOf(1), rep[2]);
133 assertEquals(String.valueOf(2L), rep[3]);
134 assertEquals(String.valueOf(Float.MAX_VALUE), rep[4]);
135 assertEquals(String.valueOf(Double.MIN_VALUE), rep[5]);
136 assertEquals("C", rep[6]);
137 }
138
139
140
141
142
143 private static final class NullThrowable extends Throwable {
144 private static final long serialVersionUID = 1L;
145
146
147
148 public NullThrowable() {
149 }
150
151
152
153
154
155
156 public void printStackTrace(final PrintWriter s) {
157 s.print((Object) null);
158 s.print((String) null);
159 s.println((Object) null);
160 s.println((String) null);
161 }
162 }
163
164
165
166
167
168
169 public void testNull() {
170 ThrowableInformation ti = new ThrowableInformation(new NullThrowable());
171 String[] rep = ti.getThrowableStrRep();
172 assertEquals(2, rep.length);
173 String nullStr = String.valueOf((Object) null);
174 assertEquals(nullStr + nullStr + nullStr, rep[0]);
175 assertEquals(nullStr, rep[1]);
176 }
177
178
179
180
181 private static final class EmptyThrowable extends Throwable {
182 private static final long serialVersionUID = 1L;
183
184
185
186 public EmptyThrowable() {
187 }
188
189
190
191
192
193
194 public void printStackTrace(final PrintWriter s) {
195 }
196 }
197
198
199
200
201
202
203 public void testEmpty() {
204 ThrowableInformation ti = new ThrowableInformation(new EmptyThrowable());
205 String[] rep = ti.getThrowableStrRep();
206 assertEquals(0, rep.length);
207 }
208
209
210
211
212 private static final class StringThrowable extends Throwable {
213 private static final long serialVersionUID = 1L;
214
215
216
217 private final String stackTrace;
218
219
220
221
222 public StringThrowable(final String trace) {
223 stackTrace = trace;
224 }
225
226
227
228
229
230
231 public void printStackTrace(final PrintWriter s) {
232 s.print(stackTrace);
233 }
234 }
235
236
237
238
239 public void testLineFeed() {
240 ThrowableInformation ti = new ThrowableInformation(new StringThrowable("\n"));
241 String[] rep = ti.getThrowableStrRep();
242 assertEquals(1, rep.length);
243 assertEquals("", rep[0]);
244 }
245
246
247
248
249 public void testCarriageReturn() {
250 ThrowableInformation ti = new ThrowableInformation(new StringThrowable("\r"));
251 String[] rep = ti.getThrowableStrRep();
252 assertEquals(1, rep.length);
253 assertEquals("", rep[0]);
254 }
255
256
257
258
259 public void testParsing() {
260 ThrowableInformation ti = new ThrowableInformation(
261 new StringThrowable("Line1\rLine2\nLine3\r\nLine4\n\rLine6"));
262 String[] rep = ti.getThrowableStrRep();
263 assertEquals(6, rep.length);
264 assertEquals("Line1", rep[0]);
265 assertEquals("Line2", rep[1]);
266 assertEquals("Line3", rep[2]);
267 assertEquals("Line4", rep[3]);
268 assertEquals("", rep[4]);
269 assertEquals("Line6", rep[5]);
270 }
271
272
273
274
275 public void testLineFeedBlank() {
276 ThrowableInformation ti = new ThrowableInformation(new StringThrowable("\n "));
277 String[] rep = ti.getThrowableStrRep();
278 assertEquals(2, rep.length);
279 assertEquals("", rep[0]);
280 assertEquals(" ", rep[1]);
281 }
282
283
284
285
286 public void testGetThrowable() {
287 Throwable t = new StringThrowable("Hello, World");
288 ThrowableInformation ti = new ThrowableInformation(t);
289 assertSame(t, ti.getThrowable());
290 }
291
292
293
294
295
296
297
298 public void testIsolation() {
299 ThrowableInformation ti = new ThrowableInformation(
300 new StringThrowable("Hello, World"));
301 String[] rep = ti.getThrowableStrRep();
302 assertEquals("Hello, World", rep[0]);
303 rep[0] = "Bonjour, Monde";
304 String[] rep2 = ti.getThrowableStrRep();
305 assertEquals("Hello, World", rep2[0]);
306 }
307
308
309
310
311
312 private static final class NastyThrowable extends Throwable {
313 private static final long serialVersionUID = 1L;
314
315
316
317 public NastyThrowable() {
318 }
319
320
321
322
323
324
325 public void printStackTrace(final PrintWriter s) {
326 s.print("NastyException");
327 throw new RuntimeException("Intentional exception");
328 }
329 }
330
331
332
333
334
335
336 public void testNastyException() {
337 ThrowableInformation ti = new ThrowableInformation(
338 new NastyThrowable());
339 String[] rep = ti.getThrowableStrRep();
340 assertEquals("NastyException", rep[0]);
341 }
342
343 }