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: 155418 $
4    * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 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 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      // ------------------------------------------------------------ Constructor
51      public TestExceptions(String testName) {
52          super(testName);
53      }
54  
55      // ------------------------------------------------------------------- Main
56      public static void main(String args[]) {
57          String[] testCaseName = { TestChallengeParser.class.getName() };
58          junit.textui.TestRunner.main(testCaseName);
59      }
60  
61      // ------------------------------------------------------- TestCase Methods
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              // Get the stack trace printed into a string
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              // Do some validation on what got printed
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             // Get the stack trace printed into a string
109             ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
110             PrintStream  stream = new PrintStream(byteStream);
111             e.printStackTrace(stream);
112             stream.flush();
113             String stackTrace = byteStream.toString();  // Assume default charset
114             
115             // Do some validation on what got printed
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 }