1 22 package org.jboss.test.webservice.message; 23 24 import junit.framework.Test; 25 import org.jboss.test.webservice.WebserviceTestBase; 26 import org.jboss.util.xml.DOMUtils; 27 import org.w3c.dom.Document ; 28 import org.w3c.dom.Element ; 29 30 import javax.naming.InitialContext ; 31 import javax.xml.namespace.QName ; 32 import javax.xml.parsers.DocumentBuilder ; 33 import javax.xml.parsers.DocumentBuilderFactory ; 34 import javax.xml.parsers.ParserConfigurationException ; 35 import javax.xml.rpc.Service ; 36 import javax.xml.soap.MessageFactory ; 37 import javax.xml.soap.Name ; 38 import javax.xml.soap.SOAPBody ; 39 import javax.xml.soap.SOAPConnection ; 40 import javax.xml.soap.SOAPConnectionFactory ; 41 import javax.xml.soap.SOAPElement ; 42 import javax.xml.soap.SOAPException ; 43 import javax.xml.soap.SOAPFactory ; 44 import javax.xml.soap.SOAPMessage ; 45 import java.io.ByteArrayInputStream ; 46 import java.net.URL ; 47 48 54 public class MessageTestCase extends WebserviceTestBase 55 { 56 private final String TARGET_ENDPOINT = "http://" + getServerHost() + ":8080/ws4ee-message"; 57 58 public MessageTestCase(String name) 59 { 60 super(name); 61 } 62 63 66 public void testSAAJClientFromEnvelope() throws Exception 67 { 68 MessageFactory mf = MessageFactory.newInstance(); 69 SOAPMessage reqMsg = mf.createMessage(); 70 71 DocumentBuilder builder = getDocumentBuilder(); 72 Document doc = builder.parse(new ByteArrayInputStream (Message.request.getBytes())); 73 reqMsg.getSOAPBody().addDocument(doc); 74 75 SOAPConnectionFactory conFactory = SOAPConnectionFactory.newInstance(); 76 SOAPConnection con = conFactory.createConnection(); 77 SOAPMessage resMsg = con.call(reqMsg, new URL (TARGET_ENDPOINT)); 78 79 SOAPBody soapBody = resMsg.getSOAPBody(); 80 SOAPElement soapElement = (SOAPElement )soapBody.getChildElements().next(); 81 82 validateResponse(soapElement); 83 } 84 85 88 public void testSAAJClientFromBody() throws Exception 89 { 90 MessageFactory mf = MessageFactory.newInstance(); 91 SOAPMessage reqMsg = mf.createMessage(); 92 93 DocumentBuilder builder = getDocumentBuilder(); 94 Document doc = builder.parse(new ByteArrayInputStream (Message.request.getBytes())); 95 reqMsg.getSOAPBody().addDocument(doc); 96 97 SOAPConnectionFactory conFactory = SOAPConnectionFactory.newInstance(); 98 SOAPConnection con = conFactory.createConnection(); 99 SOAPMessage resMsg = con.call(reqMsg, new URL (TARGET_ENDPOINT)); 100 101 SOAPBody soapBody = resMsg.getSOAPBody(); 102 SOAPElement soapElement = (SOAPElement )soapBody.getChildElements().next(); 103 104 validateResponse(soapElement); 105 } 106 107 110 public void testProcessElement() throws Exception 111 { 112 InitialContext iniCtx = getClientContext(); 113 Service service = (Service )iniCtx.lookup("java:comp/env/service/MessageService"); 114 Message port = (Message)service.getPort(Message.class); 115 116 DocumentBuilder builder = getDocumentBuilder(); 117 Document doc = builder.parse(new ByteArrayInputStream (Message.request.getBytes())); 118 Element reqElement = doc.getDocumentElement(); 119 120 Element resElement = port.processElement(reqElement); 121 validateResponse(resElement); 122 } 123 124 125 private DocumentBuilder getDocumentBuilder() throws ParserConfigurationException 126 { 127 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); 129 docBuilderFactory.setNamespaceAware(true); 130 131 DocumentBuilder builder = docBuilderFactory.newDocumentBuilder(); 132 return builder; 133 } 134 135 private void validateResponse(Element resEl) throws Exception 136 { 137 QName expName = new QName (Message.NSURI_1, "Response", Message.PREFIX_1); 138 QName elementName = new QName (resEl.getNamespaceURI(), resEl.getLocalName(), resEl.getPrefix()); 139 assertEquals(expName, elementName); 140 141 expName = new QName ("POID"); 142 Element poidEl = DOMUtils.getFirstChildElement(resEl, expName); 143 elementName = new QName (poidEl.getLocalName()); 144 assertEquals(expName, elementName); 145 146 String elementValue = DOMUtils.getTextContent(poidEl); 147 assertEquals("12345", elementValue); 148 149 expName = new QName ("Status"); 150 Element statusEl = DOMUtils.getFirstChildElement(resEl, expName); 151 elementName = new QName (statusEl.getLocalName()); 152 assertEquals(expName, elementName); 153 154 elementValue = DOMUtils.getTextContent(statusEl); 155 assertEquals("ok", elementValue); 156 } 157 158 159 public static Test suite() throws Exception 160 { 161 return getDeploySetup(MessageTestCase.class, "ws4ee-message.war, ws4ee-message-client.jar"); 162 } 163 } 164 | Popular Tags |