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