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