1   /*
2    * Copyright 2001-2004 (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.11 2004/06/25 08:03:47 maartenc Exp $
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      // Test case(s)
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      // Implementation methods
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  * Redistribution and use of this software and associated documentation
219  * ("Software"), with or without modification, are permitted provided
220  * that the following conditions are met:
221  *
222  * 1. Redistributions of source code must retain copyright
223  *    statements and notices.  Redistributions must also contain a
224  *    copy of this document.
225  *
226  * 2. Redistributions in binary form must reproduce the
227  *    above copyright notice, this list of conditions and the
228  *    following disclaimer in the documentation and/or other
229  *    materials provided with the distribution.
230  *
231  * 3. The name "DOM4J" must not be used to endorse or promote
232  *    products derived from this Software without prior written
233  *    permission of MetaStuff, Ltd.  For written permission,
234  *    please contact dom4j-info@metastuff.com.
235  *
236  * 4. Products derived from this Software may not be called "DOM4J"
237  *    nor may "DOM4J" appear in their names without prior written
238  *    permission of MetaStuff, Ltd. DOM4J is a registered
239  *    trademark of MetaStuff, Ltd.
240  *
241  * 5. Due credit should be given to the DOM4J Project - 
242  *    http://www.dom4j.org
243  *
244  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
245  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
246  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
247  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
248  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
249  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
250  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
251  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
252  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
253  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
254  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
255  * OF THE POSSIBILITY OF SUCH DAMAGE.
256  *
257  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
258  *
259  * $Id: TestXPathExamples.java,v 1.11 2004/06/25 08:03:47 maartenc Exp $
260  */