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