2011/08/05 - Jakarta Cactus has been retired.

For more information, please explore the Attic.

View Javadoc

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;
22  
23  import java.io.PrintWriter;
24  import java.io.Serializable;
25  import java.io.StringWriter;
26  
27  /**
28   * Represent the result of the execution of the Test class by the
29   * server redirector. If any exception was raised during the test, it
30   * is saved by this class.
31   *
32   * @version $Id: WebTestResult.java 238991 2004-05-22 11:34:50Z vmassol $
33   */
34  public class WebTestResult implements Serializable
35  {
36      /**
37       * Name of Root XML tag (see {@link #toXml()}).
38       */
39      public static final String XML_ROOT_ELEMENT = "webresult";
40  
41      /**
42       * Name of Exception XML tag (see {@link #toXml()}).
43       */
44      public static final String XML_EXCEPTION_ELEMENT = "exception";
45  
46      /**
47       * Name of Exception XML attribute that contains the exception classname
48       * (see {@link #toXml()}).
49       */
50      public static final String XML_EXCEPTION_CLASSNAME_ATTRIBUTE = "classname";
51  
52      /**
53       * Name of Exception Message XML tag (see {@link #toXml()}).
54       */
55      public static final String XML_EXCEPTION_MESSAGE_ELEMENT = "message";
56  
57      /**
58       * Name of Exception Stacktrace XML tag (see {@link #toXml()}).
59       */
60      public static final String XML_EXCEPTION_STACKTRACE_ELEMENT = "stacktrace";
61  
62      /**
63       * Name of the exception class if an error occurred.
64       */
65      private String exceptionClassName;
66  
67      /**
68       * Save the stack trace as text because otherwise it will not be
69       * transmitted back to the client (the stack trac field in the
70       * <code>Throwable</code> class is transient).
71       */
72      private String exceptionStackTrace;
73  
74      /**
75       * The exception message if an error occurred.
76       */
77      private String exceptionMessage;
78  
79      /**
80       * Constructor to call when the test was ok and no error was raised.
81       */
82      public WebTestResult()
83      {
84      }
85  
86      /**
87       * Constructor to call when an exception was raised during the test.
88       *
89       * @param theException the raised exception.
90       */
91      public WebTestResult(Throwable theException)
92      {
93          this.exceptionClassName = theException.getClass().getName();
94          this.exceptionMessage = theException.getMessage();
95  
96          // Save the stack trace as text
97          StringWriter sw = new StringWriter();
98          PrintWriter pw = new PrintWriter(sw);
99  
100         theException.printStackTrace(pw);
101         this.exceptionStackTrace = sw.toString();
102     }
103 
104     /**
105      * Constructor used to reconstruct a WebTestResult object from its String
106      * representation.
107      *
108      * @param theClassName the class name of the exception thrown on the server
109      *        side
110      * @param theMessage the message of the exception thrown on the server side
111      * @param theStackTrace the stack trace of the exception thrown on the
112      *        server side
113      */
114     public WebTestResult(String theClassName, String theMessage, 
115         String theStackTrace)
116     {
117         this.exceptionClassName = theClassName;
118         this.exceptionMessage = theMessage;
119         this.exceptionStackTrace = theStackTrace;
120     }
121 
122     /**
123      * @return the exception class name if an exception was raised or
124      *         <code>null</code> otherwise.
125      */
126     public String getExceptionClassName()
127     {
128         return this.exceptionClassName;
129     }
130 
131     /**
132      * @return the exception message if an exception was raised or
133      *         <code>null</code> otherwise.
134      */
135     public String getExceptionMessage()
136     {
137         return this.exceptionMessage;
138     }
139 
140     /**
141      * @return true if an exception was raised during the test, false otherwise.
142      */
143     public boolean hasException()
144     {
145         return (this.exceptionClassName != null);
146     }
147 
148     /**
149      * @return the stack trace as a string
150      */
151     public String getExceptionStackTrace()
152     {
153         return this.exceptionStackTrace;
154     }
155 
156     /**
157      * {@inheritDoc}
158      * @see Object#toString()
159      */
160     public String toString()
161     {
162         StringBuffer buffer = new StringBuffer();
163 
164         if (hasException())
165         {
166             buffer.append("Test failed, Exception message = ["
167                 + getExceptionMessage() + "]");
168         }
169         else
170         {
171             buffer.append("Test ok");
172         }
173 
174         return buffer.toString();
175     }
176 
177     /**
178      * @return an XML representation of the test result to be sent in the
179      *         HTTP response to the Cactus client.
180      */
181     public String toXml()
182     {
183         StringBuffer xmlText = new StringBuffer();
184 
185         xmlText.append("<" + XML_ROOT_ELEMENT + ">");
186 
187         if (hasException())
188         {
189             xmlText.append("<" + XML_EXCEPTION_ELEMENT + " "
190                 + XML_EXCEPTION_CLASSNAME_ATTRIBUTE + "=\"");
191             xmlText.append(this.exceptionClassName);
192             xmlText.append("\">");
193             xmlText.append("<" + XML_EXCEPTION_MESSAGE_ELEMENT + "><![CDATA[");
194             xmlText.append(this.exceptionMessage);
195             xmlText.append("]]></" + XML_EXCEPTION_MESSAGE_ELEMENT + ">");
196             xmlText.append("<" + XML_EXCEPTION_STACKTRACE_ELEMENT
197                 + "><![CDATA[");
198             xmlText.append(this.exceptionStackTrace);
199             xmlText.append("]]></" + XML_EXCEPTION_STACKTRACE_ELEMENT + ">");
200             xmlText.append("</" + XML_EXCEPTION_ELEMENT + ">");
201         }
202 
203         xmlText.append("</" + XML_ROOT_ELEMENT + ">");
204 
205         return xmlText.toString();
206     }
207 }