1
2
3
4
5
6
7
8
9
10 package org.dom4j.tree;
11
12 import java.io.ByteArrayInputStream;
13 import java.io.ByteArrayOutputStream;
14 import java.io.StringReader;
15 import junit.framework.Test;
16 import junit.framework.TestSuite;
17 import junit.textui.TestRunner;
18 import org.dom4j.AbstractTestCase;
19 import org.dom4j.Document;
20 import org.dom4j.DocumentFactory;
21 import org.dom4j.DocumentHelper;
22 import org.dom4j.Element;
23 import org.dom4j.IllegalAddException;
24 import org.dom4j.io.OutputFormat;
25 import org.dom4j.io.SAXReader;
26 import org.dom4j.io.XMLWriter;
27 import org.xml.sax.InputSource;
28
29 /*** A test harness to test the addAttribute() methods on attributes
30 *
31 * @author <a href="mailto:maartenc@users.sourceforge.net">Maarten Coene</a>
32 */
33 public class TestDefaultDocument extends AbstractTestCase {
34
35 public static void main( String[] args ) {
36 TestRunner.run( suite() );
37 }
38
39 public static Test suite() {
40 return new TestSuite( TestDefaultDocument.class );
41 }
42
43 public TestDefaultDocument(String name) {
44 super(name);
45 }
46
47
48
49 public void testDoubleRootElement() {
50 Document document = DocumentFactory.getInstance().createDocument();
51 document.addElement("root");
52
53 Element root = DocumentFactory.getInstance().createElement("anotherRoot");
54 try {
55 document.add(root);
56 fail();
57 } catch (IllegalAddException e) {
58 String msg = e.getMessage();
59 assertTrue(msg.indexOf(root.toString()) != -1);
60 }
61 }
62
63 public void testBug799656() throws Exception {
64 Document document = DocumentFactory.getInstance().createDocument();
65 Element el = document.addElement("root");
66 el.setText("text with an \u00FC in it");
67
68 System.out.println(document.asXML());
69
70 DocumentHelper.parseText(document.asXML());
71 }
72
73 public void testEncoding() throws Exception {
74 Document document = DocumentFactory.getInstance().createDocument();
75 Element el = document.addElement("root");
76 el.setText("text with an \u00FC in it");
77
78 ByteArrayOutputStream out = new ByteArrayOutputStream();
79 OutputFormat of = OutputFormat.createPrettyPrint();
80 of.setEncoding("koi8-r");
81 XMLWriter writer = new XMLWriter(out, of);
82 writer.write(document);
83
84 String result = out.toString();
85
86 System.out.println(result);
87
88 DocumentHelper.parseText(result);
89 }
90
91 }
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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