1 16 17 package org.apache.commons.latka; 18 19 import java.io.IOException ; 21 import java.io.PrintWriter ; 22 import java.io.StringWriter ; 23 import org.apache.commons.latka.event.LatkaEventListener; 25 import org.apache.commons.latka.event.ReportMessageEvent; 26 import org.apache.commons.latka.event.RequestErrorEvent; 27 import org.apache.commons.latka.event.RequestEvent; 28 import org.apache.commons.latka.event.RequestFailedEvent; 29 import org.apache.commons.latka.event.SuiteEvent; 30 import org.apache.commons.latka.http.Request; 31 import org.dom4j.Document; 33 import org.dom4j.DocumentFactory; 34 import org.dom4j.Element; 35 import org.dom4j.io.OutputFormat; 36 import org.dom4j.io.XMLWriter; 37 38 51 public class XMLReporter extends AbstractReporter implements LatkaEventListener { 52 53 protected Document _doc = null; 54 55 protected Element _rootElement = null; 56 57 protected PrintWriter _printWriter = new PrintWriter (System.out); 58 59 protected boolean _didSuiteSucceed = true; 60 61 protected DocumentFactory _factory = new DocumentFactory(); 62 63 67 public XMLReporter() { 68 _rootElement = _factory.createElement("report"); 69 _doc = _factory.createDocument(_rootElement); 71 _rootElement.addAttribute("suiteSuccess", "false"); 72 } 73 74 79 public Document getDocument() { 80 return _doc; 81 } 82 83 91 public String getDocumentAsString() throws IOException { 92 StringWriter writer = new StringWriter (); 94 XMLWriter outputter = new XMLWriter(writer, new OutputFormat(" ", true)); 95 96 outputter.write(getDocument()); 97 98 String xmlDocument = writer.toString(); 99 100 return xmlDocument; 101 } 102 103 107 public void requestError(RequestEvent event) { 108 _didSuiteSucceed = false; 109 110 _printWriter.print("E"); 111 _printWriter.flush(); 112 113 RequestErrorEvent errorEvent = (RequestErrorEvent) event; 114 Element label = _factory.createElement("label") 117 .addText(errorEvent.getError().toString()); 118 Element requestError = _factory.createElement("requestError"); 119 requestError.add(label); 120 121 Element requestElement = createRequestElement(event); 122 requestElement.add(requestError); 123 _rootElement.add(requestElement); 124 125 } 126 127 131 public void requestSucceeded(RequestEvent event) { 132 133 _printWriter.print("."); 134 _printWriter.flush(); 135 136 Element requestElement = createRequestElement(event); 137 requestElement.add(_factory.createElement("requestSuccess")); 138 _rootElement.add(requestElement); 139 } 140 141 145 public void requestFailed(RequestEvent event) { 146 _didSuiteSucceed = false; 147 148 _printWriter.print("F"); 149 _printWriter.flush(); 150 151 RequestFailedEvent failedEvent = (RequestFailedEvent) event; 152 153 Element label = _factory.createElement("label") 156 .addText(failedEvent.getValidationException() 157 .getMessage() 158 .toString()); 159 Element requestFailure = _factory.createElement("requestFailure") 160 .addAttribute("responseId", 161 event.getResponse().toString() 162 ); 163 requestFailure.add(label); 164 165 Element requestElement = createRequestElement(event); 166 requestElement.add(requestFailure); 167 _rootElement.add(requestElement); 168 } 169 170 174 public void requestSkipped(RequestEvent event) { 175 _didSuiteSucceed = false; 176 177 _printWriter.print("S"); 178 _printWriter.flush(); 179 180 Request request = event.getRequest(); 181 Element requestElement = createRequestElement(event); 183 requestElement.add(_factory.createElement("requestSkipped")); 184 _rootElement.add(requestElement); 185 186 } 187 188 194 public void reportMessage(ReportMessageEvent event) { 195 Element messageElement = _factory.createElement("reportMessage") 196 .addAttribute("message", event.getMessage()); 197 _rootElement.add(messageElement); 198 } 199 200 204 public void suiteCompleted(SuiteEvent event) { 205 _rootElement.addAttribute("suiteSuccess", 206 String.valueOf(_didSuiteSucceed)); 207 } 208 209 216 protected Element createRequestElement(RequestEvent event) { 217 Request request = event.getRequest(); 219 220 Element requestElement = _factory.createElement("request") 221 .addAttribute("requestTime", 222 String.valueOf(request.getRequestTiming())) 223 .addAttribute("url", request.getURL().toString()); 224 225 String label = request.getLabel(); 226 if (label != null) { 227 requestElement.addAttribute("label", label); 228 } 229 230 return requestElement; 231 } 232 233 241 public void setPrintWriter(PrintWriter writer) { 242 _printWriter = writer; 243 } 244 245 } 246 | Popular Tags |