1
2
3
4
5
6
7
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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178