Debian Codegraph @

test/message/TestSOAPBody.java

raw
1 /*
2 * Copyright 2002-2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package test.message;
17
18 import junit.framework.Test;
19 import junit.framework.TestCase;
20 import junit.framework.TestSuite;
21
22 import javax.xml.soap.MessageFactory;
23 import javax.xml.soap.MimeHeaders;
24 import javax.xml.soap.Name;
25 import javax.xml.soap.SOAPBody;
26 import javax.xml.soap.SOAPEnvelope;
27 import javax.xml.soap.SOAPHeader;
28 import javax.xml.soap.SOAPBodyElement;
29 import javax.xml.soap.SOAPMessage;
30 import javax.xml.soap.SOAPPart;
31 import java.io.ByteArrayInputStream;
32 import java.util.Iterator;
33 import org.w3c.dom.Node;
34 import org.w3c.dom.Element;
35 import org.apache.axis.Message;
36 import org.apache.axis.utils.XMLUtils;
37
38 /**
39 * @author [email protected]
40 * @author $Author: venkat $
41 * @version $Revision: 261460 $
42 */
43 public class TestSOAPBody extends TestCase {
44
45 /**
46 * Method suite
47 *
48 * @return
49 */
50 public static Test suite() {
51 return new TestSuite(test.message.TestSOAPBody.class);
52 }
53
54 /**
55 * Method main
56 *
57 * @param argv
58 */
59 public static void main(String[] argv) throws Exception {
60 TestSOAPBody tester = new TestSOAPBody("TestSOAPBody");
61 tester.testSoapBodyBUG();
62 }
63
64 /**
65 * Constructor TestSOAPBody
66 *
67 * @param name
68 */
69 public TestSOAPBody(String name) {
70 super(name);
71 }
72
73 String xmlString =
74 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
75 "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" +
76 " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n" +
77 " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" +
78 " <soapenv:Header>\n" +
79 " <shw:Hello xmlns:shw=\"http://www.jcommerce.net/soap/ns/SOAPHelloWorld\">\n" +
80 " <shw:Myname>Tony</shw:Myname>\n" +
81 " </shw:Hello>\n" +
82 " </soapenv:Header>\n" +
83 " <soapenv:Body>\n" +
84 " <shw:Address xmlns:shw=\"http://www.jcommerce.net/soap/ns/SOAPHelloWorld\">\n" +
85 " <shw:City>GENT</shw:City>\n" +
86 " </shw:Address>\n" +
87 " </soapenv:Body>\n" +
88 "</soapenv:Envelope>";
89
90 /**
91 * Method testSoapBodyBUG
92 *
93 * @throws Exception
94 */
95 public void testSoapBodyBUG() throws Exception {
96 MimeHeaders mimeheaders = new MimeHeaders();
97
98 mimeheaders.addHeader("Content-Type", "text/xml");
99 ByteArrayInputStream instream = new ByteArrayInputStream(xmlString.getBytes());
100 MessageFactory factory =
101 MessageFactory.newInstance();
102 SOAPMessage msg =
103 factory.createMessage(mimeheaders, instream);
104 org.apache.axis.client.AxisClient axisengine =
105 new org.apache.axis.client.AxisClient();
106
107 // need to set it not null , if not nullpointer in sp.getEnvelope()
108 ((org.apache.axis.Message) msg).setMessageContext(
109 new org.apache.axis.MessageContext(axisengine));
110 SOAPPart sp = msg.getSOAPPart();
111 javax.xml.soap.SOAPEnvelope se = sp.getEnvelope();
112 javax.xml.soap.SOAPHeader sh = se.getHeader();
113 SOAPBody sb = se.getBody();
114 Iterator it = sb.getChildElements();
115 int count = 0;
116
117 while (it.hasNext()) {
118 SOAPBodyElement el = (SOAPBodyElement) it.next();
119 count++;
120 Name name = el.getElementName();
121 System.out.println("Element:" + el);
122 System.out.println("BODY ELEMENT NAME:" + name.getPrefix() + ":"
123 + name.getLocalName() + " " + name.getURI());
124 }
125 assertTrue(count == 1);
126 }
127 /**
128 * Method testSaveChanges
129 *
130 * @throws Exception
131 */
132 public void testSaveChanges() throws Exception {
133 MimeHeaders mimeheaders = new MimeHeaders();
134
135 mimeheaders.addHeader("Content-Type", "text/xml");
136 ByteArrayInputStream instream = new ByteArrayInputStream(xmlString.getBytes());
137 MessageFactory factory =
138 MessageFactory.newInstance();
139 SOAPMessage msg =
140 factory.createMessage(mimeheaders, instream);
141 org.apache.axis.client.AxisClient axisengine =
142 new org.apache.axis.client.AxisClient();
143
144 ((Message) msg).setMessageContext(
145 new org.apache.axis.MessageContext(axisengine));
146 SOAPPart sp = msg.getSOAPPart();
147 SOAPEnvelope se = sp.getEnvelope();
148 SOAPBody sb = se.getBody();
149
150 Node myNode = (Element) sb.getElementsByTagName("City").item(0);
151 myNode.replaceChild( myNode.getOwnerDocument().createTextNode("NY"), myNode.getFirstChild());
152
153 msg.saveChanges();
154
155 sp = msg.getSOAPPart();
156 se = sp.getEnvelope();
157 sb = se.getBody();
158 myNode = (Element) sb.getElementsByTagName("City").item(0);
159 Node city = myNode.getFirstChild();
160 assertEquals("City name did not change to NY", city.toString(), "NY");
161 }
162 }