1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 package org.jaxen.dom;
61
62 import java.util.List;
63
64 import javax.xml.parsers.DocumentBuilderFactory;
65 import javax.xml.parsers.ParserConfigurationException;
66
67 import org.jaxen.JaxenException;
68 import org.jaxen.XPath;
69 import org.w3c.dom.Attr;
70 import org.w3c.dom.Element;
71
72 import junit.framework.TestCase;
73
74 public class NamespaceTest extends TestCase {
75
76 private org.w3c.dom.Document doc;
77
78 public NamespaceTest(String name) {
79 super(name);
80 }
81
82 protected void setUp() throws ParserConfigurationException {
83
84 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
85 factory.setNamespaceAware(true);
86 doc = factory.newDocumentBuilder().newDocument();
87
88 }
89
90 public void testMultipleNamespaceAxis() throws JaxenException {
91
92 org.w3c.dom.Element root = doc.createElement("root");
93 doc.appendChild(root);
94 Element child = doc.createElementNS("http://www.example.org", "child");
95 child.setAttributeNS("http://www.w3.org/2000/xmlns/" , "xmlns:pre", "value");
96 root.appendChild(child);
97
98 XPath xpath = new DOMXPath("namespace::node()");
99 List result = xpath.selectNodes(child);
100 assertEquals(3, result.size());
101
102 }
103
104 public void testNumberOfNamespaceNodes() throws JaxenException {
105
106 org.w3c.dom.Element root = doc.createElement("root");
107 doc.appendChild(root);
108 Element child = doc.createElementNS("http://www.example.org", "foo:child");
109 root.appendChild(child);
110
111 XPath xpath = new DOMXPath("//namespace::node()");
112 List result = xpath.selectNodes(doc);
113 assertEquals(3, result.size());
114
115
116 }
117
118
119 public void testNamespaceAxis() throws JaxenException {
120
121 org.w3c.dom.Element root = doc.createElement("root");
122 doc.appendChild(root);
123 Element child = doc.createElementNS("http://www.example.org", "foo:child");
124 root.appendChild(child);
125
126 XPath xpath = new DOMXPath("namespace::node()");
127 List result = xpath.selectNodes(child);
128 assertEquals(2, result.size());
129
130 }
131
132
133 public void testUnprefixedNamespaceAxis() throws JaxenException {
134
135 org.w3c.dom.Element root = doc.createElement("root");
136 doc.appendChild(root);
137 Element child = doc.createElementNS("http://www.example.org", "child");
138 root.appendChild(child);
139
140 XPath xpath = new DOMXPath("namespace::node()");
141 List result = xpath.selectNodes(child);
142 assertEquals(2, result.size());
143
144 }
145
146
147 public void testNamespaceNodesReadFromAttributes() throws JaxenException {
148
149 org.w3c.dom.Element root = doc.createElement("root");
150 doc.appendChild(root);
151 Attr a = doc.createAttributeNS("http://www.example.org/", "a");
152 a.setNodeValue("value");
153 root.setAttributeNode(a);
154
155 XPath xpath = new DOMXPath("namespace::node()");
156 List result = xpath.selectNodes(root);
157
158 assertEquals(2, result.size());
159
160 }
161
162
163 }