1 7 package com.inversoft.junit; 8 9 10 import java.io.PrintWriter ; 11 import java.io.StringWriter ; 12 13 import org.apache.log4j.Logger; 14 import org.jdom.CDATA; 15 import org.jdom.Document; 16 import org.jdom.Element; 17 18 import com.inversoft.junit.internal.AssertionFailedErrorWrapper; 19 import com.inversoft.junit.internal.LocationExceptionWrapper; 20 21 22 26 public class Result { 27 30 private static final Logger logger = Logger.getLogger(Result.class); 31 32 35 public static final String XML_CLASS_NAME_ELEMENT = "className"; 36 37 40 public static final String XML_MESSAGE_ELEMENT = "message"; 41 42 45 public static final String XML_STACK_TRACE_ELEMENT = "stackTrace"; 46 47 50 public static final String XML_ROOT_ELEMENT = "result"; 51 52 55 public static final String JUNIT_ERROR_NAME = "junit.framework.AssertionFailedError"; 56 57 60 Throwable throwable; 61 62 65 boolean success; 66 67 70 String className; 71 72 75 String message; 76 77 80 String stackTrace; 81 82 83 86 public Result() 87 { 88 this.success = true; 89 } 90 91 95 public Result(Throwable throwable) 96 { 97 this.throwable = throwable; 98 this.success = false; 99 this.className = throwable.getClass().getName(); 100 this.message = throwable.getMessage(); 101 102 StringWriter sw = new StringWriter (); 104 PrintWriter pw = new PrintWriter (sw); 105 throwable.printStackTrace(pw); 106 this.stackTrace = sw.toString(); 107 } 108 109 110 117 public Object getStatus() 118 { 119 if (success) { 120 return Boolean.TRUE; 121 } 122 123 return Boolean.FALSE; 124 } 125 126 131 public Throwable getThrowable() 132 { 133 return throwable; 134 } 135 136 139 public boolean isSuccessful() 140 { 141 return success; 142 } 143 144 147 public String getClassName() 148 { 149 return className; 150 } 151 152 155 public String getMessage() 156 { 157 return message; 158 } 159 160 163 public String getStackTrace() 164 { 165 return stackTrace; 166 } 167 168 174 public Document toXML() { 175 Element root = new Element(XML_ROOT_ELEMENT); 176 177 if (success) { 178 return new Document(root); 179 } 180 181 Element classNameElem = new Element(XML_CLASS_NAME_ELEMENT); 182 Element messageElem = new Element(XML_MESSAGE_ELEMENT); 183 Element stackTraceElem = new Element(XML_STACK_TRACE_ELEMENT); 184 185 classNameElem.addContent(new CDATA(this.className)); 186 messageElem.addContent(new CDATA(this.message)); 187 stackTraceElem.addContent(new CDATA(this.stackTrace)); 188 189 root.addContent(classNameElem); 190 root.addContent(messageElem); 191 root.addContent(stackTraceElem); 192 193 return new Document(root); 194 } 195 196 202 public void fromXML(Document document) { 203 Element rootElem = document.getRootElement(); 204 Element classNameElem = rootElem.getChild(XML_CLASS_NAME_ELEMENT); 205 Element messageElem = rootElem.getChild(XML_MESSAGE_ELEMENT); 206 Element stackTraceElem = rootElem.getChild(XML_STACK_TRACE_ELEMENT); 207 208 if (classNameElem == null || messageElem == null || stackTraceElem == null) { 209 success = true; 210 return; 211 } 212 213 this.className = classNameElem.getText(); 214 this.message = messageElem.getText(); 215 this.stackTrace = stackTraceElem.getText(); 216 success = false; 217 218 if (logger.isDebugEnabled()) { 219 logger.debug("className: " + this.className); 220 logger.debug("message: " + this.message); 221 logger.debug("stackTrace: " + this.stackTrace); 222 } 223 224 if (this.className.equals(JUNIT_ERROR_NAME)) { 225 throwable = new AssertionFailedErrorWrapper(this.message, this.stackTrace); 226 } else { 227 throwable = new LocationExceptionWrapper(this.message, this.stackTrace); 228 } 229 } 230 } | Popular Tags |