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 /* 45 * Verify getName method against both existing Attr and new Attr. 46 */ 47 @Test 48 public void testGetName() throws Exception { 49 Document document = createDOM("Attr01.xml"); 50 //test a new created Attr 51 Attr attr = document.createAttribute("newAttribute"); 52 assertEquals(attr.getName(), "newAttribute"); 53 54 //test a Attr loaded from xml file 55 Element elemNode = (Element) document.getElementsByTagName("book").item(1); 56 Attr attr2 = (Attr) elemNode.getAttributes().item(0); 57 assertEquals(attr2.getName(), "category1"); 58 } 59 60 /* 61 * Verify getOwnerElement method against both existing Attr and new Attr. 62 */ 63 @Test 64 public void testGetOwnerElement() throws Exception { 65 Document document = createDOM("Attr01.xml"); 66 67 //test Attr loaded from xml file 68 Element elemNode = (Element) document.getElementsByTagName("book").item(1); 69 NamedNodeMap nnMap = elemNode.getAttributes(); 70 for (int i = 0; i < nnMap.getLength(); i++) { 71 Attr attr = (Attr) nnMap.item(i); 72 assertEquals(attr.getOwnerElement().getNodeName(), "book"); 73 } 74 75 //test an Attr without owner node 76 Attr attr = document.createAttribute("newAttribute"); 77 assertNull(attr.getOwnerElement()); 78 79 } 80 81 /* 82 * Verify getSpecified method works as the spec. 83 */ 84 @Test 85 public void testGetSpecified1() throws Exception { 86 Document document = createDOM("Attr01.xml"); 87 88 Element elemNode = (Element) document.getElementsByTagName("book").item(1); 89 Attr attr = elemNode.getAttributeNode("category1"); 90 assertTrue(attr.getSpecified()); 91 92 } 93 94 /* 95 * In this xml file, the dtd has the value for the attrribute, but the xml 96 * element does not specify the value for the attrribute, as per the spec it 97 * should return false. 98 */ 99 @Test 100 public void testGetSpecified2() throws Exception { 101 102 Document document = createDOM("Attr2.xml"); 103 Element elemNode = (Element) document.getElementsByTagName("Name").item(0); 104 Attr attr = elemNode.getAttributeNode("type"); 105 106 assertFalse(attr.getSpecified()); 107 } 108 109 /* 110 * Creating a new attribute, the owner element is null since the attribute 111 * has just been created, getSpecified should return true. 112 */ 113 @Test 114 public void testNewCreatedAttribute() throws Exception { 115 Document document = createDOM("Attr01.xml"); 116 Attr attr = document.createAttribute("newAttribute"); 117 assertTrue(attr.getSpecified()); 118 assertNull(attr.getOwnerElement()); 119 120 } 121 122 /* 123 * The xml file includes the dtd having the IMPLIED value for the attrribute 124 * and the xml element does not specify the value. As per the spec it should 125 * not be seen as a part of the structure model hence getAttributeNode 126 * rerurn null if the attribute is even found. 127 */ 128 @Test 129 public void testIMPLIEDAttribute() throws Exception { 130 Document document = createDOM("Attr3.xml"); 131 Element elemNode = (Element) document.getElementsByTagName("Name").item(0); 132 Attr attr = elemNode.getAttributeNode("type"); 133 assertNull(attr); 134 } 135 136 /* 137 * Test setValue method and verify by getValue method. 138 */ 139 @Test 140 public void testSetValue() throws Exception { 141 Document document = createDOM("Attr01.xml"); 142 Attr attr = document.createAttribute("newAttribute"); 143 attr.setValue("newVal"); 144 assertEquals(attr.getValue(), "newVal"); 145 146 } 147 148 }