Browse under:
http://cvs.sourceforge.net/viewcvs.py/jboss/jbosstest/src/main/org/jboss/test/webservice/


  | /*
  |  * JBoss, the OpenSource J2EE webOS
  |  *
  |  * Distributable under LGPL license.
  |  * See terms of license at gnu.org.
  |  */
  | package org.jboss.test.webservice.message;
  | 
  | // $Id: MessageEndpointTestCase.java,v 1.2 2005/01/19 13:25:08 tdiesler Exp 
$
  | 
  | import junit.framework.Test;
  | import org.jboss.test.JBossTestCase;
  | import org.w3c.dom.Document;
  | import org.w3c.dom.Element;
  | 
  | import javax.naming.Context;
  | import javax.naming.InitialContext;
  | import javax.naming.NamingException;
  | import javax.xml.parsers.DocumentBuilder;
  | import javax.xml.parsers.DocumentBuilderFactory;
  | import javax.xml.parsers.ParserConfigurationException;
  | import javax.xml.rpc.Service;
  | import javax.xml.soap.MessageFactory;
  | import javax.xml.soap.Name;
  | import javax.xml.soap.SOAPBody;
  | import javax.xml.soap.SOAPConnection;
  | import javax.xml.soap.SOAPConnectionFactory;
  | import javax.xml.soap.SOAPElement;
  | import javax.xml.soap.SOAPException;
  | import javax.xml.soap.SOAPFactory;
  | import javax.xml.soap.SOAPMessage;
  | import java.io.ByteArrayInputStream;
  | import java.net.URL;
  | import java.util.Properties;
  | 
  | /**
  |  * Test unstructured message processing
  |  *
  |  * @author [EMAIL PROTECTED]
  |  * @since 26-Nov-2004
  |  */
  | public class MessageEndpointTestCase extends JBossTestCase
  | {
  |    private final String TARGET_ENDPOINT = "http://"; + getServerHost() + 
":8080/ws4ee-message";
  | 
  |    public MessageEndpointTestCase(String name)
  |    {
  |       super(name);
  |    }
  | 
  |    /** Use the SAAJ API to send the SOAP message.
  |     * This simulates an external client and tests server side message 
handling.
  |     */
  |    public void testSAAJClientFromEnvelope() throws Exception
  |    {
  |       String soapEnv =
  |               "<soapenv:Envelope 
xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>" +
  |               "<soapenv:Body>" +
  |               MessageEndpoint.request +
  |               "</soapenv:Body>" +
  |               "</soapenv:Envelope>";
  | 
  |       MessageFactory mf = MessageFactory.newInstance();
  |       SOAPMessage reqMsg = mf.createMessage(null, new 
ByteArrayInputStream(soapEnv.getBytes()));
  | 
  |       SOAPConnectionFactory conFactory = 
SOAPConnectionFactory.newInstance();
  |       SOAPConnection con = conFactory.createConnection();
  |       SOAPMessage resMsg = con.call(reqMsg, new URL(TARGET_ENDPOINT));
  | 
  |       SOAPBody soapBody = resMsg.getSOAPBody();
  |       SOAPElement soapElement = 
(SOAPElement)soapBody.getChildElements().next();
  | 
  |       validateResponse(soapElement);
  |    }
  | 
  |    /** Use the SAAJ API to send the SOAP message.
  |     * This simulates an external client and tests server side message 
handling.
  |     */
  |    public void testSAAJClientFromBody() throws Exception
  |    {
  |       MessageFactory mf = MessageFactory.newInstance();
  |       SOAPMessage reqMsg = mf.createMessage();
  | 
  |       DocumentBuilder builder = getDocumentBuilder();
  |       Document doc = builder.parse(new 
ByteArrayInputStream(MessageEndpoint.request.getBytes()));
  |       reqMsg.getSOAPBody().addDocument(doc);
  | 
  |       SOAPConnectionFactory conFactory = 
SOAPConnectionFactory.newInstance();
  |       SOAPConnection con = conFactory.createConnection();
  |       SOAPMessage resMsg = con.call(reqMsg, new URL(TARGET_ENDPOINT));
  | 
  |       SOAPBody soapBody = resMsg.getSOAPBody();
  |       SOAPElement soapElement = 
(SOAPElement)soapBody.getChildElements().next();
  | 
  |       validateResponse(soapElement);
  |    }
  | 
  |    /** Use the JBoss generated dynamic proxy send the SOAP message.
  |     * This tests server/client side message handling.
  |     */
  |    public void testProxyClient() throws Exception
  |    {
  |       InitialContext iniCtx = getInitialContext();
  |       Service service = 
(Service)iniCtx.lookup("java:comp/env/service/MessageService");
  |       MessageEndpoint port = 
(MessageEndpoint)service.getPort(MessageEndpoint.class);
  | 
  |       DocumentBuilder builder = getDocumentBuilder();
  |       Document doc = builder.parse(new 
ByteArrayInputStream(MessageEndpoint.request.getBytes()));
  |       Element element = doc.getDocumentElement();
  | 
  |       SOAPElement soapElement = (SOAPElement)port.processElement(element);
  |       validateResponse(soapElement);
  |    }
  | 
  |    private DocumentBuilder getDocumentBuilder() throws 
ParserConfigurationException
  |    {
  |       // Setup document builder
  |       DocumentBuilderFactory docBuilderFactory = 
DocumentBuilderFactory.newInstance();
  |       docBuilderFactory.setNamespaceAware(true);
  | 
  |       DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
  |       return builder;
  |    }
  | 
  |    private void validateResponse(SOAPElement soapElement)
  |            throws SOAPException
  |    {
  |       SOAPFactory factory = SOAPFactory.newInstance();
  | 
  |       Name expName = factory.createName("Response", MessageEndpoint.PREFIX, 
MessageEndpoint.NAMESPACE_URI);
  |       Name elementName = soapElement.getElementName();
  |       assertEquals(expName, elementName);
  | 
  |       expName = factory.createName("POID");
  |       soapElement = 
(SOAPElement)soapElement.getChildElements(expName).next();
  |       elementName = soapElement.getElementName();
  |       assertEquals(expName, elementName);
  | 
  |       String elementValue = soapElement.getValue();
  |       assertEquals("12345", elementValue);
  | 
  |       expName = factory.createName("Status");
  |       soapElement = (SOAPElement)soapElement.getNextSibling();
  |       elementName = soapElement.getElementName();
  |       assertEquals(expName, elementName);
  | 
  |       elementValue = soapElement.getValue();
  |       assertEquals("ok", elementValue);
  |    }
  | 
  |    /** Deploy the test ear */
  |    public static Test suite() throws Exception
  |    {
  |       return getDeploySetup(MessageEndpointTestCase.class, 
"ws4ee-message.war, ws4ee-message-client.jar");
  |    }
  | 
  |    /**
  |     * Get the client's env context, see tracker [840598] for details
  |     */
  |    protected InitialContext getInitialContext() throws NamingException
  |    {
  |       Properties env = new Properties();
  |       env.setProperty(Context.INITIAL_CONTEXT_FACTORY, 
"org.jnp.interfaces.NamingContextFactory");
  |       env.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming.client");
  |       env.setProperty(Context.PROVIDER_URL, "jnp://" + getServerHost() + 
":1099");
  |       env.setProperty("j2ee.clientName", "ws4ee-client");
  |       return new InitialContext(env);
  |    }
  | }
  | 


View the original post : 
http://www.jboss.org/index.html?module=bb&op=viewtopic&p=3864209#3864209

Reply to the post : 
http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=3864209


-------------------------------------------------------
This SF.Net email is sponsored by: IntelliVIEW -- Interactive Reporting
Tool for open source databases. Create drag-&-drop reports. Save time
by over 75%! Publish reports on the web. Export to DOC, XLS, RTF, etc.
Download a FREE copy at http://www.intelliview.com/go/osdn_nl
_______________________________________________
JBoss-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jboss-user

Reply via email to