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 * Unit tests for ThrowableInformation.
26 */
27 public class ThrowableInformationTest extends TestCase {
28 /***
29 * Create ThrowableInformationTest.
30 *
31 * @param name test name.
32 */
33 public ThrowableInformationTest(final String name) {
34 super(name);
35 }
36
37 /***
38 * Custom throwable that only calls methods
39 * overridden by VectorWriter in log4j 1.2.14 and earlier.
40 */
41 private static final class OverriddenThrowable extends Throwable {
42 /***
43 * Create new instance.
44 */
45 public OverriddenThrowable() {
46 }
47
48 /***
49 * Print stack trace.
50 *
51 * @param s print writer.
52 */
53 public void printStackTrace(final PrintWriter s) {
54 s.print((Object) "print(Object)");
55 s.print("print(char[])".toCharArray());
56 s.print("print(String)");
57 s.println((Object) "println(Object)");
58 s.println("println(char[])".toCharArray());
59 s.println("println(String)");
60 s.write("write(char[])".toCharArray());
61 s.write("write(char[], int, int)".toCharArray(), 2, 8);
62 s.write("write(String, int, int)", 2, 8);
63 }
64 }
65
66 /***
67 * Test capturing stack trace from a throwable that only uses the
68 * PrintWriter methods overridden in log4j 1.2.14 and earlier.
69 */
70 public void testOverriddenBehavior() {
71 ThrowableInformation ti = new ThrowableInformation(new OverriddenThrowable());
72 String[] rep = ti.getThrowableStrRep();
73 assertEquals(4, rep.length);
74 assertEquals("print(Object)print(char[])print(String)println(Object)", rep[0]);
75 assertEquals("println(char[])", rep[1]);
76 assertEquals("println(String)", rep[2]);
77 assertEquals("write(char[])ite(charite(Stri", rep[3]);
78 }
79
80 /***
81 * Custom throwable that calls methods
82 * not overridden by VectorWriter in log4j 1.2.14 and earlier.
83 */
84 private static final class NotOverriddenThrowable extends Throwable {
85 /***
86 * Create new instance.
87 */
88 public NotOverriddenThrowable() {
89 }
90
91 /***
92 * Print stack trace.
93 *
94 * @param s print writer.
95 */
96 public void printStackTrace(final PrintWriter s) {
97 s.print(true);
98 s.print('a');
99 s.print(1);
100 s.print(2L);
101 s.print(Float.MAX_VALUE);
102 s.print(Double.MIN_VALUE);
103 s.println(true);
104 s.println('a');
105 s.println(1);
106 s.println(2L);
107 s.println(Float.MAX_VALUE);
108 s.println(Double.MIN_VALUE);
109 s.write('C');
110 }
111 }
112
113 /***
114 * Test capturing stack trace from a throwable that uses the
115 * PrintWriter methods not overridden in log4j 1.2.14 and earlier.
116 */
117 public void testNotOverriddenBehavior() {
118 ThrowableInformation ti = new ThrowableInformation(new NotOverriddenThrowable());
119 String[] rep = ti.getThrowableStrRep();
120 assertEquals(7, rep.length);
121 StringBuffer buf = new StringBuffer(String.valueOf(true));
122 buf.append('a');
123 buf.append(String.valueOf(1));
124 buf.append(String.valueOf(2L));
125 buf.append(String.valueOf(Float.MAX_VALUE));
126 buf.append(String.valueOf(Double.MIN_VALUE));
127 buf.append(String.valueOf(true));
128 assertEquals(buf.toString(), rep[0]);
129 assertEquals("a", rep[1]);
130 assertEquals(String.valueOf(1), rep[2]);
131 assertEquals(String.valueOf(2L), rep[3]);
132 assertEquals(String.valueOf(Float.MAX_VALUE), rep[4]);
133 assertEquals(String.valueOf(Double.MIN_VALUE), rep[5]);
134 assertEquals("C", rep[6]);
135 }
136
137 /***
138 * Custom throwable that calls methods of VectorWriter
139 * with null.
140 */
141 private static final class NullThrowable extends Throwable {
142 /***
143 * Create new instance.
144 */
145 public NullThrowable() {
146 }
147
148 /***
149 * Print stack trace.
150 *
151 * @param s print writer.
152 */
153 public void printStackTrace(final PrintWriter s) {
154 s.print((Object) null);
155 s.print((String) null);
156 s.println((Object) null);
157 s.println((String) null);
158 }
159 }
160
161 /***
162 * Test capturing stack trace from a throwable that passes
163 * null to PrintWriter methods.
164 */
165
166 public void testNull() {
167 ThrowableInformation ti = new ThrowableInformation(new NullThrowable());
168 String[] rep = ti.getThrowableStrRep();
169 assertEquals(2, rep.length);
170 String nullStr = String.valueOf((Object) null);
171 assertEquals(nullStr + nullStr + nullStr, rep[0]);
172 assertEquals(nullStr, rep[1]);
173 }
174
175 /***
176 * Custom throwable that does nothing in printStackTrace.
177 */
178 private static final class EmptyThrowable extends Throwable {
179 /***
180 * Create new instance.
181 */
182 public EmptyThrowable() {
183 }
184
185 /***
186 * Print stack trace.
187 *
188 * @param s print writer.
189 */
190 public void printStackTrace(final PrintWriter s) {
191 }
192 }
193
194 /***
195 * Test capturing stack trace from a throwable that
196 * does nothing on a call to printStackTrace.
197 */
198
199 public void testEmpty() {
200 ThrowableInformation ti = new ThrowableInformation(new EmptyThrowable());
201 String[] rep = ti.getThrowableStrRep();
202 assertEquals(0, rep.length);
203 }
204
205 /***
206 * Custom throwable that emits a specified string in printStackTrace.
207 */
208 private static final class StringThrowable extends Throwable {
209 /***
210 * Stack trace.
211 */
212 private final String stackTrace;
213 /***
214 * Create new instance.
215 * @param trace stack trace.
216 */
217 public StringThrowable(final String trace) {
218 stackTrace = trace;
219 }
220
221 /***
222 * Print stack trace.
223 *
224 * @param s print writer.
225 */
226 public void printStackTrace(final PrintWriter s) {
227 s.print(stackTrace);
228 }
229 }
230
231 /***
232 * Test capturing stack trace from throwable that just has a line feed.
233 */
234 public void testLineFeed() {
235 ThrowableInformation ti = new ThrowableInformation(new StringThrowable("\n"));
236 String[] rep = ti.getThrowableStrRep();
237 assertEquals(1, rep.length);
238 assertEquals("", rep[0]);
239 }
240
241 /***
242 * Test capturing stack trace from throwable that just has a carriage return.
243 */
244 public void testCarriageReturn() {
245 ThrowableInformation ti = new ThrowableInformation(new StringThrowable("\r"));
246 String[] rep = ti.getThrowableStrRep();
247 assertEquals(1, rep.length);
248 assertEquals("", rep[0]);
249 }
250
251 /***
252 * Test parsing of line breaks.
253 */
254 public void testParsing() {
255 ThrowableInformation ti = new ThrowableInformation(
256 new StringThrowable("Line1\rLine2\nLine3\r\nLine4\n\rLine6"));
257 String[] rep = ti.getThrowableStrRep();
258 assertEquals(6, rep.length);
259 assertEquals("Line1", rep[0]);
260 assertEquals("Line2", rep[1]);
261 assertEquals("Line3", rep[2]);
262 assertEquals("Line4", rep[3]);
263 assertEquals("", rep[4]);
264 assertEquals("Line6", rep[5]);
265 }
266
267 /***
268 * Test capturing stack trace from throwable that a line feed followed by blank.
269 */
270 public void testLineFeedBlank() {
271 ThrowableInformation ti = new ThrowableInformation(new StringThrowable("\n "));
272 String[] rep = ti.getThrowableStrRep();
273 assertEquals(2, rep.length);
274 assertEquals("", rep[0]);
275 assertEquals(" ", rep[1]);
276 }
277
278 /***
279 * Test that getThrowable returns the throwable provided to the constructor.
280 */
281 public void testGetThrowable() {
282 Throwable t = new StringThrowable("Hello, World");
283 ThrowableInformation ti = new ThrowableInformation(t);
284 assertSame(t, ti.getThrowable());
285 }
286 }