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 testJaxen193() throws JaxenException
95 {
96 DOMXPath xpath = new DOMXPath( "/*[ * or processing-instruction() ]" );
97 assertEquals("/*[ * or processing-instruction() ]", xpath.toString());
98 }
99
100 public void testJaxen193InReverse() throws JaxenException
101 {
102 DOMXPath xpath = new DOMXPath( "/*[ processing-instruction() or *]" );
103 assertEquals("/*[ processing-instruction() or *]", xpath.toString());
104 }
105
106 public void testConstructionWithNamespacePrefix() throws JaxenException
107 {
108 DOMXPath xpath = new DOMXPath( "/p:foo/p:bar/a:baz" );
109 assertEquals("/p:foo/p:bar/a:baz", xpath.toString());
110 }
111
112 public void testNamespaceDeclarationsAreNotAttributes()
113 throws JaxenException {
114
115 Element root = doc.createElementNS("http://www.example.org/", "root");
116 doc.appendChild(root);
117 root.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "http://www.example.org/");
118
119 DOMXPath xpath = new DOMXPath( "count(/*/@*)" );
120
121 Number value = xpath.numberValueOf(doc);
122 assertEquals(0, value.intValue());
123
124 }
125
126
127
128 public void testConsistentNamespaceDeclarations()
129 throws JaxenException {
130
131 Element root = doc.createElement("root");
132 doc.appendChild(root);
133 Element child = doc.createElementNS("http://www.example.org", "foo:child");
134 root.appendChild(child);
135
136 child.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:foo2", "http://www.contradictory.org");
137
138 XPath xpath = new DOMXPath("//namespace::node()");
139 List result = xpath.selectNodes(doc);
140 assertEquals(4, result.size());
141
142 }
143
144
145 public void testInconsistentNamespaceDeclarations()
146 throws JaxenException {
147
148 Element root = doc.createElement("root");
149 doc.appendChild(root);
150 Element child = doc.createElementNS("http://www.example.org", "foo:child");
151 root.appendChild(child);
152
153 child.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:foo", "http://www.contradictory.org");
154
155 XPath xpath = new DOMXPath("//namespace::node()");
156 List result = xpath.selectNodes(doc);
157 assertEquals(3, result.size());
158
159 }
160
161
162 public void testIntrinsicNamespaceDeclarationOfElementBeatsContradictoryXmlnsPreAttr()
163 throws JaxenException {
164
165 Element root = doc.createElement("root");
166 doc.appendChild(root);
167 Element child = doc.createElementNS("http://www.example.org", "foo:child");
168 root.appendChild(child);
169
170 child.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:foo", "http://www.contradictory.org");
171
172 XPath xpath = new DOMXPath("//namespace::node()[name(.)='foo']");
173 List result = xpath.selectNodes(doc);
174 assertEquals(1, result.size());
175 Node node = (Node) result.get(0);
176 assertEquals("http://www.example.org", node.getNodeValue());
177
178 }
179
180
181 public void testIntrinsicNamespaceDeclarationOfAttrBeatsContradictoryXmlnsPreAttr()
182 throws JaxenException {
183
184 Element root = doc.createElement("root");
185 doc.appendChild(root);
186 root.setAttributeNS("http://www.example.org/", "foo:a", "value");
187
188 root.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:foo", "http://www.contradictory.org");
189
190 XPath xpath = new DOMXPath("//namespace::node()[name(.)='foo']");
191 List result = xpath.selectNodes(doc);
192 assertEquals(1, result.size());
193 Node node = (Node) result.get(0);
194 assertEquals("http://www.example.org/", node.getNodeValue());
195
196 }
197
198
199 public void testIntrinsicNamespaceDeclarationOfElementBeatsContradictoryIntrinsicNamespaceOfAttr()
200 throws JaxenException {
201
202 Element root = doc.createElementNS("http://www.example.org", "pre:root");
203 doc.appendChild(root);
204
205 root.setAttributeNS("http://www.contradictory.org", "pre:foo", "value");
206
207 XPath xpath = new DOMXPath("//namespace::node()[name(.)='pre']");
208 List result = xpath.selectNodes(doc);
209 assertEquals(1, result.size());
210 Node node = (Node) result.get(0);
211 assertEquals("http://www.example.org", node.getNodeValue());
212
213 }
214
215
216 public void testUpdateDOMNodesReturnedBySelectNodes()
217 throws JaxenException {
218
219 Element root = doc.createElementNS("http://www.example.org/", "root");
220 doc.appendChild(root);
221 root.appendChild(doc.createComment("data"));
222
223 DOMXPath xpath = new DOMXPath( "//comment()" );
224
225 List results = xpath.selectNodes(doc);
226 Node backroot = (Node) results.get(0);
227 backroot.setNodeValue("test");
228 assertEquals("test", backroot.getNodeValue());
229
230 }
231
232 public void testSelection()
233 throws JaxenException, ParserConfigurationException, SAXException, IOException {
234 XPath xpath = new DOMXPath( "/foo/bar/baz" );
235
236 DocumentBuilder builder = factory.newDocumentBuilder();
237
238 Document document = builder.parse( BASIC_XML );
239
240 List results = xpath.selectNodes( document );
241
242 assertEquals( 3,
243 results.size() );
244
245 Iterator iter = results.iterator();
246
247 assertEquals( "baz",
248 ((Element)iter.next()).getLocalName() );
249
250 assertEquals( "baz",
251 ((Element)iter.next()).getLocalName() );
252
253 assertEquals( "baz",
254 ((Element)iter.next()).getLocalName() );
255
256 assertTrue( ! iter.hasNext() );
257
258 }
259
260
261 public void testPrecedingAxisWithPositionalPredicate()
262 throws JaxenException, ParserConfigurationException, SAXException, IOException {
263
264 XPath xpath = new DOMXPath( "//c/preceding::*[1][name()='d']" );
265 DocumentBuilder builder = factory.newDocumentBuilder();
266
267 Document document = builder.parse( "xml/web2.xml" );
268 List result = xpath.selectNodes(document);
269 assertEquals(1, result.size());
270
271 }
272
273
274
275 public void testJaxen22()
276 throws JaxenException, ParserConfigurationException, SAXException, IOException {
277
278 XPath xpath = new DOMXPath( "name(//c/preceding::*[1])" );
279 DocumentBuilder builder = factory.newDocumentBuilder();
280
281 doc = builder.parse("xml/web2.xml");
282 Object result = xpath.evaluate(doc);
283 assertEquals("d", result);
284 }
285
286
287 public void testPrecedingAxisInDocumentOrder()
288 throws JaxenException {
289
290 XPath xpath = new DOMXPath( "preceding::*" );
291
292 Element root = doc.createElement("root");
293 doc.appendChild(root);
294
295 Element a = doc.createElement("a");
296 root.appendChild(a);
297 Element b = doc.createElement("b");
298 root.appendChild(b);
299 Element c = doc.createElement("c");
300 a.appendChild(c);
301
302 List result = xpath.selectNodes(b);
303 assertEquals(2, result.size());
304 assertEquals(a, result.get(0));
305 assertEquals(c, result.get(1));
306 }
307
308
309 }