KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > webservice > jbws707 > JBWS707TestCase


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.jbws707;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 import javax.naming.InitialContext JavaDoc;
28 import javax.xml.rpc.Service JavaDoc;
29 import javax.xml.soap.MessageFactory JavaDoc;
30 import javax.xml.soap.MimeHeaders JavaDoc;
31 import javax.xml.soap.SOAPBody JavaDoc;
32 import javax.xml.soap.SOAPElement JavaDoc;
33 import javax.xml.soap.SOAPException JavaDoc;
34 import javax.xml.soap.SOAPMessage JavaDoc;
35
36 import junit.framework.Test;
37
38 import org.jboss.test.webservice.WebserviceTestBase;
39 import org.w3c.dom.Element JavaDoc;
40 import org.w3c.dom.Node JavaDoc;
41 import org.w3c.dom.NodeList JavaDoc;
42
43 /**
44  * White spaces within <![CDATA[ ]]> element of a SOAP message are not retained
45  * http://jira.jboss.com/jira/browse/JBWS-707
46  *
47  * Invalid XML Characters not Properly Handled in Object Serialization
48  * http://jira.jboss.com/jira/browse/JBWS-716
49  *
50  * @author Anil.Saldhana@jboss.org
51  * @author Thomas.Diesler@jboss.org
52  * @since 16-Feb-2006
53  */

54 public class JBWS707TestCase extends WebserviceTestBase
55 {
56    private static TestEndpoint port;
57    
58    public JBWS707TestCase(String JavaDoc name)
59    {
60       super(name);
61    }
62
63    /** Deploy the test */
64    public static Test suite() throws Exception JavaDoc
65    {
66       return getDeploySetup(JBWS707TestCase.class, "ws4ee-jbws707.war, ws4ee-jbws707-client.jar");
67    }
68
69    public void setUp() throws Exception JavaDoc
70    {
71       super.setUp();
72       if (port == null)
73       {
74          InitialContext JavaDoc iniCtx = getClientContext();
75          Service JavaDoc service = (Service JavaDoc)iniCtx.lookup("java:comp/env/service/TestService");
76          port = (TestEndpoint)service.getPort(TestEndpoint.class);
77       }
78    }
79    
80    public void testSpecialChars() throws Exception JavaDoc
81    {
82       String JavaDoc inStr = "&Test & this &";
83       String JavaDoc outStr = port.echo(inStr);
84       assertEquals(inStr, outStr);
85       
86       inStr = "<Test < this <";
87       outStr = port.echo(inStr);
88       assertEquals(inStr, outStr);
89       
90       inStr = ">Test > this >";
91       outStr = port.echo(inStr);
92       assertEquals(inStr, outStr);
93       
94       inStr = "\"Test \" this \"";
95       outStr = port.echo(inStr);
96       assertEquals(inStr, outStr);
97    }
98    
99    public void testSpecialCharsInBean() throws Exception JavaDoc
100    {
101       UserType in = new UserType("&Test & this &");
102       UserType out = port.echo(in);
103       assertEquals(in, out);
104       
105       in = new UserType("<Test < this <");
106       out = port.echo(in);
107       assertEquals(in, out);
108       
109       in = new UserType(">Test > this >");
110       out = port.echo(in);
111       assertEquals(in, out);
112       
113       in = new UserType("\"Test \" this \"");
114       out = port.echo(in);
115       assertEquals(in, out);
116    }
117    
118    public void testCDATA() throws Exception JavaDoc
119    {
120       String JavaDoc xmlStr = "<?xml version='1.0' encoding='UTF-8' ?>" +
121       "<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/' xmlns:tns='http://uri.jboss.org'>" +
122       " <env:Body>" +
123       " <tns:testMessage><![CDATA[ Hello ]]></tns:testMessage>" +
124       " </env:Body>" +
125       "</env:Envelope>";
126
127       String JavaDoc expStr = " Hello ";
128       assertEquals(expStr, parse(xmlStr));
129       
130       String JavaDoc out = port.echo(expStr);
131       assertEquals(expStr, out);
132    }
133
134    public void testCDATAAmpersand() throws Exception JavaDoc
135    {
136       String JavaDoc xmlStr =
137          "<?xml version='1.0' encoding='UTF-8' ?>" +
138          "<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/' xmlns:tns='http://uri.jboss.org'>" +
139          " <env:Body>" +
140          " <tns:myMessage>WelcometoJBoss!<![CDATA[ Hello &&]]></tns:myMessage>" +
141          " </env:Body>" +
142          "</env:Envelope>";
143       
144       String JavaDoc expStr = "WelcometoJBoss! Hello &&";
145       assertEquals(expStr, parse(xmlStr));
146
147       String JavaDoc out = port.echo(expStr);
148       assertEquals(expStr, out);
149    }
150
151    public void testMultipleCDATA() throws Exception JavaDoc
152    {
153       String JavaDoc xmlStr =
154          "<?xml version='1.0' encoding='UTF-8' ?>" +
155          "<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/' xmlns:tns='http://uri.jboss.org'>" +
156          " <env:Body>" +
157          " <tns:myMessage><![CDATA[ Hello ]]>JBoss!<![CDATA[ ]]></tns:myMessage>" +
158          " </env:Body>" +
159          "</env:Envelope>";
160       
161       String JavaDoc expStr = " Hello JBoss! ";
162       assertEquals(expStr, parse(xmlStr));
163
164       String JavaDoc out = port.echo(expStr);
165       assertEquals(expStr, out);
166    }
167
168    public void testMultipleCDATAv2() throws Exception JavaDoc
169    {
170       String JavaDoc myXML =
171          "<?xml version='1.0' encoding='UTF-8' ?>" +
172          "<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/' xmlns:tns='http://uri.jboss.org'>" +
173          " <env:Body>" +
174          " <tns:myMessage><![CDATA[ Hello ]]>JBoss!<![CDATA[ ]]>.OSS</tns:myMessage>" +
175          " </env:Body>" +
176          "</env:Envelope>";
177       
178       String JavaDoc expStr = " Hello JBoss! .OSS";
179       assertEquals(expStr, parse(myXML));
180
181       String JavaDoc out = port.echo(expStr);
182       assertEquals(expStr, out);
183    }
184
185    private String JavaDoc parse(String JavaDoc xmlStr) throws SOAPException JavaDoc, IOException JavaDoc
186    {
187       MessageFactory JavaDoc mf = MessageFactory.newInstance();
188       MimeHeaders JavaDoc mimeHeaders = new MimeHeaders JavaDoc();
189       mimeHeaders.addHeader("Content-Type", "text/xml; charset=UTF-8");
190
191       SOAPMessage JavaDoc soapMessage = mf.createMessage(mimeHeaders, new ByteArrayInputStream JavaDoc(xmlStr.getBytes()));
192       SOAPBody JavaDoc soapBody = soapMessage.getSOAPBody();
193       SOAPElement JavaDoc soapElement = (SOAPElement JavaDoc)soapBody.getChildElements().next();
194       
195       StringBuffer JavaDoc builder = new StringBuffer JavaDoc();
196       NodeList JavaDoc nlist = soapElement.getChildNodes();
197       for (int i = 0; i < nlist.getLength(); i++)
198       {
199          Node JavaDoc child = nlist.item(i);
200          String JavaDoc nodeValue = child.getNodeValue();
201          builder.append(nodeValue);
202       }
203
204       return builder.toString();
205    }
206 }
207
Popular Tags