KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > dom4j > xpath > TestNumber


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: TestNumber.java,v 1.1 2003/07/07 10:30:30 per_nyfelt Exp $
8  */

9
10 package test.dom4j.xpath;
11
12 import junit.framework.Test;
13 import junit.framework.TestSuite;
14 import junit.textui.TestRunner;
15 import org.dom4j.Node;
16 import org.dom4j.XPath;
17 import test.dom4j.AbstractTestCase;
18
19 /** Test harness for numeric XPath expressions
20   *
21   * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan</a>
22   * @version $Revision: 1.1 $
23   */

24 public class TestNumber extends AbstractTestCase {
25
26     protected static boolean VERBOSE = false;
27
28     protected static String JavaDoc[] paths = {
29         "2+2",
30         "2 + 2",
31         "2 + number(1) + 2",
32         "number(1) * 2",
33         "2 + count(//author) + 2",
34         "2 + (2 * 5)",
35         "count(//author) + count(//author/attribute::*)",
36         "(12 + count(//author) + count(//author/attribute::*)) div 2",
37         "count(//author)",
38         "count(//author/attribute::*)",
39         "2 + number(1) * 2",
40         "count(descendant::author)",
41         "count(ancestor::author)",
42         "count(descendant::*)",
43         "count(descendant::author)+1",
44         "count(ancestor::*)",
45 // "10 + count(ancestor-or-self::author) + 5",
46
"10 + count(descendant::author) * 5",
47         "10 + (count(descendant::author) * 5)",
48     };
49
50
51     public static void main( String JavaDoc[] args ) {
52         TestRunner.run( suite() );
53     }
54
55     public static Test suite() {
56         return new TestSuite( TestNumber.class );
57     }
58
59     public TestNumber(String JavaDoc name) {
60         super(name);
61     }
62
63     // Test case(s)
64
//-------------------------------------------------------------------------
65
public void testXPaths() throws Exception JavaDoc {
66         Node element = document.selectSingleNode( "//author" );
67         int size = paths.length;
68         for ( int i = 0; i < size; i++ ) {
69             testXPath( document, paths[i] );
70             testXPath( element, paths[i] );
71         }
72         log( "Finished successfully" );
73     }
74
75     // Implementation methods
76
//-------------------------------------------------------------------------
77
protected void testXPath(Node node, String JavaDoc xpathText) throws Exception JavaDoc {
78         try {
79             XPath xpath = node.createXPath( xpathText );
80
81             if ( VERBOSE ) {
82                 log( " xpath: " + xpath );
83                 log( " for: " + node );
84             }
85
86             Number JavaDoc number = xpath.numberValueOf( node );
87
88             log( "Searched path: " + xpathText + " found: " + number );
89
90         }
91         catch (Throwable JavaDoc e) {
92             log( "Caught exception: " + e );
93             e.printStackTrace();
94             assertTrue( "Failed to process: " + xpathText + " caught exception: " + e, false );
95         }
96     }
97
98 }
99
100
101
102
103 /*
104  * Redistribution and use of this software and associated documentation
105  * ("Software"), with or without modification, are permitted provided
106  * that the following conditions are met:
107  *
108  * 1. Redistributions of source code must retain copyright
109  * statements and notices. Redistributions must also contain a
110  * copy of this document.
111  *
112  * 2. Redistributions in binary form must reproduce the
113  * above copyright notice, this list of conditions and the
114  * following disclaimer in the documentation and/or other
115  * materials provided with the distribution.
116  *
117  * 3. The name "DOM4J" must not be used to endorse or promote
118  * products derived from this Software without prior written
119  * permission of MetaStuff, Ltd. For written permission,
120  * please contact dom4j-info@metastuff.com.
121  *
122  * 4. Products derived from this Software may not be called "DOM4J"
123  * nor may "DOM4J" appear in their names without prior written
124  * permission of MetaStuff, Ltd. DOM4J is a registered
125  * trademark of MetaStuff, Ltd.
126  *
127  * 5. Due credit should be given to the DOM4J Project
128  * (http://dom4j.org/).
129  *
130  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
131  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
132  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
133  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
134  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
135  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
136  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
137  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
138  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
139  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
140  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
141  * OF THE POSSIBILITY OF SUCH DAMAGE.
142  *
143  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
144  *
145  * $Id: TestNumber.java,v 1.1 2003/07/07 10:30:30 per_nyfelt Exp $
146  */

147
Popular Tags