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