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.List;
13  
14  import org.dom4j.tree.DefaultElement;
15  import org.dom4j.xpath.DefaultXPath;
16  
17  /***
18   * A test harness to test XPath expression evaluation in DOM4J
19   * 
20   * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
21   * @version $Revision: 1.4 $
22   */
23  public class XPathTest extends AbstractTestCase {
24      protected static String[] paths = {
25              ".",
26              "*",
27              "/",
28              "/.",
29              "/*",
30              "/node()",
31              "/child::node()",
32              "/self::node()",
33              "root",
34              "/root",
35              "/root/author",
36              "text()",
37              "//author",
38              "//author/text()",
39              "//@location",
40              "//attribute::*",
41              "//namespace::*",
42              "normalize-space(/root)",
43              "//author[@location]",
44              "//author[@location='UK']",
45              "root|author",
46              "//*[.='James Strachan']",
47              "//root/author[1]",
48              "normalize-space(/root/author)",
49              "normalize-space(' a  b  c  d ')",
50              "//root|//author[1]|//author[2]",
51              "//root/author[2]",
52              "//root/author[3]"};
53  
54      public static void main(String[] args) {
55          TestRunner.run(XPathTest.class);
56      }
57  
58      // Test case(s)
59      // -------------------------------------------------------------------------
60      public void testBug1116471() throws Exception {
61          String xml = "<a><b>Water T &amp; D-46816</b></a>";
62          String expected = "Water T & D-46816";
63  
64          Document doc = DocumentHelper.parseText(xml);
65          String result = (String) doc.selectObject("string(a/b[1])");
66          
67          assertEquals("xpath result not correct", expected, result);
68          
69          Node node = doc.selectSingleNode("a/b");
70          String result2 = node.getStringValue();
71          
72          assertEquals("xpath result not correct", expected, result2);        
73      }
74      
75      public void testXPaths() throws Exception {
76          int size = paths.length;
77  
78          for (int i = 0; i < size; i++) {
79              testXPath(paths[i]);
80          }
81      }
82  
83      public void testCreateXPathBug() throws Exception {
84          Element element = new DefaultElement("foo");
85          XPath xpath = element.createXPath("//bar");
86  
87          assertTrue(("created a valid XPath: " + xpath) != null);
88      }
89  
90      public void testBug857704() throws Exception {
91          Document doc = DocumentHelper
92                  .parseText("<foo xmlns:bar='http://blort'/>");
93          doc.selectNodes("//*[preceding-sibling::*]"); // shouldn't throw NPE
94      }
95  
96      public void testBooleanValueOf() throws Exception {
97          Document doc = DocumentHelper.parseText("<root><foo>blah</foo></root>");
98  
99          XPath path = new DefaultXPath("//root");
100         assertTrue(path.booleanValueOf(doc));
101 
102         path = new DefaultXPath("//root2");
103         assertFalse(path.booleanValueOf(doc));
104     }
105 
106     // Implementation methods
107     // -------------------------------------------------------------------------
108     protected void testXPath(String xpathExpression) {
109         log("Searched path: " + xpathExpression);
110 
111         XPath xpath = DocumentHelper.createXPath(xpathExpression);
112 
113         List list = xpath.selectNodes(document);
114 
115         if (list == null) {
116             log("null");
117         } else {
118             log("[");
119 
120             for (int i = 0, size = list.size(); i < size; i++) {
121                 Object object = list.get(i);
122 
123                 String text = "null";
124 
125                 if (object instanceof Node) {
126                     Node node = (Node) object;
127 
128                     text = node.asXML();
129                 } else if (object != null) {
130                     text = object.toString();
131                 }
132 
133                 log("    " + text);
134             }
135 
136             log("]");
137         }
138 
139         log("...........................................");
140     }
141 }
142 
143 /*
144  * Redistribution and use of this software and associated documentation
145  * ("Software"), with or without modification, are permitted provided that the
146  * following conditions are met:
147  * 
148  * 1. Redistributions of source code must retain copyright statements and
149  * notices. Redistributions must also contain a copy of this document.
150  * 
151  * 2. Redistributions in binary form must reproduce the above copyright notice,
152  * this list of conditions and the following disclaimer in the documentation
153  * and/or other materials provided with the distribution.
154  * 
155  * 3. The name "DOM4J" must not be used to endorse or promote products derived
156  * from this Software without prior written permission of MetaStuff, Ltd. For
157  * written permission, please contact dom4j-info@metastuff.com.
158  * 
159  * 4. Products derived from this Software may not be called "DOM4J" nor may
160  * "DOM4J" appear in their names without prior written permission of MetaStuff,
161  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
162  * 
163  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
164  * 
165  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
166  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
167  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
168  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
169  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
170  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
171  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
172  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
173  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
174  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
175  * POSSIBILITY OF SUCH DAMAGE.
176  * 
177  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
178  */