1 /*
2 * ========================================================================
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * ========================================================================
20 */
21 package org.apache.cactus.internal.client;
22
23 import java.io.PrintStream;
24 import java.io.PrintWriter;
25
26 /**
27 * Wrapper around a <code>Throwable</code> object. Whenever an exception occurs
28 * in a test case executed on the server side, the text of this exception
29 * along with the stack trace as a String are sent back in the HTTP response.
30 * This is because some exceptions are not serializable and because the stack
31 * trace is implemented as a <code>transient</code> variable by the JDK so it
32 * cannot be transported in the response. However, we need to send a real
33 * exception object to JUnit so that the exception stack trace will be printed
34 * in the JUnit console. This class does this by being a <code>Throwable</code>
35 * and overloading the <code>printStackTrace()</code> methods to print a
36 * text stack trace.
37 *
38 * @version $Id: ServletExceptionWrapper.java 238991 2004-05-22 11:34:50Z vmassol $
39 */
40 public class ServletExceptionWrapper extends Throwable
41 {
42 /**
43 * The stack trace that was sent back from the servlet redirector as a
44 * string.
45 */
46 private String stackTrace;
47
48 /**
49 * The class name of the exception that was raised on the server side.
50 */
51 private String className;
52
53 /**
54 * Standard throwable constructor.
55 *
56 * @param theMessage the exception message
57 */
58 public ServletExceptionWrapper(String theMessage)
59 {
60 super(theMessage);
61 }
62
63 /**
64 * Standard throwable constructor.
65 */
66 public ServletExceptionWrapper()
67 {
68 super();
69 }
70
71 /**
72 * The constructor to use to simulate a real exception.
73 *
74 * @param theMessage the server exception message
75 * @param theClassName the server exception class name
76 * @param theStackTrace the server exception stack trace
77 */
78 public ServletExceptionWrapper(String theMessage, String theClassName,
79 String theStackTrace)
80 {
81 super(theMessage);
82 this.className = theClassName;
83 this.stackTrace = theStackTrace;
84 }
85
86 /**
87 * Simulates a printing of a stack trace by printing the string stack trace.
88 *
89 * @param thePs the stream to which to output the stack trace
90 */
91 public void printStackTrace(PrintStream thePs)
92 {
93 if (this.stackTrace == null)
94 {
95 thePs.print(getMessage());
96 }
97 else
98 {
99 thePs.print(this.stackTrace);
100 }
101 }
102
103 /**
104 * Simulates a printing of a stack trace by printing the string stack trace.
105 *
106 * @param thePw the writer to which to output the stack trace
107 */
108 public void printStackTrace(PrintWriter thePw)
109 {
110 if (this.stackTrace == null)
111 {
112 thePw.print(getMessage());
113 }
114 else
115 {
116 thePw.print(this.stackTrace);
117 }
118 }
119
120 /**
121 * @return the wrapped class name
122 */
123 public String getWrappedClassName()
124 {
125 return this.className;
126 }
127 }