1
2
3
4
5
6
7
8 package org.dom4j;
9
10 import junit.textui.TestRunner;
11
12 import java.util.List;
13
14 /***
15 * A test harness to test XPath expression evaluation in DOM4J
16 *
17 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
18 * @version $Revision: 1.4 $
19 */
20 public class XPathBugTest extends AbstractTestCase {
21 public static void main(String[] args) {
22 TestRunner.run(XPathBugTest.class);
23 }
24
25
26
27 public void testXPaths() throws Exception {
28 Document document = getDocument("/xml/rabo1ae.xml");
29 Element root = (Element) document
30 .selectSingleNode("/m:Msg/m:Contents/m:Content");
31
32 assertTrue("root is not null", root != null);
33
34 Namespace ns = root.getNamespaceForPrefix("ab");
35
36 assertTrue("Found namespace", ns != null);
37
38 System.out.println("Found: " + ns.getURI());
39
40 Element element = (Element) root
41 .selectSingleNode("ab:RaboPayLoad[@id='1234123']");
42
43 assertTrue("element is not null", element != null);
44
45 String value = element.valueOf("ab:AccountingEntry/ab:RateType");
46
47 assertEquals("RateType is correct", "CRRNT", value);
48 }
49
50 /***
51 * A bug found by Rob Lebowitz
52 *
53 * @throws Exception
54 * DOCUMENT ME!
55 */
56 public void testRobLebowitz() throws Exception {
57 String text = "<ul>" + " <ul>" + " <li/>"
58 + " <ul>" + " <li/>"
59 + " </ul>" + " <li/>" + " </ul>" + "</ul>";
60
61 Document document = DocumentHelper.parseText(text);
62 List lists = document.selectNodes("//ul | //ol");
63
64 int count = 0;
65
66 for (int i = 0; i < lists.size(); i++) {
67 Element list = (Element) lists.get(i);
68 List nodes = list.selectNodes("ancestor::ul");
69
70 if ((nodes != null) && (nodes.size() > 0)) {
71 continue;
72 }
73
74 nodes = list.selectNodes("ancestor::ol");
75
76 if ((nodes != null) && (nodes.size() > 0)) {
77 continue;
78 }
79 }
80 }
81
82 /***
83 * A bug found by Stefan which results in IndexOutOfBoundsException for
84 * empty results
85 *
86 * @throws Exception
87 * DOCUMENT ME!
88 */
89 public void testStefan() throws Exception {
90 String text = "<foo>hello</foo>";
91 Document document = DocumentHelper.parseText(text);
92 XPath xpath = DocumentHelper.createXPath("/x");
93 Object value = xpath.evaluate(document);
94 }
95
96 /***
97 * Test found by Mike Skells
98 *
99 * @throws Exception
100 * DOCUMENT ME!
101 */
102 public void testMikeSkells() throws Exception {
103 Document top = DocumentFactory.getInstance().createDocument();
104 Element root = top.addElement("root");
105 root.addElement("child1").addElement("child11");
106 root.addElement("child2").addElement("child21");
107 System.out.println(top.asXML());
108
109 XPath test1 = top.createXPath("/root/child1/child11");
110 XPath test2 = top.createXPath("/root/child2/child21");
111 Node position1 = test1.selectSingleNode(root);
112 Node position2 = test2.selectSingleNode(root);
113
114 System.out.println("test1= " + test1);
115 System.out.println("test2= " + test2);
116 System.out.println("Position1 Xpath = " + position1.getUniquePath());
117 System.out.println("Position2 Xpath = " + position2.getUniquePath());
118
119 System.out.println("test2.matches(position1) : "
120 + test2.matches(position1));
121
122 assertTrue("test1.matches(position1)", test1.matches(position1));
123 assertTrue("test2.matches(position2)", test2.matches(position2));
124
125 assertTrue("test2.matches(position1) should be false", !test2
126 .matches(position1));
127 }
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