|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| WebTestResult.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* ====================================================================
|
|
| 3 |
*
|
|
| 4 |
* The Apache Software License, Version 1.1
|
|
| 5 |
*
|
|
| 6 |
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
|
|
| 7 |
* reserved.
|
|
| 8 |
*
|
|
| 9 |
* Redistribution and use in source and binary forms, with or without
|
|
| 10 |
* modification, are permitted provided that the following conditions
|
|
| 11 |
* are met:
|
|
| 12 |
*
|
|
| 13 |
* 1. Redistributions of source code must retain the above copyright
|
|
| 14 |
* notice, this list of conditions and the following disclaimer.
|
|
| 15 |
*
|
|
| 16 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
| 17 |
* notice, this list of conditions and the following disclaimer in
|
|
| 18 |
* the documentation and/or other materials provided with the
|
|
| 19 |
* distribution.
|
|
| 20 |
*
|
|
| 21 |
* 3. The end-user documentation included with the redistribution, if
|
|
| 22 |
* any, must include the following acknowlegement:
|
|
| 23 |
* "This product includes software developed by the
|
|
| 24 |
* Apache Software Foundation (http://www.apache.org/)."
|
|
| 25 |
* Alternately, this acknowlegement may appear in the software itself,
|
|
| 26 |
* if and wherever such third-party acknowlegements normally appear.
|
|
| 27 |
*
|
|
| 28 |
* 4. The names "The Jakarta Project", "Cactus" and "Apache Software
|
|
| 29 |
* Foundation" must not be used to endorse or promote products
|
|
| 30 |
* derived from this software without prior written permission. For
|
|
| 31 |
* written permission, please contact apache@apache.org.
|
|
| 32 |
*
|
|
| 33 |
* 5. Products derived from this software may not be called "Apache"
|
|
| 34 |
* nor may "Apache" appear in their names without prior written
|
|
| 35 |
* permission of the Apache Group.
|
|
| 36 |
*
|
|
| 37 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
| 38 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
| 39 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
| 40 |
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
|
| 41 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
| 42 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
| 43 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
| 44 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
| 45 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
| 46 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
| 47 |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
| 48 |
* SUCH DAMAGE.
|
|
| 49 |
* ====================================================================
|
|
| 50 |
*
|
|
| 51 |
* This software consists of voluntary contributions made by many
|
|
| 52 |
* individuals on behalf of the Apache Software Foundation. For more
|
|
| 53 |
* information on the Apache Software Foundation, please see
|
|
| 54 |
* <http://www.apache.org/>.
|
|
| 55 |
*
|
|
| 56 |
*/
|
|
| 57 |
package org.apache.cactus;
|
|
| 58 |
|
|
| 59 |
import java.io.PrintWriter;
|
|
| 60 |
import java.io.Serializable;
|
|
| 61 |
import java.io.StringWriter;
|
|
| 62 |
|
|
| 63 |
/**
|
|
| 64 |
* Represent the result of the execution of the Test class by the
|
|
| 65 |
* server redirector.If any exception was raised during the test, it
|
|
| 66 |
* is saved by this class.
|
|
| 67 |
*
|
|
| 68 |
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
|
| 69 |
*
|
|
| 70 |
* @version $Id: WebTestResult.java,v 1.7 2003/05/26 11:45:23 cmlenz Exp $
|
|
| 71 |
*/
|
|
| 72 |
public class WebTestResult implements Serializable |
|
| 73 |
{
|
|
| 74 |
/**
|
|
| 75 |
* Name of Root XML tag (see {@link #toXml()}).
|
|
| 76 |
*/
|
|
| 77 |
public static final String XML_ROOT_ELEMENT = "webresult"; |
|
| 78 |
|
|
| 79 |
/**
|
|
| 80 |
* Name of Exception XML tag (see {@link #toXml()}).
|
|
| 81 |
*/
|
|
| 82 |
public static final String XML_EXCEPTION_ELEMENT = "exception"; |
|
| 83 |
|
|
| 84 |
/**
|
|
| 85 |
* Name of Exception XML attribute that contains the exception classname
|
|
| 86 |
* (see {@link #toXml()}).
|
|
| 87 |
*/
|
|
| 88 |
public static final String XML_EXCEPTION_CLASSNAME_ATTRIBUTE = "classname"; |
|
| 89 |
|
|
| 90 |
/**
|
|
| 91 |
* Name of Exception Message XML tag (see {@link #toXml()}).
|
|
| 92 |
*/
|
|
| 93 |
public static final String XML_EXCEPTION_MESSAGE_ELEMENT = "message"; |
|
| 94 |
|
|
| 95 |
/**
|
|
| 96 |
* Name of Exception Stacktrace XML tag (see {@link #toXml()}).
|
|
| 97 |
*/
|
|
| 98 |
public static final String XML_EXCEPTION_STACKTRACE_ELEMENT = "stacktrace"; |
|
| 99 |
|
|
| 100 |
/**
|
|
| 101 |
* Name of the exception class if an error occurred
|
|
| 102 |
*/
|
|
| 103 |
private String exceptionClassName;
|
|
| 104 |
|
|
| 105 |
/**
|
|
| 106 |
* Save the stack trace as text because otherwise it will not be
|
|
| 107 |
* transmitted back to the client (the stack trac field in the
|
|
| 108 |
* <code>Throwable</code> class is transient).
|
|
| 109 |
*/
|
|
| 110 |
private String exceptionStackTrace;
|
|
| 111 |
|
|
| 112 |
/**
|
|
| 113 |
* The exception message if an error occurred
|
|
| 114 |
*/
|
|
| 115 |
private String exceptionMessage;
|
|
| 116 |
|
|
| 117 |
/**
|
|
| 118 |
* Constructor to call when the test was ok and no error was raised.
|
|
| 119 |
*/
|
|
| 120 | 0 |
public WebTestResult()
|
| 121 |
{
|
|
| 122 |
} |
|
| 123 |
|
|
| 124 |
/**
|
|
| 125 |
* Constructor to call when an exception was raised during the test.
|
|
| 126 |
*
|
|
| 127 |
* @param theException the raised exception.
|
|
| 128 |
*/
|
|
| 129 | 0 |
public WebTestResult(Throwable theException)
|
| 130 |
{
|
|
| 131 | 0 |
this.exceptionClassName = theException.getClass().getName();
|
| 132 | 0 |
this.exceptionMessage = theException.getMessage();
|
| 133 |
|
|
| 134 |
// Save the stack trace as text
|
|
| 135 | 0 |
StringWriter sw = new StringWriter();
|
| 136 | 0 |
PrintWriter pw = new PrintWriter(sw);
|
| 137 |
|
|
| 138 | 0 |
theException.printStackTrace(pw); |
| 139 | 0 |
this.exceptionStackTrace = sw.toString();
|
| 140 |
} |
|
| 141 |
|
|
| 142 |
/**
|
|
| 143 |
* Constructor used to reconstruct a WebTestResult object from its String
|
|
| 144 |
* representation.
|
|
| 145 |
*
|
|
| 146 |
* @param theClassName the class name of the exception thrown on the server
|
|
| 147 |
* side
|
|
| 148 |
* @param theMessage the message of the exception thrown on the server side
|
|
| 149 |
* @param theStackTrace the stack trace of the exception thrown on the
|
|
| 150 |
* server side
|
|
| 151 |
*/
|
|
| 152 | 0 |
public WebTestResult(String theClassName, String theMessage,
|
| 153 |
String theStackTrace) |
|
| 154 |
{
|
|
| 155 | 0 |
this.exceptionClassName = theClassName;
|
| 156 | 0 |
this.exceptionMessage = theMessage;
|
| 157 | 0 |
this.exceptionStackTrace = theStackTrace;
|
| 158 |
} |
|
| 159 |
|
|
| 160 |
/**
|
|
| 161 |
* @return the exception class name if an exception was raised or
|
|
| 162 |
* <code>null</code> otherwise.
|
|
| 163 |
*/
|
|
| 164 | 0 |
public String getExceptionClassName()
|
| 165 |
{
|
|
| 166 | 0 |
return this.exceptionClassName; |
| 167 |
} |
|
| 168 |
|
|
| 169 |
/**
|
|
| 170 |
* @return the exception message if an exception was raised or
|
|
| 171 |
* <code>null</code> otherwise.
|
|
| 172 |
*/
|
|
| 173 | 0 |
public String getExceptionMessage()
|
| 174 |
{
|
|
| 175 | 0 |
return this.exceptionMessage; |
| 176 |
} |
|
| 177 |
|
|
| 178 |
/**
|
|
| 179 |
* @return true if an exception was raised during the test, false otherwise.
|
|
| 180 |
*/
|
|
| 181 | 0 |
public boolean hasException() |
| 182 |
{
|
|
| 183 | 0 |
return (this.exceptionClassName != null); |
| 184 |
} |
|
| 185 |
|
|
| 186 |
/**
|
|
| 187 |
* @return the stack trace as a string
|
|
| 188 |
*/
|
|
| 189 | 0 |
public String getExceptionStackTrace()
|
| 190 |
{
|
|
| 191 | 0 |
return this.exceptionStackTrace; |
| 192 |
} |
|
| 193 |
|
|
| 194 |
/**
|
|
| 195 |
* @see Object#toString()
|
|
| 196 |
*/
|
|
| 197 | 0 |
public String toString()
|
| 198 |
{
|
|
| 199 | 0 |
StringBuffer buffer = new StringBuffer();
|
| 200 |
|
|
| 201 | 0 |
if (hasException())
|
| 202 |
{
|
|
| 203 | 0 |
buffer.append("Test failed, Exception message = ["
|
| 204 |
+ getExceptionMessage() + "]");
|
|
| 205 |
} |
|
| 206 |
else
|
|
| 207 |
{
|
|
| 208 | 0 |
buffer.append("Test ok");
|
| 209 |
} |
|
| 210 |
|
|
| 211 | 0 |
return buffer.toString();
|
| 212 |
} |
|
| 213 |
|
|
| 214 |
/**
|
|
| 215 |
* @return an XML representation of the test result to be sent in the
|
|
| 216 |
* HTTP response to the Cactus client.
|
|
| 217 |
*/
|
|
| 218 | 0 |
public String toXml()
|
| 219 |
{
|
|
| 220 | 0 |
StringBuffer xmlText = new StringBuffer();
|
| 221 |
|
|
| 222 | 0 |
xmlText.append("<" + XML_ROOT_ELEMENT + ">"); |
| 223 |
|
|
| 224 | 0 |
if (hasException())
|
| 225 |
{
|
|
| 226 | 0 |
xmlText.append("<" + XML_EXCEPTION_ELEMENT + " " |
| 227 |
+ XML_EXCEPTION_CLASSNAME_ATTRIBUTE + "=\"");
|
|
| 228 | 0 |
xmlText.append(this.exceptionClassName);
|
| 229 | 0 |
xmlText.append("\">");
|
| 230 | 0 |
xmlText.append("<" + XML_EXCEPTION_MESSAGE_ELEMENT + "><![CDATA["); |
| 231 | 0 |
xmlText.append(this.exceptionMessage);
|
| 232 | 0 |
xmlText.append("]]></" + XML_EXCEPTION_MESSAGE_ELEMENT + ">"); |
| 233 | 0 |
xmlText.append("<" + XML_EXCEPTION_STACKTRACE_ELEMENT
|
| 234 |
+ "><![CDATA[");
|
|
| 235 | 0 |
xmlText.append(this.exceptionStackTrace);
|
| 236 | 0 |
xmlText.append("]]></" + XML_EXCEPTION_STACKTRACE_ELEMENT + ">"); |
| 237 | 0 |
xmlText.append("</" + XML_EXCEPTION_ELEMENT + ">"); |
| 238 |
} |
|
| 239 |
|
|
| 240 | 0 |
xmlText.append("</" + XML_ROOT_ELEMENT + ">"); |
| 241 |
|
|
| 242 | 0 |
return xmlText.toString();
|
| 243 |
} |
|
| 244 |
} |
|
| 245 |
|
|
||||||||||