1   /* $Header$
2    * $Revision$
3    * $Date$
4    *
5    * ====================================================================
6    *
7    * Copyright 2005 Elliotte Rusty Harold.
8    * All rights reserved.
9    *
10   * Redistribution and use in source and binary forms, with or without
11   * modification, are permitted provided that the following conditions are
12   * met:
13   * 
14   *   * Redistributions of source code must retain the above copyright
15   *     notice, this list of conditions and the following disclaimer.
16   * 
17   *   * Redistributions in binary form must reproduce the above copyright
18   *     notice, this list of conditions and the following disclaimer in the
19   *     documentation and/or other materials provided with the distribution.
20   * 
21   *   * Neither the name of the Jaxen Project nor the names of its
22   *     contributors may be used to endorse or promote products derived 
23   *     from this software without specific prior written permission.
24   * 
25   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
26   * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27   * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
28   * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
29   * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36   *
37   * ====================================================================
38   * This software consists of voluntary contributions made by many 
39   * individuals on behalf of the Jaxen Project and was originally 
40   * created by bob mcwhirter <bob@werken.com> and 
41   * James Strachan <jstrachan@apache.org>.  For more information on the 
42   * Jaxen Project, please see <http://www.jaxen.org/>.
43   * 
44   * $Id$
45   */
46  package org.jaxen.test;
47  
48  import java.util.List;
49  
50  import javax.xml.parsers.DocumentBuilderFactory;
51  import javax.xml.parsers.ParserConfigurationException;
52  
53  import org.jaxen.JaxenException;
54  import org.jaxen.SimpleNamespaceContext;
55  import org.jaxen.UnresolvableException;
56  import org.jaxen.XPath;
57  import org.jaxen.dom.DOMXPath;
58  import org.w3c.dom.Attr;
59  import org.w3c.dom.Element;
60  
61  import junit.framework.TestCase;
62  
63  public class NamespaceTest extends TestCase {
64  
65      private org.w3c.dom.Document doc;
66  
67      public NamespaceTest(String name) {
68          super(name);
69      }
70      
71      protected void setUp() throws ParserConfigurationException {
72          
73          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
74          factory.setNamespaceAware(true);
75          doc = factory.newDocumentBuilder().newDocument();
76          
77      }     
78      
79      public void testMultipleNamespaceAxis() throws JaxenException {
80          
81          Element root = doc.createElement("root");
82          doc.appendChild(root);
83          Element child = doc.createElementNS("http://www.example.org", "child");
84          child.setAttributeNS("http://www.w3.org/2000/xmlns/" , "xmlns:pre", "value");
85          root.appendChild(child);
86          
87          XPath xpath = new DOMXPath("namespace::node()");
88          List result = xpath.selectNodes(child);
89          assertEquals(3, result.size());
90     
91      }
92      
93      public void testNumberOfNamespaceNodes() throws JaxenException {
94          
95          Element root = doc.createElement("root");
96          doc.appendChild(root);
97          Element child = doc.createElementNS("http://www.example.org", "foo:child");
98          root.appendChild(child);
99          
100         XPath xpath = new DOMXPath("//namespace::node()");
101         List result = xpath.selectNodes(doc);
102         assertEquals(3, result.size());
103         // 1 for xml prefix on root; 1 for foo prefix on child; 1 for xml prefix on child
104    
105     }
106     
107     
108     public void testNamespaceAxis() throws JaxenException {
109         
110         Element root = doc.createElement("root");
111         doc.appendChild(root);
112         Element child = doc.createElementNS("http://www.example.org", "foo:child");
113         root.appendChild(child);
114         
115         XPath xpath = new DOMXPath("namespace::node()");
116         List result = xpath.selectNodes(child);
117         assertEquals(2, result.size());
118    
119     }
120     
121     
122     public void testUnprefixedNamespaceAxis() throws JaxenException {
123         
124         Element root = doc.createElement("root");
125         doc.appendChild(root);
126         Element child = doc.createElementNS("http://www.example.org", "child");
127         root.appendChild(child);
128         
129         XPath xpath = new DOMXPath("namespace::node()");
130         List result = xpath.selectNodes(child);
131         assertEquals(2, result.size());
132    
133     }   
134     
135     
136     public void testNamespaceNodesReadFromAttributes() throws JaxenException {
137         
138         Element root = doc.createElement("root");
139         doc.appendChild(root);
140         Attr a = doc.createAttributeNS("http://www.example.org/", "a");
141         a.setNodeValue("value");
142         root.setAttributeNode(a);
143         
144         XPath xpath = new DOMXPath("namespace::node()");
145         List result = xpath.selectNodes(root);
146         // one for the xml prefix; one from the attribute node
147         assertEquals(2, result.size());
148    
149     }   
150     
151     
152     public void testUnboundNamespaceUsedInXPathExpression() throws JaxenException {
153         
154         Element root = doc.createElementNS("http://www.example.org/", "root");
155         doc.appendChild(root);
156         XPath xpath = new DOMXPath("/pre:root");
157         try {
158             xpath.selectNodes(root);
159             fail("Used unresolvable prefix");
160         }
161         catch (UnresolvableException ex) {
162             assertNotNull(ex.getMessage());
163         }
164    
165     }   
166     
167     
168     public void testQueryDefaultNamespace() throws JaxenException {
169         
170         Element root = doc.createElementNS("http://www.example.org/", "root");
171         doc.appendChild(root);
172         XPath xpath = new DOMXPath("/pre:root");
173         xpath.addNamespace("pre", "http://www.example.org/");
174         List result = xpath.selectNodes(root);
175         assertEquals(1, result.size());
176    
177     }   
178     
179     
180     public void testQueryDefaultNamespaceWithContext() throws JaxenException {
181         
182         Element root = doc.createElementNS("http://www.example.org/", "root");
183         doc.appendChild(root);
184         XPath xpath = new DOMXPath("/pre:root");
185         SimpleNamespaceContext context = new SimpleNamespaceContext();
186         context.addNamespace("pre", "http://www.example.org/");
187         xpath.setNamespaceContext(context);
188         List result = xpath.selectNodes(root);
189         assertEquals(1, result.size());
190    
191     }   
192     
193     
194 }