1
2
3
4
5
6
7
8 package org.dom4j.io;
9
10 import junit.textui.TestRunner;
11
12 import java.io.ByteArrayOutputStream;
13 import java.io.File;
14 import java.io.StringReader;
15 import java.io.StringWriter;
16 import java.util.List;
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[] args) {
30 TestRunner.run(SAXReaderTest.class);
31 }
32
33
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 {
43 File file = getFile("/xml/#.xml");
44 new SAXReader().read(file);
45 }
46
47 public void testEncoding() throws Exception {
48 String 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(xml));
52
53 assertEquals("encoding incorrect", "ISO-8859-1", doc.getXMLEncoding());
54 }
55
56 public void testRussian() throws Exception {
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 writer = new StringWriter();
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 {
72 Document doc = getDocument("/xml/russArticle.xml");
73 XMLWriter xmlWriter = new XMLWriter(new OutputFormat("", false,
74 "koi8-r"));
75 ByteArrayOutputStream out = new ByteArrayOutputStream();
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 {
84 SAXReader reader = new SAXReader();
85 reader.setIncludeExternalDTDDeclarations(true);
86 getDocument("/xml/dtd/external.xml", reader);
87 }
88
89 public void testBug527062() throws Exception {
90 Document doc = getDocument("/xml/test/test.xml");
91 List 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 {
103 String txt = "<eg><!-- <head> & <body> --></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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146