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.jdom;
64
65 import junit.framework.TestCase;
66
67 import java.io.IOException;
68 import java.io.StringReader;
69 import java.util.Iterator;
70 import java.util.List;
71
72 import org.jaxen.JaxenException;
73 import org.jaxen.XPath;
74 import org.jdom.Attribute;
75 import org.jdom.Document;
76 import org.jdom.Element;
77 import org.jdom.JDOMException;
78 import org.jdom.Namespace;
79 import org.jdom.Text;
80 import org.jdom.input.SAXBuilder;
81
82 public class XPathTest extends TestCase
83 {
84
85 private static final String BASIC_XML = "xml/basic.xml";
86
87 public XPathTest(String name)
88 {
89 super( name );
90 }
91
92 public void testConstruction() throws JaxenException
93 {
94 new JDOMXPath( "/foo/bar/baz" );
95 }
96
97 public void testSelection() throws JaxenException, JDOMException, IOException
98 {
99 XPath xpath = new JDOMXPath( "/foo/bar/baz" );
100
101 SAXBuilder builder = new SAXBuilder();
102
103 Document doc = builder.build( BASIC_XML );
104
105 List results = xpath.selectNodes( doc );
106
107 assertEquals( 3,
108 results.size() );
109
110 Iterator iter = results.iterator();
111
112 assertEquals( "baz",
113 ((Element)iter.next()).getName() );
114
115 assertEquals( "baz",
116 ((Element)iter.next()).getName() );
117
118 assertEquals( "baz",
119 ((Element)iter.next()).getName() );
120
121 assertTrue( ! iter.hasNext() );
122 }
123
124
125 public void testGetDocumentNode() throws JaxenException, JDOMException, IOException
126 {
127 XPath xpath = new JDOMXPath( "/" );
128
129 SAXBuilder builder = new SAXBuilder();
130
131 Document doc = builder.build( BASIC_XML );
132
133 Element root = doc.getRootElement();
134 List results = xpath.selectNodes( root );
135
136 assertEquals( 1,
137 results.size() );
138
139 Iterator iter = results.iterator();
140
141 assertEquals( doc, iter.next());
142
143 }
144
145 public void testJaxen53Text() throws JaxenException, JDOMException, IOException
146 {
147 XPath xpath = new JDOMXPath( "//data/text() " );
148
149 SAXBuilder builder = new SAXBuilder();
150
151 Document doc = builder.build( new StringReader("<root>\n<data>1</data>\n</root>") );
152
153 List results = xpath.selectNodes( doc );
154
155 assertEquals( 1,
156 results.size() );
157
158 Iterator iter = results.iterator();
159
160 Text result = (Text) iter.next();
161 assertEquals( "1", result.getValue());
162
163 }
164
165 public void testJaxen20AttributeNamespaceNodes() throws JaxenException
166 {
167 Namespace ns1 = Namespace.getNamespace("p1", "www.acme1.org");
168 Namespace ns2 = Namespace.getNamespace("p2", "www.acme2.org");
169 Element element = new Element("test", ns1);
170 Attribute attribute = new Attribute("foo", "bar", ns2);
171 element.setAttribute(attribute);
172 Document doc = new Document(element);
173
174 XPath xpath = new JDOMXPath( "//namespace::node()" );
175
176 List results = xpath.selectNodes( doc );
177
178 assertEquals( 3,
179 results.size() );
180
181 }
182
183 public void testNamespaceNodesAreInherited() throws JaxenException
184 {
185 Namespace ns0 = Namespace.getNamespace("p0", "www.acme0.org");
186 Namespace ns1 = Namespace.getNamespace("p1", "www.acme1.org");
187 Namespace ns2 = Namespace.getNamespace("p2", "www.acme2.org");
188 Element element = new Element("test", ns1);
189 Attribute attribute = new Attribute("foo", "bar", ns2);
190 element.setAttribute(attribute);
191 Element root = new Element("root", ns0);
192 root.addContent(element);
193 Document doc = new Document(root);
194
195 XPath xpath = new JDOMXPath( "/*/*/namespace::node()" );
196
197 List results = xpath.selectNodes( doc );
198
199 assertEquals( 4, results.size() );
200
201 }
202
203 }