KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > webservice > jbws84 > JBWS84TestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.webservice.jbws84;
23
24 import junit.framework.Test;
25 import org.jboss.test.webservice.WebserviceTestBase;
26 import org.w3c.dom.Document JavaDoc;
27
28 import javax.naming.InitialContext JavaDoc;
29 import javax.xml.parsers.DocumentBuilder JavaDoc;
30 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
31 import javax.xml.parsers.ParserConfigurationException JavaDoc;
32 import javax.xml.rpc.Service JavaDoc;
33 import javax.xml.soap.MessageFactory JavaDoc;
34 import javax.xml.soap.Name JavaDoc;
35 import javax.xml.soap.SOAPBody JavaDoc;
36 import javax.xml.soap.SOAPElement JavaDoc;
37 import javax.xml.soap.SOAPEnvelope JavaDoc;
38 import javax.xml.soap.SOAPException JavaDoc;
39 import javax.xml.soap.SOAPFactory JavaDoc;
40 import javax.xml.soap.SOAPMessage JavaDoc;
41 import java.io.ByteArrayInputStream JavaDoc;
42
43 /**
44  * Test unstructured message processing
45  *
46  * @author Thomas.Diesler@jboss.org
47  * @since 26-Nov-2004
48  */

49 public class JBWS84TestCase extends WebserviceTestBase
50 {
51
52    public JBWS84TestCase(String JavaDoc name)
53    {
54       super(name);
55    }
56
57    /** Deploy the test ear */
58    public static Test suite() throws Exception JavaDoc
59    {
60       return getDeploySetup(JBWS84TestCase.class, "ws4ee-jbws84.war, ws4ee-jbws84-client.jar");
61    }
62
63    /** Use the JBoss generated dynamic proxy send the SOAP message.
64     * This tests server/client side message handling.
65     */

66    public void testProcessSOAPElement() throws Exception JavaDoc
67    {
68       InitialContext JavaDoc iniCtx = getClientContext();
69       Service JavaDoc service = (Service JavaDoc)iniCtx.lookup("java:comp/env/service/MessageService");
70       Message port = (Message)service.getPort(Message.class);
71
72       MessageFactory JavaDoc factory = MessageFactory.newInstance();
73       SOAPMessage JavaDoc reqMessage = factory.createMessage();
74
75       DocumentBuilder JavaDoc builder = getDocumentBuilder();
76       Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(Message.request.getBytes()));
77       reqMessage.getSOAPBody().addDocument(doc);
78
79       SOAPEnvelope JavaDoc soapEnvelope = reqMessage.getSOAPPart().getEnvelope();
80       SOAPBody JavaDoc soapBody = soapEnvelope.getBody();
81       SOAPElement JavaDoc reqElement = (SOAPElement JavaDoc)soapBody.getChildElements().next();
82
83       SOAPElement JavaDoc resElement = port.processSOAPElement(reqElement);
84       validateResponse(resElement);
85    }
86
87    private DocumentBuilder JavaDoc getDocumentBuilder() throws ParserConfigurationException JavaDoc
88    {
89       // Setup document builder
90
DocumentBuilderFactory JavaDoc docBuilderFactory = DocumentBuilderFactory.newInstance();
91       docBuilderFactory.setNamespaceAware(true);
92
93       DocumentBuilder JavaDoc builder = docBuilderFactory.newDocumentBuilder();
94       return builder;
95    }
96
97    private void validateResponse(SOAPElement JavaDoc resElement)
98            throws SOAPException JavaDoc
99    {
100       SOAPFactory JavaDoc factory = SOAPFactory.newInstance();
101
102       Name JavaDoc expName = factory.createName("Response", Message.PREFIX, Message.NAMESPACE_URI);
103       Name JavaDoc elementName = resElement.getElementName();
104       assertEquals(expName, elementName);
105
106       expName = factory.createName("POID");
107       SOAPElement JavaDoc poidElement = (SOAPElement JavaDoc)resElement.getChildElements(expName).next();
108       elementName = poidElement.getElementName();
109       assertEquals(expName, elementName);
110
111       String JavaDoc elementValue = poidElement.getValue();
112       assertEquals("12345", elementValue);
113
114       expName = factory.createName("Status");
115       SOAPElement JavaDoc statusElement = (SOAPElement JavaDoc)resElement.getChildElements(expName).next();
116       elementName = statusElement.getElementName();
117       assertEquals(expName, elementName);
118
119       elementValue = statusElement.getValue();
120       assertEquals("ok", elementValue);
121    }
122 }
123
Popular Tags