1   /*
2    * Copyright 2001-2005 (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   
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.5 $
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      // Test case(s)
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      // Implementation methods
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 exception = test.attributeValue("exception");
123         if ((exception != null) && exception.equals("true")) {
124             try {
125                 testContext.selectNodes(xpath);
126                 fail("Exception was not thrown");
127             } catch (XPathException e) {
128                 return;
129             }
130         }
131         
132         String count = test.attributeValue("count");
133 
134         if (count != null) {
135             int expectedSize = Integer.parseInt(count);
136             List results = testContext.selectNodes(xpath);
137 
138             log(description + " found result size: " + results.size());
139 
140             assertEquals(description + " wrong result size", expectedSize,
141                     results.size());
142         }
143 
144         Element valueOf = test.element("valueOf");
145 
146         if (valueOf != null) {
147             Node node = testContext.selectSingleNode(xpath);
148             assertTrue(description + " found node", node != null);
149 
150             String expected = valueOf.getText();
151             String result = node.valueOf(valueOf.attributeValue("select"));
152 
153             log(description);
154             log("\texpected: " + expected + " result: " + result);
155 
156             assertEquals(description, expected, result);
157         }
158     }
159 
160     protected void testValueOf(Element documentTest, Element context,
161             Element valueOf) throws Exception {
162         String xpath = valueOf.attributeValue("select");
163         String description = "valueOf: " + xpath;
164 
165         String expected = valueOf.getText();
166         String result = testContext.valueOf(xpath);
167 
168         log(description);
169         log("\texpected: " + expected + " result: " + result);
170 
171         assertEquals(description, expected, result);
172     }
173 
174     protected void testPattern(Element documentTest, Element context,
175             Element patternElement) throws Exception {
176         String match = patternElement.attributeValue("match");
177         String description = "match: " + match;
178 
179         log("");
180         log(description);
181 
182         Pattern pattern = factory.createPattern(match);
183 
184         assertTrue(description, pattern.matches(testContext));
185     }
186 
187     protected void testFilter(Element documentTest, Element context,
188             Element pattern) throws Exception {
189         String match = pattern.attributeValue("match");
190         String description = "match: " + match;
191 
192         assertTrue(description, testContext.matches(match));
193     }
194 }
195 
196 /*
197  * Redistribution and use of this software and associated documentation
198  * ("Software"), with or without modification, are permitted provided that the
199  * following conditions are met:
200  * 
201  * 1. Redistributions of source code must retain copyright statements and
202  * notices. Redistributions must also contain a copy of this document.
203  * 
204  * 2. Redistributions in binary form must reproduce the above copyright notice,
205  * this list of conditions and the following disclaimer in the documentation
206  * and/or other materials provided with the distribution.
207  * 
208  * 3. The name "DOM4J" must not be used to endorse or promote products derived
209  * from this Software without prior written permission of MetaStuff, Ltd. For
210  * written permission, please contact dom4j-info@metastuff.com.
211  * 
212  * 4. Products derived from this Software may not be called "DOM4J" nor may
213  * "DOM4J" appear in their names without prior written permission of MetaStuff,
214  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
215  * 
216  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
217  * 
218  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
219  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
220  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
221  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
222  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
223  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
224  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
225  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
226  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
227  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
228  * POSSIBILITY OF SUCH DAMAGE.
229  * 
230  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
231  */