1 /*
   2  * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.w3c.dom.ptests;
  24 
  25 import static org.testng.Assert.assertEquals;
  26 import static org.testng.Assert.assertNull;
  27 import static org.w3c.dom.ptests.DOMTestUtil.createDOMWithNS;
  28 import jaxp.library.JAXPFileBaseTest;
  29 
  30 import org.testng.annotations.Test;
  31 import org.w3c.dom.Attr;
  32 import org.w3c.dom.Document;
  33 import org.w3c.dom.NamedNodeMap;
  34 import org.w3c.dom.Node;
  35 import org.w3c.dom.NodeList;
  36 
  37 /*
  38  * @summary Test for the methods of NamedNodeMap Interface
  39  */
  40 public class NamedNodeMapTest extends JAXPFileBaseTest {
  41     /*
  42      * Test setNamedItemNS method with a node having the same namespaceURI and
  43      * qualified name as an existing one, and then test with a non-existing node.
  44      */
  45     @Test
  46     public void testSetNamedItemNS() throws Exception {
  47         final String nsURI = "urn:BooksAreUs.org:BookInfo";
  48         Document document = createDOMWithNS("NamedNodeMap01.xml");
  49         NodeList nodeList = document.getElementsByTagName("body");
  50         nodeList = nodeList.item(0).getChildNodes();
  51         Node n = nodeList.item(3);
  52 
  53         NamedNodeMap namedNodeMap = n.getAttributes();
  54 
  55         // creating an Attribute using createAttributeNS
  56         // method having the same namespaceURI
  57         // and the same qualified name as the existing one in the xml file
  58         Attr attr = document.createAttributeNS(nsURI, "b:style");
  59         // setting to a new Value
  60         attr.setValue("newValue");
  61         Node replacedAttr = namedNodeMap.setNamedItemNS(attr); // return the replaced attr
  62         assertEquals(replacedAttr.getNodeValue(), "font-family"); 
  63         Node updatedAttr = namedNodeMap.getNamedItemNS(nsURI, "style");
  64         assertEquals(updatedAttr.getNodeValue(), "newValue");
  65         
  66         
  67         // creating a non existing attribute node
  68         attr = document.createAttributeNS(nsURI, "b:newNode");
  69         attr.setValue("newValue");
  70 
  71         assertNull(namedNodeMap.setNamedItemNS(attr)); // return null
  72 
  73         // checking if the node could be accessed
  74         // using the getNamedItemNS method
  75         Node newAttr = namedNodeMap.getNamedItemNS(nsURI, "newNode");
  76         assertEquals(newAttr.getNodeValue(), "newValue");
  77     }
  78 
  79     /*
  80      * Verify getNamedItemNS works as the spec
  81      */
  82     @Test
  83     public void testGetNamedItemNS() throws Exception {
  84         Document document = createDOMWithNS("NamedNodeMap03.xml");
  85         NodeList nodeList = document.getElementsByTagName("body");
  86         nodeList = nodeList.item(0).getChildNodes();
  87         Node n = nodeList.item(7);
  88         NamedNodeMap namedNodeMap = n.getAttributes();
  89         Node node = namedNodeMap.getNamedItemNS("urn:BooksAreUs.org:BookInfo", "aaa");
  90         assertEquals(node.getNodeValue(), "value");
  91 
  92     }
  93 
  94     /*
  95      * Test setNamedItem method with a node having the same name as an existing
  96      * one, and then test with a non-existing node.
  97      */
  98     @Test
  99     public void testSetNamedItem() throws Exception {
 100         Document document = createDOMWithNS("NamedNodeMap03.xml");
 101         NodeList nodeList = document.getElementsByTagName("body");
 102         nodeList = nodeList.item(0).getChildNodes();
 103         Node n = nodeList.item(1);
 104 
 105         NamedNodeMap namedNodeMap = n.getAttributes();
 106         Attr attr = document.createAttribute("name");
 107         Node replacedAttr = namedNodeMap.setNamedItem(attr);
 108         assertEquals(replacedAttr.getNodeValue(), "attributeValue");
 109         Node updatedAttrNode = namedNodeMap.getNamedItem("name");
 110         assertEquals(updatedAttrNode.getNodeValue(), "");
 111 
 112         Attr newAttr = document.createAttribute("nonExistingName");
 113         assertNull(namedNodeMap.setNamedItem(newAttr));
 114         Node newAttrNode = namedNodeMap.getNamedItem("nonExistingName");
 115         assertEquals(newAttrNode.getNodeValue(), "");
 116     }
 117 
 118 }