1
2
3
4
5
6
7
8 package org.dom4j;
9
10 import junit.textui.TestRunner;
11
12 import java.util.Iterator;
13 import java.util.List;
14
15 import org.dom4j.io.SAXReader;
16 import org.dom4j.rule.Pattern;
17
18 /***
19 * Performs a number of unit test cases on the XPath engine
20 *
21 * @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
22 * @version $Revision: 1.4 $
23 */
24 public class XPathExamplesTest extends AbstractTestCase {
25 protected SAXReader xmlReader = new SAXReader();
26
27 /*** The document on which the tests are being run */
28 protected Document testDocument;
29
30 /*** The context node on which the tests are being run */
31 protected Node testContext;
32
33 /*** factory for XPath, Patterns and nodes */
34 protected DocumentFactory factory = DocumentFactory.getInstance();
35
36 public static void main(String[] args) {
37 TestRunner.run(XPathExamplesTest.class);
38 }
39
40
41
42 public void testXPaths() throws Exception {
43 Document document = getDocument("/xml/test/xpath/tests.xml");
44 Element root = document.getRootElement();
45
46 Iterator iter = root.elementIterator("document");
47
48 while (iter.hasNext()) {
49 Element documentTest = (Element) iter.next();
50 testDocument(documentTest);
51 }
52 }
53
54
55
56 protected void testDocument(Element documentTest) throws Exception {
57 String url = documentTest.attributeValue("url");
58 testDocument = xmlReader.read(getFile(url));
59 assertTrue("Loaded test document: " + url, testDocument != null);
60
61 log("Loaded document: " + url);
62
63 for (Iterator iter = documentTest.elementIterator("context"); iter
64 .hasNext();) {
65 Element context = (Element) iter.next();
66 testContext(documentTest, context);
67 }
68 }
69
70 protected void testContext(Element documentTest, Element context)
71 throws Exception {
72 String xpath = context.attributeValue("select");
73
74 List list = testDocument.selectNodes(xpath);
75
76 assertTrue("Found at least one context nodes to test for path: "
77 + xpath, (list != null) && (list.size() > 0));
78
79 for (Iterator iter = list.iterator(); iter.hasNext();) {
80 Object object = iter.next();
81 assertTrue("Context node is a Node: " + object,
82 object instanceof Node);
83 testContext = (Node) object;
84
85 log("Context is now: " + testContext);
86 runTests(documentTest, context);
87 log("");
88 }
89 }
90
91 protected void runTests(Element documentTest, Element context)
92 throws Exception {
93 for (Iterator iter = context.elementIterator("test"); iter.hasNext();) {
94 Element test = (Element) iter.next();
95 runTest(documentTest, context, test);
96 }
97
98 for (Iterator it = context.elementIterator("valueOf"); it.hasNext();) {
99 Element valueOf = (Element) it.next();
100 testValueOf(documentTest, context, valueOf);
101 }
102
103 for (Iterator it = context.elementIterator("pattern"); it.hasNext();) {
104 Element pattern = (Element) it.next();
105 testPattern(documentTest, context, pattern);
106 }
107
108 Iterator it = context.elementIterator("fileter");
109
110 while (it.hasNext()) {
111 Element filter = (Element) it.next();
112 testFilter(documentTest, context, filter);
113 }
114 }
115
116 protected void runTest(Element documentTest, Element context, Element test)
117 throws Exception {
118 String xpath = test.attributeValue("select");
119
120 String description = "Path: " + xpath;
121
122 String count = test.attributeValue("count");
123
124 if (count != null) {
125 int expectedSize = Integer.parseInt(count);
126 List results = testContext.selectNodes(xpath);
127
128 log(description + " found result size: " + results.size());
129
130 assertEquals(description + " wrong result size", expectedSize,
131 results.size());
132 }
133
134 Element valueOf = test.element("valueOf");
135
136 if (valueOf != null) {
137 Node node = testContext.selectSingleNode(xpath);
138 assertTrue(description + " found node", node != null);
139
140 String expected = valueOf.getText();
141 String result = node.valueOf(valueOf.attributeValue("select"));
142
143 log(description);
144 log("\texpected: " + expected + " result: " + result);
145
146 assertEquals(description, expected, result);
147 }
148 }
149
150 protected void testValueOf(Element documentTest, Element context,
151 Element valueOf) throws Exception {
152 String xpath = valueOf.attributeValue("select");
153 String description = "valueOf: " + xpath;
154
155 String expected = valueOf.getText();
156 String result = testContext.valueOf(xpath);
157
158 log(description);
159 log("\texpected: " + expected + " result: " + result);
160
161 assertEquals(description, expected, result);
162 }
163
164 protected void testPattern(Element documentTest, Element context,
165 Element patternElement) throws Exception {
166 String match = patternElement.attributeValue("match");
167 String description = "match: " + match;
168
169 log("");
170 log(description);
171
172 Pattern pattern = factory.createPattern(match);
173
174 assertTrue(description, pattern.matches(testContext));
175 }
176
177 protected void testFilter(Element documentTest, Element context,
178 Element pattern) throws Exception {
179 String match = pattern.attributeValue("match");
180 String description = "match: " + match;
181
182 assertTrue(description, testContext.matches(match));
183 }
184 }
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221