1 19 20 21 package org.apache.slide.webdav.logger; 22 23 import java.io.IOException ; 24 import java.util.Enumeration ; 25 import java.util.Iterator ; 26 import java.util.Vector ; 27 28 import org.apache.slide.webdav.util.WebdavStatus; 29 import org.jdom.CDATA; 30 import org.jdom.Document; 31 import org.jdom.Element; 32 import org.jdom.output.XMLOutputter; 33 34 35 40 41 42 public class XMLTestCaseGenerator { 43 44 45 46 private XMLOutputter xmlOut = new XMLOutputter(org.jdom.output.Format.getPrettyFormat()); 48 49 private XHttpServletRequestFacade request; 50 private XHttpServletResponseFacade response; 51 52 private Element root = new Element( "test" ); 53 private Document doc = new Document( root ); 54 55 56 59 private String threadName; 60 61 public void setThreadName( String threadName ) { 62 this.threadName = threadName; 63 } 64 public String getThreadName() { 65 return threadName; 66 } 67 68 69 70 73 public XMLTestCaseGenerator( XHttpServletRequestFacade request, XHttpServletResponseFacade response){ 74 this.request = request; 75 this.response = response; 76 } 77 78 81 public String toString() { 82 String result = ""; 83 84 root.addContent( printXMLstep() ); 85 try { 86 result = xmlOut.outputString( doc.getRootElement().getChild("step") ); 87 } catch ( Exception e ) { 88 e.printStackTrace(); 89 } 90 return result; 91 } 92 93 96 private Element printXMLstep() { 97 Element stepElem = new Element( "step" ); 98 stepElem.addContent( printXMLrequest() ); 100 stepElem.addContent( printXMLresponse() ); 101 return stepElem; 102 } 103 106 private Element printXMLrequest() { 107 Element stepElem = new Element( "request" ); 108 stepElem.addContent( printXMLrequestCommand() ); 109 Iterator it = printXMLrequestHeaders(); 110 while ( it.hasNext() ) { 111 stepElem.addContent( (Element)it.next() ); 112 } 113 stepElem.addContent( printXMLrequestBody() ); 114 return stepElem; 115 } 116 117 120 private Element printXMLresponse() { 121 Element respElem = new Element( "response" ); 122 respElem.addContent( printXMLresponseCommand() ); 123 Iterator it = printXMLresponseHeaders(); 124 while ( it.hasNext() ) { 125 respElem.addContent( (Element)it.next() ); 126 } 127 respElem.addContent( printXMLresponseBody() ); 128 return respElem; 129 } 130 131 134 private Element printXMLrequestCommand() { 135 Element reqComElem = new Element( "command" ); 136 reqComElem.addContent( request.getMethod() + " " + request.getRequestURI() + " " + getProtocol() ); 137 return reqComElem; 138 } 139 140 141 144 private Iterator printXMLrequestHeaders() { 145 Vector vector = new Vector (); 146 Enumeration e = request.getHeaderNames(); 147 if ( e != null ) { 148 while ( e.hasMoreElements() ) { 149 String headerName = (String )e.nextElement(); 150 String headerValue = request.getHeader(headerName); 151 Element elem = new Element( "header" ); 152 elem.addContent( headerName + ": " + headerValue ); 153 vector.add( elem ); 154 } 155 } 156 return vector.iterator(); 157 } 158 159 162 private Element printXMLrequestBody(){ 163 Element bodyElem = new Element( "body" ); 164 try { 165 bodyElem.addContent( new CDATA(request.getRequestBody()) ); 166 } 167 catch (IOException e) { 168 e.printStackTrace(); 169 } 170 return bodyElem; 171 } 172 173 176 private Element printXMLresponseCommand() { 177 Element respComElem = new Element( "command" ); 178 respComElem.addContent( 179 getProtocol() + " " + 180 response.getStatus() + " " + 181 WebdavStatus.getStatusText(response.getStatus())); 182 return respComElem; 183 } 184 185 188 private String getProtocol() { 189 String result = request.getProtocol(); 190 while (result.endsWith("\n")) { 191 result = result.substring(0, result.length()-"\n".length()-1); 192 } 193 return result; 194 } 195 196 199 private Iterator printXMLresponseHeaders() { 200 Vector vector = new Vector (); 201 XResponseHeader respHeader; 202 Enumeration e = response.getResponseHeaders(); 203 if ( e != null ) { 204 while ( e.hasMoreElements() ) { 205 Element elem = new Element( "header" ); 206 elem.addContent( ((XResponseHeader)e.nextElement()).toString() ); 207 vector.add( elem ); 208 } 209 } 210 return vector.iterator(); 211 } 212 213 216 private Element printXMLresponseBody() { 217 Element bodyElem = new Element( "body" ); 218 try { 219 bodyElem.addContent( new CDATA(response.getResponseBody()) ); 220 } catch ( IOException e ) { 221 e.printStackTrace(); 222 } 223 return bodyElem; 224 } 225 226 } 227 | Popular Tags |