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 package org.apache.commons.httpclient;
30
31 import java.io.ByteArrayOutputStream;
32 import java.io.IOException;
33 import java.io.PrintStream;
34 import java.io.PrintWriter;
35 import java.io.StringWriter;
36
37 import org.apache.commons.httpclient.auth.*;
38
39 import junit.framework.Test;
40 import junit.framework.TestCase;
41 import junit.framework.TestSuite;
42
43 /***
44 *
45 * @author <a href="mailto:laura@lwerner.org">Laura Werner</a>
46 */
47 public class TestExceptions extends TestCase
48 {
49
50
51 public TestExceptions(String testName) {
52 super(testName);
53 }
54
55
56 public static void main(String args[]) {
57 String[] testCaseName = { TestChallengeParser.class.getName() };
58 junit.textui.TestRunner.main(testCaseName);
59 }
60
61
62
63 public static Test suite() {
64 return new TestSuite(TestChallengeParser.class);
65 }
66
67 /*** Make sure that you can retrieve the "cause" from an HttpException */
68 public void testGetCause() {
69
70 Exception aCause = new IOException("the cause");
71
72 try {
73 throw new HttpException("http exception", aCause);
74 }
75 catch (HttpException e) {
76 assertEquals("Retrieve cause from caught exception", e.getCause(), aCause);
77 }
78 }
79
80 /*** Make sure HttpConnection prints its stack trace to a PrintWriter properly */
81 public void testStackTraceWriter() {
82
83 Exception aCause = new IOException("initial exception");
84 try {
85 throw new HttpException("http exception", aCause);
86 }
87 catch (HttpException e) {
88
89 StringWriter stringWriter = new StringWriter();
90 PrintWriter writer = new PrintWriter(stringWriter);
91 e.printStackTrace(writer);
92 writer.flush();
93 String stackTrace = stringWriter.toString();
94
95
96 validateStackTrace(e, stackTrace);
97 }
98 }
99
100 /*** Make sure HttpConnection prints its stack trace to a PrintStream properly */
101 public void testStackTraceStream() {
102
103 Exception aCause = new IOException("initial exception");
104 try {
105 throw new HttpException("http exception", aCause);
106 }
107 catch (HttpException e) {
108
109 ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
110 PrintStream stream = new PrintStream(byteStream);
111 e.printStackTrace(stream);
112 stream.flush();
113 String stackTrace = byteStream.toString();
114
115
116 validateStackTrace(e, stackTrace);
117 }
118 }
119
120 /***
121 * Make sure an HttpException stack trace has the right info in it.
122 * This doesn't bother parsing the whole thing, just does some sanity checks.
123 */
124 private void validateStackTrace(HttpException exception, String stackTrace) {
125 assertTrue("Starts with exception string", stackTrace.startsWith(exception.toString()));
126
127 Throwable cause = exception.getCause();
128 if (cause != null) {
129 assertTrue("Contains 'cause'", stackTrace.toLowerCase().indexOf("cause") != -1);
130 assertTrue("Contains cause.toString()", stackTrace.indexOf(cause.toString()) != -1);
131 }
132 }
133 }