KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > webservice > jbws812 > JBWS812TestCase


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.jbws812;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.net.HttpURLConnection JavaDoc;
27 import java.net.URL JavaDoc;
28
29 import javax.naming.InitialContext JavaDoc;
30 import javax.xml.rpc.Service JavaDoc;
31 import javax.xml.soap.MessageFactory JavaDoc;
32 import javax.xml.soap.MimeHeaders JavaDoc;
33 import javax.xml.soap.Name JavaDoc;
34 import javax.xml.soap.SOAPConnection JavaDoc;
35 import javax.xml.soap.SOAPConnectionFactory JavaDoc;
36 import javax.xml.soap.SOAPElement JavaDoc;
37 import javax.xml.soap.SOAPEnvelope JavaDoc;
38 import javax.xml.soap.SOAPMessage JavaDoc;
39
40 import junit.framework.Test;
41
42 import org.jboss.test.webservice.WebserviceTestBase;
43 import org.jboss.util.xml.DOMUtils;
44 import org.jboss.util.xml.DOMWriter;
45 import org.w3c.dom.Element JavaDoc;
46
47 /**
48  * Invalid byte 2 of 2-byte UTF-8 sequence
49  *
50  * http://jira.jboss.com/jira/browse/JBWS-812
51  *
52  * @author Thomas.Diesler@jboss.org
53  * @since 11-Apr-2006
54  */

55 public class JBWS812TestCase extends WebserviceTestBase
56 {
57    private String JavaDoc reqEnv =
58       "<?xml version='1.0' encoding='UTF-8'?>" +
59       "<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'>" +
60       " <env:Body>" +
61       " <ns1:echoSimple xmlns:ns1='http://org.jboss.test.webservice/jbws812'>" +
62       " <String_1>&#xA0;</String_1>" +
63       " </ns1:echoSimple>" +
64       " </env:Body>" +
65       "</env:Envelope>";
66    
67    public JBWS812TestCase(String JavaDoc name)
68    {
69       super(name);
70    }
71
72    public static Test suite() throws Exception JavaDoc
73    {
74       return getDeploySetup(JBWS812TestCase.class, "ws4ee-jbws812.war, ws4ee-jbws812-client.jar");
75    }
76
77    /** Send the raw bytes via an HttpURLConnection
78     */

79    public void testHttpURLConnection() throws Exception JavaDoc
80    {
81       String JavaDoc targetAddress = "http://" + getServerHost() + ":8080/ws4ee-jbws812";
82       HttpURLConnection JavaDoc con = (HttpURLConnection JavaDoc)new URL JavaDoc(targetAddress).openConnection();
83       con.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
84       con.setRequestMethod("POST");
85       con.setDoOutput(true);
86       con.connect();
87       
88       OutputStream JavaDoc outs = con.getOutputStream();
89       outs.write(reqEnv.getBytes("UTF-8"));
90       outs.close();
91       
92       int resCode = con.getResponseCode();
93       assertEquals(200, resCode);
94    }
95
96    /** Test DOMUtils, DOMWriter roundtrip and send the via an HttpURLConnection
97     */

98    public void testDOMParseWrite() throws Exception JavaDoc
99    {
100       String JavaDoc targetAddress = "http://" + getServerHost() + ":8080/ws4ee-jbws812";
101       HttpURLConnection JavaDoc con = (HttpURLConnection JavaDoc)new URL JavaDoc(targetAddress).openConnection();
102       con.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
103       con.setRequestMethod("POST");
104       con.setDoOutput(true);
105       con.connect();
106       
107       OutputStream JavaDoc outs = con.getOutputStream();
108       Element JavaDoc reqMsg = DOMUtils.parse(reqEnv);
109       new DOMWriter(outs).print(reqMsg);
110       outs.close();
111       
112       int resCode = con.getResponseCode();
113       assertEquals(200, resCode);
114    }
115
116    /** Send the SOAPMessage via an HttpURLConnection
117     */

118    public void testSOAPMessage() throws Exception JavaDoc
119    {
120       String JavaDoc targetAddress = "http://" + getServerHost() + ":8080/ws4ee-jbws812";
121       HttpURLConnection JavaDoc con = (HttpURLConnection JavaDoc)new URL JavaDoc(targetAddress).openConnection();
122       con.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
123       con.setRequestMethod("POST");
124       con.setDoOutput(true);
125       con.connect();
126       
127       MessageFactory JavaDoc factory = MessageFactory.newInstance();
128       MimeHeaders JavaDoc mimeHeaders = new MimeHeaders JavaDoc();
129       mimeHeaders.addHeader("Content-Type", "text/xml; charset=UTF-8");
130       SOAPMessage JavaDoc reqMsg = factory.createMessage(mimeHeaders, new ByteArrayInputStream JavaDoc(reqEnv.getBytes("UTF-8")));
131
132       OutputStream JavaDoc outs = con.getOutputStream();
133       reqMsg.writeTo(outs);
134       outs.close();
135       
136       int resCode = con.getResponseCode();
137       assertEquals(200, resCode);
138    }
139
140    /** Send the SOAPMessage via SOAPConnection
141     */

142    public void testSOAPConnection() throws Exception JavaDoc
143    {
144       MessageFactory JavaDoc factory = MessageFactory.newInstance();
145       MimeHeaders JavaDoc mimeHeaders = new MimeHeaders JavaDoc();
146       mimeHeaders.addHeader("Content-Type", "text/xml; charset=UTF-8");
147       SOAPMessage JavaDoc reqMsg = factory.createMessage(mimeHeaders, new ByteArrayInputStream JavaDoc(reqEnv.getBytes("UTF-8")));
148
149       SOAPConnection JavaDoc con = SOAPConnectionFactory.newInstance().createConnection();
150       String JavaDoc targetAddress = "http://" + getServerHost() + ":8080/ws4ee-jbws812";
151       SOAPMessage JavaDoc resMsg = con.call(reqMsg, targetAddress);
152       
153       SOAPEnvelope JavaDoc resEnv = resMsg.getSOAPPart().getEnvelope();
154       Name JavaDoc name = resEnv.createName("echoSimpleResponse", "ns1", "http://org.jboss.test.webservice/jbws812");
155       SOAPElement JavaDoc soapElement = (SOAPElement JavaDoc)resMsg.getSOAPBody().getChildElements(name).next();
156       soapElement = (SOAPElement JavaDoc)soapElement.getChildElements(resEnv.createName("result")).next();
157       
158       String JavaDoc resValue = soapElement.getValue();
159       assertEquals(160, resValue.charAt(0));
160    }
161
162
163    /** Test client proxy API
164     */

165    public void testClientProxy() throws Exception JavaDoc
166    {
167       InitialContext JavaDoc iniCtx = getClientContext();
168       Service JavaDoc service = (Service JavaDoc)iniCtx.lookup("java:comp/env/service/TestService");
169       TestEndpoint port = (TestEndpoint)service.getPort(TestEndpoint.class);
170       
171       String JavaDoc resStr = port.echoSimple("\u00a0");
172       assertEquals(160, resStr.charAt(0));
173    }
174 }
175
Popular Tags