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 javax.xml.XMLConstants.XML_NS_URI; 26 import static org.testng.Assert.assertEquals; 27 import static org.testng.Assert.assertNull; 28 import static org.testng.Assert.assertTrue; 29 import static org.testng.Assert.fail; 30 import static org.w3c.dom.DOMException.INUSE_ATTRIBUTE_ERR; 31 import static org.w3c.dom.ptests.DOMTestUtil.DOMEXCEPTION_EXPECTED; 32 import static org.w3c.dom.ptests.DOMTestUtil.createDOM; 33 import static org.w3c.dom.ptests.DOMTestUtil.createDOMWithNS; 34 import static org.w3c.dom.ptests.DOMTestUtil.createNewDocument; 35 36 import java.io.StringReader; 37 38 import javax.xml.parsers.DocumentBuilderFactory; 39 40 import jaxp.library.JAXPFileBaseTest; 41 42 import org.testng.annotations.DataProvider; 43 import org.testng.annotations.Test; 44 import org.w3c.dom.Attr; 45 import org.w3c.dom.DOMException; 46 import org.w3c.dom.Document; 47 import org.w3c.dom.Element; 48 import org.w3c.dom.Node; 49 import org.w3c.dom.NodeList; 50 import org.xml.sax.InputSource; 51 52 /* 53 * @summary Test for the methods of Element Interface 54 */ 55 public class ElementTest extends JAXPFileBaseTest { 56 @Test 57 public void testGetAttributeNS() throws Exception { 58 Document document = createDOMWithNS("ElementSample01.xml"); 59 Element elemNode = (Element) document.getElementsByTagName("book").item(0); 60 String s = elemNode.getAttributeNS("urn:BooksAreUs.org:BookInfo", "category"); 61 assertEquals(s, "research"); 62 } 63 64 @Test 65 public void testGetAttributeNodeNS() throws Exception { 66 Document document = createDOMWithNS("ElementSample01.xml"); 67 Element elemNode = (Element) document.getElementsByTagName("book").item(0); 68 Attr attr = elemNode.getAttributeNodeNS("urn:BooksAreUs.org:BookInfo", "category"); 69 assertEquals(attr.getValue(), "research"); 70 71 } 72 73 /* 74 * Test getAttributeNode to get a Attr and then remove it successfully by 75 * removeAttributeNode. 76 */ 77 @Test 78 public void testRemoveAttributeNode() throws Exception { 79 Document document = createDOMWithNS("ElementSample01.xml"); 80 Element elemNode = (Element) document.getElementsByTagName("book").item(1); 81 Attr attr = elemNode.getAttributeNode("category1"); 82 assertEquals(attr.getValue(), "research"); 83 84 assertEquals(elemNode.getTagName(), "book"); 85 elemNode.removeAttributeNode(attr); 86 assertEquals(elemNode.getAttribute("category1"), ""); 87 } 88 89 /* 90 * Test removing an Attribute Node with removeAttributeNS(String 91 * namespaceURI, String localName). 92 */ 93 @Test 94 public void testRemoveAttributeNS() throws Exception { 95 final String nsURI = "urn:BooksAreUs.org:BookInfo"; 96 final String localName = "category"; 97 Document document = createDOMWithNS("ElementSample01.xml"); 98 Element elemNode = (Element) document.getElementsByTagName("book").item(0); 99 elemNode.removeAttributeNS(nsURI, localName); 100 101 assertNull(elemNode.getAttributeNodeNS(nsURI, localName)); 102 } 103 104 /* 105 * Test getFirstChild and getLastChild. 106 */ 107 @Test 108 public void testGetChild() throws Exception { 109 Document document = createDOMWithNS("ElementSample01.xml"); 110 Element elemNode = (Element) document.getElementsByTagName("b:aaa").item(0); 111 elemNode.normalize(); 112 Node firstChild = elemNode.getFirstChild(); 113 Node lastChild = elemNode.getLastChild(); 114 assertEquals(firstChild.getNodeValue(), "fjfjf"); 115 assertEquals(lastChild.getNodeValue(), "fjfjf"); 116 } 117 118 /* 119 * Test setAttributeNode with an Attr from createAttribute. 120 */ 121 @Test 122 public void testSetAttributeNode() throws Exception { 123 final String attrName = "myAttr"; 124 final String attrValue = "attrValue"; 125 Document document = createDOM("ElementSample02.xml"); 126 Element elemNode = document.createElement("pricetag2"); 127 Attr myAttr = document.createAttribute(attrName); 128 myAttr.setValue(attrValue); 129 130 assertNull(elemNode.setAttributeNode(myAttr)); 131 assertEquals(elemNode.getAttribute(attrName), attrValue); 132 } 133 134 @DataProvider(name = "attribute") 135 public Object[][] getAttributeData() { 136 return new Object[][] { 137 { "thisisname", "thisisitsvalue" }, 138 { "style", "font-Family" } }; 139 } 140 141 @Test(dataProvider = "attribute") 142 public void testSetAttribute(String name, String value) throws Exception { 143 Document document = createDOM("ElementSample02.xml"); 144 Element elemNode = document.createElement("pricetag2"); 145 elemNode.setAttribute(name, value); 146 assertEquals(elemNode.getAttribute(name), value); 147 } 148 149 /* 150 * Negative test for setAttribute, null is not a valid name. 151 */ 152 @Test(expectedExceptions = DOMException.class) 153 public void testSetAttributeNeg() throws Exception { 154 Document document = createDOM("ElementSample02.xml"); 155 Element elemNode = document.createElement("pricetag2"); 156 elemNode.setAttribute(null, null); 157 } 158 159 /* 160 * Test setAttributeNode, newAttr can't be an attribute of another Element 161 * object, must explicitly clone Attr nodes to re-use them in other 162 * elements. 163 */ 164 @Test 165 public void testDuplicateAttributeNode() throws Exception { 166 final String name = "testAttrName"; 167 final String value = "testAttrValue"; 168 Document document = createNewDocument(); 169 Attr attr = document.createAttribute(name); 170 attr.setValue(value); 171 172 Element element1 = document.createElement("AFirstElement"); 173 element1.setAttributeNode(attr); 174 Element element2 = document.createElement("ASecondElement"); 175 Attr attr2 = (Attr) attr.cloneNode(true); 176 element2.setAttributeNode(attr2); 177 assertEquals(element1.getAttribute(name), element2.getAttribute(name)); 178 179 Element element3 = document.createElement("AThirdElement"); 180 try { 181 element3.setAttributeNode(attr); 182 fail(DOMEXCEPTION_EXPECTED); 183 } catch (DOMException doe) { 184 assertEquals(doe.code, INUSE_ATTRIBUTE_ERR); 185 } 186 } 187 188 /* 189 * If not setting the namsepace aware method of DocumentBuilderFactory to 190 * true, can't retrieve element by namespace and local name. 191 */ 192 @Test 193 public void testNamespaceAware() throws Exception { 194 Document document = createDOM("ElementSample02.xml"); 195 196 NodeList nl = document.getElementsByTagNameNS("urn:BooksAreUs.org:BookInfo", "author"); 197 assertNull(nl.item(0)); 198 199 nl = document.getDocumentElement().getElementsByTagNameNS("urn:BooksAreUs.org:BookInfo", "author"); 200 assertNull(nl.item(0)); 201 } 202 203 @DataProvider(name = "nsattribute") 204 public Object[][] getNSAttributeData() { 205 return new Object[][] { 206 { "h:html", "html", "attrValue" }, 207 { "b:style", "style", "attrValue" } }; 208 } 209 210 /* 211 * setAttributeNodeNS and verify it with getAttributeNS. 212 */ 213 @Test(dataProvider = "nsattribute") 214 public void testSetAttributeNodeNS(String qualifiedName, String localName, String value) throws Exception { 215 Document document = createDOM("ElementSample03.xml"); 216 Element elemNode = document.createElement("pricetag2"); 217 Attr myAttr = document.createAttributeNS(XML_NS_URI, qualifiedName); 218 myAttr.setValue(value); 219 assertNull(elemNode.setAttributeNodeNS(myAttr)); 220 assertEquals(elemNode.getAttributeNS(XML_NS_URI, localName), value); 221 } 222 223 @Test 224 public void testHasAttributeNS() throws Exception { 225 Document document = createDOMWithNS("ElementSample04.xml"); 226 NodeList nodeList = document.getElementsByTagName("body"); 227 NodeList childList = nodeList.item(0).getChildNodes(); 228 Element child = (Element) childList.item(7); 229 assertTrue(child.hasAttributeNS("urn:BooksAreUs.org:BookInfo", "style")); 230 } 231 232 @Test 233 public void testToString() throws Exception { 234 final String xml = 235 "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" 236 + "<!DOCTYPE datacenterlist>" 237 + "<datacenterlist>" 238 + " <datacenterinfo" 239 + " id=\"0\"" 240 + " naddrs=\"1\"" 241 + " nnodes=\"1\"" 242 + " ismaster=\"0\">\n" 243 + " <gateway ipaddr=\"192.168.100.27:26000\"/>" 244 + " </datacenterinfo>" 245 + "</datacenterlist>"; 246 247 Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(xml))); 248 Element root = doc.getDocumentElement(); 249 250 assertEquals(root.toString(), "[datacenterlist: null]"); 251 } 252 253 }