KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > dom4j > TestXPathBug


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: TestXPathBug.java,v 1.2 2003/11/02 18:31:28 per_nyfelt Exp $
8  */

9
10 package test.dom4j;
11
12 import junit.framework.Test;
13 import junit.framework.TestSuite;
14 import junit.textui.TestRunner;
15 import org.dom4j.*;
16 import org.dom4j.io.SAXReader;
17 import org.ozoneDB.xml.dom4j.O3DocumentHelper;
18
19 import java.util.List JavaDoc;
20
21 /** A test harness to test XPath expression evaluation in DOM4J
22   *
23   * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan</a>
24   * @version $Revision: 1.2 $
25   */

26 public class TestXPathBug extends AbstractTestCase {
27
28     public static void main( String JavaDoc[] args ) {
29         TestRunner.run( suite() );
30     }
31
32     public static Test suite() {
33         return new TestSuite( TestXPathBug.class );
34     }
35
36     public TestXPathBug(String JavaDoc name) {
37         super(name);
38     }
39
40     // Test case(s)
41
//-------------------------------------------------------------------------
42
public void testXPaths() throws Exception JavaDoc {
43         SAXReader reader = new SAXReader();
44         Document document = reader.read( "xml/rabo1ae.xml" );
45         Element root = (Element) document.selectSingleNode( "/m:Msg/m:Contents/m:Content" );
46
47         assertTrue( "root is not null", root != null );
48
49         Namespace ns = root.getNamespaceForPrefix( "ab" );
50
51         assertTrue( "Found namespace", ns != null );
52
53         System.out.println( "Found: " + ns.getURI() );
54 /*
55         Element element = (Element) root.selectSingleNode( "ab:RaboPayLoad" );
56
57         assertTrue( "element is not null", element != null );
58
59         String value = element.valueOf( "ab:RateType" );
60
61         assertEquals( "RateType is correct", "CRRNT", value );
62 */

63     }
64
65     /** A bug found by Rob Lebowitz
66      */

67     public void testRobLebowitz() throws Exception JavaDoc {
68         String JavaDoc text = "<ul>"
69             + " <ul>"
70             + " <li/>"
71             + " <ul>"
72             + " <li/>"
73             + " </ul>"
74             + " <li/>"
75             + " </ul>"
76             + "</ul>";
77
78         Document document = O3DocumentHelper.parseText( text );
79         List JavaDoc lists = document.selectNodes( "//ul | //ol" );
80
81         int count = 0;
82         for (int i = 0; i < lists.size(); i++) {
83             Element list = (Element)lists.get(i);
84             List JavaDoc nodes = list.selectNodes("ancestor::ul");
85             if ((nodes != null) && (nodes.size() > 0)) {
86                 continue;
87             }
88             nodes = list.selectNodes("ancestor::ol");
89             if ((nodes != null) && (nodes.size() > 0)) {
90                 continue;
91             }
92         }
93     }
94
95     /** A bug found by Stefan which results in
96      * IndexOutOfBoundsException for empty results
97      */

98     public void testStefan() throws Exception JavaDoc {
99         String JavaDoc text = "<foo>hello</foo>";
100         Document document = O3DocumentHelper.parseText( text );
101         XPath xpath = O3DocumentHelper.createXPath( "/x" );
102         Object JavaDoc value = xpath.evaluate( document );
103     }
104
105
106     /** Test found by Mike Skells
107      */

108     public void testMikeSkells() throws Exception JavaDoc {
109         Document top = nodeFactory.createDocument();
110         Element root = top.addElement("root");
111         root.addElement("child1").addElement("child11");
112         root.addElement("child2").addElement("child21");
113         System.out.println(top.asXML());
114         XPath test1 = top.createXPath("/root/child1/child11");
115         XPath test2 = top.createXPath("/root/child2/child21");
116         Node position1 = test1.selectSingleNode(root);
117         Node position2 = test2.selectSingleNode(root);
118
119         System.out.println("test1= "+test1);
120         System.out.println("test2= "+test2);
121         System.out.println("Position1 Xpath = "+position1.getUniquePath());
122         System.out.println("Position2 Xpath = "+position2.getUniquePath());
123
124         System.out.println("test2.matches(position1) : "+test2.matches(position1));
125
126         assertTrue( "test1.matches(position1)", test1.matches(position1) );
127         assertTrue( "test2.matches(position2)", test2.matches(position2) );
128
129         assertTrue( "test2.matches(position1) should be false", ! test2.matches(position1) );
130     }
131
132 }
133
134
135
136
137 /*
138  * Redistribution and use of this software and associated documentation
139  * ("Software"), with or without modification, are permitted provided
140  * that the following conditions are met:
141  *
142  * 1. Redistributions of source code must retain copyright
143  * statements and notices. Redistributions must also contain a
144  * copy of this document.
145  *
146  * 2. Redistributions in binary form must reproduce the
147  * above copyright notice, this list of conditions and the
148  * following disclaimer in the documentation and/or other
149  * materials provided with the distribution.
150  *
151  * 3. The name "DOM4J" must not be used to endorse or promote
152  * products derived from this Software without prior written
153  * permission of MetaStuff, Ltd. For written permission,
154  * please contact dom4j-info@metastuff.com.
155  *
156  * 4. Products derived from this Software may not be called "DOM4J"
157  * nor may "DOM4J" appear in their names without prior written
158  * permission of MetaStuff, Ltd. DOM4J is a registered
159  * trademark of MetaStuff, Ltd.
160  *
161  * 5. Due credit should be given to the DOM4J Project
162  * (http://dom4j.org/).
163  *
164  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
165  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
166  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
167  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
168  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
169  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
170  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
171  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
172  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
173  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
174  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
175  * OF THE POSSIBILITY OF SUCH DAMAGE.
176  *
177  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
178  *
179  * $Id: TestXPathBug.java,v 1.2 2003/11/02 18:31:28 per_nyfelt Exp $
180  */

181
Popular Tags