KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > internal > WebTestResult


1 /*
2  * ========================================================================
3  *
4  * Copyright 2001-2004 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;
21
22 import java.io.PrintWriter JavaDoc;
23 import java.io.Serializable JavaDoc;
24 import java.io.StringWriter JavaDoc;
25
26 /**
27  * Represent the result of the execution of the Test class by the
28  * server redirector. If any exception was raised during the test, it
29  * is saved by this class.
30  *
31  * @version $Id: WebTestResult.java,v 1.1 2004/05/22 11:34:47 vmassol Exp $
32  */

33 public class WebTestResult implements Serializable JavaDoc
34 {
35     /**
36      * Name of Root XML tag (see {@link #toXml()}).
37      */

38     public static final String JavaDoc XML_ROOT_ELEMENT = "webresult";
39
40     /**
41      * Name of Exception XML tag (see {@link #toXml()}).
42      */

43     public static final String JavaDoc XML_EXCEPTION_ELEMENT = "exception";
44
45     /**
46      * Name of Exception XML attribute that contains the exception classname
47      * (see {@link #toXml()}).
48      */

49     public static final String JavaDoc XML_EXCEPTION_CLASSNAME_ATTRIBUTE = "classname";
50
51     /**
52      * Name of Exception Message XML tag (see {@link #toXml()}).
53      */

54     public static final String JavaDoc XML_EXCEPTION_MESSAGE_ELEMENT = "message";
55
56     /**
57      * Name of Exception Stacktrace XML tag (see {@link #toXml()}).
58      */

59     public static final String JavaDoc XML_EXCEPTION_STACKTRACE_ELEMENT = "stacktrace";
60
61     /**
62      * Name of the exception class if an error occurred
63      */

64     private String JavaDoc exceptionClassName;
65
66     /**
67      * Save the stack trace as text because otherwise it will not be
68      * transmitted back to the client (the stack trac field in the
69      * <code>Throwable</code> class is transient).
70      */

71     private String JavaDoc exceptionStackTrace;
72
73     /**
74      * The exception message if an error occurred
75      */

76     private String JavaDoc exceptionMessage;
77
78     /**
79      * Constructor to call when the test was ok and no error was raised.
80      */

81     public WebTestResult()
82     {
83     }
84
85     /**
86      * Constructor to call when an exception was raised during the test.
87      *
88      * @param theException the raised exception.
89      */

90     public WebTestResult(Throwable JavaDoc theException)
91     {
92         this.exceptionClassName = theException.getClass().getName();
93         this.exceptionMessage = theException.getMessage();
94
95         // Save the stack trace as text
96
StringWriter JavaDoc sw = new StringWriter JavaDoc();
97         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
98
99         theException.printStackTrace(pw);
100         this.exceptionStackTrace = sw.toString();
101     }
102
103     /**
104      * Constructor used to reconstruct a WebTestResult object from its String
105      * representation.
106      *
107      * @param theClassName the class name of the exception thrown on the server
108      * side
109      * @param theMessage the message of the exception thrown on the server side
110      * @param theStackTrace the stack trace of the exception thrown on the
111      * server side
112      */

113     public WebTestResult(String JavaDoc theClassName, String JavaDoc theMessage,
114         String JavaDoc theStackTrace)
115     {
116         this.exceptionClassName = theClassName;
117         this.exceptionMessage = theMessage;
118         this.exceptionStackTrace = theStackTrace;
119     }
120
121     /**
122      * @return the exception class name if an exception was raised or
123      * <code>null</code> otherwise.
124      */

125     public String JavaDoc getExceptionClassName()
126     {
127         return this.exceptionClassName;
128     }
129
130     /**
131      * @return the exception message if an exception was raised or
132      * <code>null</code> otherwise.
133      */

134     public String JavaDoc getExceptionMessage()
135     {
136         return this.exceptionMessage;
137     }
138
139     /**
140      * @return true if an exception was raised during the test, false otherwise.
141      */

142     public boolean hasException()
143     {
144         return (this.exceptionClassName != null);
145     }
146
147     /**
148      * @return the stack trace as a string
149      */

150     public String JavaDoc getExceptionStackTrace()
151     {
152         return this.exceptionStackTrace;
153     }
154
155     /**
156      * @see Object#toString()
157      */

158     public String JavaDoc toString()
159     {
160         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
161
162         if (hasException())
163         {
164             buffer.append("Test failed, Exception message = ["
165                 + getExceptionMessage() + "]");
166         }
167         else
168         {
169             buffer.append("Test ok");
170         }
171
172         return buffer.toString();
173     }
174
175     /**
176      * @return an XML representation of the test result to be sent in the
177      * HTTP response to the Cactus client.
178      */

179     public String JavaDoc toXml()
180     {
181         StringBuffer JavaDoc xmlText = new StringBuffer JavaDoc();
182
183         xmlText.append("<" + XML_ROOT_ELEMENT + ">");
184
185         if (hasException())
186         {
187             xmlText.append("<" + XML_EXCEPTION_ELEMENT + " "
188                 + XML_EXCEPTION_CLASSNAME_ATTRIBUTE + "=\"");
189             xmlText.append(this.exceptionClassName);
190             xmlText.append("\">");
191             xmlText.append("<" + XML_EXCEPTION_MESSAGE_ELEMENT + "><![CDATA[");
192             xmlText.append(this.exceptionMessage);
193             xmlText.append("]]></" + XML_EXCEPTION_MESSAGE_ELEMENT + ">");
194             xmlText.append("<" + XML_EXCEPTION_STACKTRACE_ELEMENT
195                 + "><![CDATA[");
196             xmlText.append(this.exceptionStackTrace);
197             xmlText.append("]]></" + XML_EXCEPTION_STACKTRACE_ELEMENT + ">");
198             xmlText.append("</" + XML_EXCEPTION_ELEMENT + ">");
199         }
200
201         xmlText.append("</" + XML_ROOT_ELEMENT + ">");
202
203         return xmlText.toString();
204     }
205 }
206
Popular Tags