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 ; 7 import org.w3c.dom.Document ; 8 import org.custommonkey.xmlunit.XMLUnit; 9 10 import javax.xml.soap.MessageFactory ; 11 import javax.xml.soap.MimeHeaders ; 12 import javax.xml.soap.Name ; 13 import javax.xml.soap.SOAPEnvelope ; 14 import javax.xml.soap.SOAPFactory ; 15 import javax.xml.soap.SOAPHeader ; 16 import javax.xml.soap.SOAPHeaderElement ; 17 import javax.xml.soap.SOAPMessage ; 18 import javax.xml.soap.SOAPPart ; 19 import javax.xml.soap.SOAPBody ; 20 import javax.xml.soap.SOAPElement ; 21 import javax.xml.soap.SOAPBodyElement ; 22 import java.io.ByteArrayInputStream ; 23 import java.io.ByteArrayOutputStream ; 24 import java.util.Iterator ; 25 26 import test.AxisTestBase; 27 28 public class TestDOM extends AxisTestBase { 29 public TestDOM(String name) { 30 super(name); 31 } 32 33 public void testOwnerDocument() throws Exception { 34 final SOAPMessage message = MessageFactory.newInstance().createMessage(); 35 SOAPPart 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 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 getSOAPMessageFromString(String str) throws Exception { 55 MimeHeaders mimeHeaders = new MimeHeaders (); 56 mimeHeaders.addHeader("content-type", "text/xml"); 57 SOAPMessage message = MessageFactory.newInstance().createMessage( 58 mimeHeaders, 59 new ByteArrayInputStream (str.getBytes())); 60 SOAPEnvelope envelope = message.getSOAPPart().getEnvelope(); 61 SOAPHeader header = message.getSOAPHeader(); 62 if (header == null) { 63 header = envelope.addHeader(); 64 } 65 return message; 66 } 67 68 public void testSAAJSerialization() throws Exception { 69 SOAPMessage message1 = this.getSOAPMessageFromString(SAMPLE_1); 70 SOAPHeader header1 = message1.getSOAPHeader(); 71 72 boolean oldIgnore = XMLUnit.getIgnoreWhitespace(); 73 XMLUnit.setIgnoreWhitespace(true); 74 try { 75 SOAPFactory soapFactory = SOAPFactory.newInstance(); 77 Name headerName = soapFactory.createName("Claim", 78 "wsi", "http://ws-i.org/schemas/conformanceClaim/"); 79 SOAPHeaderElement headerElement = 80 header1.addHeaderElement(headerName); 81 headerElement.addAttribute(soapFactory.createName("conformsTo"), "http://ws-i.org/profiles/basic1.0/"); 82 final String domToString1 = XMLUtils.PrettyDocumentToString( 83 message1.getSOAPPart()); 84 final String messageToString1 = messageToString(message1); 85 86 assertXMLEqual(domToString1, messageToString1); 87 } finally { 88 XMLUnit.setIgnoreWhitespace(oldIgnore); 89 } 90 } 91 92 public void testSAAJSerialization2() throws Exception { 93 SOAPMessage message2 = this.getSOAPMessageFromString(SAMPLE_1); 94 SOAPHeader 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 domToString2 = XMLUtils.PrettyDocumentToString( 106 message2.getSOAPPart()); 107 final String messageToString2 = messageToString(message2); 108 109 assertXMLEqual(domToString2, messageToString2); 110 } finally { 111 XMLUnit.setIgnoreWhitespace(oldIgnore); 112 } 113 } 114 115 public void testRPCParams() throws Exception { 116 SOAPMessage 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 []{arg1, arg2}); 123 SOAPPart sp = message.getSOAPPart(); 124 SOAPEnvelope se = sp.getEnvelope(); 125 SOAPBody sb = se.getBody(); 126 sb.addChildElement(body); 127 Iterator it = sb.getChildElements(); 128 assertTrue(it.hasNext()); 129 130 SOAPElement elem = (SOAPElement ) it.next(); 131 132 Name name2 = se.createName("testParam1", "", "urn:myNamespace"); 133 Iterator it2 = elem.getChildElements(name2); 134 assertTrue(it2.hasNext()); 135 while (it2.hasNext()) { 136 SOAPElement elem2 = (SOAPElement ) it2.next(); 137 System.out.println("child = " + elem2); 138 } 139 140 Name name3 = se.createName("testParam2", "", "urn:myNamespace"); 141 Iterator it3 = elem.getChildElements(name3); 142 assertTrue(it3.hasNext()); 143 while (it3.hasNext()) { 144 SOAPElement elem3 = (SOAPElement ) it3.next(); 145 System.out.println("child = " + elem3); 146 } 147 } 148 149 public void testAddDocument() throws Exception { 150 String 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 document = XMLUtils.newDocument(new ByteArrayInputStream (xml.getBytes())); 159 160 MessageFactory factory = new org.apache.axis.soap.MessageFactoryImpl(); 161 SOAPMessage msg = factory.createMessage(); 162 msg.getSOAPBody(); 163 SOAPBody body = msg.getSOAPBody(); 164 165 SOAPBodyElement soapBodyElt = body.addDocument(document); 166 assertXMLEqual(xml, soapBodyElt.toString()); 167 } 168 169 private String messageToString(SOAPMessage message) throws Exception { 170 ByteArrayOutputStream baos = new ByteArrayOutputStream (1024); 171 message.writeTo(baos); 172 return new String (baos.toByteArray()); 173 } 174 175 } 176 | Popular Tags |