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

For more information, please explore the Attic.

View Javadoc

1   package org.apache.cactus.internal;
2   /* 
3    * ========================================================================
4    * 
5    * Licensed to the Apache Software Foundation (ASF) under one or more
6    * contributor license agreements.  See the NOTICE file distributed with
7    * this work for additional information regarding copyright ownership.
8    * The ASF licenses this file to You under the Apache License, Version 2.0
9    * (the "License"); you may not use this file except in compliance with
10   * the License.  You may obtain a copy of the License at
11   * 
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   * 
20   * ========================================================================
21   */
22  import java.io.PrintWriter;
23  import java.io.StringWriter;
24  
25  /**
26   * Prototype of EJBRedirector for Cactus.
27   * @author Siddhartha P. Chandurkar (siddhartha@visioncodified.com)
28   */
29  public class EJBTestResult 
30  {
31      /**
32           * Name of the exception class if an error occurred
33           */
34      private String exceptionClassName;
35  
36      /**
37       * Save the stack trace as text because otherwise it will not be
38       * transmitted back to the client (the stack trac field in the
39       * <code>Throwable</code> class is transient).
40       */
41      private String exceptionStackTrace;
42  
43      /**
44       * The exception message if an error occurred
45       */
46      private String exceptionMessage;
47  
48      /**
49       * Name of Root XML tag (see {@link #toXml()}).
50       */
51      public static final String XML_ROOT_ELEMENT = "webresult";
52  
53      /**
54       * Name of Exception XML tag (see {@link #toXml()}).
55       */
56      public static final String XML_EXCEPTION_ELEMENT = "exception";
57  
58      /**
59       * Name of Exception XML attribute that contains the exception classname
60       * (see {@link #toXml()}).
61       */
62      public static final String XML_EXCEPTION_CLASSNAME_ATTRIBUTE = "classname";
63  
64      /**
65       * Name of Exception Message XML tag (see {@link #toXml()}).
66       */
67      public static final String XML_EXCEPTION_MESSAGE_ELEMENT = "message";
68  
69      /**
70       * Name of Exception Stacktrace XML tag (see {@link #toXml()}).
71       */
72      public static final String XML_EXCEPTION_STACKTRACE_ELEMENT = "stacktrace";
73  
74      /**
75       * Constructor to call when the test was ok and no error was raised.
76       */
77      public EJBTestResult() 
78      {
79      }
80  
81      /**
82       * Constructor to call when an exception was raised during the test.
83       *
84       * @param theException the raised exception.
85       */
86      public EJBTestResult(Throwable theException) 
87      {
88          this.exceptionClassName = theException.getClass().getName();
89          this.exceptionMessage = theException.getMessage();
90  
91          // Save the stack trace as text
92          StringWriter sw = new StringWriter();
93          PrintWriter pw = new PrintWriter(sw);
94          theException.printStackTrace(pw);
95          this.exceptionStackTrace = sw.toString();
96      }
97  
98      /**
99       * Constructor used to reconstruct a WebTestResult object from its String
100      * representation.
101      *
102      * @param theClassName the class name of the exception thrown on the server
103      *        side
104      * @param theMessage the message of the exception thrown on the server side
105      * @param theStackTrace the stack trace of the exception thrown on the
106      *        server side
107      */
108     public EJBTestResult(
109         String theClassName,
110         String theMessage,
111         String theStackTrace) 
112     {
113         this.exceptionClassName = theClassName;
114         this.exceptionMessage = theMessage;
115         this.exceptionStackTrace = theStackTrace;
116     }
117 
118     /**
119      * @return the exception class name if an exception was raised or
120      *         <code>null</code> otherwise.
121      */
122     public String getExceptionClassName() 
123     {
124         return this.exceptionClassName;
125     }
126 
127     /**
128      * @return the exception message if an exception was raised or
129      *         <code>null</code> otherwise.
130      */
131     public String getExceptionMessage() 
132     {
133         return this.exceptionMessage;
134     }
135 
136     /**
137      * @return true if an exception was raised during the test, false otherwise.
138      */
139     public boolean hasException() 
140     {
141         return (this.exceptionClassName != null);
142     }
143 
144     /**
145      * @return the stack trace as a string
146      */
147     public String getExceptionStackTrace() 
148     {
149         return this.exceptionStackTrace;
150     }
151 
152     /**
153      * @see Object#toString()
154      */
155     public String toString() 
156     {
157         StringBuffer buffer = new StringBuffer();
158 
159         if (hasException()) 
160         {
161             buffer.append(
162                 "Test failed, Exception message = ["
163                     + getExceptionMessage()
164                     + "]");
165         } 
166         else 
167         {
168             buffer.append("Test ok");
169         }
170 
171         return buffer.toString();
172     }
173 
174     /**
175      * @return an XML representation of the test result to be sent in the
176      *         HTTP response to the Cactus client.
177      */
178     public String toXml() 
179     {
180         StringBuffer xmlText = new StringBuffer();
181         xmlText.append("<" + XML_ROOT_ELEMENT + ">");
182 
183         if (hasException()) 
184         {
185             xmlText.append(
186                 "<"
187                     + XML_EXCEPTION_ELEMENT
188                     + " "
189                     + XML_EXCEPTION_CLASSNAME_ATTRIBUTE
190                     + "=\"");
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(
197                 "<" + XML_EXCEPTION_STACKTRACE_ELEMENT + "><![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 }