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