1
2
3
4
5
6
7
8 package org.dom4j.tree;
9
10 import junit.textui.TestRunner;
11 import org.dom4j.AbstractTestCase;
12 import org.dom4j.Document;
13 import org.dom4j.DocumentFactory;
14 import org.dom4j.DocumentHelper;
15 import org.dom4j.Element;
16 import org.dom4j.Node;
17
18 import java.util.List;
19
20 /***
21 * JUnit tests for <code>DefaultElement</code>.
22 *
23 * @author Maarten Coene
24 */
25 public class DefaultElementTest extends AbstractTestCase {
26 public static void main(String[] args) {
27 TestRunner.run(DefaultElementTest.class);
28 }
29
30
31
32 public void testParentAfterSetContent() throws Exception {
33 Document doc = DocumentHelper.parseText("<root>" + "<a>a</a>"
34 + "<b>b</b>" + "<x>x</x>" + "<d>d</d>" + "</root>");
35 Node x = doc.selectSingleNode("/root/x");
36 List content = doc.getRootElement().content();
37 int position = content.indexOf(x);
38 Element c = DocumentHelper.createElement("c");
39 c.setText("c");
40 content.add(position, c);
41 assertNotNull(c.getParent());
42 doc.getRootElement().setContent(content);
43 assertNotNull("Parent is null of setting content", c.getParent());
44 }
45
46 public void testGetStringValue() throws Exception {
47 Document doc = getDocument("xml/test/test_text.xml");
48 Element message = doc.getRootElement();
49
50 String text = message.getStringValue();
51 assertEquals("String value incorrect", "This should work", text.trim());
52
53 String xpathText = (String) doc
54 .selectObject("normalize-space(/message)");
55 assertEquals("xpath value incorrect", "This should work", xpathText);
56 }
57
58 public void testBug894878() {
59 Element foo = DocumentFactory.getInstance().createElement("foo");
60 foo.addText("bla").addAttribute("foo", "bar");
61 assertEquals("<foo foo=\"bar\">bla</foo>", foo.asXML());
62
63 foo = DocumentFactory.getInstance().createElement("foo");
64 foo.addAttribute("foo", "bar").addText("bla");
65 assertEquals("<foo foo=\"bar\">bla</foo>", foo.asXML());
66 }
67
68 public void testGetNamespacesForURI() throws Exception {
69 String xml = "<schema targetNamespace='http://SharedTest.org/xsd' "
70 + " xmlns='http://www.w3.org/2001/XMLSchema' "
71 + " xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"
72 + " <complexType name='AllStruct'>" + " <all>"
73 + " <element name='arString' type='xsd:string'/>"
74 + " <element name='varInt' type='xsd:int'/>"
75 + " </all>" + " </complexType>" + "</schema>";
76 Document doc = DocumentHelper.parseText(xml);
77 Element schema = doc.getRootElement();
78 List namespaces = schema
79 .getNamespacesForURI("http://www.w3.org/2001/XMLSchema");
80
81 assertNotNull(namespaces);
82 assertEquals(2, namespaces.size());
83 }
84
85 public void testDeclaredNamespaces() throws Exception {
86 String xml = "<a xmlns:ns1=\"uri1\">" + " <ns1:b/>"
87 + " <ns2:c xmlns:ns2=\"uri2\"/>" + "</a>";
88 Document doc = DocumentHelper.parseText(xml);
89
90 Element a = doc.getRootElement();
91 List ns = a.declaredNamespaces();
92 assertEquals(1, ns.size());
93 assertSame(a.getNamespaceForPrefix("ns1"), ns.get(0));
94
95 Element b = a.element("b");
96 ns = b.declaredNamespaces();
97 assertEquals(0, ns.size());
98
99 Element c = a.element("c");
100 ns = c.declaredNamespaces();
101 assertEquals(1, ns.size());
102 assertSame(c.getNamespaceForPrefix("ns2"), ns.get(0));
103 }
104
105 public void testAdditionalNamespaces() throws Exception {
106 String xml = "<a xmlns:ns1=\"uri1\">" + " <ns1:b/>"
107 + " <ns2:c xmlns:ns2=\"uri2\"/>" + "</a>";
108 Document doc = DocumentHelper.parseText(xml);
109
110 Element a = doc.getRootElement();
111 List ns = a.additionalNamespaces();
112 assertEquals(1, ns.size());
113 assertSame(a.getNamespaceForPrefix("ns1"), ns.get(0));
114
115 Element b = a.element("b");
116 ns = b.additionalNamespaces();
117 assertEquals(0, ns.size());
118
119 Element c = a.element("c");
120 ns = c.additionalNamespaces();
121 assertEquals(0, ns.size());
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
155
156
157
158
159
160