KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > webservice > utf16 > UTF16TestCase


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.utf16;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.OutputStreamWriter JavaDoc;
29
30 import javax.naming.InitialContext JavaDoc;
31 import javax.xml.rpc.Service JavaDoc;
32 import javax.xml.soap.MessageFactory JavaDoc;
33 import javax.xml.soap.MimeHeaders 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.SOAPException JavaDoc;
38 import javax.xml.soap.SOAPMessage JavaDoc;
39
40 import junit.framework.Test;
41
42 import org.jboss.test.webservice.WebserviceTestBase;
43
44 /**
45  * Tests R4001 in the WSI Basic Profile 1.0:
46  * A RECEIVER MUST accept messages that include the Unicode Byte Order Mark (BOM).
47  *
48  * @author Thomas.Diesler@jboss.org
49  * @since 09-Dec-2005
50  */

51 public class UTF16TestCase extends WebserviceTestBase
52 {
53    private SOAPConnection JavaDoc soapCon;
54    
55    private static String JavaDoc reqEnv =
56       "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'>" +
57       " <soapenv:Body>" +
58       " <ns1:hello xmlns:ns1='http://org.jboss.test.webservice/utf16'>" +
59       " <String_1>Kermit</String_1>" +
60       " </ns1:hello>" +
61       " </soapenv:Body>" +
62       "</soapenv:Envelope>";
63    
64    public UTF16TestCase(String JavaDoc name)
65    {
66       super(name);
67    }
68
69    /** Deploy the test */
70    public static Test suite() throws Exception JavaDoc
71    {
72       return getDeploySetup(UTF16TestCase.class, "ws4ee-utf16.war, ws4ee-utf16-client.jar");
73    }
74
75    public void setUp() throws Exception JavaDoc
76    {
77       super.setUp();
78       SOAPConnectionFactory JavaDoc conFactory = SOAPConnectionFactory.newInstance();
79       soapCon = conFactory.createConnection();
80    }
81    
82    public void testClientAccess() throws Exception JavaDoc
83    {
84       InitialContext JavaDoc iniCtx = getClientContext();
85       Service JavaDoc service = (Service JavaDoc)iniCtx.lookup("java:comp/env/service/TestService");
86       Hello endpoint = (Hello)service.getPort(Hello.class);
87       
88       String JavaDoc retObj = endpoint.hello("Kermit");
89       assertEquals("Kermit", retObj);
90    }
91    
92    /** Test SAAJ access without MIME headers, using the default encoding
93     */

94    public void testDefaultAccess() throws Exception JavaDoc
95    {
96       SOAPMessage JavaDoc reqMsg = createSOAPMessage(reqEnv, null);
97       SOAPMessage JavaDoc resMsg = soapCon.call(reqMsg, getTargetAddress());
98       verifyResponseMessage(resMsg);
99    }
100
101    /** Test UTF-8 SAAJ access
102     */

103    public void testUTF8Access() throws Exception JavaDoc
104    {
105       SOAPMessage JavaDoc reqMsg = createSOAPMessage(reqEnv, "UTF-8");
106       SOAPMessage JavaDoc resMsg = soapCon.call(reqMsg, getTargetAddress());
107       verifyResponseMessage(resMsg);
108    }
109
110    /** Test UTF-16 SAAJ access
111     */

112    public void testUTF16Access() throws Exception JavaDoc
113    {
114       SOAPMessage JavaDoc reqMsg = createSOAPMessage(reqEnv, "UTF-16");
115       SOAPMessage JavaDoc resMsg = soapCon.call(reqMsg, getTargetAddress());
116       verifyResponseMessage(resMsg);
117    }
118
119    private SOAPMessage JavaDoc createSOAPMessage(String JavaDoc envStr, String JavaDoc csName) throws IOException JavaDoc, SOAPException JavaDoc
120    {
121       MessageFactory JavaDoc msgFactory = MessageFactory.newInstance();
122       
123       MimeHeaders JavaDoc headers = null;
124       if (csName != null)
125       {
126          headers = new MimeHeaders JavaDoc();
127          headers.addHeader("Content-Type", "text/xml; charset=\"" + csName + "\"");
128          envStr = "<?xml version='1.0' encoding='" + csName + "'?>" + envStr;
129       }
130       
131       InputStream JavaDoc is = getInputStreamForString(envStr, csName);
132       SOAPMessage JavaDoc reqMsg = msgFactory.createMessage(headers, is);
133       return reqMsg;
134    }
135
136    private InputStream JavaDoc getInputStreamForString(String JavaDoc envStr, String JavaDoc csName) throws IOException JavaDoc
137    {
138       ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
139       OutputStreamWriter JavaDoc osw;
140       if (csName != null)
141       {
142          osw = new OutputStreamWriter JavaDoc(bos, csName);
143       }
144       else
145       {
146          osw = new OutputStreamWriter JavaDoc(bos, "UTF-8");
147       }
148       osw.write(envStr);
149       osw.flush();
150       
151       ByteArrayInputStream JavaDoc bis = new ByteArrayInputStream JavaDoc(bos.toByteArray());
152       return bis;
153    }
154    
155    private void verifyResponseMessage(SOAPMessage JavaDoc resMsg) throws SOAPException JavaDoc
156    {
157       SOAPElement JavaDoc soapElement = (SOAPElement JavaDoc)resMsg.getSOAPBody().getChildElements().next();
158       soapElement = (SOAPElement JavaDoc)soapElement.getChildElements().next();
159       String JavaDoc retObj = soapElement.getValue();
160       assertEquals("Kermit", retObj);
161    }
162    
163    private String JavaDoc getTargetAddress()
164    {
165       return "http://" + getServerHost() + ":8080/ws4ee-utf16";
166    }
167 }
168
Popular Tags