KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > utils > TestXMLUtils


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 JavaDoc;
12 import org.w3c.dom.Element JavaDoc;
13 import org.w3c.dom.NodeList JavaDoc;
14 import org.xml.sax.InputSource JavaDoc;
15
16 import javax.xml.parsers.SAXParser JavaDoc;
17 import javax.xml.soap.SOAPEnvelope JavaDoc;
18 import javax.xml.soap.SOAPBodyElement JavaDoc;
19 import java.io.ByteArrayInputStream JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.OutputStreamWriter JavaDoc;
22 import java.io.PipedOutputStream JavaDoc;
23 import java.io.Reader JavaDoc;
24 import java.io.StringReader JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 public class TestXMLUtils extends AxisTestBase
28 {
29
30     public TestXMLUtils (String JavaDoc name) {
31         super(name);
32     }
33
34     public static Test suite() {
35         return new TestSuite(TestXMLUtils.class);
36     }
37
38     public void setup() {
39     }
40
41     public void testNewDocumentNoArgConstructor() throws Exception JavaDoc
42     {
43         Document JavaDoc doc = XMLUtils.newDocument();
44         assertNotNull("Did not get a new Document", doc);
45     }
46
47     public void testNewDocumentInputSource() throws Exception JavaDoc
48     {
49         Reader JavaDoc reader = (Reader JavaDoc)this.getTestXml("reader");
50         InputSource JavaDoc inputsrc = new InputSource JavaDoc(reader);
51         Document JavaDoc doc = XMLUtils.newDocument(inputsrc);
52         assertNotNull("Did not get a new Document", doc);
53     }
54
55     public void testNewDocumentInputStream() throws Exception JavaDoc
56     {
57         InputStream JavaDoc iostream = (InputStream JavaDoc)this.getTestXml("inputstream");
58         InputSource JavaDoc inputsrc = new InputSource JavaDoc(iostream);
59         Document JavaDoc doc = XMLUtils.newDocument(inputsrc);
60         assertNotNull("Did not get a new Document", doc);
61     }
62
63     /* This test will fail unless you are connected to the Web, so just skip
64     * it unless you really want to test it. When not connected to the Web you
65     * will get an UnknownHostException.
66     */

67
68     public void testNewDocumentURI() throws Exception JavaDoc
69     {
70         if(isOnline()) {
71             String JavaDoc uri = "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd";
72             Document JavaDoc doc = XMLUtils.newDocument(uri);
73             assertNotNull("Did not get a new Document", doc);
74         }
75     }
76
77     public void testDocumentToString() throws Exception JavaDoc
78     {
79         Reader JavaDoc reader = (Reader JavaDoc)this.getTestXml("reader");
80         InputSource JavaDoc inputsrc = new InputSource JavaDoc(reader);
81         Document JavaDoc doc = XMLUtils.newDocument(inputsrc);
82
83         String JavaDoc xmlString = (String JavaDoc)this.getTestXml("string");
84         String JavaDoc result = XMLUtils.DocumentToString(doc);
85         assertXMLEqual("xmlString is not the same as result", xmlString, result);
86     }
87
88     /**
89     * This test method is somewhat complex, but it solves a problem people have
90     * asked me about, which is how to unit test a method that has void return
91     * type but writes its output to a writer. So half the reason for
92     * creating and using it here is as a reference point.
93     */

94     public void testElementToWriter() throws Exception JavaDoc
95     {
96         /* Get the Document and one of its elements. */
97         Reader JavaDoc xmlReader = (Reader JavaDoc)this.getTestXml("reader");
98         InputSource JavaDoc inputsrc = new InputSource JavaDoc(xmlReader);
99         Document JavaDoc doc = XMLUtils.newDocument(inputsrc);
100         NodeList JavaDoc nl = doc.getElementsByTagName("display-name");
101         Element elem = (Element)nl.item(0);
102         String JavaDoc expected = "<display-name>Apache-Axis</display-name>";
103
104         /*
105         * Create a PipedOutputStream to get the output from the tested method.
106         * Pass the PipedOutputStream to the ConsumerPipe's constructor, which
107         * will create a PipedInputStream in a separate thread.
108         */

109         PipedOutputStream JavaDoc out = new PipedOutputStream JavaDoc();
110         OutputStreamWriter JavaDoc writer = new OutputStreamWriter JavaDoc(out);
111         ConsumerPipe cpipe = new ConsumerPipe(out);
112
113         /*
114         * Call the method under test, passing the PipedOutStream to trap the
115         * results.
116         */

117         XMLUtils.ElementToWriter(elem, writer);
118
119         /*
120         * The output of the test will be piped to ConsumerPipe's PipedInputStream, which
121         * is used to read the bytes of the stream into an array. It then creates a
122         * String for comparison from that byte array.
123         */

124         out.flush();
125         String JavaDoc result = cpipe.getResult();
126         //don't forget to close this end of the pipe (ConsumerPipe closes the other end).
127
out.close();
128
129         assertEquals("Did not get the expected result", expected, result);
130     }
131
132     /**
133     * For explanation of the methodology used to test this method, see notes in
134     * previous test method.
135     */

136     public void testDocumentToStream() throws Exception JavaDoc
137     {
138         Reader JavaDoc reader = (Reader JavaDoc)this.getTestXml("reader");
139         InputSource JavaDoc inputsrc = new InputSource JavaDoc(reader);
140         Document JavaDoc doc = XMLUtils.newDocument(inputsrc);
141
142         PipedOutputStream JavaDoc out = new PipedOutputStream JavaDoc();
143         ConsumerPipe cpipe = new ConsumerPipe(out);
144
145         XMLUtils.DocumentToStream(doc, out);
146         out.flush();
147         String JavaDoc result = cpipe.getResult();
148         out.close();
149
150         String JavaDoc expected = (String JavaDoc)this.getTestXml("string");
151         assertXMLEqual("Did not get the expected result", expected, result);
152     }
153
154     public void testElementToString() throws Exception JavaDoc
155     {
156         Reader JavaDoc reader = (Reader JavaDoc)this.getTestXml("reader");
157         InputSource JavaDoc inputsrc = new InputSource JavaDoc(reader);
158         Document JavaDoc doc = XMLUtils.newDocument(inputsrc);
159
160         NodeList JavaDoc nl = doc.getElementsByTagName("display-name");
161         Element elem = (Element)nl.item(0);
162         String JavaDoc expected = "<display-name>Apache-Axis</display-name>";
163         String JavaDoc result = XMLUtils.ElementToString(elem);
164         assertEquals("Element tag name is not 'display-name', it is: " + elem.getTagName(),
165                      "display-name", elem.getTagName());
166         assertEquals("Did not get the expected result", expected, result);
167     }
168
169     public void testGetInnerXMLString() throws Exception JavaDoc
170     {
171         Reader JavaDoc reader = (Reader JavaDoc)this.getTestXml("reader");
172         InputSource JavaDoc inputsrc = new InputSource JavaDoc(reader);
173         Document JavaDoc doc = XMLUtils.newDocument(inputsrc);
174
175         NodeList JavaDoc nl = doc.getElementsByTagName("display-name");
176         Element elem = (Element)nl.item(0);
177         String JavaDoc expected = "Apache-Axis";
178         String JavaDoc result = XMLUtils.getInnerXMLString(elem);
179         assertEquals(expected, result);
180     }
181
182     public void testGetPrefix() throws Exception JavaDoc
183     {
184         Document JavaDoc doc = XMLUtils.newDocument();
185
186         Element elem = doc.createElementNS("", "svg");
187         elem.setAttribute("xmlns:svg", "\"http://www.w3.org/2000/svg\"");
188         elem.setAttribute("xmlns:xlink", "\"http://www.w3.org/1999/xlink\"");
189         elem.setAttribute("xmlns:xhtml", "\"http://www.w3.org/1999/xhtml\"");
190
191         String JavaDoc expected = "svg";
192         String JavaDoc result = XMLUtils.getPrefix("\"http://www.w3.org/2000/svg\"", elem);
193         assertEquals("Did not get the expected result", expected, result);
194         expected = "xlink";
195         result = XMLUtils.getPrefix("\"http://www.w3.org/1999/xlink\"", elem);
196         assertEquals("Did not get the expected result", expected, result);
197         expected = "xhtml";
198         result = XMLUtils.getPrefix("\"http://www.w3.org/1999/xhtml\"", elem);
199         assertEquals("Did not get the expected result", expected, result);
200     }
201
202     public void testGetNamespace() throws Exception JavaDoc
203     {
204         String JavaDoc testDoc = "<svg xmlns:svg=\"http://www.w3.org/2000/svg\"/>";
205         InputSource JavaDoc inputsrc = new InputSource JavaDoc(new StringReader JavaDoc(testDoc));
206         Document JavaDoc doc = XMLUtils.newDocument(inputsrc);
207         assertNotNull("Got a null document", doc);
208
209         NodeList JavaDoc nl = doc.getElementsByTagName("svg");
210         Element elem = (Element)nl.item(0);
211
212         String JavaDoc expected = "http://www.w3.org/2000/svg";
213         String JavaDoc result = XMLUtils.getNamespace("svg", elem);
214         assertEquals("Did not get the expected result", expected, result);
215     }
216
217     /**
218     * This is a utility method for creating XML document input sources for this
219     * JUnit test class. The returned Object should be cast to the type you
220     * request via the gimme parameter.
221     *
222     * @param gimme A String specifying the underlying type you want the XML
223     * input source returned as; one of "string", "reader", or "inputstream."
224     */

225     public Object JavaDoc getTestXml(String JavaDoc gimme)
226     {
227         String JavaDoc lineSep = System.getProperty("line.separator");
228         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
229           sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + lineSep)
230         //The System ID will cause an unknown host exception unless you are
231
//connected to the Internet, so comment it out for testing.
232
//.append("<!DOCTYPE web-app PUBLIC \"-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN\"" + lineSep)
233
//.append("\"http://java.sun.com/j2ee/dtds/web-app_2.2.dtd\">" + lineSep)
234
.append("<web-app>" + lineSep)
235           .append("<display-name>Apache-Axis</display-name>" + lineSep)
236           .append("<servlet>" + lineSep)
237           .append("<servlet-name>AxisServlet</servlet-name>" + lineSep)
238           .append("<display-name>Apache-Axis Servlet</display-name>" + lineSep)
239           .append("<servlet-class>" + lineSep)
240           .append("org.apache.axis.transport.http.AxisServlet" + lineSep)
241           .append("</servlet-class>" + lineSep)
242           .append("</servlet>" + lineSep)
243           .append("<servlet-mapping>" + lineSep)
244           .append("<servlet-name>AxisServlet</servlet-name>" + lineSep)
245           .append("<url-pattern>servlet/AxisServlet</url-pattern>" + lineSep)
246           .append("<url-pattern>*.jws</url-pattern>" + lineSep)
247           .append("</servlet-mapping>" + lineSep)
248           .append("</web-app>");
249
250         String JavaDoc xmlString = sb.toString();
251
252         if (gimme.equals("string"))
253         {
254             return xmlString;
255         }
256         else if (gimme.equals("reader"))
257         {
258             StringReader JavaDoc strReader = new StringReader JavaDoc(xmlString);
259             return strReader;
260         }
261         else if (gimme.equals("inputstream"))
262         {
263             ByteArrayInputStream JavaDoc byteStream = new ByteArrayInputStream JavaDoc(xmlString.getBytes());
264             return byteStream;
265         }
266         else return null;
267     }
268
269     public void testDOM2Writer() throws Exception JavaDoc
270     {
271         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
272         sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
273         sb.append("<xsd:schema targetNamespace=\"http://tempuri.org\"");
274         sb.append(" xmlns=\"http://tempuri.org\"");
275         sb.append(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">");
276         sb.append(" <xsd:annotation>");
277         sb.append(" <xsd:documentation xml:lang=\"en\">");
278         sb.append(" Purchase order schema for Example.com.");
279         sb.append(" Copyright 2000 Example.com. All rights reserved.");
280         sb.append(" </xsd:documentation>");
281         sb.append(" </xsd:annotation>");
282         sb.append("</xsd:schema>");
283
284         StringReader JavaDoc strReader = new StringReader JavaDoc(sb.toString());
285         InputSource JavaDoc inputsrc = new InputSource JavaDoc(strReader);
286         Document JavaDoc doc = XMLUtils.newDocument(inputsrc);
287
288         String JavaDoc output = org.apache.axis.utils.DOM2Writer.nodeToString(doc,false);
289         assertTrue(output.indexOf("http://www.w3.org/XML/1998/namespace")==-1);
290     }
291     
292     public void testDOMXXE() throws Exception JavaDoc
293     {
294         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
295         sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
296         sb.append("<!DOCTYPE project [");
297         sb.append("<!ENTITY buildxml SYSTEM \"file:build.xml\">");
298         sb.append("]>");
299         sb.append("<xsd:schema targetNamespace=\"http://tempuri.org\"");
300         sb.append(" xmlns=\"http://tempuri.org\"");
301         sb.append(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">");
302         sb.append(" <xsd:annotation>");
303         sb.append(" <xsd:documentation xml:lang=\"en\">");
304         sb.append(" &buildxml;");
305         sb.append(" Purchase order schema for Example.com.");
306         sb.append(" Copyright 2000 Example.com. All rights reserved.");
307         sb.append(" </xsd:documentation>");
308         sb.append(" </xsd:annotation>");
309         sb.append("</xsd:schema>");
310
311         StringReader JavaDoc strReader = new StringReader JavaDoc(sb.toString());
312         InputSource JavaDoc inputsrc = new InputSource JavaDoc(strReader);
313         Document JavaDoc doc = XMLUtils.newDocument(inputsrc);
314         String JavaDoc output = org.apache.axis.utils.DOM2Writer.nodeToString(doc,false);
315     }
316
317     String JavaDoc msg = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
318         "<!DOCTYPE project [" +
319         "<!ENTITY buildxml SYSTEM \"file:build.xml\">" +
320         "]>" +
321         "<SOAP-ENV:Envelope " +
322         "xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
323         "xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" > " +
324         "<SOAP-ENV:Body>\n" +
325         "&buildxml;" +
326         "<echo:Echo xmlns:echo=\"EchoService\">\n" +
327         "<symbol>IBM</symbol>\n" +
328         "</echo:Echo>\n" +
329         "</SOAP-ENV:Body></SOAP-ENV:Envelope>\n";
330     
331     public void testSAXXXE1() throws Exception JavaDoc
332     {
333         StringReader JavaDoc strReader = new StringReader JavaDoc(msg);
334         InputSource JavaDoc inputsrc = new InputSource JavaDoc(strReader);
335         SAXParser JavaDoc parser = XMLUtils.getSAXParser();
336         parser.getParser().parse(inputsrc);
337     }
338
339     public void testSAXXXE2() throws Exception JavaDoc
340     {
341         StringReader JavaDoc strReader2 = new StringReader JavaDoc(msg);
342         InputSource JavaDoc inputsrc2 = new InputSource JavaDoc(strReader2);
343         SAXParser JavaDoc parser2 = XMLUtils.getSAXParser();
344         parser2.getXMLReader().parse(inputsrc2);
345     }
346         
347     // If we are using DeserializationContext, we do not allow
348
// a DOCTYPE to be specified to prevent denial of service attacks
349
// via the ENTITY processing intstruction.
350
String JavaDoc msg2 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
351         "<SOAP-ENV:Envelope " +
352         "xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
353         "xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" > " +
354         "<SOAP-ENV:Body>\n" +
355         "<echo:Echo xmlns:echo=\"EchoService\">\n" +
356         "<symbol xml:lang=\"en\">IBM</symbol>\n" +
357         "</echo:Echo>\n" +
358         "</SOAP-ENV:Body></SOAP-ENV:Envelope>\n";
359     
360     /**
361      * Confirm we can parse a SOAP Envelope, and make sure that the
362      * xml:lang attribute is handled OK while we're at it.
363      *
364      * @throws Exception
365      */

366     public void testSAXXXE3() throws Exception JavaDoc
367     {
368         StringReader JavaDoc strReader3 = new StringReader JavaDoc(msg2);
369         DeserializationContext dser = new DeserializationContext(
370             new InputSource JavaDoc(strReader3), null, org.apache.axis.Message.REQUEST);
371         dser.parse();
372         SOAPEnvelope JavaDoc env = dser.getEnvelope();
373         SOAPBodyElement JavaDoc body = (SOAPBodyElement JavaDoc)env.getBody().getChildElements().next();
374         assertNotNull(body);
375         MessageElement child = (MessageElement)body.getChildElements().next();
376         assertNotNull(child);
377         Iterator JavaDoc i = child.getAllAttributes();
378         assertNotNull(i);
379         PrefixedQName attr = (PrefixedQName)i.next();
380         assertNotNull(attr);
381         assertEquals("Prefix for attribute was not 'xml'", attr.getPrefix(), "xml");
382         assertEquals("Namespace for attribute was not correct", attr.getURI(),
383                      Constants.NS_URI_XML);
384     }
385
386     String JavaDoc msg3 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
387             "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
388             " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " +
389             " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
390             " <soapenv:Header>" +
391             " <TrackingHeader xmlns=\"http://testns.org/test\">1234</TrackingHeader>" +
392             " <ns1:Security soapenv:mustUnderstand=\"0\" xmlns:ns1=\"http://schemas.xmlsoap.org/ws/2002/07/secext\">" +
393             " <ns1:UsernameToken>" +
394             " <ns1:Username xsi:type=\"xsd:string\">foobar</ns1:Username>" +
395             " <ns1:Password xsi:type=\"xsd:string\">wibble</ns1:Password>" +
396             " </ns1:UsernameToken>" +
397             " </ns1:Security>" +
398             " </soapenv:Header>" +
399             " <soapenv:Body>" +
400             " <EchoString>asdadsf</EchoString>" +
401             " </soapenv:Body>" +
402             "</soapenv:Envelope>";
403     
404     /**
405      * Test for Bug 22980
406      * @throws Exception
407      */

408     public void testNSStack() throws Exception JavaDoc
409     {
410         StringReader JavaDoc strReader3 = new StringReader JavaDoc(msg3);
411         DeserializationContext dser = new DeserializationContext(
412             new InputSource JavaDoc(strReader3), null, org.apache.axis.Message.REQUEST);
413         dser.parse();
414         org.apache.axis.message.SOAPEnvelope env = dser.getEnvelope();
415         String JavaDoc xml = env.toString();
416         assertXMLEqual(xml,msg3);
417     }
418     
419     public static void main(String JavaDoc[] args) throws Exception JavaDoc
420     {
421         TestXMLUtils test = new TestXMLUtils("TestXMLUtils");
422         test.testSAXXXE3();
423         test.testNSStack();
424     }
425 }
426
Popular Tags