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 24 package org.w3c.dom.ptests; 25 26 import static org.testng.Assert.assertEquals; 27 import static org.testng.Assert.assertFalse; 28 import static org.testng.Assert.assertNull; 29 import static org.testng.Assert.assertTrue; 30 import static org.w3c.dom.ptests.DOMTestUtil.createDOM; 31 import jaxp.library.JAXPFileBaseTest; 32 33 import org.testng.annotations.Test; 34 import org.w3c.dom.Attr; 35 import org.w3c.dom.Document; 36 import org.w3c.dom.Element; 37 import org.w3c.dom.NamedNodeMap; 38 39 40 /* 41 * @summary Test for the Attr Interface 42 */ 43 public class AttrTest extends JAXPFileBaseTest { 44 @Test 45 public void testGetName() throws Exception { 46 Document document = createDOM("Attr01.xml"); 47 //test a new created Attr 48 Attr attr = document.createAttribute("newAttribute"); 49 assertEquals(attr.getName(), "newAttribute"); 50 51 //test a Attr loaded from xml file 52 Element elemNode = (Element) document.getElementsByTagName("book").item(1); 53 Attr attr2 = (Attr) elemNode.getAttributes().item(0); 54 assertEquals(attr2.getName(), "category1"); 55 } 56 57 @Test 58 public void testGetOwnerElement() throws Exception { 59 Document document = createDOM("Attr01.xml"); 60 61 //test Attr loaded from xml file 62 Element elemNode = (Element) document.getElementsByTagName("book").item(1); 63 NamedNodeMap nnMap = elemNode.getAttributes(); 64 for (int i = 0; i < nnMap.getLength(); i++) { 65 Attr attr = (Attr) nnMap.item(i); 66 assertEquals(attr.getOwnerElement().getNodeName(), "book"); 67 } 68 69 //test an Attr without owner node 70 Attr attr = document.createAttribute("newAttribute"); 71 assertNull(attr.getOwnerElement()); 72 73 } 74 75 @Test 76 public void testGetSpecified1() throws Exception { 77 Document document = createDOM("Attr01.xml"); 78 79 Element elemNode = (Element) document.getElementsByTagName("book").item(1); 80 Attr attr = elemNode.getAttributeNode("category1"); 81 assertTrue(attr.getSpecified()); 82 83 } 84 85 /* 86 * In this xml file, the dtd has the value for the attrribute, but the xml 87 * element does not specify the value for the attrribute, as per the spec it 88 * should return false. 89 */ 90 @Test 91 public void testGetSpecified2() throws Exception { 92 93 Document document = createDOM("Attr2.xml"); 94 Element elemNode = (Element) document.getElementsByTagName("Name").item(0); 95 Attr attr = elemNode.getAttributeNode("type"); 96 97 assertFalse(attr.getSpecified()); 98 } 99 100 /* 101 * Creating a new attribute, the owner element is null since the attribute 102 * has just been created, getSpecified should return true. 103 */ 104 @Test 105 public void testNewCreatedAttribute() throws Exception { 106 Document document = createDOM("Attr01.xml"); 107 Attr attr = document.createAttribute("newAttribute"); 108 assertTrue(attr.getSpecified()); 109 assertNull(attr.getOwnerElement()); 110 111 } 112 113 /* 114 * The xml file includes the dtd having the IMPLIED value for the attrribute 115 * and the xml element does not specify the value. As per the spec it should 116 * not be seen as a part of the structure model hence getAttributeNode 117 * rerurn null if the attribute is even found. 118 */ 119 @Test 120 public void testIMPLIEDAttribute() throws Exception { 121 Document document = createDOM("Attr3.xml"); 122 Element elemNode = (Element) document.getElementsByTagName("Name").item(0); 123 Attr attr = elemNode.getAttributeNode("type"); 124 assertNull(attr); 125 } 126 127 @Test 128 public void testSetValue() throws Exception { 129 Document document = createDOM("Attr01.xml"); 130 Attr attr = document.createAttribute("newAttribute"); 131 attr.setValue("newVal"); 132 assertEquals(attr.getValue(), "newVal"); 133 134 } 135 136 }