KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > internal > client > ServletExceptionWrapper


1 /*
2  * ========================================================================
3  *
4  * Copyright 2001-2003 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * ========================================================================
19  */

20 package org.apache.cactus.internal.client;
21
22 import java.io.PrintStream JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24
25 /**
26  * Wrapper around a <code>Throwable</code> object. Whenever an exception occurs
27  * in a test case executed on the server side, the text of this exception
28  * along with the stack trace as a String are sent back in the HTTP response.
29  * This is because some exceptions are not serializable and because the stack
30  * trace is implemented as a <code>transient</code> variable by the JDK so it
31  * cannot be transported in the response. However, we need to send a real
32  * exception object to JUnit so that the exception stack trace will be printed
33  * in the JUnit console. This class does this by being a <code>Throwable</code>
34  * and overloading the <code>printStackTrace()</code> methods to print a
35  * text stack trace.
36  *
37  * @version $Id: ServletExceptionWrapper.java,v 1.1 2004/05/22 11:34:47 vmassol Exp $
38  */

39 public class ServletExceptionWrapper extends Throwable JavaDoc
40 {
41     /**
42      * The stack trace that was sent back from the servlet redirector as a
43      * string.
44      */

45     private String JavaDoc stackTrace;
46
47     /**
48      * The class name of the exception that was raised on the server side.
49      */

50     private String JavaDoc className;
51
52     /**
53      * Standard throwable constructor.
54      *
55      * @param theMessage the exception message
56      */

57     public ServletExceptionWrapper(String JavaDoc theMessage)
58     {
59         super(theMessage);
60     }
61
62     /**
63      * Standard throwable constructor.
64      */

65     public ServletExceptionWrapper()
66     {
67         super();
68     }
69
70     /**
71      * The constructor to use to simulate a real exception.
72      *
73      * @param theMessage the server exception message
74      * @param theClassName the server exception class name
75      * @param theStackTrace the server exception stack trace
76      */

77     public ServletExceptionWrapper(String JavaDoc theMessage, String JavaDoc theClassName,
78         String JavaDoc theStackTrace)
79     {
80         super(theMessage);
81         this.className = theClassName;
82         this.stackTrace = theStackTrace;
83     }
84
85     /**
86      * Simulates a printing of a stack trace by printing the string stack trace
87      *
88      * @param thePs the stream to which to output the stack trace
89      */

90     public void printStackTrace(PrintStream JavaDoc thePs)
91     {
92         if (this.stackTrace == null)
93         {
94             thePs.print(getMessage());
95         }
96         else
97         {
98             thePs.print(this.stackTrace);
99         }
100     }
101
102     /**
103      * Simulates a printing of a stack trace by printing the string stack trace
104      *
105      * @param thePw the writer to which to output the stack trace
106      */

107     public void printStackTrace(PrintWriter JavaDoc thePw)
108     {
109         if (this.stackTrace == null)
110         {
111             thePw.print(getMessage());
112         }
113         else
114         {
115             thePw.print(this.stackTrace);
116         }
117     }
118
119     /**
120      * @return the wrapped class name
121      */

122     public String JavaDoc getWrappedClassName()
123     {
124         return this.className;
125     }
126 }
127
Popular Tags