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.xpath;
9
10 import junit.textui.TestRunner;
11
12 import org.dom4j.AbstractTestCase;
13 import org.dom4j.Node;
14 import org.dom4j.XPath;
15
16 /***
17 * Test harness for numeric XPath expressions
18 *
19 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
20 * @version $Revision: 1.3 $
21 */
22 public class NumberTest extends AbstractTestCase {
23 protected static String[] paths = {"2+2", "2 + 2", "2 + number(1) + 2",
24 "number(1) * 2", "2 + count(//author) + 2", "2 + (2 * 5)",
25 "count(//author) + count(//author/attribute::*)",
26 "(12 + count(//author) + count(//author/attribute::*)) div 2",
27 "count(//author)", "count(//author/attribute::*)",
28 "2 + number(1) * 2", "count(descendant::author)",
29 "count(ancestor::author)", "count(descendant::*)",
30 "count(descendant::author)+1", "count(ancestor::*)",
31 "10 + count(ancestor-or-self::author) + 5",
32 "10 + count(descendant::author) * 5",
33 "10 + (count(descendant::author) * 5)"};
34
35 public static void main(String[] args) {
36 TestRunner.run(NumberTest.class);
37 }
38
39 // Test case(s)
40 // -------------------------------------------------------------------------
41 public void testXPaths() throws Exception {
42 Node element = document.selectSingleNode("//author");
43 int size = paths.length;
44
45 for (int i = 0; i < size; i++) {
46 testXPath(document, paths[i]);
47 testXPath(element, paths[i]);
48 }
49
50 log("Finished successfully");
51 }
52
53 // Implementation methods
54 // -------------------------------------------------------------------------
55 protected void testXPath(Node node, String xpathText) throws Exception {
56 try {
57 XPath xpath = node.createXPath(xpathText);
58
59 Number number = xpath.numberValueOf(node);
60
61 log("Searched path: " + xpathText + " found: " + number);
62 } catch (Throwable e) {
63 log("Caught exception: " + e);
64 e.printStackTrace();
65 assertTrue("Failed to process: " + xpathText
66 + " caught exception: " + e, false);
67 }
68 }
69 }
70
71 /*
72 * Redistribution and use of this software and associated documentation
73 * ("Software"), with or without modification, are permitted provided that the
74 * following conditions are met:
75 *
76 * 1. Redistributions of source code must retain copyright statements and
77 * notices. Redistributions must also contain a copy of this document.
78 *
79 * 2. Redistributions in binary form must reproduce the above copyright notice,
80 * this list of conditions and the following disclaimer in the documentation
81 * and/or other materials provided with the distribution.
82 *
83 * 3. The name "DOM4J" must not be used to endorse or promote products derived
84 * from this Software without prior written permission of MetaStuff, Ltd. For
85 * written permission, please contact dom4j-info@metastuff.com.
86 *
87 * 4. Products derived from this Software may not be called "DOM4J" nor may
88 * "DOM4J" appear in their names without prior written permission of MetaStuff,
89 * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
90 *
91 * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
92 *
93 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
94 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
95 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
96 * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
97 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
98 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
99 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
100 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
101 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
103 * POSSIBILITY OF SUCH DAMAGE.
104 *
105 * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
106 */