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.Iterator;
63 import java.util.List;
64
65 import javax.xml.parsers.DocumentBuilderFactory;
66 import javax.xml.parsers.ParserConfigurationException;
67
68 import org.jaxen.*;
69 import org.w3c.dom.*;
70
71 import junit.framework.TestCase;
72
73 public class DOM3NamespaceTest extends TestCase {
74
75
76 private NamespaceNode xmlNS;
77 private NamespaceNode rootNS;
78 private NamespaceNode attributeNS;
79
80
81 public DOM3NamespaceTest(String name) {
82 super(name);
83 }
84
85 protected void setUp() throws ParserConfigurationException, JaxenException {
86
87 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
88 factory.setNamespaceAware(true);
89 Document doc = factory.newDocumentBuilder().newDocument();
90 org.w3c.dom.Element root = doc.createElementNS("http://www.root.com/", "root");
91 doc.appendChild(root);
92 Attr a = doc.createAttributeNS("http://www.example.org/", "pre:a");
93 a.setNodeValue("value");
94 root.setAttributeNode(a);
95
96 XPath xpath = new DOMXPath("namespace::node()");
97 List result = xpath.selectNodes(root);
98
99 Iterator iterator = result.iterator();
100 while (iterator.hasNext()) {
101 NamespaceNode node = (NamespaceNode) iterator.next();
102 if (node.getLocalName() == null) rootNS = node;
103 else if (node.getLocalName().equals("xml")) xmlNS = node;
104 else if (node.getLocalName().equals("pre")) attributeNS = node;
105 }
106
107 }
108
109
110 public void testGetTextContent() {
111 assertEquals("http://www.w3.org/XML/1998/namespace", xmlNS.getTextContent());
112 }
113
114 public void testSetTextContent() {
115
116 try {
117 rootNS.setTextContent("http://www.a.com");
118 fail("set text content");
119 }
120 catch (DOMException ex) {
121 assertEquals(DOMException.NO_MODIFICATION_ALLOWED_ERR, ex.code);
122 }
123 }
124
125
126 public void testGetFeature() {
127 assertNull(attributeNS.getFeature("name", "value"));
128 }
129
130
131 // XXX need to distinguish these two cases
132 public void testIsEqualNode() {
133 assertFalse(rootNS.isEqualNode(xmlNS));
134 assertTrue(rootNS.isEqualNode(rootNS));
135 }
136
137 public void testIsSameNode() {
138 assertFalse(rootNS.isSameNode(xmlNS));
139 assertTrue(rootNS.isSameNode(rootNS));
140 }
141
142 }