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.dom4j;
64
65 import junit.framework.Test;
66 import junit.framework.TestCase;
67 import junit.framework.TestSuite;
68 import junit.textui.TestRunner;
69
70 import java.util.Iterator;
71 import java.util.List;
72
73 import org.dom4j.Attribute;
74 import org.dom4j.Document;
75 import org.dom4j.DocumentException;
76 import org.dom4j.Element;
77 import org.dom4j.Namespace;
78 import org.dom4j.io.SAXReader;
79 import org.dom4j.tree.DefaultAttribute;
80 import org.dom4j.tree.DefaultDocument;
81 import org.dom4j.tree.DefaultElement;
82 import org.jaxen.JaxenException;
83 import org.jaxen.XPath;
84 import org.jaxen.XPathSyntaxException;
85 import org.jaxen.saxpath.helpers.XPathReaderFactory;
86
87 public class XPathTest extends TestCase
88 {
89
90 private static final String BASIC_XML = "xml/basic.xml";
91
92 public static void main( String[] args )
93 {
94 TestRunner.run( suite() );
95 }
96
97 public static Test suite()
98 {
99 return new TestSuite( XPathTest.class );
100 }
101
102 public XPathTest(String name)
103 {
104 super( name );
105 }
106
107 public void setUp()
108 {
109 System.setProperty( XPathReaderFactory.DRIVER_PROPERTY,
110 "" );
111 }
112
113 public void testConstruction() throws JaxenException
114 {
115 new Dom4jXPath( "/foo/bar/baz" );
116 }
117
118 public void testSelection() throws JaxenException, DocumentException
119 {
120
121 XPath xpath = new Dom4jXPath( "/foo/bar/baz" );
122 SAXReader reader = new SAXReader();
123 Document doc = reader.read( BASIC_XML );
124 List results = xpath.selectNodes( doc );
125 assertEquals( 3, results.size() );
126 Iterator iter = results.iterator();
127 assertEquals( "baz",
128 ((Element)iter.next()).getName() );
129 assertEquals( "baz",
130 ((Element)iter.next()).getName() );
131 assertEquals( "baz",
132 ((Element)iter.next()).getName() );
133 assertTrue( ! iter.hasNext() );
134
135 }
136
137 public void testAsBoolean() throws JaxenException, DocumentException
138 {
139 XPath xpath = new Dom4jXPath( "/root/a = 'a'" );
140 SAXReader reader = new SAXReader();
141 Document doc = reader.read( "xml/simple.xml" );
142 boolean answer = xpath.booleanValueOf( doc );
143 assertTrue( "Xpath worked: " + xpath, answer );
144 xpath = new Dom4jXPath( "'a' = 'b'" );
145 answer = xpath.booleanValueOf( doc );
146 assertTrue( "XPath should return false: " + xpath, ! answer );
147 }
148
149 public void testJaxen20AttributeNamespaceNodes() throws JaxenException
150 {
151
152 Namespace ns1 = Namespace.get("p1", "www.acme1.org");
153 Namespace ns2 = Namespace.get("p2", "www.acme2.org");
154 Element element = new DefaultElement("test", ns1);
155 Attribute attribute = new DefaultAttribute("pre:foo", "bar", ns2);
156 element.add(attribute);
157 Document doc = new DefaultDocument(element);
158
159 XPath xpath = new Dom4jXPath( "//namespace::node()" );
160 List results = xpath.selectNodes( doc );
161 assertEquals( 3, results.size() );
162
163 }
164
165 public void testNamespaceNodesAreInherited() throws JaxenException
166 {
167 Namespace ns0 = Namespace.get("p0", "www.acme0.org");
168 Namespace ns1 = Namespace.get("p1", "www.acme1.org");
169 Namespace ns2 = Namespace.get("p2", "www.acme2.org");
170 Element element = new DefaultElement("test", ns1);
171 Attribute attribute = new DefaultAttribute("pre:foo", "bar", ns2);
172 element.add(attribute);
173 Element root = new DefaultElement("root", ns0);
174 root.add(element);
175 Document doc = new DefaultDocument(root);
176
177 XPath xpath = new Dom4jXPath( "/*/*/namespace::node()" );
178
179 List results = xpath.selectNodes( doc );
180
181 assertEquals( 4,
182 results.size() );
183 }
184
185 public void testSyntaxException() throws JaxenException {
186
187 String path = "/row/[some_node='val1']|[some_node='val2']";
188 try {
189 new Dom4jXPath(path);
190 fail("Allowed union of non-node-sets");
191 }
192 catch (XPathSyntaxException success) {
193 assertNotNull(success.getMessage());
194 }
195
196 }
197
198
199 }