KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > saaj > TestDOM


1 package test.saaj;
2
3 import org.apache.axis.utils.XMLUtils;
4 import org.apache.axis.message.RPCParam;
5 import org.apache.axis.message.RPCElement;
6 import org.w3c.dom.Element JavaDoc;
7 import org.w3c.dom.Document JavaDoc;
8 import org.custommonkey.xmlunit.XMLUnit;
9
10 import javax.xml.soap.MessageFactory JavaDoc;
11 import javax.xml.soap.MimeHeaders JavaDoc;
12 import javax.xml.soap.Name JavaDoc;
13 import javax.xml.soap.SOAPEnvelope JavaDoc;
14 import javax.xml.soap.SOAPFactory JavaDoc;
15 import javax.xml.soap.SOAPHeader JavaDoc;
16 import javax.xml.soap.SOAPHeaderElement JavaDoc;
17 import javax.xml.soap.SOAPMessage JavaDoc;
18 import javax.xml.soap.SOAPPart JavaDoc;
19 import javax.xml.soap.SOAPBody JavaDoc;
20 import javax.xml.soap.SOAPElement JavaDoc;
21 import javax.xml.soap.SOAPBodyElement JavaDoc;
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 import test.AxisTestBase;
27
28 public class TestDOM extends AxisTestBase {
29     public TestDOM(String JavaDoc name) {
30         super(name);
31     }
32
33     public void testOwnerDocument() throws Exception JavaDoc {
34         final SOAPMessage JavaDoc message = MessageFactory.newInstance().createMessage();
35         SOAPPart JavaDoc soapPart = message.getSOAPPart();
36         assertNotNull("envelope should have an owner document",
37                 message.getSOAPPart().getEnvelope().getOwnerDocument());
38         assertNotNull("soap part must have a document element",
39                 soapPart.getDocumentElement());
40         assertNotNull(
41                 "soap part's document element's owner document should not be null",
42                 soapPart.getDocumentElement().getOwnerDocument());
43     }
44
45     private static final String JavaDoc SAMPLE_1 =
46             "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">" + "\n" +
47                 "<SOAP-ENV:Body> " + "\n" +
48                     "<m:GetLastTradePrice xmlns:m=\"http://wombat.ztrade.com\">" + "\n" +
49                         "<symbol>SUNW</symbol> " + "\n" +
50                     "</m:GetLastTradePrice> " + "\n" +
51                 "</SOAP-ENV:Body> " + "\n" +
52             "</SOAP-ENV:Envelope>";
53
54     private SOAPMessage JavaDoc getSOAPMessageFromString(String JavaDoc str) throws Exception JavaDoc {
55         MimeHeaders JavaDoc mimeHeaders = new MimeHeaders JavaDoc();
56         mimeHeaders.addHeader("content-type", "text/xml");
57         SOAPMessage JavaDoc message = MessageFactory.newInstance().createMessage(
58                 mimeHeaders,
59                 new ByteArrayInputStream JavaDoc(str.getBytes()));
60         SOAPEnvelope JavaDoc envelope = message.getSOAPPart().getEnvelope();
61         SOAPHeader JavaDoc header = message.getSOAPHeader();
62         if (header == null) {
63             header = envelope.addHeader();
64         }
65         return message;
66     }
67     
68     public void testSAAJSerialization() throws Exception JavaDoc {
69         SOAPMessage JavaDoc message1 = this.getSOAPMessageFromString(SAMPLE_1);
70         SOAPHeader JavaDoc header1 = message1.getSOAPHeader();
71
72         boolean oldIgnore = XMLUnit.getIgnoreWhitespace();
73         XMLUnit.setIgnoreWhitespace(true);
74         try {
75             //this is how header element is added in sun's example
76
SOAPFactory JavaDoc soapFactory = SOAPFactory.newInstance();
77             Name JavaDoc headerName = soapFactory.createName("Claim",
78                     "wsi", "http://ws-i.org/schemas/conformanceClaim/");
79             SOAPHeaderElement JavaDoc headerElement =
80                     header1.addHeaderElement(headerName);
81             headerElement.addAttribute(soapFactory.createName("conformsTo"), "http://ws-i.org/profiles/basic1.0/");
82             final String JavaDoc domToString1 = XMLUtils.PrettyDocumentToString(
83                     message1.getSOAPPart());
84             final String JavaDoc messageToString1 = messageToString(message1);
85     
86             assertXMLEqual(domToString1, messageToString1);
87         } finally {
88             XMLUnit.setIgnoreWhitespace(oldIgnore);
89         }
90     }
91
92     public void testSAAJSerialization2() throws Exception JavaDoc {
93         SOAPMessage JavaDoc message2 = this.getSOAPMessageFromString(SAMPLE_1);
94         SOAPHeader JavaDoc header2 = message2.getSOAPHeader();
95
96         boolean oldIgnore = XMLUnit.getIgnoreWhitespace();
97         XMLUnit.setIgnoreWhitespace(true);
98         try {
99             Element header2Element = header2.getOwnerDocument().createElementNS(
100                     "http://ws-i.org/schemas/conformanceClaim/", "wsi:Claim");
101             header2Element.setAttributeNS(
102                     "http://ws-i.org/schemas/conformanceClaim/",
103                     "wsi:conformsTo", "http://ws-i.org/profiles/basic1.0/");
104             header2.appendChild(header2Element);
105             final String JavaDoc domToString2 = XMLUtils.PrettyDocumentToString(
106                     message2.getSOAPPart());
107             final String JavaDoc messageToString2 = messageToString(message2);
108
109             assertXMLEqual(domToString2, messageToString2);
110         } finally {
111             XMLUnit.setIgnoreWhitespace(oldIgnore);
112         }
113     }
114     
115     public void testRPCParams() throws Exception JavaDoc {
116         SOAPMessage JavaDoc message = MessageFactory.newInstance().createMessage();
117         RPCParam arg1 = new RPCParam("urn:myNamespace", "testParam1",
118                 "this is a string #1");
119         RPCParam arg2 = new RPCParam("urn:myNamespace", "testParam2",
120                 "this is a string #2");
121         RPCElement body = new RPCElement("urn:myNamespace", "method1",
122                 new Object JavaDoc[]{arg1, arg2});
123         SOAPPart JavaDoc sp = message.getSOAPPart();
124         SOAPEnvelope JavaDoc se = sp.getEnvelope();
125         SOAPBody JavaDoc sb = se.getBody();
126         sb.addChildElement(body);
127         Iterator JavaDoc it = sb.getChildElements();
128         assertTrue(it.hasNext());
129
130         SOAPElement JavaDoc elem = (SOAPElement JavaDoc) it.next();
131
132         Name JavaDoc name2 = se.createName("testParam1", "", "urn:myNamespace");
133         Iterator JavaDoc it2 = elem.getChildElements(name2);
134         assertTrue(it2.hasNext());
135         while (it2.hasNext()) {
136             SOAPElement JavaDoc elem2 = (SOAPElement JavaDoc) it2.next();
137             System.out.println("child = " + elem2);
138         }
139
140         Name JavaDoc name3 = se.createName("testParam2", "", "urn:myNamespace");
141         Iterator JavaDoc it3 = elem.getChildElements(name3);
142         assertTrue(it3.hasNext());
143         while (it3.hasNext()) {
144             SOAPElement JavaDoc elem3 = (SOAPElement JavaDoc) it3.next();
145             System.out.println("child = " + elem3);
146         }
147     }
148     
149     public void testAddDocument() throws Exception JavaDoc {
150         String JavaDoc xml = "<bank:getBalance xmlns:bank=\"http://myservice.test.com/banking/\">\n" +
151                      " <gb:getBalanceReq xmlns:gb=\"http://myservice.test.com/banking/getBalance\">\n" +
152                      " <bt:account acctType=\"domestic\" customerId=\"654321\" xmlns:bt=\"http://myservice.test.com/banking/bankTypes\">\n" +
153                      " <bt:accountNumber>1234567890</bt:accountNumber>\n" +
154                      " <bt:currency>USD</bt:currency>\n" +
155                      " </bt:account>\n" +
156                      " </gb:getBalanceReq>\n" +
157                      "</bank:getBalance>";
158         Document JavaDoc document = XMLUtils.newDocument(new ByteArrayInputStream JavaDoc(xml.getBytes()));
159         
160         MessageFactory JavaDoc factory = new org.apache.axis.soap.MessageFactoryImpl();
161         SOAPMessage JavaDoc msg = factory.createMessage();
162         msg.getSOAPBody();
163         SOAPBody JavaDoc body = msg.getSOAPBody();
164         
165         SOAPBodyElement JavaDoc soapBodyElt = body.addDocument(document);
166         assertXMLEqual(xml, soapBodyElt.toString());
167     }
168
169     private String JavaDoc messageToString(SOAPMessage JavaDoc message) throws Exception JavaDoc {
170         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc(1024);
171         message.writeTo(baos);
172         return new String JavaDoc(baos.toByteArray());
173     }
174
175 }
176
Popular Tags