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 @Test 74 public void testRemoveAttributeNode() throws Exception { 75 Document document = createDOMWithNS("ElementSample01.xml"); 76 Element elemNode = (Element) document.getElementsByTagName("book").item(1); 77 Attr attr = elemNode.getAttributeNode("category1"); 78 assertEquals(attr.getValue(), "research"); 79 80 assertEquals(elemNode.getTagName(), "book"); 81 elemNode.removeAttributeNode(attr); 82 assertEquals(elemNode.getAttribute("category1"), ""); 83 } 84 85 @Test 86 public void testRemoveAttributeNS() throws Exception { 87 final String nsURI = "urn:BooksAreUs.org:BookInfo"; 88 final String localName = "category"; 89 Document document = createDOMWithNS("ElementSample01.xml"); 90 Element elemNode = (Element) document.getElementsByTagName("book").item(0); 91 elemNode.removeAttributeNS(nsURI, localName); 92 93 assertNull(elemNode.getAttributeNodeNS(nsURI, localName)); 94 } 95 96 @Test 97 public void testGetChild() throws Exception { 98 Document document = createDOMWithNS("ElementSample01.xml"); 99 Element elemNode = (Element) document.getElementsByTagName("b:aaa").item(0); 100 elemNode.normalize(); 101 Node firstChild = elemNode.getFirstChild(); 102 Node lastChild = elemNode.getLastChild(); 103 assertEquals(firstChild.getNodeValue(), "fjfjf"); 104 assertEquals(lastChild.getNodeValue(), "fjfjf"); 105 } 106 107 @Test 108 public void testSetAttributeNode() throws Exception { 109 final String attrName = "myAttr"; 110 final String attrValue = "attrValue"; 111 Document document = createDOM("ElementSample02.xml"); 112 Element elemNode = document.createElement("pricetag2"); 113 Attr myAttr = document.createAttribute(attrName); 114 myAttr.setValue(attrValue); 115 116 assertNull(elemNode.setAttributeNode(myAttr)); 117 assertEquals(elemNode.getAttribute(attrName), attrValue); 118 } 119 120 @DataProvider(name = "attribute") 121 public Object[][] getAttributeData() { 122 return new Object[][] { 123 { "thisisname", "thisisitsvalue" }, 124 { "style", "font-Family" } }; 125 } 126 127 @Test(dataProvider = "attribute") 128 public void testSetAttribute(String name, String value) throws Exception { 129 Document document = createDOM("ElementSample02.xml"); 130 Element elemNode = document.createElement("pricetag2"); 131 elemNode.setAttribute(name, value); 132 assertEquals(elemNode.getAttribute(name), value); 133 } 134 135 @Test(expectedExceptions = DOMException.class) 136 public void testSetAttributeNeg() throws Exception { 137 Document document = createDOM("ElementSample02.xml"); 138 Element elemNode = document.createElement("pricetag2"); 139 elemNode.setAttribute(null, null); 140 } 141 142 @Test 143 public void testDuplicateAttributeNode() throws Exception { 144 final String name = "testAttrName"; 145 final String value = "testAttrValue"; 146 Document document = createNewDocument(); 147 Attr attr = document.createAttribute(name); 148 attr.setValue(value); 149 150 Element element1 = document.createElement("AFirstElement"); 151 element1.setAttributeNode(attr); 152 Element element2 = document.createElement("ASecondElement"); 153 Attr attr2 = (Attr) attr.cloneNode(true); 154 element2.setAttributeNode(attr2); 155 assertEquals(element1.getAttribute(name), element2.getAttribute(name)); 156 157 Element element3 = document.createElement("AThirdElement"); 158 try { 159 element3.setAttributeNode(attr); 160 fail(DOMEXCEPTION_EXPECTED); 161 } catch (DOMException doe) { 162 assertEquals(doe.code, INUSE_ATTRIBUTE_ERR); 163 } 164 } 165 166 @Test 167 public void testNamespaceAware() throws Exception { 168 Document document = createDOM("ElementSample02.xml"); 169 170 NodeList nl = document.getElementsByTagNameNS("urn:BooksAreUs.org:BookInfo", "author"); 171 assertNull(nl.item(0)); 172 173 nl = document.getDocumentElement().getElementsByTagNameNS("urn:BooksAreUs.org:BookInfo", "author"); 174 assertNull(nl.item(0)); 175 } 176 177 @DataProvider(name = "nsattribute") 178 public Object[][] getNSAttributeData() { 179 return new Object[][] { 180 { "h:html", "html", "attrValue" }, 181 { "b:style", "style", "attrValue" } }; 182 } 183 184 @Test(dataProvider = "nsattribute") 185 public void testSetAttributeNodeNS(String qualifiedName, String localName, String value) throws Exception { 186 Document document = createDOM("ElementSample03.xml"); 187 Element elemNode = document.createElement("pricetag2"); 188 Attr myAttr = document.createAttributeNS(XML_NS_URI, qualifiedName); 189 myAttr.setValue(value); 190 assertNull(elemNode.setAttributeNodeNS(myAttr)); 191 assertEquals(elemNode.getAttributeNS(XML_NS_URI, localName), value); 192 } 193 194 @Test 195 public void testHasAttributeNS() throws Exception { 196 Document document = createDOMWithNS("ElementSample04.xml"); 197 NodeList nodeList = document.getElementsByTagName("body"); 198 NodeList childList = nodeList.item(0).getChildNodes(); 199 Element child = (Element) childList.item(7); 200 assertTrue(child.hasAttributeNS("urn:BooksAreUs.org:BookInfo", "style")); 201 } 202 203 @Test 204 public void testToString() throws Exception { 205 final String xml = 206 "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" 207 + "<!DOCTYPE datacenterlist>" 208 + "<datacenterlist>" 209 + " <datacenterinfo" 210 + " id=\"0\"" 211 + " naddrs=\"1\"" 212 + " nnodes=\"1\"" 213 + " ismaster=\"0\">\n" 214 + " <gateway ipaddr=\"192.168.100.27:26000\"/>" 215 + " </datacenterinfo>" 216 + "</datacenterlist>"; 217 218 Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(xml))); 219 Element root = doc.getDocumentElement(); 220 221 assertEquals(root.toString(), "[datacenterlist: null]"); 222 } 223 224 }