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: TestXPathBug.java,v 1.8 2004/06/25 08:03:47 maartenc Exp $
8    */
9   
10  package org.dom4j;
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.io.SAXReader;
19  
20  /*** A test harness to test XPath expression evaluation in DOM4J
21    *
22    * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
23    * @version $Revision: 1.8 $
24    */
25  public class TestXPathBug extends AbstractTestCase {
26      
27      public static void main( String[] args ) {
28          TestRunner.run( suite() );
29      }
30      
31      public static Test suite() {
32          return new TestSuite( TestXPathBug.class );
33      }
34      
35      public TestXPathBug(String name) {
36          super(name);
37      }
38  
39      // Test case(s)
40      //-------------------------------------------------------------------------                    
41      public void testXPaths() throws Exception {        
42          SAXReader reader = new SAXReader();
43          Document document = reader.read(getClass().getResource("/xml/rabo1ae.xml"));
44          Element root = (Element) document.selectSingleNode( "/m:Msg/m:Contents/m:Content" );
45          
46          assertTrue( "root is not null", root != null );
47          
48          Namespace ns = root.getNamespaceForPrefix( "ab" );
49          
50          assertTrue( "Found namespace", ns != null );
51          
52          System.out.println( "Found: " + ns.getURI() );
53  
54          Element element = (Element) root.selectSingleNode( "ab:RaboPayLoad[@id='1234123']" );
55          
56          assertTrue( "element is not null", element != null );
57          
58          String value = element.valueOf( "ab:AccountingEntry/ab:RateType" );
59          
60          assertEquals( "RateType is correct", "CRRNT", value );
61      }
62      
63      /*** A bug found by Rob Lebowitz
64       */
65      public void testRobLebowitz() throws Exception {        
66          String text = "<ul>"
67              + "    <ul>"
68              + "        <li/>"
69              + "            <ul>"
70              + "                <li/>"
71              + "            </ul>"
72              + "        <li/>"
73              + "    </ul>"
74              + "</ul>";
75          
76          Document document = DocumentHelper.parseText( text );
77          List lists = document.selectNodes( "//ul | //ol" );
78          
79          int count = 0;
80          for (int i = 0; i < lists.size(); i++) {
81              Element list = (Element)lists.get(i);
82              List nodes = list.selectNodes("ancestor::ul");
83              if ((nodes != null) && (nodes.size() > 0)) {
84                  continue;
85              }
86              nodes = list.selectNodes("ancestor::ol");
87              if ((nodes != null) && (nodes.size() > 0)) {
88                  continue;
89              }
90          }
91      }
92      
93      /*** A bug found by Stefan which results in 
94       * IndexOutOfBoundsException for empty results
95       */
96      public void testStefan() throws Exception {
97          String text = "<foo>hello</foo>";
98          Document document = DocumentHelper.parseText( text );
99          XPath xpath = DocumentHelper.createXPath( "/x" );
100         Object value = xpath.evaluate( document );
101     }
102     
103 
104     /*** Test found by Mike Skells 
105      */
106     public void testMikeSkells() throws Exception {
107         Document top = DocumentFactory.getInstance().createDocument();
108         Element root = top.addElement("root");
109         root.addElement("child1").addElement("child11");
110         root.addElement("child2").addElement("child21");
111         System.out.println(top.asXML());
112         XPath test1 = top.createXPath("/root/child1/child11");
113         XPath test2 = top.createXPath("/root/child2/child21");
114         Node position1 = test1.selectSingleNode(root);
115         Node position2 = test2.selectSingleNode(root);
116         
117         System.out.println("test1= "+test1);
118         System.out.println("test2= "+test2);
119         System.out.println("Position1 Xpath = "+position1.getUniquePath());
120         System.out.println("Position2 Xpath = "+position2.getUniquePath());
121         
122         System.out.println("test2.matches(position1) : "+test2.matches(position1));
123         
124         assertTrue( "test1.matches(position1)", test1.matches(position1) );
125         assertTrue( "test2.matches(position2)", test2.matches(position2) );
126         
127         assertTrue( "test2.matches(position1) should be false", ! test2.matches(position1) );
128     }
129 
130 }
131 
132 
133 
134 
135 /*
136  * Redistribution and use of this software and associated documentation
137  * ("Software"), with or without modification, are permitted provided
138  * that the following conditions are met:
139  *
140  * 1. Redistributions of source code must retain copyright
141  *    statements and notices.  Redistributions must also contain a
142  *    copy of this document.
143  *
144  * 2. Redistributions in binary form must reproduce the
145  *    above copyright notice, this list of conditions and the
146  *    following disclaimer in the documentation and/or other
147  *    materials provided with the distribution.
148  *
149  * 3. The name "DOM4J" must not be used to endorse or promote
150  *    products derived from this Software without prior written
151  *    permission of MetaStuff, Ltd.  For written permission,
152  *    please contact dom4j-info@metastuff.com.
153  *
154  * 4. Products derived from this Software may not be called "DOM4J"
155  *    nor may "DOM4J" appear in their names without prior written
156  *    permission of MetaStuff, Ltd. DOM4J is a registered
157  *    trademark of MetaStuff, Ltd.
158  *
159  * 5. Due credit should be given to the DOM4J Project - 
160  *    http://www.dom4j.org
161  *
162  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
163  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
164  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
165  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
166  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
167  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
168  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
169  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
170  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
171  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
172  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
173  * OF THE POSSIBILITY OF SUCH DAMAGE.
174  *
175  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
176  *
177  * $Id: TestXPathBug.java,v 1.8 2004/06/25 08:03:47 maartenc Exp $
178  */