1 package test.encoding; 2 3 import junit.framework.TestCase; 4 import org.apache.axis.AxisEngine; 5 import org.apache.axis.Message; 6 import org.apache.axis.MessageContext; 7 import org.apache.axis.message.MessageElement; 8 import org.apache.axis.message.SOAPBodyElement; 9 import org.apache.axis.message.SOAPEnvelope; 10 import org.apache.axis.message.SOAPHeaderElement; 11 import org.apache.axis.server.AxisServer; 12 import org.apache.axis.utils.XMLUtils; 13 import org.custommonkey.xmlunit.XMLTestCase; 14 15 22 23 public class TestDOM extends XMLTestCase { 24 25 public TestDOM(String name) { 26 super(name); 27 } 28 29 private String header = 30 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 31 "<SOAP-ENV:Envelope" + 32 " SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"" + 33 " xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"" + 34 " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" + 35 " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" + 36 " <SOAP-ENV:Header>\n" + 37 " <SOAP-SEC:signature SOAP-ENV:actor=\"null\" SOAP-ENV:mustUnderstand=\"1\"" + 38 " xmlns:SOAP-SEC=\"http://schemas.xmlsoap.org/soap/security/\">\n" + 39 " <Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\">\n" + 40 " </Signature>\n" + 41 " </SOAP-SEC:signature>\n" + 42 " </SOAP-ENV:Header>\n" + 43 " <SOAP-ENV:Body id=\"body\">\n" + 44 " <ns1:getQuote xmlns:ns1=\"urn:xmltoday-delayed-quotes\">\n"; 45 46 private String request1 = 47 " <symbol xsi:type=\"xsd:string\">IBM</symbol>\n"; 48 49 private String request2 = 50 " <addResult xsi:type=\"xsd:int\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">4</addResult>\n"; 51 52 private String footer = 53 " </ns1:getQuote>\n" + 54 " </SOAP-ENV:Body>\n" + 55 "</SOAP-ENV:Envelope>"; 56 57 public void testDOM() throws Exception { 58 59 AxisEngine engine = new AxisServer(); 61 engine.init(); 62 MessageContext msgContext = new MessageContext(engine); 63 msgContext.setHighFidelity(true); 64 String request = header + request1 + footer; 65 Message message = new Message(request); 66 message.setMessageContext(msgContext); 67 68 message.getSOAPEnvelope(); 70 String result = message.getSOAPPartAsString(); 72 73 assertXMLEqual("Request is not the same as the result.", request, result); 74 } 75 76 public void testHeaders() throws Exception { 77 AxisEngine engine = new AxisServer(); 78 engine.init(); 79 MessageContext msgContext = new MessageContext(engine); 80 msgContext.setHighFidelity(true); 81 String request = header + request1 + footer; 82 Message message = new Message(request); 83 message.setMessageContext(msgContext); 84 85 SOAPEnvelope envelope = message.getSOAPEnvelope(); 87 envelope.addHeader(new SOAPHeaderElement("foo1", "foo1")); 88 envelope.addHeader(new SOAPHeaderElement("foo2", "foo2")); 89 envelope.addHeader(new SOAPHeaderElement("foo3", "foo3")); 90 String result = message.getSOAPPartAsString(); 91 92 assertTrue(result.indexOf("foo1")!=-1); 93 assertTrue(result.indexOf("foo2")!=-1); 94 assertTrue(result.indexOf("foo3")!=-1); 95 96 Message message2 = new Message(result); 97 message2.setMessageContext(msgContext); 98 message2.getSOAPEnvelope(); 99 String result2 = message2.getSOAPPartAsString(); 100 101 assertTrue(result2.indexOf("foo1")!=-1); 102 assertTrue(result2.indexOf("foo2")!=-1); 103 assertTrue(result2.indexOf("foo3")!=-1); 104 } 105 106 109 public void testAttributes() throws Exception { 110 AxisEngine engine = new AxisServer(); 111 engine.init(); 112 MessageContext msgContext = new MessageContext(engine); 113 msgContext.setHighFidelity(true); 114 String request = header + request2 + footer; 115 Message message = new Message(request); 116 message.setMessageContext(msgContext); 117 SOAPEnvelope envelope = message.getSOAPEnvelope(); 118 SOAPBodyElement bodyElement = (SOAPBodyElement)envelope.getBodyElements().elementAt(0); 119 MessageElement me = (MessageElement) bodyElement.getChildren().get(0); 120 org.xml.sax.Attributes atts = me.getCompleteAttributes(); 121 assertTrue(atts.getLength()==2); 122 } 123 124 public void testEmptyNode() throws Exception 125 { 126 SOAPBodyElement body = new SOAPBodyElement(XMLUtils.newDocument().createElementNS(null,"tmp")); 127 assertXMLEqual("<tmp/>",body.toString()); 128 } 129 130 public void testNodeWithAttribute() throws Exception 131 { 132 org.w3c.dom.Element element = XMLUtils.newDocument().createElementNS(null,"tmp"); 133 element.setAttributeNS(null,"attrib", "foo"); 134 SOAPBodyElement body = new SOAPBodyElement(element); 135 assertXMLEqual("<tmp attrib=\"foo\"/>",body.toString()); 136 } 137 138 public static void main(String [] args) throws Exception 139 { 140 TestDOM tester = new TestDOM("TestDOM"); 141 tester.testAttributes(); 142 tester.testHeaders(); 143 tester.testNodeWithAttribute(); 144 tester.testEmptyNode(); 145 tester.testDOM(); 146 } 147 } 148 149 | Popular Tags |