|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| WebTestResult.java | 50% | 30.3% | 44.4% | 34.8% |
|
||||||||||||||
| 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;
|
|
| 23 |
import java.io.Serializable;
|
|
| 24 |
import java.io.StringWriter;
|
|
| 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 238991 2004-05-22 11:34:50Z vmassol $
|
|
| 32 |
*/
|
|
| 33 |
public class WebTestResult implements Serializable |
|
| 34 |
{
|
|
| 35 |
/**
|
|
| 36 |
* Name of Root XML tag (see {@link #toXml()}).
|
|
| 37 |
*/
|
|
| 38 |
public static final String XML_ROOT_ELEMENT = "webresult"; |
|
| 39 |
|
|
| 40 |
/**
|
|
| 41 |
* Name of Exception XML tag (see {@link #toXml()}).
|
|
| 42 |
*/
|
|
| 43 |
public static final String 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 XML_EXCEPTION_CLASSNAME_ATTRIBUTE = "classname"; |
|
| 50 |
|
|
| 51 |
/**
|
|
| 52 |
* Name of Exception Message XML tag (see {@link #toXml()}).
|
|
| 53 |
*/
|
|
| 54 |
public static final String XML_EXCEPTION_MESSAGE_ELEMENT = "message"; |
|
| 55 |
|
|
| 56 |
/**
|
|
| 57 |
* Name of Exception Stacktrace XML tag (see {@link #toXml()}).
|
|
| 58 |
*/
|
|
| 59 |
public static final String XML_EXCEPTION_STACKTRACE_ELEMENT = "stacktrace"; |
|
| 60 |
|
|
| 61 |
/**
|
|
| 62 |
* Name of the exception class if an error occurred
|
|
| 63 |
*/
|
|
| 64 |
private String 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 exceptionStackTrace;
|
|
| 72 |
|
|
| 73 |
/**
|
|
| 74 |
* The exception message if an error occurred
|
|
| 75 |
*/
|
|
| 76 |
private String exceptionMessage;
|
|
| 77 |
|
|
| 78 |
/**
|
|
| 79 |
* Constructor to call when the test was ok and no error was raised.
|
|
| 80 |
*/
|
|
| 81 | 48 |
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 | 0 |
public WebTestResult(Throwable theException)
|
| 91 |
{
|
|
| 92 | 0 |
this.exceptionClassName = theException.getClass().getName();
|
| 93 | 0 |
this.exceptionMessage = theException.getMessage();
|
| 94 |
|
|
| 95 |
// Save the stack trace as text
|
|
| 96 | 0 |
StringWriter sw = new StringWriter();
|
| 97 | 0 |
PrintWriter pw = new PrintWriter(sw);
|
| 98 |
|
|
| 99 | 0 |
theException.printStackTrace(pw); |
| 100 | 0 |
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 | 0 |
public WebTestResult(String theClassName, String theMessage,
|
| 114 |
String theStackTrace) |
|
| 115 |
{
|
|
| 116 | 0 |
this.exceptionClassName = theClassName;
|
| 117 | 0 |
this.exceptionMessage = theMessage;
|
| 118 | 0 |
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 | 0 |
public String getExceptionClassName()
|
| 126 |
{
|
|
| 127 | 0 |
return this.exceptionClassName; |
| 128 |
} |
|
| 129 |
|
|
| 130 |
/**
|
|
| 131 |
* @return the exception message if an exception was raised or
|
|
| 132 |
* <code>null</code> otherwise.
|
|
| 133 |
*/
|
|
| 134 | 0 |
public String getExceptionMessage()
|
| 135 |
{
|
|
| 136 | 0 |
return this.exceptionMessage; |
| 137 |
} |
|
| 138 |
|
|
| 139 |
/**
|
|
| 140 |
* @return true if an exception was raised during the test, false otherwise.
|
|
| 141 |
*/
|
|
| 142 | 96 |
public boolean hasException() |
| 143 |
{
|
|
| 144 | 96 |
return (this.exceptionClassName != null); |
| 145 |
} |
|
| 146 |
|
|
| 147 |
/**
|
|
| 148 |
* @return the stack trace as a string
|
|
| 149 |
*/
|
|
| 150 | 0 |
public String getExceptionStackTrace()
|
| 151 |
{
|
|
| 152 | 0 |
return this.exceptionStackTrace; |
| 153 |
} |
|
| 154 |
|
|
| 155 |
/**
|
|
| 156 |
* @see Object#toString()
|
|
| 157 |
*/
|
|
| 158 | 48 |
public String toString()
|
| 159 |
{
|
|
| 160 | 48 |
StringBuffer buffer = new StringBuffer();
|
| 161 |
|
|
| 162 | 48 |
if (hasException())
|
| 163 |
{
|
|
| 164 | 0 |
buffer.append("Test failed, Exception message = ["
|
| 165 |
+ getExceptionMessage() + "]");
|
|
| 166 |
} |
|
| 167 |
else
|
|
| 168 |
{
|
|
| 169 | 48 |
buffer.append("Test ok");
|
| 170 |
} |
|
| 171 |
|
|
| 172 | 48 |
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 | 24 |
public String toXml()
|
| 180 |
{
|
|
| 181 | 24 |
StringBuffer xmlText = new StringBuffer();
|
| 182 |
|
|
| 183 | 24 |
xmlText.append("<" + XML_ROOT_ELEMENT + ">"); |
| 184 |
|
|
| 185 | 24 |
if (hasException())
|
| 186 |
{
|
|
| 187 | 0 |
xmlText.append("<" + XML_EXCEPTION_ELEMENT + " " |
| 188 |
+ XML_EXCEPTION_CLASSNAME_ATTRIBUTE + "=\"");
|
|
| 189 | 0 |
xmlText.append(this.exceptionClassName);
|
| 190 | 0 |
xmlText.append("\">");
|
| 191 | 0 |
xmlText.append("<" + XML_EXCEPTION_MESSAGE_ELEMENT + "><![CDATA["); |
| 192 | 0 |
xmlText.append(this.exceptionMessage);
|
| 193 | 0 |
xmlText.append("]]></" + XML_EXCEPTION_MESSAGE_ELEMENT + ">"); |
| 194 | 0 |
xmlText.append("<" + XML_EXCEPTION_STACKTRACE_ELEMENT
|
| 195 |
+ "><![CDATA[");
|
|
| 196 | 0 |
xmlText.append(this.exceptionStackTrace);
|
| 197 | 0 |
xmlText.append("]]></" + XML_EXCEPTION_STACKTRACE_ELEMENT + ">"); |
| 198 | 0 |
xmlText.append("</" + XML_EXCEPTION_ELEMENT + ">"); |
| 199 |
} |
|
| 200 |
|
|
| 201 | 24 |
xmlText.append("</" + XML_ROOT_ELEMENT + ">"); |
| 202 |
|
|
| 203 | 24 |
return xmlText.toString();
|
| 204 |
} |
|
| 205 |
} |
|
| 206 |
|
|
||||||||||