|
|||||||||||||||||||
| 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 | |||||||||||||||
| WebTestResultParser.java | 16.7% | 27.7% | 33.3% | 26.2% |
|
||||||||||||||
| 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.client;
|
|
| 21 |
|
|
| 22 |
import org.apache.cactus.internal.WebTestResult;
|
|
| 23 |
|
|
| 24 |
/**
|
|
| 25 |
* Parse a string representing a Test result and transform it into a
|
|
| 26 |
* <code>WebTestResult</code> object.
|
|
| 27 |
*
|
|
| 28 |
* @see WebTestResult
|
|
| 29 |
*
|
|
| 30 |
* @version $Id: WebTestResultParser.java 238991 2004-05-22 11:34:50Z vmassol $
|
|
| 31 |
*/
|
|
| 32 |
public class WebTestResultParser |
|
| 33 |
{
|
|
| 34 |
/**
|
|
| 35 |
* Parsed exception class name
|
|
| 36 |
*/
|
|
| 37 |
protected String exceptionClassname;
|
|
| 38 |
|
|
| 39 |
/**
|
|
| 40 |
* Parsed exception message
|
|
| 41 |
*/
|
|
| 42 |
protected String exceptionMessage;
|
|
| 43 |
|
|
| 44 |
/**
|
|
| 45 |
* Parsed exception stack trace
|
|
| 46 |
*/
|
|
| 47 |
protected String exceptionStacktrace;
|
|
| 48 |
|
|
| 49 |
/**
|
|
| 50 |
* Parse a string and transform it into a <code>WebTestResult</code> object.
|
|
| 51 |
*
|
|
| 52 |
* @param theData the string to parse
|
|
| 53 |
* @return the <code>WebTestResult</code> object corresponding to the data
|
|
| 54 |
* string
|
|
| 55 |
* @exception ParsingException if an error happens during parsing
|
|
| 56 |
*/
|
|
| 57 | 24 |
public WebTestResult parse(String theData) throws ParsingException |
| 58 |
{
|
|
| 59 | 24 |
String buffer; |
| 60 | 24 |
WebTestResult result; |
| 61 |
|
|
| 62 | 24 |
buffer = readRootElement(theData); |
| 63 |
|
|
| 64 | 24 |
if (buffer.length() == 0)
|
| 65 |
{
|
|
| 66 | 24 |
result = new WebTestResult();
|
| 67 |
} |
|
| 68 |
else
|
|
| 69 |
{
|
|
| 70 | 0 |
buffer = readExceptionClassname(buffer); |
| 71 | 0 |
buffer = readExceptionMessage(buffer); |
| 72 | 0 |
buffer = readExceptionStacktrace(buffer); |
| 73 | 0 |
result = new WebTestResult(this.exceptionClassname, |
| 74 |
this.exceptionMessage, this.exceptionStacktrace); |
|
| 75 |
} |
|
| 76 |
|
|
| 77 | 24 |
return result;
|
| 78 |
} |
|
| 79 |
|
|
| 80 |
/**
|
|
| 81 |
* Read the {@link WebTestResult#XML_ROOT_ELEMENT} portion.
|
|
| 82 |
*
|
|
| 83 |
* @param theData the string buffer to parse
|
|
| 84 |
* @return the string buffer minus what has been read
|
|
| 85 |
* @exception ParsingException if an error happens during parsing
|
|
| 86 |
*/
|
|
| 87 | 24 |
protected String readRootElement(String theData) throws ParsingException |
| 88 |
{
|
|
| 89 | 24 |
String startRootString = "<" + WebTestResult.XML_ROOT_ELEMENT + ">"; |
| 90 | 24 |
String endRootString = "</" + WebTestResult.XML_ROOT_ELEMENT + ">"; |
| 91 | 24 |
String buffer; |
| 92 |
|
|
| 93 |
// It is possible that some end of line character are inserted at the
|
|
| 94 |
// end of the string. This is valid, which is why we trim the string
|
|
| 95 |
// before perfoming the checks.
|
|
| 96 | 24 |
String trimmedData = theData.trim(); |
| 97 |
|
|
| 98 | 24 |
if (trimmedData.startsWith(startRootString)
|
| 99 |
&& trimmedData.endsWith(endRootString)) |
|
| 100 |
{
|
|
| 101 | 24 |
buffer = trimmedData.substring(startRootString.length(), |
| 102 |
trimmedData.length() - endRootString.length()); |
|
| 103 |
} |
|
| 104 |
else
|
|
| 105 |
{
|
|
| 106 | 0 |
throw new ParsingException(formatError(theData)); |
| 107 |
} |
|
| 108 |
|
|
| 109 | 24 |
return buffer;
|
| 110 |
} |
|
| 111 |
|
|
| 112 |
/**
|
|
| 113 |
* Read the {@link WebTestResult#XML_EXCEPTION_CLASSNAME_ATTRIBUTE} portion
|
|
| 114 |
* and extract the exception classname.
|
|
| 115 |
*
|
|
| 116 |
* @param theData the string buffer to parse
|
|
| 117 |
* @return the string buffer minus what has been read
|
|
| 118 |
* @exception ParsingException if an error happens during parsing
|
|
| 119 |
*/
|
|
| 120 | 0 |
protected String readExceptionClassname(String theData)
|
| 121 |
throws ParsingException
|
|
| 122 |
{
|
|
| 123 | 0 |
String startString = "<" + WebTestResult.XML_EXCEPTION_ELEMENT + " " |
| 124 |
+ WebTestResult.XML_EXCEPTION_CLASSNAME_ATTRIBUTE + "=\"";
|
|
| 125 | 0 |
String endString = "</" + WebTestResult.XML_EXCEPTION_ELEMENT + ">"; |
| 126 | 0 |
String buffer; |
| 127 |
|
|
| 128 | 0 |
if (theData.startsWith(startString) && theData.endsWith(endString))
|
| 129 |
{
|
|
| 130 | 0 |
int pos = theData.indexOf('\"', startString.length());
|
| 131 |
|
|
| 132 | 0 |
this.exceptionClassname = theData.substring(startString.length(),
|
| 133 |
pos); |
|
| 134 | 0 |
buffer = theData.substring(startString.length() |
| 135 |
+ this.exceptionClassname.length() + 2,
|
|
| 136 |
theData.length() - endString.length()); |
|
| 137 |
} |
|
| 138 |
else
|
|
| 139 |
{
|
|
| 140 | 0 |
throw new ParsingException(formatError(theData)); |
| 141 |
} |
|
| 142 |
|
|
| 143 | 0 |
return buffer;
|
| 144 |
} |
|
| 145 |
|
|
| 146 |
/**
|
|
| 147 |
* Read the {@link WebTestResult#XML_EXCEPTION_MESSAGE_ELEMENT} portion
|
|
| 148 |
* and extract the exception message.
|
|
| 149 |
*
|
|
| 150 |
* @param theData the string buffer to parse
|
|
| 151 |
* @return the string buffer minus what has been read
|
|
| 152 |
* @exception ParsingException if an error happens during parsing
|
|
| 153 |
*/
|
|
| 154 | 0 |
protected String readExceptionMessage(String theData)
|
| 155 |
throws ParsingException
|
|
| 156 |
{
|
|
| 157 | 0 |
String startString = "<" + WebTestResult.XML_EXCEPTION_MESSAGE_ELEMENT
|
| 158 |
+ "><![CDATA[";
|
|
| 159 | 0 |
String endString = "]]></"
|
| 160 |
+ WebTestResult.XML_EXCEPTION_MESSAGE_ELEMENT + ">";
|
|
| 161 | 0 |
String buffer; |
| 162 |
|
|
| 163 | 0 |
if (theData.startsWith(startString))
|
| 164 |
{
|
|
| 165 | 0 |
int pos = theData.indexOf(endString, startString.length());
|
| 166 |
|
|
| 167 | 0 |
this.exceptionMessage = theData.substring(startString.length(),
|
| 168 |
pos); |
|
| 169 | 0 |
buffer = theData.substring(pos + endString.length()); |
| 170 |
} |
|
| 171 |
else
|
|
| 172 |
{
|
|
| 173 | 0 |
throw new ParsingException(formatError(theData)); |
| 174 |
} |
|
| 175 |
|
|
| 176 | 0 |
return buffer;
|
| 177 |
} |
|
| 178 |
|
|
| 179 |
/**
|
|
| 180 |
* Read the {@link WebTestResult#XML_EXCEPTION_STACKTRACE_ELEMENT} portion
|
|
| 181 |
* and extract the exception stacktrace.
|
|
| 182 |
*
|
|
| 183 |
* @param theData the string buffer to parse
|
|
| 184 |
* @return the string buffer minus what has been read
|
|
| 185 |
* @exception ParsingException if an error happens during parsing
|
|
| 186 |
*/
|
|
| 187 | 0 |
protected String readExceptionStacktrace(String theData)
|
| 188 |
throws ParsingException
|
|
| 189 |
{
|
|
| 190 | 0 |
String startString = "<"
|
| 191 |
+ WebTestResult.XML_EXCEPTION_STACKTRACE_ELEMENT + "><![CDATA[";
|
|
| 192 | 0 |
String endString = "]]></"
|
| 193 |
+ WebTestResult.XML_EXCEPTION_STACKTRACE_ELEMENT + ">";
|
|
| 194 | 0 |
String buffer; |
| 195 |
|
|
| 196 | 0 |
if (theData.startsWith(startString))
|
| 197 |
{
|
|
| 198 | 0 |
int pos = theData.indexOf(endString, startString.length());
|
| 199 |
|
|
| 200 | 0 |
this.exceptionStacktrace = theData.substring(startString.length(),
|
| 201 |
pos); |
|
| 202 | 0 |
buffer = theData.substring(pos + endString.length()); |
| 203 |
} |
|
| 204 |
else
|
|
| 205 |
{
|
|
| 206 | 0 |
throw new ParsingException(formatError(theData)); |
| 207 |
} |
|
| 208 |
|
|
| 209 | 0 |
return buffer;
|
| 210 |
} |
|
| 211 |
|
|
| 212 |
/**
|
|
| 213 |
* @param theData the data to format
|
|
| 214 |
* @return the first 100 characters (or less if the data has fewer
|
|
| 215 |
* characters) of the invalid data as it can be very big
|
|
| 216 |
*/
|
|
| 217 | 0 |
private String formatError(String theData)
|
| 218 |
{
|
|
| 219 | 0 |
int nbChars = theData.length() > 100 ? 100 : theData.length();
|
| 220 |
|
|
| 221 | 0 |
return "Not a valid response. First " + nbChars |
| 222 |
+ " characters of the reponse: ["
|
|
| 223 |
+ theData.substring(0, nbChars) + "]";
|
|
| 224 |
} |
|
| 225 |
} |
|
| 226 |
|
|
||||||||||