1
2
3
4
5
6
7
8 package org.dom4j.xpath;
9
10 import junit.textui.TestRunner;
11
12 import java.util.Iterator;
13 import java.util.List;
14
15 import org.dom4j.AbstractTestCase;
16 import org.dom4j.Attribute;
17 import org.dom4j.Branch;
18 import org.dom4j.Document;
19 import org.dom4j.DocumentFactory;
20 import org.dom4j.DocumentHelper;
21 import org.dom4j.Element;
22 import org.dom4j.Node;
23 import org.dom4j.QName;
24
25 /***
26 * Test harness for the GetPath() method
27 *
28 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
29 * @version $Revision: 1.4 $
30 */
31 public class GetPathTest extends AbstractTestCase {
32 public static void main(String[] args) {
33 TestRunner.run(GetPathTest.class);
34 }
35
36
37
38 public void testGetPath() throws Exception {
39 log("Testing paths");
40
41
42 testPath(document, "/");
43
44 Element root = document.getRootElement();
45
46 testPath(root, "/root");
47
48 List elements = root.elements();
49
50 testPath((Node) elements.get(0), "/root/author", "/root/author[1]");
51
52 for (int i = 0, size = elements.size(); i < size; i++) {
53 String path = "/root/author";
54 String uniquePath = "/root/author";
55 String pathRel = "author";
56 String uniquePathRel = "author";
57
58 if (size > 1) {
59 uniquePath = "/root/author[" + (i + 1) + "]";
60 uniquePathRel = "author[" + (i + 1) + "]";
61 }
62
63 Element element = (Element) elements.get(i);
64 testPath(element, path, uniquePath);
65 testRelativePath(root, element, pathRel, uniquePathRel);
66
67 Attribute attribute = element.attribute("name");
68 testPath(attribute, path + "/@name", uniquePath + "/@name");
69 testRelativePath(root, attribute, pathRel + "/@name", uniquePathRel
70 + "/@name");
71
72 Element child = element.element("url");
73 testPath(child, path + "/url", uniquePath + "/url");
74 testRelativePath(root, child, pathRel + "/url", uniquePathRel
75 + "/url");
76 }
77 }
78
79 public void testDefaultNamespace() throws Exception {
80 Document doc = getDocument("/xml/test/defaultNamespace.xml");
81 Element root = doc.getRootElement();
82 testPath(root, "/*[name()='a']");
83
84 Element child = (Element) root.elements().get(0);
85 testPath(child, "/*[name()='a']/*[name()='b']");
86 testRelativePath(root, child, "*[name()='b']");
87 }
88
89 public void testBug770410() {
90 Document doc = DocumentHelper.createDocument();
91 Element a = doc.addElement("a");
92 Element b = a.addElement("b");
93 Element c = b.addElement("c");
94
95 b.detach();
96
97 String relativePath = b.getPath(b);
98 assertSame(b, b.selectSingleNode(relativePath));
99 }
100
101 public void testBug569927() {
102 Document doc = DocumentHelper.createDocument();
103 QName elName = DocumentFactory.getInstance().createQName("a", "ns",
104 "uri://myuri");
105 Element a = doc.addElement(elName);
106 QName attName = DocumentFactory.getInstance().createQName("attribute",
107 "ns", "uri://myuri");
108 a = a.addAttribute(attName, "test");
109
110 Attribute att = a.attribute(attName);
111
112 assertSame(att, doc.selectSingleNode(att.getPath()));
113 assertSame(att, doc.selectSingleNode(att.getUniquePath()));
114 }
115
116 protected void testPath(Node node, String value) {
117 testPath(node, value, value);
118 }
119
120 protected void testPath(Node node, String path, String uniquePath) {
121 assertEquals("getPath expression should be what is expected", path,
122 node.getPath());
123 assertEquals("getUniquePath expression should be what is expected",
124 uniquePath, node.getUniquePath());
125 }
126
127 protected void testRelativePath(Element context, Node node, String path) {
128 testRelativePath(context, node, path, path);
129 }
130
131 protected void testRelativePath(Element context, Node node, String pathRel,
132 String uniquePathRel) {
133 assertEquals("relative getPath expression should be what is expected",
134 pathRel, node.getPath(context));
135 assertEquals("relative getUniquePath expression not correct",
136 uniquePathRel, node.getUniquePath(context));
137 }
138
139 protected void testBranchPath(Branch branch) {
140 testNodePath(branch);
141
142 if (branch instanceof Element) {
143 Element element = (Element) branch;
144
145 for (Iterator iter = element.attributeIterator(); iter.hasNext();) {
146 Node node = (Node) iter.next();
147 testNodePath(node);
148 }
149 }
150
151 for (Iterator iter = branch.nodeIterator(); iter.hasNext();) {
152 Node node = (Node) iter.next();
153
154 if (node instanceof Branch) {
155 testBranchPath((Branch) node);
156 } else {
157 testNodePath(node);
158 }
159 }
160 }
161
162 protected void testNodePath(Node node) {
163 String path = node.getPath();
164
165 log("Path: " + path + " node: " + node);
166 }
167 }
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204