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