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