001: /*
002: * ========================================================================
003: *
004: * Copyright 2001-2004 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus.internal;
021:
022: import java.io.PrintWriter;
023: import java.io.Serializable;
024: import java.io.StringWriter;
025:
026: /**
027: * Represent the result of the execution of the Test class by the
028: * server redirector. If any exception was raised during the test, it
029: * is saved by this class.
030: *
031: * @version $Id: WebTestResult.java 238991 2004-05-22 11:34:50Z vmassol $
032: */
033: public class WebTestResult implements Serializable {
034: /**
035: * Name of Root XML tag (see {@link #toXml()}).
036: */
037: public static final String XML_ROOT_ELEMENT = "webresult";
038:
039: /**
040: * Name of Exception XML tag (see {@link #toXml()}).
041: */
042: public static final String XML_EXCEPTION_ELEMENT = "exception";
043:
044: /**
045: * Name of Exception XML attribute that contains the exception classname
046: * (see {@link #toXml()}).
047: */
048: public static final String XML_EXCEPTION_CLASSNAME_ATTRIBUTE = "classname";
049:
050: /**
051: * Name of Exception Message XML tag (see {@link #toXml()}).
052: */
053: public static final String XML_EXCEPTION_MESSAGE_ELEMENT = "message";
054:
055: /**
056: * Name of Exception Stacktrace XML tag (see {@link #toXml()}).
057: */
058: public static final String XML_EXCEPTION_STACKTRACE_ELEMENT = "stacktrace";
059:
060: /**
061: * Name of the exception class if an error occurred
062: */
063: private String exceptionClassName;
064:
065: /**
066: * Save the stack trace as text because otherwise it will not be
067: * transmitted back to the client (the stack trac field in the
068: * <code>Throwable</code> class is transient).
069: */
070: private String exceptionStackTrace;
071:
072: /**
073: * The exception message if an error occurred
074: */
075: private String exceptionMessage;
076:
077: /**
078: * Constructor to call when the test was ok and no error was raised.
079: */
080: public WebTestResult() {
081: }
082:
083: /**
084: * Constructor to call when an exception was raised during the test.
085: *
086: * @param theException the raised exception.
087: */
088: public WebTestResult(Throwable theException) {
089: this .exceptionClassName = theException.getClass().getName();
090: this .exceptionMessage = theException.getMessage();
091:
092: // Save the stack trace as text
093: StringWriter sw = new StringWriter();
094: PrintWriter pw = new PrintWriter(sw);
095:
096: theException.printStackTrace(pw);
097: this .exceptionStackTrace = sw.toString();
098: }
099:
100: /**
101: * Constructor used to reconstruct a WebTestResult object from its String
102: * representation.
103: *
104: * @param theClassName the class name of the exception thrown on the server
105: * side
106: * @param theMessage the message of the exception thrown on the server side
107: * @param theStackTrace the stack trace of the exception thrown on the
108: * server side
109: */
110: public WebTestResult(String theClassName, String theMessage,
111: String theStackTrace) {
112: this .exceptionClassName = theClassName;
113: this .exceptionMessage = theMessage;
114: this .exceptionStackTrace = theStackTrace;
115: }
116:
117: /**
118: * @return the exception class name if an exception was raised or
119: * <code>null</code> otherwise.
120: */
121: public String getExceptionClassName() {
122: return this .exceptionClassName;
123: }
124:
125: /**
126: * @return the exception message if an exception was raised or
127: * <code>null</code> otherwise.
128: */
129: public String getExceptionMessage() {
130: return this .exceptionMessage;
131: }
132:
133: /**
134: * @return true if an exception was raised during the test, false otherwise.
135: */
136: public boolean hasException() {
137: return (this .exceptionClassName != null);
138: }
139:
140: /**
141: * @return the stack trace as a string
142: */
143: public String getExceptionStackTrace() {
144: return this .exceptionStackTrace;
145: }
146:
147: /**
148: * @see Object#toString()
149: */
150: public String toString() {
151: StringBuffer buffer = new StringBuffer();
152:
153: if (hasException()) {
154: buffer.append("Test failed, Exception message = ["
155: + getExceptionMessage() + "]");
156: } else {
157: buffer.append("Test ok");
158: }
159:
160: return buffer.toString();
161: }
162:
163: /**
164: * @return an XML representation of the test result to be sent in the
165: * HTTP response to the Cactus client.
166: */
167: public String toXml() {
168: StringBuffer xmlText = new StringBuffer();
169:
170: xmlText.append("<" + XML_ROOT_ELEMENT + ">");
171:
172: if (hasException()) {
173: xmlText.append("<" + XML_EXCEPTION_ELEMENT + " "
174: + XML_EXCEPTION_CLASSNAME_ATTRIBUTE + "=\"");
175: xmlText.append(this .exceptionClassName);
176: xmlText.append("\">");
177: xmlText.append("<" + XML_EXCEPTION_MESSAGE_ELEMENT
178: + "><![CDATA[");
179: xmlText.append(this .exceptionMessage);
180: xmlText.append("]]></" + XML_EXCEPTION_MESSAGE_ELEMENT
181: + ">");
182: xmlText.append("<" + XML_EXCEPTION_STACKTRACE_ELEMENT
183: + "><![CDATA[");
184: xmlText.append(this .exceptionStackTrace);
185: xmlText.append("]]></" + XML_EXCEPTION_STACKTRACE_ELEMENT
186: + ">");
187: xmlText.append("</" + XML_EXCEPTION_ELEMENT + ">");
188: }
189:
190: xmlText.append("</" + XML_ROOT_ELEMENT + ">");
191:
192: return xmlText.toString();
193: }
194: }
|