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 package org.jaxen.test;
50
51 import junit.framework.TestCase;
52
53 import javax.xml.parsers.DocumentBuilder;
54 import javax.xml.parsers.DocumentBuilderFactory;
55 import javax.xml.parsers.ParserConfigurationException;
56
57 import java.io.IOException;
58 import java.util.Iterator;
59 import java.util.List;
60
61 import org.jaxen.JaxenException;
62 import org.jaxen.XPath;
63 import org.jaxen.dom.DOMXPath;
64 import org.w3c.dom.Document;
65 import org.w3c.dom.Element;
66 import org.w3c.dom.Node;
67 import org.xml.sax.SAXException;
68
69 public class DOMXPathTest extends TestCase
70 {
71
72 private static final String BASIC_XML = "xml/basic.xml";
73 private Document doc;
74 private DocumentBuilderFactory factory;
75
76 public DOMXPathTest(String name)
77 {
78 super( name );
79 }
80
81 public void setUp() throws ParserConfigurationException {
82 factory = DocumentBuilderFactory.newInstance();
83 factory.setNamespaceAware(true);
84 doc = factory.newDocumentBuilder().newDocument();
85 }
86
87
88 public void testConstruction() throws JaxenException
89 {
90 DOMXPath xpath = new DOMXPath( "/foo/bar/baz" );
91 assertEquals("/foo/bar/baz", xpath.toString());
92 }
93
94 public void testConstructionWithNamespacePrefix() throws JaxenException
95 {
96 DOMXPath xpath = new DOMXPath( "/p:foo/p:bar/a:baz" );
97 assertEquals("/p:foo/p:bar/a:baz", xpath.toString());
98 }
99
100 public void testNamespaceDeclarationsAreNotAttributes()
101 throws JaxenException {
102
103 Element root = doc.createElementNS("http://www.example.org/", "root");
104 doc.appendChild(root);
105 root.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "http://www.example.org/");
106
107 DOMXPath xpath = new DOMXPath( "count(/*/@*)" );
108
109 Number value = xpath.numberValueOf(doc);
110 assertEquals(0, value.intValue());
111
112 }
113
114
115
116 public void testConsistentNamespaceDeclarations()
117 throws JaxenException {
118
119 Element root = doc.createElement("root");
120 doc.appendChild(root);
121 Element child = doc.createElementNS("http://www.example.org", "foo:child");
122 root.appendChild(child);
123
124 child.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:foo2", "http://www.contradictory.org");
125
126 XPath xpath = new DOMXPath("//namespace::node()");
127 List result = xpath.selectNodes(doc);
128 assertEquals(4, result.size());
129
130 }
131
132
133 public void testInconsistentNamespaceDeclarations()
134 throws JaxenException {
135
136 Element root = doc.createElement("root");
137 doc.appendChild(root);
138 Element child = doc.createElementNS("http://www.example.org", "foo:child");
139 root.appendChild(child);
140
141 child.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:foo", "http://www.contradictory.org");
142
143 XPath xpath = new DOMXPath("//namespace::node()");
144 List result = xpath.selectNodes(doc);
145 assertEquals(3, result.size());
146
147 }
148
149
150 public void testIntrinsicNamespaceDeclarationOfElementBeatsContradictoryXmlnsPreAttr()
151 throws JaxenException {
152
153 Element root = doc.createElement("root");
154 doc.appendChild(root);
155 Element child = doc.createElementNS("http://www.example.org", "foo:child");
156 root.appendChild(child);
157
158 child.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:foo", "http://www.contradictory.org");
159
160 XPath xpath = new DOMXPath("//namespace::node()[name(.)='foo']");
161 List result = xpath.selectNodes(doc);
162 assertEquals(1, result.size());
163 Node node = (Node) result.get(0);
164 assertEquals("http://www.example.org", node.getNodeValue());
165
166 }
167
168
169 public void testIntrinsicNamespaceDeclarationOfAttrBeatsContradictoryXmlnsPreAttr()
170 throws JaxenException {
171
172 Element root = doc.createElement("root");
173 doc.appendChild(root);
174 root.setAttributeNS("http://www.example.org/", "foo:a", "value");
175
176 root.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:foo", "http://www.contradictory.org");
177
178 XPath xpath = new DOMXPath("//namespace::node()[name(.)='foo']");
179 List result = xpath.selectNodes(doc);
180 assertEquals(1, result.size());
181 Node node = (Node) result.get(0);
182 assertEquals("http://www.example.org/", node.getNodeValue());
183
184 }
185
186
187 public void testIntrinsicNamespaceDeclarationOfElementBeatsContradictoryIntrinsicNamespaceOfAttr()
188 throws JaxenException {
189
190 Element root = doc.createElementNS("http://www.example.org", "pre:root");
191 doc.appendChild(root);
192
193 root.setAttributeNS("http://www.contradictory.org", "pre:foo", "value");
194
195 XPath xpath = new DOMXPath("//namespace::node()[name(.)='pre']");
196 List result = xpath.selectNodes(doc);
197 assertEquals(1, result.size());
198 Node node = (Node) result.get(0);
199 assertEquals("http://www.example.org", node.getNodeValue());
200
201 }
202
203
204 public void testUpdateDOMNodesReturnedBySelectNodes()
205 throws JaxenException {
206
207 Element root = doc.createElementNS("http://www.example.org/", "root");
208 doc.appendChild(root);
209 root.appendChild(doc.createComment("data"));
210
211 DOMXPath xpath = new DOMXPath( "//comment()" );
212
213 List results = xpath.selectNodes(doc);
214 Node backroot = (Node) results.get(0);
215 backroot.setNodeValue("test");
216 assertEquals("test", backroot.getNodeValue());
217
218 }
219
220 public void testSelection()
221 throws JaxenException, ParserConfigurationException, SAXException, IOException {
222 XPath xpath = new DOMXPath( "/foo/bar/baz" );
223
224 DocumentBuilder builder = factory.newDocumentBuilder();
225
226 Document document = builder.parse( BASIC_XML );
227
228 List results = xpath.selectNodes( document );
229
230 assertEquals( 3,
231 results.size() );
232
233 Iterator iter = results.iterator();
234
235 assertEquals( "baz",
236 ((Element)iter.next()).getLocalName() );
237
238 assertEquals( "baz",
239 ((Element)iter.next()).getLocalName() );
240
241 assertEquals( "baz",
242 ((Element)iter.next()).getLocalName() );
243
244 assertTrue( ! iter.hasNext() );
245
246 }
247
248
249 public void testPrecedingAxisWithPositionalPredicate()
250 throws JaxenException, ParserConfigurationException, SAXException, IOException {
251
252 XPath xpath = new DOMXPath( "//c/preceding::*[1][name()='d']" );
253 DocumentBuilder builder = factory.newDocumentBuilder();
254
255 Document document = builder.parse( "xml/web2.xml" );
256 List result = xpath.selectNodes(document);
257 assertEquals(1, result.size());
258
259 }
260
261
262
263 public void testJaxen22()
264 throws JaxenException, ParserConfigurationException, SAXException, IOException {
265
266 XPath xpath = new DOMXPath( "name(//c/preceding::*[1])" );
267 DocumentBuilder builder = factory.newDocumentBuilder();
268
269 doc = builder.parse("xml/web2.xml");
270 Object result = xpath.evaluate(doc);
271 assertEquals("d", result);
272 }
273
274
275 public void testPrecedingAxisInDocumentOrder()
276 throws JaxenException {
277
278 XPath xpath = new DOMXPath( "preceding::*" );
279
280 Element root = doc.createElement("root");
281 doc.appendChild(root);
282
283 Element a = doc.createElement("a");
284 root.appendChild(a);
285 Element b = doc.createElement("b");
286 root.appendChild(b);
287 Element c = doc.createElement("c");
288 a.appendChild(c);
289
290 List result = xpath.selectNodes(b);
291 assertEquals(2, result.size());
292 assertEquals(a, result.get(0));
293 assertEquals(c, result.get(1));
294 }
295
296
297 }