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