KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > webservice > message > MessageTestCase


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.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 JavaDoc;
28 import org.w3c.dom.Element JavaDoc;
29
30 import javax.naming.InitialContext JavaDoc;
31 import javax.xml.namespace.QName JavaDoc;
32 import javax.xml.parsers.DocumentBuilder JavaDoc;
33 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
34 import javax.xml.parsers.ParserConfigurationException JavaDoc;
35 import javax.xml.rpc.Service JavaDoc;
36 import javax.xml.soap.MessageFactory JavaDoc;
37 import javax.xml.soap.Name JavaDoc;
38 import javax.xml.soap.SOAPBody JavaDoc;
39 import javax.xml.soap.SOAPConnection JavaDoc;
40 import javax.xml.soap.SOAPConnectionFactory JavaDoc;
41 import javax.xml.soap.SOAPElement JavaDoc;
42 import javax.xml.soap.SOAPException JavaDoc;
43 import javax.xml.soap.SOAPFactory JavaDoc;
44 import javax.xml.soap.SOAPMessage JavaDoc;
45 import java.io.ByteArrayInputStream JavaDoc;
46 import java.net.URL JavaDoc;
47
48 /**
49  * Test unstructured message processing
50  *
51  * @author Thomas.Diesler@jboss.org
52  * @since 26-Nov-2004
53  */

54 public class MessageTestCase extends WebserviceTestBase
55 {
56    private final String JavaDoc TARGET_ENDPOINT = "http://" + getServerHost() + ":8080/ws4ee-message";
57
58    public MessageTestCase(String JavaDoc name)
59    {
60       super(name);
61    }
62
63    /** Use the SAAJ API to send the SOAP message.
64     * This simulates an external client and tests server side message handling.
65     */

66    public void testSAAJClientFromEnvelope() throws Exception JavaDoc
67    {
68       MessageFactory JavaDoc mf = MessageFactory.newInstance();
69       SOAPMessage JavaDoc reqMsg = mf.createMessage();
70
71       DocumentBuilder JavaDoc builder = getDocumentBuilder();
72       Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(Message.request.getBytes()));
73       reqMsg.getSOAPBody().addDocument(doc);
74
75       SOAPConnectionFactory JavaDoc conFactory = SOAPConnectionFactory.newInstance();
76       SOAPConnection JavaDoc con = conFactory.createConnection();
77       SOAPMessage JavaDoc resMsg = con.call(reqMsg, new URL JavaDoc(TARGET_ENDPOINT));
78
79       SOAPBody JavaDoc soapBody = resMsg.getSOAPBody();
80       SOAPElement JavaDoc soapElement = (SOAPElement JavaDoc)soapBody.getChildElements().next();
81
82       validateResponse(soapElement);
83    }
84
85    /** Use the SAAJ API to send the SOAP message.
86     * This simulates an external client and tests server side message handling.
87     */

88    public void testSAAJClientFromBody() throws Exception JavaDoc
89    {
90       MessageFactory JavaDoc mf = MessageFactory.newInstance();
91       SOAPMessage JavaDoc reqMsg = mf.createMessage();
92
93       DocumentBuilder JavaDoc builder = getDocumentBuilder();
94       Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(Message.request.getBytes()));
95       reqMsg.getSOAPBody().addDocument(doc);
96
97       SOAPConnectionFactory JavaDoc conFactory = SOAPConnectionFactory.newInstance();
98       SOAPConnection JavaDoc con = conFactory.createConnection();
99       SOAPMessage JavaDoc resMsg = con.call(reqMsg, new URL JavaDoc(TARGET_ENDPOINT));
100
101       SOAPBody JavaDoc soapBody = resMsg.getSOAPBody();
102       SOAPElement JavaDoc soapElement = (SOAPElement JavaDoc)soapBody.getChildElements().next();
103
104       validateResponse(soapElement);
105    }
106
107    /** Use the JBoss generated dynamic proxy send the SOAP message.
108     * This tests server/client side message handling.
109     */

110    public void testProcessElement() throws Exception JavaDoc
111    {
112       InitialContext JavaDoc iniCtx = getClientContext();
113       Service JavaDoc service = (Service JavaDoc)iniCtx.lookup("java:comp/env/service/MessageService");
114       Message port = (Message)service.getPort(Message.class);
115
116       DocumentBuilder JavaDoc builder = getDocumentBuilder();
117       Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(Message.request.getBytes()));
118       Element JavaDoc reqElement = doc.getDocumentElement();
119
120       Element JavaDoc resElement = port.processElement(reqElement);
121       validateResponse(resElement);
122    }
123
124
125    private DocumentBuilder JavaDoc getDocumentBuilder() throws ParserConfigurationException JavaDoc
126    {
127       // Setup document builder
128
DocumentBuilderFactory JavaDoc docBuilderFactory = DocumentBuilderFactory.newInstance();
129       docBuilderFactory.setNamespaceAware(true);
130
131       DocumentBuilder JavaDoc builder = docBuilderFactory.newDocumentBuilder();
132       return builder;
133    }
134
135    private void validateResponse(Element JavaDoc resEl) throws Exception JavaDoc
136    {
137       QName JavaDoc expName = new QName JavaDoc(Message.NSURI_1, "Response", Message.PREFIX_1);
138       QName JavaDoc elementName = new QName JavaDoc(resEl.getNamespaceURI(), resEl.getLocalName(), resEl.getPrefix());
139       assertEquals(expName, elementName);
140
141       expName = new QName JavaDoc("POID");
142       Element JavaDoc poidEl = DOMUtils.getFirstChildElement(resEl, expName);
143       elementName = new QName JavaDoc(poidEl.getLocalName());
144       assertEquals(expName, elementName);
145
146       String JavaDoc elementValue = DOMUtils.getTextContent(poidEl);
147       assertEquals("12345", elementValue);
148
149       expName = new QName JavaDoc("Status");
150       Element JavaDoc statusEl = DOMUtils.getFirstChildElement(resEl, expName);
151       elementName = new QName JavaDoc(statusEl.getLocalName());
152       assertEquals(expName, elementName);
153
154       elementValue = DOMUtils.getTextContent(statusEl);
155       assertEquals("ok", elementValue);
156    }
157
158    /** Deploy the test ear */
159    public static Test suite() throws Exception JavaDoc
160    {
161       return getDeploySetup(MessageTestCase.class, "ws4ee-message.war, ws4ee-message-client.jar");
162    }
163 }
164
Popular Tags