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
61
62
63 package org.jaxen.dom;
64
65 import junit.framework.TestCase;
66
67 import javax.xml.parsers.DocumentBuilder;
68 import javax.xml.parsers.DocumentBuilderFactory;
69 import javax.xml.parsers.ParserConfigurationException;
70
71 import java.io.IOException;
72 import java.util.Iterator;
73 import java.util.List;
74
75 import org.jaxen.JaxenException;
76 import org.jaxen.XPath;
77 import org.w3c.dom.Document;
78 import org.w3c.dom.Element;
79 import org.w3c.dom.Node;
80 import org.xml.sax.SAXException;
81
82 public class XPathTest extends TestCase
83 {
84
85 private static final String BASIC_XML = "xml/basic.xml";
86 private Document doc;
87 private DocumentBuilderFactory factory;
88
89 public XPathTest(String name)
90 {
91 super( name );
92 }
93
94 public void setUp() throws ParserConfigurationException {
95 factory = DocumentBuilderFactory.newInstance();
96 factory.setNamespaceAware(true);
97 doc = factory.newDocumentBuilder().newDocument();
98 }
99
100
101 public void testConstruction() throws JaxenException
102 {
103 DOMXPath xpath = new DOMXPath( "/foo/bar/baz" );
104 assertEquals("/foo/bar/baz", xpath.toString());
105 }
106
107 public void testConstructionWithNamespacePrefix() throws JaxenException
108 {
109 DOMXPath xpath = new DOMXPath( "/p:foo/p:bar/a:baz" );
110 assertEquals("/p:foo/p:bar/a:baz", xpath.toString());
111 }
112
113 public void testNamespaceDeclarationsAreNotAttributes()
114 throws JaxenException {
115
116 Element root = doc.createElementNS("http://www.example.org/", "root");
117 doc.appendChild(root);
118 root.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "http://www.example.org/");
119
120 DOMXPath xpath = new DOMXPath( "count(/*/@*)" );
121
122 Number value = xpath.numberValueOf(doc);
123 assertEquals(0, value.intValue());
124
125 }
126
127
128 public void testUpdateDOMNodesReturnedBySelectNodes()
129 throws JaxenException {
130
131 Element root = doc.createElementNS("http://www.example.org/", "root");
132 doc.appendChild(root);
133 root.appendChild(doc.createComment("data"));
134
135 DOMXPath xpath = new DOMXPath( "//comment()" );
136
137 List results = xpath.selectNodes(doc);
138 Node backroot = (Node) results.get(0);
139 backroot.setNodeValue("test");
140 assertEquals("test", backroot.getNodeValue());
141
142 }
143
144 public void testSelection() throws JaxenException, ParserConfigurationException, SAXException, IOException
145 {
146 XPath xpath = new DOMXPath( "/foo/bar/baz" );
147
148 DocumentBuilder builder = factory.newDocumentBuilder();
149
150 Document doc = builder.parse( BASIC_XML );
151
152 List results = xpath.selectNodes( doc );
153
154 assertEquals( 3,
155 results.size() );
156
157 Iterator iter = results.iterator();
158
159 assertEquals( "baz",
160 ((Element)iter.next()).getLocalName() );
161
162 assertEquals( "baz",
163 ((Element)iter.next()).getLocalName() );
164
165 assertEquals( "baz",
166 ((Element)iter.next()).getLocalName() );
167
168 assertTrue( ! iter.hasNext() );
169
170 }
171
172
173 public void testPrecedingAxisWithPositionalPredicate()
174 throws JaxenException, ParserConfigurationException, SAXException, IOException {
175
176 XPath xpath = new DOMXPath( "//c/preceding::*[1][name()='d']" );
177 DocumentBuilder builder = factory.newDocumentBuilder();
178
179 Document doc = builder.parse( "xml/web2.xml" );
180 List result = xpath.selectNodes(doc);
181 assertEquals(1, result.size());
182
183 }
184
185
186
187 public void testJaxen22()
188 throws JaxenException, ParserConfigurationException, SAXException, IOException {
189
190 XPath xpath = new DOMXPath( "name(//c/preceding::*[1])" );
191 DocumentBuilder builder = factory.newDocumentBuilder();
192
193 doc = builder.parse("xml/web2.xml");
194 Object result = xpath.evaluate(doc);
195 assertEquals("d", result);
196 }
197
198
199 public void testPrecedingAxisInDocumentOrder()
200 throws JaxenException {
201
202 XPath xpath = new DOMXPath( "preceding::*" );
203
204 Element root = doc.createElement("root");
205 doc.appendChild(root);
206
207 Element a = doc.createElement("a");
208 root.appendChild(a);
209 Element b = doc.createElement("b");
210 root.appendChild(b);
211 Element c = doc.createElement("c");
212 a.appendChild(c);
213
214 List result = xpath.selectNodes(b);
215 assertEquals(2, result.size());
216 assertEquals(a, result.get(0));
217 assertEquals(c, result.get(1));
218 }
219
220
221 }