1 /*
   2  * Copyright (c) 2003, 2016, 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 
  29 import org.testng.annotations.Listeners;
  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 @Listeners({jaxp.library.FilePolicy.class})
  41 public class NamedNodeMapTest {
  42     /*
  43      * Test setNamedItemNS method with a node having the same namespaceURI and
  44      * qualified name as an existing one, and then test with a non-existing node.
  45      */
  46     @Test
  47     public void testSetNamedItemNS() throws Exception {
  48         final String nsURI = "urn:BooksAreUs.org:BookInfo";
  49         Document document = createDOMWithNS("NamedNodeMap01.xml");
  50         NodeList nodeList = document.getElementsByTagName("body");
  51         nodeList = nodeList.item(0).getChildNodes();
  52         Node n = nodeList.item(3);
  53 
  54         NamedNodeMap namedNodeMap = n.getAttributes();
  55 
  56         // creating an Attribute using createAttributeNS
  57         // method having the same namespaceURI
  58         // and the same qualified name as the existing one in the xml file
  59         Attr attr = document.createAttributeNS(nsURI, "b:style");
  60         // setting to a new Value
  61         attr.setValue("newValue");
  62         Node replacedAttr = namedNodeMap.setNamedItemNS(attr); // return the replaced attr
  63         assertEquals(replacedAttr.getNodeValue(), "font-family");
  64         Node updatedAttr = namedNodeMap.getNamedItemNS(nsURI, "style");
  65         assertEquals(updatedAttr.getNodeValue(), "newValue");
  66 
  67 
  68         // creating a non existing attribute node
  69         attr = document.createAttributeNS(nsURI, "b:newNode");
  70         attr.setValue("newValue");
  71 
  72         assertNull(namedNodeMap.setNamedItemNS(attr)); // return null
  73 
  74         // checking if the node could be accessed
  75         // using the getNamedItemNS method
  76         Node newAttr = namedNodeMap.getNamedItemNS(nsURI, "newNode");
  77         assertEquals(newAttr.getNodeValue(), "newValue");
  78     }
  79 
  80     /*
  81      * Verify getNamedItemNS works as the spec
  82      */
  83     @Test
  84     public void testGetNamedItemNS() throws Exception {
  85         Document document = createDOMWithNS("NamedNodeMap03.xml");
  86         NodeList nodeList = document.getElementsByTagName("body");
  87         nodeList = nodeList.item(0).getChildNodes();
  88         Node n = nodeList.item(7);
  89         NamedNodeMap namedNodeMap = n.getAttributes();
  90         Node node = namedNodeMap.getNamedItemNS("urn:BooksAreUs.org:BookInfo", "aaa");
  91         assertEquals(node.getNodeValue(), "value");
  92 
  93     }
  94 
  95     /*
  96      * Test setNamedItem method with a node having the same name as an existing
  97      * one, and then test with a non-existing node.
  98      */
  99     @Test
 100     public void testSetNamedItem() throws Exception {
 101         Document document = createDOMWithNS("NamedNodeMap03.xml");
 102         NodeList nodeList = document.getElementsByTagName("body");
 103         nodeList = nodeList.item(0).getChildNodes();
 104         Node n = nodeList.item(1);
 105 
 106         NamedNodeMap namedNodeMap = n.getAttributes();
 107         Attr attr = document.createAttribute("name");
 108         Node replacedAttr = namedNodeMap.setNamedItem(attr);
 109         assertEquals(replacedAttr.getNodeValue(), "attributeValue");
 110         Node updatedAttrNode = namedNodeMap.getNamedItem("name");
 111         assertEquals(updatedAttrNode.getNodeValue(), "");
 112 
 113         Attr newAttr = document.createAttribute("nonExistingName");
 114         assertNull(namedNodeMap.setNamedItem(newAttr));
 115         Node newAttrNode = namedNodeMap.getNamedItem("nonExistingName");
 116         assertEquals(newAttrNode.getNodeValue(), "");
 117     }
 118 
 119 }