1
2
3
4
5
6
7
8 package org.dom4j.tree;
9
10 import junit.textui.TestRunner;
11
12 import java.io.ByteArrayOutputStream;
13
14 import org.dom4j.AbstractTestCase;
15 import org.dom4j.Document;
16 import org.dom4j.DocumentFactory;
17 import org.dom4j.DocumentHelper;
18 import org.dom4j.Element;
19 import org.dom4j.IllegalAddException;
20 import org.dom4j.io.OutputFormat;
21 import org.dom4j.io.XMLWriter;
22
23 /***
24 * Some tests on DefaultDocument.
25 *
26 * @author <a href="mailto:maartenc@users.sourceforge.net">Maarten Coene </a>
27 */
28 public class DefaultDocumentTest extends AbstractTestCase {
29 public static void main(String[] args) {
30 TestRunner.run(DefaultDocumentTest.class);
31 }
32
33
34
35 public void testDoubleRootElement() {
36 Document document = DocumentFactory.getInstance().createDocument();
37 document.addElement("root");
38
39 Element root = DocumentFactory.getInstance().createElement(
40 "anotherRoot");
41
42 try {
43 document.add(root);
44 fail();
45 } catch (IllegalAddException e) {
46 String msg = e.getMessage();
47 assertTrue(msg.indexOf(root.toString()) != -1);
48 }
49 }
50
51 public void testBug799656() throws Exception {
52 Document document = DocumentFactory.getInstance().createDocument();
53 Element el = document.addElement("root");
54 el.setText("text with an \u00FC in it");
55
56 System.out.println(document.asXML());
57
58 DocumentHelper.parseText(document.asXML());
59 }
60
61 public void testAsXMLWithEncoding() throws Exception {
62 DefaultDocument document = new DefaultDocument();
63 document.addElement("root");
64 document.setXMLEncoding("ISO-8859-1");
65
66 Document doc = DocumentHelper.parseText("<?xml version='1.0' "
67 + "encoding='ISO-8859-1'?><root/>");
68
69 String xml1 = document.asXML();
70 String xml2 = doc.asXML();
71
72 assertTrue(xml1.indexOf("ISO-8859-1") != -1);
73 assertTrue(xml2.indexOf("ISO-8859-1") != -1);
74 }
75
76 public void testBug1156909() throws Exception {
77 Document doc = DocumentHelper.parseText("<?xml version='1.0' "
78 + "encoding='ISO-8859-1'?><root/>");
79
80 assertEquals("XMLEncoding not correct", "ISO-8859-1", doc
81 .getXMLEncoding());
82 }
83
84 public void testAsXMLWithEncodingAndContent() throws Exception {
85 DefaultDocument document = new DefaultDocument();
86 document.setXMLEncoding("UTF-16");
87 Element root = document.addElement("root");
88 root.setText("text with an \u00FC in it");
89
90 String xml = document.asXML();
91 assertTrue(xml.indexOf("UTF-16") != -1);
92 assertTrue(xml.indexOf('\u00FC') != -1);
93 }
94
95 public void testEncoding() throws Exception {
96 Document document = DocumentFactory.getInstance().createDocument(
97 "koi8-r");
98 Element el = document.addElement("root");
99 el.setText("text with an \u00FC in it");
100
101 System.out.println(document.asXML());
102
103 ByteArrayOutputStream out = new ByteArrayOutputStream();
104 OutputFormat of = OutputFormat.createPrettyPrint();
105 of.setEncoding("koi8-r");
106
107 XMLWriter writer = new XMLWriter(out, of);
108 writer.write(document);
109
110 String result = out.toString("koi8-r");
111 System.out.println(result);
112
113 Document doc2 = DocumentHelper.parseText(result);
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
147
148
149
150
151
152
153
154