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