1   /*
2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestExceptions.java,v 1.4 2004/03/25 20:37:20 olegk Exp $
3    * $Revision: 164398 $
4    * $Date: 2005-04-23 11:45:07 -0400 (Sat, 23 Apr 2005) $
5    *
6    * ====================================================================
7    *
8    *  Copyright 1999-2004 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
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 junit.framework.Test;
38  import junit.framework.TestCase;
39  import junit.framework.TestSuite;
40  
41  /***
42   * 
43   * @author <a href="mailto:laura@lwerner.org">Laura Werner</a>
44   */
45  public class TestExceptions extends TestCase
46  {
47  
48      // ------------------------------------------------------------ Constructor
49      public TestExceptions(String testName) {
50          super(testName);
51      }
52  
53      // ------------------------------------------------------------------- Main
54      public static void main(String args[]) {
55          String[] testCaseName = { TestExceptions.class.getName() };
56          junit.textui.TestRunner.main(testCaseName);
57      }
58  
59      // ------------------------------------------------------- TestCase Methods
60  
61      public static Test suite() {
62          return new TestSuite(TestExceptions.class);
63      }
64  
65      /*** Make sure that you can retrieve the "cause" from an HttpException */
66      public void testGetCause() {
67          
68          Exception aCause = new IOException("the cause");
69          
70          try {
71              throw new HttpException("http exception", aCause);
72          }
73          catch (HttpException e) {
74              assertEquals("Retrieve cause from caught exception", e.getCause(), aCause);
75          }
76      }
77      
78      /*** Make sure HttpConnection prints its stack trace to a PrintWriter properly */
79      public void testStackTraceWriter() {
80          
81          Exception aCause = new IOException("initial exception");
82          try {
83              throw new HttpException("http exception", aCause);
84          }
85          catch (HttpException e) {
86              // Get the stack trace printed into a string
87              StringWriter stringWriter = new StringWriter();
88              PrintWriter  writer = new PrintWriter(stringWriter);
89              e.printStackTrace(writer);
90              writer.flush();
91              String stackTrace = stringWriter.toString();
92              
93              // Do some validation on what got printed
94              validateStackTrace(e, stackTrace);
95          }
96      }
97      
98      /*** Make sure HttpConnection prints its stack trace to a PrintStream properly */
99      public void testStackTraceStream() {
100         
101         Exception aCause = new IOException("initial exception");
102         try {
103             throw new HttpException("http exception", aCause);
104         }
105         catch (HttpException e) {
106             // Get the stack trace printed into a string
107             ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
108             PrintStream  stream = new PrintStream(byteStream);
109             e.printStackTrace(stream);
110             stream.flush();
111             String stackTrace = byteStream.toString();  // Assume default charset
112             
113             // Do some validation on what got printed
114             validateStackTrace(e, stackTrace);
115         }
116     }
117     
118     /***
119      * Make sure an HttpException stack trace has the right info in it.
120      * This doesn't bother parsing the whole thing, just does some sanity checks.
121      */
122     private void validateStackTrace(HttpException exception, String stackTrace) {
123         assertTrue("Starts with exception string", stackTrace.startsWith(exception.toString()));
124         
125         Throwable cause = exception.getCause();
126         if (cause != null) {
127             assertTrue("Contains 'cause'", stackTrace.toLowerCase().indexOf("cause") != -1);
128             assertTrue("Contains cause.toString()", stackTrace.indexOf(cause.toString()) != -1);
129         }
130     }
131 }