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