KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > webservice > jbws167 > JBWS167TestCase


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.jbws167;
23
24 import java.net.URL JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 import javax.xml.soap.MessageFactory JavaDoc;
29 import javax.xml.soap.MimeHeaders JavaDoc;
30 import javax.xml.soap.Name JavaDoc;
31 import javax.xml.soap.SOAPBody JavaDoc;
32 import javax.xml.soap.SOAPBodyElement JavaDoc;
33 import javax.xml.soap.SOAPConnection JavaDoc;
34 import javax.xml.soap.SOAPConnectionFactory JavaDoc;
35 import javax.xml.soap.SOAPElement JavaDoc;
36 import javax.xml.soap.SOAPEnvelope JavaDoc;
37 import javax.xml.soap.SOAPMessage JavaDoc;
38 import javax.xml.soap.SOAPPart JavaDoc;
39
40 import junit.framework.Test;
41
42 import org.jboss.test.webservice.WebserviceTestBase;
43
44 /**
45  * Tests that we can send and receive HTTP headers. Also test the we support multiple
46  * values per header (like cookies)
47  *
48  * @author <a HREF="mailto:jason.greene@jboss.com">Jason T. Greene</a>
49  * @since 05-Mar-2005
50  */

51 public class JBWS167TestCase extends WebserviceTestBase
52 {
53    /**
54     * Construct the test case with a given name
55     */

56    public JBWS167TestCase(String JavaDoc name)
57    {
58       super(name);
59    }
60
61    /** Deploy the test */
62    public static Test suite() throws Exception JavaDoc
63    {
64       return getDeploySetup(JBWS167TestCase.class, "ws4ee-jbws167.war");
65    }
66
67    public void testHttpHeaders() throws Exception JavaDoc
68    {
69       String JavaDoc NS_PREFIX = "ns1";
70       String JavaDoc NS_URI = "http://org.jboss.test.webservice/jbws167";
71       String JavaDoc rpcMethodName = "hello";
72
73       MessageFactory JavaDoc mf = MessageFactory.newInstance();
74       SOAPMessage JavaDoc msg = mf.createMessage();
75       SOAPPart JavaDoc sp = msg.getSOAPPart();
76       SOAPEnvelope JavaDoc envelope = sp.getEnvelope();
77       SOAPBody JavaDoc bdy = envelope.getBody();
78       SOAPBodyElement JavaDoc sbe = bdy.addBodyElement(envelope.createName(rpcMethodName, NS_PREFIX, NS_URI));
79
80       // Add a some child elements
81
sbe.addChildElement(envelope.createName("String_1")).addTextNode("Some hello");
82
83       MimeHeaders JavaDoc headers = msg.getMimeHeaders();
84       headers.addHeader("SOAPAction", "/foo/bar");
85       headers.addHeader("testme", "testme");
86       headers.addHeader("testme", "testme2");
87
88       SOAPConnectionFactory JavaDoc conFactory = SOAPConnectionFactory.newInstance();
89       SOAPConnection JavaDoc con = conFactory.createConnection();
90       SOAPMessage JavaDoc resMessage = con.call(msg, new URL JavaDoc("http://" + getServerHost() + ":8080/ws4ee-jbws167"));
91
92       SOAPBody JavaDoc soapBody = resMessage.getSOAPBody();
93       SOAPEnvelope JavaDoc soapEnvelope = (SOAPEnvelope JavaDoc) soapBody.getParentElement();
94
95       Name JavaDoc rpcName = soapEnvelope.createName(rpcMethodName + "Response", NS_PREFIX, NS_URI);
96       Iterator JavaDoc childElements = soapBody.getChildElements(rpcName);
97       assertTrue("Expexted child: " + rpcName, childElements.hasNext());
98
99       SOAPElement JavaDoc bodyChild = (SOAPElement JavaDoc) childElements.next();
100       Name JavaDoc resName = soapEnvelope.createName("result");
101       SOAPElement JavaDoc resElement = (SOAPElement JavaDoc) bodyChild.getChildElements(resName).next();
102       String JavaDoc value = resElement.getValue();
103       assertEquals("[pass]", value);
104
105       verifyResponseHeaders(resMessage);
106    }
107
108    private void verifyResponseHeaders(SOAPMessage JavaDoc message)
109    {
110       MimeHeaders JavaDoc headers = message.getMimeHeaders();
111       assertNotNull(headers);
112
113       HashSet JavaDoc expectedValues = new HashSet JavaDoc();
114       expectedValues.add("1");
115       expectedValues.add("2");
116       expectedValues.add("3");
117
118       String JavaDoc[] values = headers.getHeader("server-test-header");
119       assertTrue("Expected 3 values for server test header", values != null && values.length == 3);
120       for (int i = 0; i < 3; i++)
121       {
122          assertTrue("Expected results did not match (got " + values[i] + " for a result)", expectedValues.contains(values[i]));
123          expectedValues.remove(values[i]);
124       }
125     }
126 }
127
Popular Tags