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