1
2
3
4
5
6
7
8
9
10 package org.dom4j.tree;
11
12 import java.util.List;
13 import junit.framework.*;
14 import junit.textui.TestRunner;
15 import org.dom4j.*;
16
17 /***
18 *
19 * @author Maarten Coene
20 */
21 public class TestDefaultElement extends AbstractTestCase {
22
23 public static void main( String[] args ) {
24 TestRunner.run( suite() );
25 }
26
27 public static Test suite() {
28 return new TestSuite( TestDefaultElement.class );
29 }
30
31 public TestDefaultElement(String name) {
32 super(name);
33 }
34
35
36
37 public void testBug894878() {
38 Element foo = DocumentFactory.getInstance().createElement("foo");
39 foo.addText("bla").addAttribute("foo", "bar");
40 assertEquals("<foo foo=\"bar\">bla</foo>", foo.asXML());
41
42 foo = DocumentFactory.getInstance().createElement("foo");
43 foo.addAttribute("foo", "bar").addText("bla");
44 assertEquals("<foo foo=\"bar\">bla</foo>", foo.asXML());
45 }
46
47 public void testGetNamespacesForURI() throws Exception {
48 String xml =
49 "<schema targetNamespace='http://SharedTest.org/xsd' " +
50 " xmlns='http://www.w3.org/2001/XMLSchema' " +
51 " xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
52 " <complexType name='AllStruct'>" +
53 " <all>" +
54 " <element name='arString' type='xsd:string'/>" +
55 " <element name='varInt' type='xsd:int'/>" +
56 " </all>" +
57 " </complexType>" +
58 "</schema>";
59 Document doc = DocumentHelper.parseText(xml);
60 Element schema = doc.getRootElement();
61 List namespaces = schema.getNamespacesForURI("http://www.w3.org/2001/XMLSchema");
62
63 assertNotNull(namespaces);
64 assertEquals(2, namespaces.size());
65 }
66
67 public void testDeclaredNamespaces() throws Exception {
68 String xml = "<a xmlns:ns1=\"uri1\">" +
69 " <ns1:b/>" +
70 " <ns2:c xmlns:ns2=\"uri2\"/>" +
71 "</a>";
72 Document doc = DocumentHelper.parseText(xml);
73
74 Element a = doc.getRootElement();
75 List ns = a.declaredNamespaces();
76 assertEquals(1, ns.size());
77 assertSame(a.getNamespaceForPrefix("ns1"), ns.get(0));
78
79 Element b = a.element("b");
80 ns = b.declaredNamespaces();
81 assertEquals(0, ns.size());
82
83 Element c = a.element("c");
84 ns = c.declaredNamespaces();
85 assertEquals(1, ns.size());
86 assertSame(c.getNamespaceForPrefix("ns2"), ns.get(0));
87 }
88
89 public void testAdditionalNamespaces() throws Exception {
90 String xml = "<a xmlns:ns1=\"uri1\">" +
91 " <ns1:b/>" +
92 " <ns2:c xmlns:ns2=\"uri2\"/>" +
93 "</a>";
94 Document doc = DocumentHelper.parseText(xml);
95
96 Element a = doc.getRootElement();
97 List ns = a.additionalNamespaces();
98 assertEquals(1, ns.size());
99 assertSame(a.getNamespaceForPrefix("ns1"), ns.get(0));
100
101 Element b = a.element("b");
102 ns = b.additionalNamespaces();
103 assertEquals(0, ns.size());
104
105 Element c = a.element("c");
106 ns = c.additionalNamespaces();
107 assertEquals(0, ns.size());
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
147
148
149
150
151
152
153
154
155
156
157