1 20 package org.apache.cactus.internal; 21 22 import java.io.PrintWriter ; 23 import java.io.Serializable ; 24 import java.io.StringWriter ; 25 26 33 public class WebTestResult implements Serializable 34 { 35 38 public static final String XML_ROOT_ELEMENT = "webresult"; 39 40 43 public static final String XML_EXCEPTION_ELEMENT = "exception"; 44 45 49 public static final String XML_EXCEPTION_CLASSNAME_ATTRIBUTE = "classname"; 50 51 54 public static final String XML_EXCEPTION_MESSAGE_ELEMENT = "message"; 55 56 59 public static final String XML_EXCEPTION_STACKTRACE_ELEMENT = "stacktrace"; 60 61 64 private String exceptionClassName; 65 66 71 private String exceptionStackTrace; 72 73 76 private String exceptionMessage; 77 78 81 public WebTestResult() 82 { 83 } 84 85 90 public WebTestResult(Throwable theException) 91 { 92 this.exceptionClassName = theException.getClass().getName(); 93 this.exceptionMessage = theException.getMessage(); 94 95 StringWriter sw = new StringWriter (); 97 PrintWriter pw = new PrintWriter (sw); 98 99 theException.printStackTrace(pw); 100 this.exceptionStackTrace = sw.toString(); 101 } 102 103 113 public WebTestResult(String theClassName, String theMessage, 114 String theStackTrace) 115 { 116 this.exceptionClassName = theClassName; 117 this.exceptionMessage = theMessage; 118 this.exceptionStackTrace = theStackTrace; 119 } 120 121 125 public String getExceptionClassName() 126 { 127 return this.exceptionClassName; 128 } 129 130 134 public String getExceptionMessage() 135 { 136 return this.exceptionMessage; 137 } 138 139 142 public boolean hasException() 143 { 144 return (this.exceptionClassName != null); 145 } 146 147 150 public String getExceptionStackTrace() 151 { 152 return this.exceptionStackTrace; 153 } 154 155 158 public String toString() 159 { 160 StringBuffer buffer = new StringBuffer (); 161 162 if (hasException()) 163 { 164 buffer.append("Test failed, Exception message = [" 165 + getExceptionMessage() + "]"); 166 } 167 else 168 { 169 buffer.append("Test ok"); 170 } 171 172 return buffer.toString(); 173 } 174 175 179 public String toXml() 180 { 181 StringBuffer xmlText = new StringBuffer (); 182 183 xmlText.append("<" + XML_ROOT_ELEMENT + ">"); 184 185 if (hasException()) 186 { 187 xmlText.append("<" + XML_EXCEPTION_ELEMENT + " " 188 + XML_EXCEPTION_CLASSNAME_ATTRIBUTE + "=\""); 189 xmlText.append(this.exceptionClassName); 190 xmlText.append("\">"); 191 xmlText.append("<" + XML_EXCEPTION_MESSAGE_ELEMENT + "><![CDATA["); 192 xmlText.append(this.exceptionMessage); 193 xmlText.append("]]></" + XML_EXCEPTION_MESSAGE_ELEMENT + ">"); 194 xmlText.append("<" + XML_EXCEPTION_STACKTRACE_ELEMENT 195 + "><![CDATA["); 196 xmlText.append(this.exceptionStackTrace); 197 xmlText.append("]]></" + XML_EXCEPTION_STACKTRACE_ELEMENT + ">"); 198 xmlText.append("</" + XML_EXCEPTION_ELEMENT + ">"); 199 } 200 201 xmlText.append("</" + XML_ROOT_ELEMENT + ">"); 202 203 return xmlText.toString(); 204 } 205 } 206 | Popular Tags |