1   /*
2    * Copyright 2001-2005 (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   
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      // Test case(s)
37      // -------------------------------------------------------------------------
38      public void testGetPath() throws Exception {
39          log("Testing paths");
40  
41          // testBranchPath( document );
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  * Redistribution and use of this software and associated documentation
171  * ("Software"), with or without modification, are permitted provided that the
172  * following conditions are met:
173  * 
174  * 1. Redistributions of source code must retain copyright statements and
175  * notices. Redistributions must also contain a copy of this document.
176  * 
177  * 2. Redistributions in binary form must reproduce the above copyright notice,
178  * this list of conditions and the following disclaimer in the documentation
179  * and/or other materials provided with the distribution.
180  * 
181  * 3. The name "DOM4J" must not be used to endorse or promote products derived
182  * from this Software without prior written permission of MetaStuff, Ltd. For
183  * written permission, please contact dom4j-info@metastuff.com.
184  * 
185  * 4. Products derived from this Software may not be called "DOM4J" nor may
186  * "DOM4J" appear in their names without prior written permission of MetaStuff,
187  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
188  * 
189  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
190  * 
191  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
192  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
193  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
194  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
195  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
196  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
197  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
198  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
199  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
200  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
201  * POSSIBILITY OF SUCH DAMAGE.
202  * 
203  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
204  */