KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dom4j > io > SAXReaderTest


1 /*
2  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
3  *
4  * This software is open source.
5  * See the bottom of this file for the licence.
6  */

7
8 package org.dom4j.io;
9
10 import junit.textui.TestRunner;
11
12 import java.io.ByteArrayOutputStream JavaDoc;
13 import java.io.File JavaDoc;
14 import java.io.StringReader JavaDoc;
15 import java.io.StringWriter JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.dom4j.AbstractTestCase;
19 import org.dom4j.Document;
20 import org.dom4j.DocumentHelper;
21 import org.dom4j.Element;
22
23 /**
24  * A test harness to test the content API in DOM4J
25  *
26  * @author <a HREF="mailto:maartenc@sourceforge.net">Maarten Coene </a>
27  */

28 public class SAXReaderTest extends AbstractTestCase {
29     public static void main(String JavaDoc[] args) {
30         TestRunner.run(SAXReaderTest.class);
31     }
32
33     // Test case(s)
34
// -------------------------------------------------------------------------
35

36     /**
37      * Test bug reported by Christian Oetterli http://tinyurl.com/6po8v
38      *
39      * @throws Exception
40      * DOCUMENT ME!
41      */

42     public void testReadFile() throws Exception JavaDoc {
43         File JavaDoc file = getFile("/xml/#.xml");
44         new SAXReader().read(file);
45     }
46     
47     public void testEncoding() throws Exception JavaDoc {
48         String JavaDoc xml = "<?xml version='1.0' encoding='ISO-8859-1'?><root/>";
49         SAXReader reader = new SAXReader();
50         reader.setEncoding("ISO-8859-1");
51         Document doc = reader.read(new StringReader JavaDoc(xml));
52         
53         assertEquals("encoding incorrect", "ISO-8859-1", doc.getXMLEncoding());
54     }
55
56     public void testRussian() throws Exception JavaDoc {
57         Document doc = getDocument("/xml/russArticle.xml");
58
59         assertEquals("encoding not correct", "koi8-r", doc.getXMLEncoding());
60
61         Element el = doc.getRootElement();
62
63         StringWriter JavaDoc writer = new StringWriter JavaDoc();
64         XMLWriter xmlWriter = new XMLWriter(writer);
65         OutputFormat format = OutputFormat.createPrettyPrint();
66         format.setEncoding("koi8-r");
67         xmlWriter.write(doc);
68         log(writer.toString());
69     }
70
71     public void testRussian2() throws Exception JavaDoc {
72         Document doc = getDocument("/xml/russArticle.xml");
73         XMLWriter xmlWriter = new XMLWriter(new OutputFormat("", false,
74                 "koi8-r"));
75         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
76         xmlWriter.setOutputStream(out);
77         xmlWriter.write(doc);
78         xmlWriter.flush();
79         xmlWriter.close();
80         log(out.toString());
81     }
82
83     public void testBug833765() throws Exception JavaDoc {
84         SAXReader reader = new SAXReader();
85         reader.setIncludeExternalDTDDeclarations(true);
86         getDocument("/xml/dtd/external.xml", reader);
87     }
88
89     public void testBug527062() throws Exception JavaDoc {
90         Document doc = getDocument("/xml/test/test.xml");
91         List JavaDoc l = doc.selectNodes("//broked/junk");
92
93         for (int i = 0; i < l.size(); i++) {
94             System.out.println("Found node: "
95                     + ((Element) l.get(i)).getStringValue());
96         }
97
98         assertEquals("hi there", ((Element) l.get(0)).getStringValue());
99         assertEquals("hello world", ((Element) l.get(1)).getStringValue());
100     }
101
102     public void testEscapedComment() throws Exception JavaDoc {
103         String JavaDoc txt = "<eg>&lt;!-- &lt;head> &amp; &lt;body> --&gt;</eg>";
104         Document doc = DocumentHelper.parseText(txt);
105         Element eg = doc.getRootElement();
106         System.out.println(doc.asXML());
107         assertEquals("<!-- <head> & <body> -->", eg.getText());
108     }
109 }
110
111 /*
112  * Redistribution and use of this software and associated documentation
113  * ("Software"), with or without modification, are permitted provided that the
114  * following conditions are met:
115  *
116  * 1. Redistributions of source code must retain copyright statements and
117  * notices. Redistributions must also contain a copy of this document.
118  *
119  * 2. Redistributions in binary form must reproduce the above copyright notice,
120  * this list of conditions and the following disclaimer in the documentation
121  * and/or other materials provided with the distribution.
122  *
123  * 3. The name "DOM4J" must not be used to endorse or promote products derived
124  * from this Software without prior written permission of MetaStuff, Ltd. For
125  * written permission, please contact dom4j-info@metastuff.com.
126  *
127  * 4. Products derived from this Software may not be called "DOM4J" nor may
128  * "DOM4J" appear in their names without prior written permission of MetaStuff,
129  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
130  *
131  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
132  *
133  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
134  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
135  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
136  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
137  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
138  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
139  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
140  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
141  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
142  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
143  * POSSIBILITY OF SUCH DAMAGE.
144  *
145  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
146  */

147
Popular Tags