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 package org.jaxen.test;
47
48 import java.util.List;
49
50 import javax.xml.parsers.DocumentBuilderFactory;
51 import javax.xml.parsers.ParserConfigurationException;
52
53 import org.jaxen.JaxenException;
54 import org.jaxen.SimpleNamespaceContext;
55 import org.jaxen.UnresolvableException;
56 import org.jaxen.XPath;
57 import org.jaxen.dom.DOMXPath;
58 import org.w3c.dom.Attr;
59 import org.w3c.dom.Element;
60
61 import junit.framework.TestCase;
62
63 public class NamespaceTest extends TestCase {
64
65 private org.w3c.dom.Document doc;
66
67 public NamespaceTest(String name) {
68 super(name);
69 }
70
71 protected void setUp() throws ParserConfigurationException {
72
73 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
74 factory.setNamespaceAware(true);
75 doc = factory.newDocumentBuilder().newDocument();
76
77 }
78
79 public void testMultipleNamespaceAxis() throws JaxenException {
80
81 Element root = doc.createElement("root");
82 doc.appendChild(root);
83 Element child = doc.createElementNS("http://www.example.org", "child");
84 child.setAttributeNS("http://www.w3.org/2000/xmlns/" , "xmlns:pre", "value");
85 root.appendChild(child);
86
87 XPath xpath = new DOMXPath("namespace::node()");
88 List result = xpath.selectNodes(child);
89 assertEquals(3, result.size());
90
91 }
92
93 public void testNumberOfNamespaceNodes() throws JaxenException {
94
95 Element root = doc.createElement("root");
96 doc.appendChild(root);
97 Element child = doc.createElementNS("http://www.example.org", "foo:child");
98 root.appendChild(child);
99
100 XPath xpath = new DOMXPath("//namespace::node()");
101 List result = xpath.selectNodes(doc);
102 assertEquals(3, result.size());
103
104
105 }
106
107
108 public void testNamespaceAxis() throws JaxenException {
109
110 Element root = doc.createElement("root");
111 doc.appendChild(root);
112 Element child = doc.createElementNS("http://www.example.org", "foo:child");
113 root.appendChild(child);
114
115 XPath xpath = new DOMXPath("namespace::node()");
116 List result = xpath.selectNodes(child);
117 assertEquals(2, result.size());
118
119 }
120
121
122 public void testUnprefixedNamespaceAxis() throws JaxenException {
123
124 Element root = doc.createElement("root");
125 doc.appendChild(root);
126 Element child = doc.createElementNS("http://www.example.org", "child");
127 root.appendChild(child);
128
129 XPath xpath = new DOMXPath("namespace::node()");
130 List result = xpath.selectNodes(child);
131 assertEquals(2, result.size());
132
133 }
134
135
136 public void testNamespaceNodesReadFromAttributes() throws JaxenException {
137
138 Element root = doc.createElement("root");
139 doc.appendChild(root);
140 Attr a = doc.createAttributeNS("http://www.example.org/", "a");
141 a.setNodeValue("value");
142 root.setAttributeNode(a);
143
144 XPath xpath = new DOMXPath("namespace::node()");
145 List result = xpath.selectNodes(root);
146
147 assertEquals(2, result.size());
148
149 }
150
151
152 public void testUnboundNamespaceUsedInXPathExpression() throws JaxenException {
153
154 Element root = doc.createElementNS("http://www.example.org/", "root");
155 doc.appendChild(root);
156 XPath xpath = new DOMXPath("/pre:root");
157 try {
158 xpath.selectNodes(root);
159 fail("Used unresolvable prefix");
160 }
161 catch (UnresolvableException ex) {
162 assertNotNull(ex.getMessage());
163 }
164
165 }
166
167
168 public void testQueryDefaultNamespace() throws JaxenException {
169
170 Element root = doc.createElementNS("http://www.example.org/", "root");
171 doc.appendChild(root);
172 XPath xpath = new DOMXPath("/pre:root");
173 xpath.addNamespace("pre", "http://www.example.org/");
174 List result = xpath.selectNodes(root);
175 assertEquals(1, result.size());
176
177 }
178
179
180 public void testQueryDefaultNamespaceWithContext() throws JaxenException {
181
182 Element root = doc.createElementNS("http://www.example.org/", "root");
183 doc.appendChild(root);
184 XPath xpath = new DOMXPath("/pre:root");
185 SimpleNamespaceContext context = new SimpleNamespaceContext();
186 context.addNamespace("pre", "http://www.example.org/");
187 xpath.setNamespaceContext(context);
188 List result = xpath.selectNodes(root);
189 assertEquals(1, result.size());
190
191 }
192
193
194 }