KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jaxen > dom > NamespaceTest


1 /* $Header$
2  * $Revision$
3  * $Date$
4  *
5  * ====================================================================
6  *
7  * Copyright (C) 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
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  * notice, this list of conditions, and the following disclaimer.
16  *
17  * 2. Redistributions in binary form must reproduce the above copyright
18  * notice, this list of conditions, and the disclaimer that follows
19  * these conditions in the documentation and/or other materials
20  * provided with the distribution.
21  *
22  * 3. The name "Jaxen" must not be used to endorse or promote products
23  * derived from this software without prior written permission. For
24  * written permission, please contact license@jaxen.org.
25  *
26  * 4. Products derived from this software may not be called "Jaxen", nor
27  * may "Jaxen" appear in their name, without prior written permission
28  * from the Jaxen Project Management (pm@jaxen.org).
29  *
30  * In addition, we request (but do not require) that you include in the
31  * end-user documentation provided with the redistribution and/or in the
32  * software itself an acknowledgement equivalent to the following:
33  * "This product includes software developed by the
34  * Jaxen Project (http://www.jaxen.org/)."
35  * Alternatively, the acknowledgment may be graphical using the logos
36  * available at http://www.jaxen.org/
37  *
38  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
39  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
40  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41  * DISCLAIMED. IN NO EVENT SHALL THE Jaxen AUTHORS OR THE PROJECT
42  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
45  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
46  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
48  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49  * SUCH DAMAGE.
50  *
51  * ====================================================================
52  * This software consists of voluntary contributions made by many
53  * individuals on behalf of the Jaxen Project and was originally
54  * created by bob mcwhirter <bob@werken.com> and
55  * James Strachan <jstrachan@apache.org>. For more information on the
56  * Jaxen Project, please see <http://www.jaxen.org/>.
57  *
58  * $Id$
59  */

60 package org.jaxen.dom;
61
62 import java.util.List JavaDoc;
63
64 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
65 import javax.xml.parsers.ParserConfigurationException JavaDoc;
66
67 import org.jaxen.JaxenException;
68 import org.jaxen.XPath;
69 import org.w3c.dom.Attr JavaDoc;
70 import org.w3c.dom.Element JavaDoc;
71
72 import junit.framework.TestCase;
73
74 public class NamespaceTest extends TestCase {
75
76     private org.w3c.dom.Document JavaDoc doc;
77
78     public NamespaceTest(String JavaDoc name) {
79         super(name);
80     }
81     
82     protected void setUp() throws ParserConfigurationException JavaDoc {
83         
84         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
85         factory.setNamespaceAware(true);
86         doc = factory.newDocumentBuilder().newDocument();
87         
88     }
89     
90     public void testMultipleNamespaceAxis() throws JaxenException {
91         
92         org.w3c.dom.Element JavaDoc root = doc.createElement("root");
93         doc.appendChild(root);
94         Element JavaDoc child = doc.createElementNS("http://www.example.org", "child");
95         child.setAttributeNS("http://www.w3.org/2000/xmlns/" , "xmlns:pre", "value");
96         root.appendChild(child);
97         
98         XPath xpath = new DOMXPath("namespace::node()");
99         List JavaDoc result = xpath.selectNodes(child);
100         assertEquals(3, result.size());
101    
102     }
103     
104     public void testNumberOfNamespaceNodes() throws JaxenException {
105         
106         org.w3c.dom.Element JavaDoc root = doc.createElement("root");
107         doc.appendChild(root);
108         Element JavaDoc child = doc.createElementNS("http://www.example.org", "foo:child");
109         root.appendChild(child);
110         
111         XPath xpath = new DOMXPath("//namespace::node()");
112         List JavaDoc result = xpath.selectNodes(doc);
113         assertEquals(3, result.size());
114         // 1 for xml prefix on root; 1 for foo prefix on child; 1 for xml prefix on child
115

116     }
117     
118     
119     public void testNamespaceAxis() throws JaxenException {
120         
121         org.w3c.dom.Element JavaDoc root = doc.createElement("root");
122         doc.appendChild(root);
123         Element JavaDoc child = doc.createElementNS("http://www.example.org", "foo:child");
124         root.appendChild(child);
125         
126         XPath xpath = new DOMXPath("namespace::node()");
127         List JavaDoc result = xpath.selectNodes(child);
128         assertEquals(2, result.size());
129    
130     }
131     
132     
133     public void testUnprefixedNamespaceAxis() throws JaxenException {
134         
135         org.w3c.dom.Element JavaDoc root = doc.createElement("root");
136         doc.appendChild(root);
137         Element JavaDoc child = doc.createElementNS("http://www.example.org", "child");
138         root.appendChild(child);
139         
140         XPath xpath = new DOMXPath("namespace::node()");
141         List JavaDoc result = xpath.selectNodes(child);
142         assertEquals(2, result.size());
143    
144     }
145     
146     
147     public void testNamespaceNodesReadFromAttributes() throws JaxenException {
148         
149         org.w3c.dom.Element JavaDoc root = doc.createElement("root");
150         doc.appendChild(root);
151         Attr JavaDoc a = doc.createAttributeNS("http://www.example.org/", "a");
152         a.setNodeValue("value");
153         root.setAttributeNode(a);
154         
155         XPath xpath = new DOMXPath("namespace::node()");
156         List JavaDoc result = xpath.selectNodes(root);
157         // one for the xml prefix; one from the attribute node
158
assertEquals(2, result.size());
159    
160     }
161     
162     
163 }
164
Popular Tags