Debian Codegraph @

test/utils/TestXMLUtils.java

raw
1 package test.utils;
2
3 import junit.framework.Test;
4 import junit.framework.TestSuite;
5 import org.apache.axis.encoding.DeserializationContext;
6 import test.AxisTestBase;
7 import org.apache.axis.utils.XMLUtils;
8 import org.apache.axis.message.PrefixedQName;
9 import org.apache.axis.message.MessageElement;
10 import org.apache.axis.Constants;
11 import org.w3c.dom.Document;
12 import org.w3c.dom.Element;
13 import org.w3c.dom.NodeList;
14 import org.xml.sax.InputSource;
15 import org.custommonkey.xmlunit.XMLUnit;
16
17 import javax.xml.parsers.SAXParser;
18 import javax.xml.soap.SOAPEnvelope;
19 import javax.xml.soap.SOAPBodyElement;
20 import java.io.ByteArrayInputStream;
21 import java.io.InputStream;
22 import java.io.OutputStreamWriter;
23 import java.io.PipedOutputStream;
24 import java.io.Reader;
25 import java.io.StringReader;
26 import java.util.Iterator;
27
28 public class TestXMLUtils extends AxisTestBase
29 {
30
31 public TestXMLUtils (String name) {
32 super(name);
33 }
34
35 public static Test suite() {
36 return new TestSuite(TestXMLUtils.class);
37 }
38
39 public void setup() {
40 }
41
42 public void testNewDocumentNoArgConstructor() throws Exception
43 {
44 Document doc = XMLUtils.newDocument();
45 assertNotNull("Did not get a new Document", doc);
46 }
47
48 public void testNewDocumentInputSource() throws Exception
49 {
50 Reader reader = (Reader)this.getTestXml("reader");
51 InputSource inputsrc = new InputSource(reader);
52 Document doc = XMLUtils.newDocument(inputsrc);
53 assertNotNull("Did not get a new Document", doc);
54 }
55
56 public void testNewDocumentInputStream() throws Exception
57 {
58 InputStream iostream = (InputStream)this.getTestXml("inputstream");
59 InputSource inputsrc = new InputSource(iostream);
60 Document doc = XMLUtils.newDocument(inputsrc);
61 assertNotNull("Did not get a new Document", doc);
62 }
63
64 /* This test will fail unless you are connected to the Web, so just skip
65 * it unless you really want to test it. When not connected to the Web you
66 * will get an UnknownHostException.
67 */
68
69 public void testNewDocumentURI() throws Exception
70 {
71 if(isOnline()) {
72 String uri = "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd";
73 Document doc = XMLUtils.newDocument(uri);
74 assertNotNull("Did not get a new Document", doc);
75 }
76 }
77
78 public void testDocumentToString() throws Exception
79 {
80 Reader reader = (Reader)this.getTestXml("reader");
81 InputSource inputsrc = new InputSource(reader);
82 Document doc = XMLUtils.newDocument(inputsrc);
83
84 String xmlString = (String)this.getTestXml("string");
85 String result = XMLUtils.DocumentToString(doc);
86 assertXMLEqual("xmlString is not the same as result", xmlString, result);
87 }
88
89 /**
90 * This test method is somewhat complex, but it solves a problem people have
91 * asked me about, which is how to unit test a method that has void return
92 * type but writes its output to a writer. So half the reason for
93 * creating and using it here is as a reference point.
94 */
95 public void testElementToWriter() throws Exception
96 {
97 /* Get the Document and one of its elements. */
98 Reader xmlReader = (Reader)this.getTestXml("reader");
99 InputSource inputsrc = new InputSource(xmlReader);
100 Document doc = XMLUtils.newDocument(inputsrc);
101 NodeList nl = doc.getElementsByTagName("display-name");
102 Element elem = (Element)nl.item(0);
103 String expected = "<display-name>Apache-Axis</display-name>";
104
105 /*
106 * Create a PipedOutputStream to get the output from the tested method.
107 * Pass the PipedOutputStream to the ConsumerPipe's constructor, which
108 * will create a PipedInputStream in a separate thread.
109 */
110 PipedOutputStream out = new PipedOutputStream();
111 OutputStreamWriter writer = new OutputStreamWriter(out);
112 ConsumerPipe cpipe = new ConsumerPipe(out);
113
114 /*
115 * Call the method under test, passing the PipedOutStream to trap the
116 * results.
117 */
118 XMLUtils.ElementToWriter(elem, writer);
119
120 /*
121 * The output of the test will be piped to ConsumerPipe's PipedInputStream, which
122 * is used to read the bytes of the stream into an array. It then creates a
123 * String for comparison from that byte array.
124 */
125 out.flush();
126 String result = cpipe.getResult();
127 //don't forget to close this end of the pipe (ConsumerPipe closes the other end).
128 out.close();
129
130 assertEquals("Did not get the expected result", expected, result);
131 }
132
133 /**
134 * For explanation of the methodology used to test this method, see notes in
135 * previous test method.
136 */
137 public void testDocumentToStream() throws Exception
138 {
139 Reader reader = (Reader)this.getTestXml("reader");
140 InputSource inputsrc = new InputSource(reader);
141 Document doc = XMLUtils.newDocument(inputsrc);
142
143 PipedOutputStream out = new PipedOutputStream();
144 ConsumerPipe cpipe = new ConsumerPipe(out);
145
146 XMLUtils.DocumentToStream(doc, out);
147 out.flush();
148 String result = cpipe.getResult();
149 out.close();
150
151 String expected = (String)this.getTestXml("string");
152 assertXMLEqual("Did not get the expected result", expected, result);
153 }
154
155 public void testElementToString() throws Exception
156 {
157 Reader reader = (Reader)this.getTestXml("reader");
158 InputSource inputsrc = new InputSource(reader);
159 Document doc = XMLUtils.newDocument(inputsrc);
160
161 NodeList nl = doc.getElementsByTagName("display-name");
162 Element elem = (Element)nl.item(0);
163 String expected = "<display-name>Apache-Axis</display-name>";
164 String result = XMLUtils.ElementToString(elem);
165 assertEquals("Element tag name is not 'display-name', it is: " + elem.getTagName(),
166 "display-name", elem.getTagName());
167 assertEquals("Did not get the expected result", expected, result);
168 }
169
170 public void testGetInnerXMLString() throws Exception
171 {
172 Reader reader = (Reader)this.getTestXml("reader");
173 InputSource inputsrc = new InputSource(reader);
174 Document doc = XMLUtils.newDocument(inputsrc);
175
176 NodeList nl = doc.getElementsByTagName("display-name");
177 Element elem = (Element)nl.item(0);
178 String expected = "Apache-Axis";
179 String result = XMLUtils.getInnerXMLString(elem);
180 assertEquals(expected, result);
181 }
182
183 public void testGetPrefix() throws Exception
184 {
185 Document doc = XMLUtils.newDocument();
186
187 Element elem = doc.createElementNS("", "svg");
188 elem.setAttribute("xmlns:svg", "\"http://www.w3.org/2000/svg\"");
189 elem.setAttribute("xmlns:xlink", "\"http://www.w3.org/1999/xlink\"");
190 elem.setAttribute("xmlns:xhtml", "\"http://www.w3.org/1999/xhtml\"");
191
192 String expected = "svg";
193 String result = XMLUtils.getPrefix("\"http://www.w3.org/2000/svg\"", elem);
194 assertEquals("Did not get the expected result", expected, result);
195 expected = "xlink";
196 result = XMLUtils.getPrefix("\"http://www.w3.org/1999/xlink\"", elem);
197 assertEquals("Did not get the expected result", expected, result);
198 expected = "xhtml";
199 result = XMLUtils.getPrefix("\"http://www.w3.org/1999/xhtml\"", elem);
200 assertEquals("Did not get the expected result", expected, result);
201 }
202
203 public void testGetNamespace() throws Exception
204 {
205 String testDoc = "<svg xmlns:svg=\"http://www.w3.org/2000/svg\"/>";
206 InputSource inputsrc = new InputSource(new StringReader(testDoc));
207 Document doc = XMLUtils.newDocument(inputsrc);
208 assertNotNull("Got a null document", doc);
209
210 NodeList nl = doc.getElementsByTagName("svg");
211 Element elem = (Element)nl.item(0);
212
213 String expected = "http://www.w3.org/2000/svg";
214 String result = XMLUtils.getNamespace("svg", elem);
215 assertEquals("Did not get the expected result", expected, result);
216 }
217
218 /**
219 * This is a utility method for creating XML document input sources for this
220 * JUnit test class. The returned Object should be cast to the type you
221 * request via the gimme parameter.
222 *
223 * @param gimme A String specifying the underlying type you want the XML
224 * input source returned as; one of "string", "reader", or "inputstream."
225 */
226 public Object getTestXml(String gimme)
227 {
228 String lineSep = System.getProperty("line.separator");
229 StringBuffer sb = new StringBuffer();
230 sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + lineSep)
231 //The System ID will cause an unknown host exception unless you are
232 //connected to the Internet, so comment it out for testing.
233 //.append("<!DOCTYPE web-app PUBLIC \"-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN\"" + lineSep)
234 //.append("\"http://java.sun.com/j2ee/dtds/web-app_2.2.dtd\">" + lineSep)
235 .append("<web-app>" + lineSep)
236 .append("<display-name>Apache-Axis</display-name>" + lineSep)
237 .append("<servlet>" + lineSep)
238 .append("<servlet-name>AxisServlet</servlet-name>" + lineSep)
239 .append("<display-name>Apache-Axis Servlet</display-name>" + lineSep)
240 .append("<servlet-class>" + lineSep)
241 .append("org.apache.axis.transport.http.AxisServlet" + lineSep)
242 .append("</servlet-class>" + lineSep)
243 .append("</servlet>" + lineSep)
244 .append("<servlet-mapping>" + lineSep)
245 .append("<servlet-name>AxisServlet</servlet-name>" + lineSep)
246 .append("<url-pattern>servlet/AxisServlet</url-pattern>" + lineSep)
247 .append("<url-pattern>*.jws</url-pattern>" + lineSep)
248 .append("</servlet-mapping>" + lineSep)
249 .append("</web-app>");
250
251 String xmlString = sb.toString();
252
253 if (gimme.equals("string"))
254 {
255 return xmlString;
256 }
257 else if (gimme.equals("reader"))
258 {
259 StringReader strReader = new StringReader(xmlString);
260 return strReader;
261 }
262 else if (gimme.equals("inputstream"))
263 {
264 ByteArrayInputStream byteStream = new ByteArrayInputStream(xmlString.getBytes());
265 return byteStream;
266 }
267 else return null;
268 }
269
270 public void testDOM2Writer() throws Exception
271 {
272 StringBuffer sb = new StringBuffer();
273 sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
274 sb.append("<xsd:schema targetNamespace=\"http://tempuri.org\"");
275 sb.append(" xmlns=\"http://tempuri.org\"");
276 sb.append(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">");
277 sb.append(" <xsd:annotation>");
278 sb.append(" <xsd:documentation xml:lang=\"en\">");
279 sb.append(" Purchase order schema for Example.com.");
280 sb.append(" Copyright 2000 Example.com. All rights reserved.");
281 sb.append(" </xsd:documentation>");
282 sb.append(" </xsd:annotation>");
283 sb.append("</xsd:schema>");
284
285 StringReader strReader = new StringReader(sb.toString());
286 InputSource inputsrc = new InputSource(strReader);
287 Document doc = XMLUtils.newDocument(inputsrc);
288
289 String output = org.apache.axis.utils.DOM2Writer.nodeToString(doc,false);
290 assertTrue(output.indexOf("http://www.w3.org/XML/1998/namespace")==-1);
291 }
292
293 public void testDOMXXE() throws Exception
294 {
295 StringBuffer sb = new StringBuffer();
296 sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
297 sb.append("<!DOCTYPE project [");
298 sb.append("<!ENTITY buildxml SYSTEM \"file:build.xml\">");
299 sb.append("]>");
300 sb.append("<xsd:schema targetNamespace=\"http://tempuri.org\"");
301 sb.append(" xmlns=\"http://tempuri.org\"");
302 sb.append(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">");
303 sb.append(" <xsd:annotation>");
304 sb.append(" <xsd:documentation xml:lang=\"en\">");
305 sb.append(" &buildxml;");
306 sb.append(" Purchase order schema for Example.com.");
307 sb.append(" Copyright 2000 Example.com. All rights reserved.");
308 sb.append(" </xsd:documentation>");
309 sb.append(" </xsd:annotation>");
310 sb.append("</xsd:schema>");
311
312 StringReader strReader = new StringReader(sb.toString());
313 InputSource inputsrc = new InputSource(strReader);
314 Document doc = XMLUtils.newDocument(inputsrc);
315 String output = org.apache.axis.utils.DOM2Writer.nodeToString(doc,false);
316 }
317
318 String msg = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
319 "<!DOCTYPE project [" +
320 "<!ENTITY buildxml SYSTEM \"file:build.xml\">" +
321 "]>" +
322 "<SOAP-ENV:Envelope " +
323 "xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
324 "xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" > " +
325 "<SOAP-ENV:Body>\n" +
326 "&buildxml;" +
327 "<echo:Echo xmlns:echo=\"EchoService\">\n" +
328 "<symbol>IBM</symbol>\n" +
329 "</echo:Echo>\n" +
330 "</SOAP-ENV:Body></SOAP-ENV:Envelope>\n";
331
332 public void testSAXXXE1() throws Exception
333 {
334 StringReader strReader = new StringReader(msg);
335 InputSource inputsrc = new InputSource(strReader);
336 SAXParser parser = XMLUtils.getSAXParser();
337 parser.getParser().parse(inputsrc);
338 }
339
340 public void testSAXXXE2() throws Exception
341 {
342 StringReader strReader2 = new StringReader(msg);
343 InputSource inputsrc2 = new InputSource(strReader2);
344 SAXParser parser2 = XMLUtils.getSAXParser();
345 parser2.getXMLReader().parse(inputsrc2);
346 }
347
348 // If we are using DeserializationContext, we do not allow
349 // a DOCTYPE to be specified to prevent denial of service attacks
350 // via the ENTITY processing intstruction.
351 String msg2 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
352 "<SOAP-ENV:Envelope " +
353 "xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
354 "xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" > " +
355 "<SOAP-ENV:Body>\n" +
356 "<echo:Echo xmlns:echo=\"EchoService\">\n" +
357 "<symbol xml:lang=\"en\">IBM</symbol>\n" +
358 "</echo:Echo>\n" +
359 "</SOAP-ENV:Body></SOAP-ENV:Envelope>\n";
360
361 /**
362 * Confirm we can parse a SOAP Envelope, and make sure that the
363 * xml:lang attribute is handled OK while we're at it.
364 *
365 * @throws Exception
366 */
367 public void testSAXXXE3() throws Exception
368 {
369 StringReader strReader3 = new StringReader(msg2);
370 DeserializationContext dser = new DeserializationContext(
371 new InputSource(strReader3), null, org.apache.axis.Message.REQUEST);
372 dser.parse();
373 SOAPEnvelope env = dser.getEnvelope();
374 SOAPBodyElement body = (SOAPBodyElement)env.getBody().getChildElements().next();
375 assertNotNull(body);
376 MessageElement child = (MessageElement)body.getChildElements().next();
377 assertNotNull(child);
378 Iterator i = child.getAllAttributes();
379 assertNotNull(i);
380 PrefixedQName attr = (PrefixedQName)i.next();
381 assertNotNull(attr);
382 assertEquals("Prefix for attribute was not 'xml'", attr.getPrefix(), "xml");
383 assertEquals("Namespace for attribute was not correct", attr.getURI(),
384 Constants.NS_URI_XML);
385 }
386
387 String msg3 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
388 "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
389 " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " +
390 " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
391 " <soapenv:Header>" +
392 " <TrackingHeader xmlns=\"http://testns.org/test\">1234</TrackingHeader>" +
393 " <ns1:Security soapenv:mustUnderstand=\"0\" xmlns:ns1=\"http://schemas.xmlsoap.org/ws/2002/07/secext\">" +
394 " <ns1:UsernameToken>" +
395 " <ns1:Username xsi:type=\"xsd:string\">foobar</ns1:Username>" +
396 " <ns1:Password xsi:type=\"xsd:string\">wibble</ns1:Password>" +
397 " </ns1:UsernameToken>" +
398 " </ns1:Security>" +
399 " </soapenv:Header>" +
400 " <soapenv:Body>" +
401 " <EchoString>asdadsf</EchoString>" +
402 " </soapenv:Body>" +
403 "</soapenv:Envelope>";
404
405 /**
406 * Test for Bug 22980
407 * @throws Exception
408 */
409 public void testNSStack() throws Exception
410 {
411 StringReader strReader3 = new StringReader(msg3);
412 DeserializationContext dser = new DeserializationContext(
413 new InputSource(strReader3), null, org.apache.axis.Message.REQUEST);
414 dser.parse();
415 org.apache.axis.message.SOAPEnvelope env = dser.getEnvelope();
416 String xml = env.toString();
417 boolean oldIgnore = XMLUnit.getIgnoreWhitespace();
418 XMLUnit.setIgnoreWhitespace(true);
419 try {
420 assertXMLEqual(xml,msg3);
421 } finally {
422 XMLUnit.setIgnoreWhitespace(oldIgnore);
423 }
424 }
425
426 public static void main(String[] args) throws Exception
427 {
428 TestXMLUtils test = new TestXMLUtils("TestXMLUtils");
429 test.testSAXXXE3();
430 test.testNSStack();
431 }
432 }