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