KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > dom4j > TestXPathExamples


1 /*
2  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
3  *
4  * This software is open source.
5  * See the bottom of this file for the licence.
6  *
7  * $Id: TestXPathExamples.java,v 1.2 2003/11/02 18:31:28 per_nyfelt Exp $
8  */

9
10 package test.dom4j;
11
12 import junit.framework.Test;
13 import junit.framework.TestSuite;
14 import junit.textui.TestRunner;
15 import org.dom4j.Document;
16 import org.dom4j.Element;
17 import org.dom4j.Node;
18 import org.dom4j.XPathFactory;
19 import org.dom4j.io.SAXReader;
20 import org.dom4j.rule.Pattern;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25
26 /** Performs a number of unit test cases on the XPath engine
27   *
28   * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
29   * @version $Revision: 1.2 $
30   */

31 public class TestXPathExamples extends AbstractTestCase {
32
33     protected static boolean VERBOSE = true;
34
35     protected SAXReader xmlReader = new SAXReader();
36
37     /** The document on which the tests are being run */
38     protected Document testDocument;
39
40     /** The context node on which the tests are being run */
41     protected Node testContext;
42
43     /** factory for XPath, Patterns and nodes */
44     protected XPathFactory factory;
45
46
47     public static void main( String JavaDoc[] args ) {
48         TestRunner.run( suite() );
49     }
50
51     public static Test suite() {
52         return new TestSuite( TestXPathExamples.class );
53     }
54
55     public TestXPathExamples(String JavaDoc name) {
56         super(name);
57     }
58
59     public void log(String JavaDoc text) {
60         System.out.println(text);
61     }
62
63     // Test case(s)
64
//-------------------------------------------------------------------------
65
public void testXPaths() throws Exception JavaDoc {
66         Document document = xmlReader.read( "xml/test/xpath/tests.xml" );
67         Element root = document.getRootElement();
68         for ( Iterator JavaDoc iter = root.elementIterator( "document" ); iter.hasNext(); ) {
69             Element documentTest = (Element) iter.next();
70             testDocument( documentTest );
71         }
72     }
73
74     // Implementation methods
75
//-------------------------------------------------------------------------
76
protected void testDocument(Element documentTest) throws Exception JavaDoc {
77         String JavaDoc url = documentTest.attributeValue( "url" );
78         testDocument = xmlReader.read( url );
79         assertTrue( "Loaded test document: " + url, testDocument != null );
80
81         log( "Loaded document: " + url );
82
83         for ( Iterator JavaDoc iter = documentTest.elementIterator( "context" ); iter.hasNext(); ) {
84             Element context = (Element) iter.next();
85             testContext( documentTest, context );
86         }
87     }
88
89     protected void testContext(Element documentTest, Element context) throws Exception JavaDoc {
90         String JavaDoc xpath = context.attributeValue( "select" );
91
92         if ( VERBOSE ) {
93             log( "Selecting nodes for XPath: " + testDocument.createXPath( xpath ) );
94         }
95
96         List JavaDoc list = testDocument.selectNodes( xpath );
97
98         assertTrue( "Found at least one context nodes to test for path: " + xpath, list != null && list.size() > 0 );
99
100         for ( Iterator JavaDoc iter = list.iterator(); iter.hasNext(); ) {
101             Object JavaDoc object = iter.next();
102             assertTrue( "Context node is a Node: " + object, object instanceof Node );
103             testContext = (Node) object;
104
105             log( "Context is now: " + testContext );
106             runTests( documentTest, context );
107             log( "" );
108         }
109     }
110
111     protected void runTests(Element documentTest, Element context) throws Exception JavaDoc {
112         for ( Iterator JavaDoc iter = context.elementIterator( "test" ); iter.hasNext(); ) {
113             Element test = (Element) iter.next();
114             runTest( documentTest, context, test );
115         }
116         for ( Iterator JavaDoc iter = context.elementIterator( "valueOf" ); iter.hasNext(); ) {
117             Element valueOf = (Element) iter.next();
118             testValueOf( documentTest, context, valueOf );
119         }
120         for ( Iterator JavaDoc iter = context.elementIterator( "pattern" ); iter.hasNext(); ) {
121             Element pattern = (Element) iter.next();
122             testPattern( documentTest, context, pattern );
123         }
124         for ( Iterator JavaDoc iter = context.elementIterator( "filter" ); iter.hasNext(); ) {
125             Element filter = (Element) iter.next();
126             testFilter( documentTest, context, filter );
127         }
128     }
129
130     protected void runTest(Element documentTest, Element context, Element test) throws Exception JavaDoc {
131         String JavaDoc xpath = test.attributeValue( "select" );
132
133         String JavaDoc description = "Path: " + xpath;
134
135         if ( VERBOSE ) {
136             log( "" );
137             log( "XPath for: " + xpath );
138             log( "is: " + testContext.createXPath( xpath ) );
139             log( "" );
140         }
141
142         String JavaDoc count = test.attributeValue( "count" );
143         if ( count != null ) {
144             int expectedSize = Integer.parseInt( count );
145             List JavaDoc results = testContext.selectNodes( xpath );
146
147             log( description + " found result size: " + results.size() );
148
149             assertEquals( description + " wrong result size", expectedSize, results.size() );
150         }
151
152         Element valueOf = test.element( "valueOf" );
153         if ( valueOf != null ) {
154             Node node = testContext.selectSingleNode( xpath );
155             assertTrue( description + " found node", node != null );
156
157             String JavaDoc expected = valueOf.getText();
158             String JavaDoc result = node.valueOf( valueOf.attributeValue( "select" ) );
159
160             log( description );
161             log( "\texpected: " + expected + " result: " + result );
162
163             assertEquals( description, expected, result );
164         }
165     }
166
167     protected void testValueOf(Element documentTest, Element context, Element valueOf) throws Exception JavaDoc {
168         String JavaDoc xpath = valueOf.attributeValue( "select" );
169         String JavaDoc description = "valueOf: " + xpath;
170
171         if ( VERBOSE ) {
172             log( "XPath: " + testContext.createXPath( xpath ) );
173         }
174
175         String JavaDoc expected = valueOf.getText();
176         String JavaDoc result = testContext.valueOf( xpath );
177
178         log( description );
179         log( "\texpected: " + expected + " result: " + result );
180
181         assertEquals( description, expected, result );
182     }
183
184     protected void testPattern(Element documentTest, Element context, Element patternElement) throws Exception JavaDoc {
185         String JavaDoc match = patternElement.attributeValue( "match" );
186         String JavaDoc description = "match: " + match;
187
188         log( "" );
189         log( description );
190
191         Pattern pattern = factory.createPattern( match );
192
193         if ( VERBOSE ) {
194             log( "Pattern: " + pattern );
195         }
196
197         assertTrue( description, pattern.matches( testContext ) );
198
199     }
200
201     protected void testFilter(Element documentTest, Element context, Element pattern) throws Exception JavaDoc {
202         String JavaDoc match = pattern.attributeValue( "match" );
203         String JavaDoc description = "match: " + match;
204
205         log( "" );
206         log( description );
207
208         if ( VERBOSE ) {
209             log( "Pattern: " + factory.createXPathFilter( match ) );
210         }
211
212         assertTrue( description, testContext.matches( match ) );
213     }
214 }
215
216
217
218
219 /*
220  * Redistribution and use of this software and associated documentation
221  * ("Software"), with or without modification, are permitted provided
222  * that the following conditions are met:
223  *
224  * 1. Redistributions of source code must retain copyright
225  * statements and notices. Redistributions must also contain a
226  * copy of this document.
227  *
228  * 2. Redistributions in binary form must reproduce the
229  * above copyright notice, this list of conditions and the
230  * following disclaimer in the documentation and/or other
231  * materials provided with the distribution.
232  *
233  * 3. The name "DOM4J" must not be used to endorse or promote
234  * products derived from this Software without prior written
235  * permission of MetaStuff, Ltd. For written permission,
236  * please contact dom4j-info@metastuff.com.
237  *
238  * 4. Products derived from this Software may not be called "DOM4J"
239  * nor may "DOM4J" appear in their names without prior written
240  * permission of MetaStuff, Ltd. DOM4J is a registered
241  * trademark of MetaStuff, Ltd.
242  *
243  * 5. Due credit should be given to the DOM4J Project
244  * (http://dom4j.org/).
245  *
246  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
247  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
248  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
249  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
250  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
251  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
252  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
253  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
254  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
255  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
256  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
257  * OF THE POSSIBILITY OF SUCH DAMAGE.
258  *
259  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
260  *
261  * $Id: TestXPathExamples.java,v 1.2 2003/11/02 18:31:28 per_nyfelt Exp $
262  */

263
Popular Tags