1 11 package org.jboss.portal.junit; 12 13 import org.jboss.portal.common.util.XML; 14 import org.jboss.portal.common.util.Tools; 15 import org.jboss.portal.junit.response.ResultSetResponse; 16 import org.jboss.portal.junit.response.ErrorResponse; 17 import org.w3c.dom.Document ; 18 import org.w3c.dom.Element ; 19 import org.apache.commons.httpclient.HttpMethod; 20 import org.xml.sax.SAXException ; 21 22 import javax.xml.parsers.DocumentBuilder ; 23 import javax.xml.parsers.ParserConfigurationException ; 24 import java.io.ByteArrayInputStream ; 25 import java.io.IOException ; 26 import java.util.Map ; 27 import java.util.HashMap ; 28 import java.util.List ; 29 30 import junit.framework.AssertionFailedError; 31 32 38 public class ServerResponseFactory 39 { 40 41 public static ServerResponse create(HttpMethod method) throws IOException 42 { 43 try 44 { 45 int status = method.getStatusCode(); 46 if (status == 200) 47 { 48 String s = method.getResponseBodyAsString(); 49 DocumentBuilder builder = XML.getDocumentBuilderFactory().newDocumentBuilder(); 50 Document doc = builder.parse(new ByteArrayInputStream (s.getBytes("UTF-8"))); 51 Element root = doc.getDocumentElement(); 52 if ("results".equals(root.getNodeName())) 53 { 54 Map results = new HashMap (); 55 List portletElts = XML.getChildren(root, "result"); 56 for (int i = 0;i < portletElts.size();i++) 57 { 58 Element portletElt = (Element )portletElts.get(i); 59 Element idElt = XML.getUniqueChild(portletElt, "id", true); 60 Element contentElt = XML.getUniqueChild(portletElt, "content", true); 61 String id = XML.asString(idElt); 62 String content = XML.asString(contentElt); 63 Result result = ResultCodec.unmarshallResult(content); 64 results.put(id, result); 65 } 66 return new ResultSetResponse(results); 67 } 68 else if ("error".equals(root.getNodeName())) 69 { 70 Element messageElt = XML.getUniqueChild(root, "message", true); 71 String message = XML.asString(messageElt); 72 return new ErrorResponse(message); 73 } 74 } 75 return null; 76 } 77 catch (ParserConfigurationException e) 78 { 79 AssertionFailedError error = new AssertionFailedError(); 80 error.initCause(error); 81 throw error; 82 } 83 catch (SAXException e) 84 { 85 AssertionFailedError error = new AssertionFailedError(); 86 error.initCause(error); 87 throw error; 88 } 89 } 90 } 91 | Popular Tags |