KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > message > TestSOAPBody


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 JavaDoc;
23 import javax.xml.soap.MimeHeaders JavaDoc;
24 import javax.xml.soap.Name JavaDoc;
25 import javax.xml.soap.SOAPBody JavaDoc;
26 import javax.xml.soap.SOAPBodyElement JavaDoc;
27 import javax.xml.soap.SOAPMessage JavaDoc;
28 import javax.xml.soap.SOAPPart JavaDoc;
29 import java.io.ByteArrayInputStream JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 /**
33  * @author john.gregg@techarch.com
34  * @author $Author: dims $
35  * @version $Revision: 1.3 $
36  */

37 public class TestSOAPBody extends TestCase {
38
39     /**
40      * Method suite
41      *
42      * @return
43      */

44     public static Test suite() {
45         return new TestSuite(test.message.TestSOAPBody.class);
46     }
47
48     /**
49      * Method main
50      *
51      * @param argv
52      */

53     public static void main(String JavaDoc[] argv) throws Exception JavaDoc {
54         TestSOAPBody tester = new TestSOAPBody("TestSOAPBody");
55         tester.testSoapBodyBUG();
56     }
57
58     /**
59      * Constructor TestSOAPBody
60      *
61      * @param name
62      */

63     public TestSOAPBody(String JavaDoc name) {
64         super(name);
65     }
66
67     String JavaDoc xmlString =
68             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
69             "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" +
70             " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n" +
71             " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" +
72             " <soapenv:Header>\n" +
73             " <shw:Hello xmlns:shw=\"http://www.jcommerce.net/soap/ns/SOAPHelloWorld\">\n" +
74             " <shw:Myname>Tony</shw:Myname>\n" +
75             " </shw:Hello>\n" +
76             " </soapenv:Header>\n" +
77             " <soapenv:Body>\n" +
78             " <shw:Address xmlns:shw=\"http://www.jcommerce.net/soap/ns/SOAPHelloWorld\">\n" +
79             " <shw:City>GENT</shw:City>\n" +
80             " </shw:Address>\n" +
81             " </soapenv:Body>\n" +
82             "</soapenv:Envelope>";
83
84     /**
85      * Method testSoapBodyBUG
86      *
87      * @throws Exception
88      */

89     public void testSoapBodyBUG() throws Exception JavaDoc {
90         MimeHeaders JavaDoc mimeheaders = new MimeHeaders JavaDoc();
91
92         mimeheaders.addHeader("Content-Type", "text/xml");
93         ByteArrayInputStream JavaDoc instream = new ByteArrayInputStream JavaDoc(xmlString.getBytes());
94         MessageFactory JavaDoc factory =
95                 MessageFactory.newInstance();
96         SOAPMessage JavaDoc msg =
97                 factory.createMessage(mimeheaders, instream);
98         org.apache.axis.client.AxisClient axisengine =
99                 new org.apache.axis.client.AxisClient();
100
101         // need to set it not null , if not nullpointer in sp.getEnvelope()
102
((org.apache.axis.Message) msg).setMessageContext(
103                 new org.apache.axis.MessageContext(axisengine));
104         SOAPPart JavaDoc sp = msg.getSOAPPart();
105         javax.xml.soap.SOAPEnvelope JavaDoc se = sp.getEnvelope();
106         javax.xml.soap.SOAPHeader JavaDoc sh = se.getHeader();
107         SOAPBody JavaDoc sb = se.getBody();
108         Iterator JavaDoc it = sb.getChildElements();
109         int count = 0;
110
111         while (it.hasNext()) {
112             SOAPBodyElement JavaDoc el = (SOAPBodyElement JavaDoc) it.next();
113             count++;
114             Name JavaDoc name = el.getElementName();
115             System.out.println("Element:" + el);
116             System.out.println("BODY ELEMENT NAME:" + name.getPrefix() + ":"
117                     + name.getLocalName() + " " + name.getURI());
118         }
119         assertTrue(count == 1);
120     }
121 }
122
Popular Tags