| 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.encoding.DeserializationContext; |
| 8 |
import org.apache.axis.encoding.SerializationContext; |
| 9 |
import org.apache.axis.message.MessageElement; |
| 10 |
import org.apache.axis.message.SOAPBodyElement; |
| 11 |
import org.apache.axis.message.SOAPEnvelope; |
| 12 |
import org.apache.axis.message.SOAPHeaderElement; |
| 13 |
import org.apache.axis.message.SOAPBody; |
| 14 |
import org.apache.axis.server.AxisServer; |
| 15 |
import org.apache.axis.utils.XMLUtils; |
| 16 |
import org.custommonkey.xmlunit.XMLTestCase; |
| 17 |
import org.custommonkey.xmlunit.Diff; |
| 18 |
import org.xml.sax.InputSource; |
| 19 |
|
| 20 |
import java.io.StringReader; |
| 21 |
import java.io.StringWriter; |
| 22 |
|
| 23 |
/** |
| 24 |
|
| 25 |
* Verify that deserialization actually can cause the soap service |
| 26 |
|
| 27 |
* to be set... |
| 28 |
|
| 29 |
*/ |
| 30 |
|
| 31 |
public class TestDOM extends XMLTestCase { |
| 32 |
|
| 33 |
public TestDOM(String name) { |
| 34 |
super(name); |
| 35 |
} |
| 36 |
|
| 37 |
private String header = |
| 38 |
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + |
| 39 |
"<SOAP-ENV:Envelope" + |
| 40 |
" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"" + |
| 41 |
" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"" + |
| 42 |
" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" + |
| 43 |
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" + |
| 44 |
" <SOAP-ENV:Header>\n" + |
| 45 |
" <SOAP-SEC:signature SOAP-ENV:actor=\"null\" SOAP-ENV:mustUnderstand=\"1\"" + |
| 46 |
" xmlns:SOAP-SEC=\"http://schemas.xmlsoap.org/soap/security/\">\n" + |
| 47 |
" <Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\">\n" + |
| 48 |
" </Signature>\n" + |
| 49 |
" </SOAP-SEC:signature>\n" + |
| 50 |
" </SOAP-ENV:Header>\n" + |
| 51 |
" <SOAP-ENV:Body id=\"body\">\n" + |
| 52 |
" <ns1:getQuote xmlns:ns1=\"urn:xmltoday-delayed-quotes\">\n"; |
| 53 |
|
| 54 |
private String request1 = |
| 55 |
" <symbol xsi:type=\"xsd:string\">IBM</symbol>\n"; |
| 56 |
|
| 57 |
private String request2 = |
| 58 |
" <addResult xsi:type=\"xsd:int\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">4</addResult>\n"; |
| 59 |
|
| 60 |
private String footer = |
| 61 |
" </ns1:getQuote>\n" + |
| 62 |
" </SOAP-ENV:Body>\n" + |
| 63 |
"</SOAP-ENV:Envelope>"; |
| 64 |
|
| 65 |
public void testDOM() throws Exception { |
| 66 |
|
| 67 |
// setup |
| 68 |
AxisEngine engine = new AxisServer(); |
| 69 |
engine.init(); |
| 70 |
MessageContext msgContext = new MessageContext(engine); |
| 71 |
msgContext.setHighFidelity(true); |
| 72 |
String request = header + request1 + footer; |
| 73 |
Message message = new Message(request); |
| 74 |
message.setMessageContext(msgContext); |
| 75 |
|
| 76 |
// Now completely round trip it |
| 77 |
message.getSOAPEnvelope(); |
| 78 |
// Element dom = message.getAsDOM(); |
| 79 |
String result = message.getSOAPPartAsString(); |
| 80 |
|
| 81 |
assertXMLEqual("Request is not the same as the result.", request, result); |
| 82 |
} |
| 83 |
|
| 84 |
public void testHeaders() throws Exception { |
| 85 |
AxisEngine engine = new AxisServer(); |
| 86 |
engine.init(); |
| 87 |
MessageContext msgContext = new MessageContext(engine); |
| 88 |
msgContext.setHighFidelity(true); |
| 89 |
String request = header + request1 + footer; |
| 90 |
Message message = new Message(request); |
| 91 |
message.setMessageContext(msgContext); |
| 92 |
|
| 93 |
// Now completely round trip it |
| 94 |
SOAPEnvelope envelope = message.getSOAPEnvelope(); |
| 95 |
envelope.addHeader(new SOAPHeaderElement("foo1", "foo1")); |
| 96 |
envelope.addHeader(new SOAPHeaderElement("foo2", "foo2")); |
| 97 |
envelope.addHeader(new SOAPHeaderElement("foo3", "foo3")); |
| 98 |
String result = message.getSOAPPartAsString(); |
| 99 |
|
| 100 |
assertTrue(result.indexOf("foo1")!=-1); |
| 101 |
assertTrue(result.indexOf("foo2")!=-1); |
| 102 |
assertTrue(result.indexOf("foo3")!=-1); |
| 103 |
|
| 104 |
Message message2 = new Message(result); |
| 105 |
message2.setMessageContext(msgContext); |
| 106 |
message2.getSOAPEnvelope(); |
| 107 |
String result2 = message2.getSOAPPartAsString(); |
| 108 |
|
| 109 |
assertTrue(result2.indexOf("foo1")!=-1); |
| 110 |
assertTrue(result2.indexOf("foo2")!=-1); |
| 111 |
assertTrue(result2.indexOf("foo3")!=-1); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Test for Bug 7132 |
| 116 |
*/ |
| 117 |
public void testAttributes() throws Exception { |
| 118 |
AxisEngine engine = new AxisServer(); |
| 119 |
engine.init(); |
| 120 |
MessageContext msgContext = new MessageContext(engine); |
| 121 |
msgContext.setHighFidelity(true); |
| 122 |
String request = header + request2 + footer; |
| 123 |
Message message = new Message(request); |
| 124 |
message.setMessageContext(msgContext); |
| 125 |
SOAPEnvelope envelope = message.getSOAPEnvelope(); |
| 126 |
SOAPBodyElement bodyElement = (SOAPBodyElement)envelope.getBodyElements().elementAt(0); |
| 127 |
MessageElement me = (MessageElement) bodyElement.getChildren().get(0); |
| 128 |
org.xml.sax.Attributes atts = me.getCompleteAttributes(); |
| 129 |
assertTrue(atts.getLength()==2); |
| 130 |
} |
| 131 |
|
| 132 |
public void testEmptyNode() throws Exception |
| 133 |
{ |
| 134 |
SOAPBodyElement body = new SOAPBodyElement(XMLUtils.newDocument().createElementNS(null,"tmp")); |
| 135 |
assertXMLEqual("<tmp/>",body.toString()); |
| 136 |
} |
| 137 |
|
| 138 |
public void testNodeWithAttribute() throws Exception |
| 139 |
{ |
| 140 |
org.w3c.dom.Element element = XMLUtils.newDocument().createElementNS(null,"tmp"); |
| 141 |
element.setAttributeNS(null,"attrib", "foo"); |
| 142 |
SOAPBodyElement body = new SOAPBodyElement(element); |
| 143 |
assertXMLEqual("<tmp attrib=\"foo\"/>",body.toString()); |
| 144 |
} |
| 145 |
|
| 146 |
public void testDOM2() throws Exception |
| 147 |
{ |
| 148 |
// Simulate receiving a signed message. |
| 149 |
// |
| 150 |
String xml1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + |
| 151 |
"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" + |
| 152 |
" <soapenv:Body>\n"; |
| 153 |
String xml2 = " <SASLResponse xmlns=\"urn:liberty:sa:2004-04\">\n" + |
| 154 |
" <Status code=\"OK\" comment=\"Authenticated\"/>\n" + |
| 155 |
" </SASLResponse>\n"; |
| 156 |
String xml3 = |
| 157 |
" </soapenv:Body>\n" + |
| 158 |
"</soapenv:Envelope>"; |
| 159 |
|
| 160 |
DeserializationContext ctx = new DeserializationContext(new InputSource(new StringReader(xml1 + xml2 + xml3)), null, "response"); |
| 161 |
ctx.parse(); |
| 162 |
|
| 163 |
SOAPEnvelope env = ctx.getEnvelope(); |
| 164 |
SOAPBody body = (SOAPBody) env.getBody(); |
| 165 |
|
| 166 |
// I am using the body child as my "token". The basic idea is that |
| 167 |
// this element must be serialized _exactly_ as it was received. |
| 168 |
MessageElement elt = (MessageElement) body.getFirstChild(); |
| 169 |
assertTrue(!elt.isDirty()); |
| 170 |
|
| 171 |
StringWriter writer = new StringWriter(); |
| 172 |
SerializationContext serializeContext = new SerializationContext(writer, null); |
| 173 |
serializeContext.setSendDecl(false); |
| 174 |
elt.output(serializeContext); |
| 175 |
writer.close(); |
| 176 |
|
| 177 |
assertXMLIdentical("Deserialization invalidated XML", |
| 178 |
new Diff(xml2, writer.getBuffer().toString()), true); |
| 179 |
} |
| 180 |
|
| 181 |
public static void main(String [] args) throws Exception |
| 182 |
{ |
| 183 |
TestDOM tester = new TestDOM("TestDOM"); |
| 184 |
tester.testAttributes(); |
| 185 |
tester.testHeaders(); |
| 186 |
tester.testNodeWithAttribute(); |
| 187 |
tester.testEmptyNode(); |
| 188 |
tester.testDOM(); |
| 189 |
} |
| 190 |
} |
| 191 |
|