1   /*
2    * $Header$
3    * $Revision$
4    * $Date$
5    *
6    * ====================================================================
7    *
8    * Copyright 2000-2003 bob mcwhirter & James Strachan.
9    * All rights reserved.
10   *
11   * Redistribution and use in source and binary forms, with or without
12   * modification, are permitted provided that the following conditions are
13   * met:
14   * 
15   *   * Redistributions of source code must retain the above copyright
16   *     notice, this list of conditions and the following disclaimer.
17   * 
18   *   * Redistributions in binary form must reproduce the above copyright
19   *     notice, this list of conditions and the following disclaimer in the
20   *     documentation and/or other materials provided with the distribution.
21   * 
22   *   * Neither the name of the Jaxen Project nor the names of its
23   *     contributors may be used to endorse or promote products derived 
24   *     from this software without specific prior written permission.
25   * 
26   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27   * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28   * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
29   * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
30   * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37   *
38   * ====================================================================
39   * This software consists of voluntary contributions made by many 
40   * individuals on behalf of the Jaxen Project and was originally 
41   * created by bob mcwhirter <bob@werken.com> and 
42   * James Strachan <jstrachan@apache.org>.  For more information on the 
43   * Jaxen Project, please see <http://www.jaxen.org/>.
44   * 
45   * $Id$
46   */
47  
48  
49  package org.jaxen.test;
50  
51  import junit.framework.TestCase;
52  
53  import javax.xml.parsers.DocumentBuilder;
54  import javax.xml.parsers.DocumentBuilderFactory;
55  import javax.xml.parsers.ParserConfigurationException;
56  
57  import java.io.IOException;
58  import java.util.Iterator;
59  import java.util.List;
60  
61  import org.jaxen.JaxenException;
62  import org.jaxen.XPath;
63  import org.jaxen.dom.DOMXPath;
64  import org.w3c.dom.Document;
65  import org.w3c.dom.Element;
66  import org.w3c.dom.Node;
67  import org.xml.sax.SAXException;
68  
69  public class DOMXPathTest extends TestCase
70  {
71  
72      private static final String BASIC_XML = "xml/basic.xml";
73      private Document doc;
74      private DocumentBuilderFactory factory;
75  
76      public DOMXPathTest(String name)
77      {
78          super( name );
79      }
80      
81      public void setUp() throws ParserConfigurationException {
82          factory = DocumentBuilderFactory.newInstance();
83          factory.setNamespaceAware(true);
84          doc = factory.newDocumentBuilder().newDocument();        
85      }
86      
87  
88      public void testConstruction() throws JaxenException
89      {
90          DOMXPath xpath = new DOMXPath( "/foo/bar/baz" );
91          assertEquals("/foo/bar/baz", xpath.toString());
92      }
93      
94      public void testJaxen193() throws JaxenException
95      {
96          DOMXPath xpath = new DOMXPath( "/*[ * or processing-instruction() ]" );
97          assertEquals("/*[ * or processing-instruction() ]", xpath.toString());
98      }
99      
100     public void testJaxen193InReverse() throws JaxenException
101     {
102         DOMXPath xpath = new DOMXPath( "/*[ processing-instruction() or *]" );
103         assertEquals("/*[ processing-instruction() or *]", xpath.toString());
104     }
105     
106     public void testConstructionWithNamespacePrefix() throws JaxenException
107     {
108         DOMXPath xpath = new DOMXPath( "/p:foo/p:bar/a:baz" );
109         assertEquals("/p:foo/p:bar/a:baz", xpath.toString());
110     }
111     
112     public void testNamespaceDeclarationsAreNotAttributes() 
113       throws JaxenException {
114         
115         Element root = doc.createElementNS("http://www.example.org/", "root");
116         doc.appendChild(root);
117         root.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "http://www.example.org/");
118         
119         DOMXPath xpath = new DOMXPath( "count(/*/@*)" );
120         
121         Number value = xpath.numberValueOf(doc);
122         assertEquals(0, value.intValue());
123         
124     }
125 
126     
127     // see JAXEN-105
128     public void testConsistentNamespaceDeclarations() 
129       throws JaxenException {
130         
131         Element root = doc.createElement("root");
132         doc.appendChild(root);
133         Element child = doc.createElementNS("http://www.example.org", "foo:child");
134         root.appendChild(child);
135         // different prefix
136         child.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:foo2", "http://www.contradictory.org");
137         
138         XPath xpath = new DOMXPath("//namespace::node()");
139         List result = xpath.selectNodes(doc);
140         assertEquals(4, result.size());
141         
142     }
143 
144     // see JAXEN-105
145     public void testInconsistentNamespaceDeclarations() 
146       throws JaxenException {
147         
148         Element root = doc.createElement("root");
149         doc.appendChild(root);
150         Element child = doc.createElementNS("http://www.example.org", "foo:child");
151         root.appendChild(child);
152         // same prefix
153         child.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:foo", "http://www.contradictory.org");
154         
155         XPath xpath = new DOMXPath("//namespace::node()");
156         List result = xpath.selectNodes(doc);
157         assertEquals(3, result.size());
158         
159     }
160 
161     // see JAXEN-105
162     public void testIntrinsicNamespaceDeclarationOfElementBeatsContradictoryXmlnsPreAttr() 
163       throws JaxenException {
164         
165         Element root = doc.createElement("root");
166         doc.appendChild(root);
167         Element child = doc.createElementNS("http://www.example.org", "foo:child");
168         root.appendChild(child);
169         // same prefix
170         child.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:foo", "http://www.contradictory.org");
171         
172         XPath xpath = new DOMXPath("//namespace::node()[name(.)='foo']");
173         List result = xpath.selectNodes(doc);
174         assertEquals(1, result.size());
175         Node node = (Node) result.get(0);
176         assertEquals("http://www.example.org", node.getNodeValue());
177         
178     }    
179     
180     // see JAXEN-105
181     public void testIntrinsicNamespaceDeclarationOfAttrBeatsContradictoryXmlnsPreAttr() 
182       throws JaxenException {
183         
184         Element root = doc.createElement("root");
185         doc.appendChild(root);
186         root.setAttributeNS("http://www.example.org/", "foo:a", "value");
187         // same prefix, different namespace
188         root.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:foo", "http://www.contradictory.org");
189         
190         XPath xpath = new DOMXPath("//namespace::node()[name(.)='foo']");
191         List result = xpath.selectNodes(doc);
192         assertEquals(1, result.size());
193         Node node = (Node) result.get(0);
194         assertEquals("http://www.example.org/", node.getNodeValue());
195         
196     }    
197     
198     // see JAXEN-105
199     public void testIntrinsicNamespaceDeclarationOfElementBeatsContradictoryIntrinsicNamespaceOfAttr() 
200       throws JaxenException {
201         
202         Element root = doc.createElementNS("http://www.example.org", "pre:root");
203         doc.appendChild(root);
204         // same prefix
205         root.setAttributeNS("http://www.contradictory.org", "pre:foo", "value");
206         
207         XPath xpath = new DOMXPath("//namespace::node()[name(.)='pre']");
208         List result = xpath.selectNodes(doc);
209         assertEquals(1, result.size());
210         Node node = (Node) result.get(0);
211         assertEquals("http://www.example.org", node.getNodeValue());
212         
213     }    
214     
215     // Jaxen-54
216     public void testUpdateDOMNodesReturnedBySelectNodes() 
217       throws JaxenException {
218         
219         Element root = doc.createElementNS("http://www.example.org/", "root");
220         doc.appendChild(root);
221         root.appendChild(doc.createComment("data"));
222         
223         DOMXPath xpath = new DOMXPath( "//comment()" );
224         
225         List results = xpath.selectNodes(doc);
226         Node backroot = (Node) results.get(0);
227         backroot.setNodeValue("test");
228         assertEquals("test", backroot.getNodeValue());
229         
230     }
231 
232     public void testSelection() 
233       throws JaxenException, ParserConfigurationException, SAXException, IOException {
234         XPath xpath = new DOMXPath( "/foo/bar/baz" );
235 
236         DocumentBuilder builder = factory.newDocumentBuilder();
237     
238         Document document = builder.parse( BASIC_XML );
239 
240         List results = xpath.selectNodes( document );
241 
242         assertEquals( 3,
243                       results.size() );
244 
245         Iterator iter = results.iterator();
246 
247         assertEquals( "baz",
248                       ((Element)iter.next()).getLocalName() );
249 
250         assertEquals( "baz",
251                       ((Element)iter.next()).getLocalName() );
252 
253         assertEquals( "baz",
254                       ((Element)iter.next()).getLocalName() );
255 
256         assertTrue( ! iter.hasNext() );
257 
258     }
259     
260     // Jaxen-22
261     public void testPrecedingAxisWithPositionalPredicate() 
262       throws JaxenException, ParserConfigurationException, SAXException, IOException {
263         
264         XPath xpath = new DOMXPath( "//c/preceding::*[1][name()='d']" );
265         DocumentBuilder builder = factory.newDocumentBuilder();
266     
267         Document document = builder.parse( "xml/web2.xml" );
268         List result = xpath.selectNodes(document);
269         assertEquals(1, result.size());
270         
271     }
272     
273      
274     // Jaxen-22
275     public void testJaxen22() 
276       throws JaxenException, ParserConfigurationException, SAXException, IOException {
277         
278         XPath xpath = new DOMXPath( "name(//c/preceding::*[1])" );
279         DocumentBuilder builder = factory.newDocumentBuilder();
280     
281         doc = builder.parse("xml/web2.xml");
282         Object result = xpath.evaluate(doc);
283         assertEquals("d", result);
284     }
285     
286      
287     public void testPrecedingAxisInDocumentOrder() 
288       throws JaxenException {
289         
290         XPath xpath = new DOMXPath( "preceding::*" );
291     
292         Element root = doc.createElement("root");
293         doc.appendChild(root);
294         
295         Element a = doc.createElement("a");
296         root.appendChild(a);
297         Element b = doc.createElement("b");
298         root.appendChild(b);
299         Element c = doc.createElement("c");
300         a.appendChild(c);
301         
302         List result = xpath.selectNodes(b);
303         assertEquals(2, result.size());
304         assertEquals(a, result.get(0));
305         assertEquals(c, result.get(1));
306     }
307     
308      
309 }