ElementTest.java revision 722:c3a1f0059b98
1171095Ssam/*
2171095Ssam * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
3171095Ssam * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4171095Ssam *
5171095Ssam * This code is free software; you can redistribute it and/or modify it
6171095Ssam * under the terms of the GNU General Public License version 2 only, as
7171095Ssam * published by the Free Software Foundation.
8171095Ssam *
9171095Ssam * This code is distributed in the hope that it will be useful, but WITHOUT
10171095Ssam * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11171095Ssam * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12171095Ssam * version 2 for more details (a copy is included in the LICENSE file that
13171095Ssam * accompanied this code).
14171095Ssam *
15171095Ssam * You should have received a copy of the GNU General Public License version
16171095Ssam * 2 along with this work; if not, write to the Free Software Foundation,
17171095Ssam * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18171095Ssam *
19171095Ssam * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20171095Ssam * or visit www.oracle.com if you need additional information or have any
21171095Ssam * questions.
22171095Ssam */
23171095Ssampackage org.w3c.dom.ptests;
24171095Ssam
25171095Ssamimport static javax.xml.XMLConstants.XML_NS_URI;
26171095Ssamimport static org.testng.Assert.assertEquals;
27171095Ssamimport static org.testng.Assert.assertNull;
28171095Ssamimport static org.testng.Assert.assertTrue;
29171095Ssamimport static org.testng.Assert.fail;
30171095Ssamimport static org.w3c.dom.DOMException.INUSE_ATTRIBUTE_ERR;
31171095Ssamimport static org.w3c.dom.ptests.DOMTestUtil.DOMEXCEPTION_EXPECTED;
32173139Srwatsonimport static org.w3c.dom.ptests.DOMTestUtil.createDOM;
33173139Srwatsonimport static org.w3c.dom.ptests.DOMTestUtil.createDOMWithNS;
34173139Srwatsonimport static org.w3c.dom.ptests.DOMTestUtil.createNewDocument;
35173139Srwatson
36173139Srwatsonimport java.io.StringReader;
37173139Srwatson
38173139Srwatsonimport javax.xml.parsers.DocumentBuilderFactory;
39171095Ssam
40173139Srwatsonimport jaxp.library.JAXPFileBaseTest;
41173139Srwatson
42173139Srwatsonimport org.testng.annotations.DataProvider;
43173139Srwatsonimport org.testng.annotations.Test;
44171095Ssamimport org.w3c.dom.Attr;
45171095Ssamimport org.w3c.dom.DOMException;
46171095Ssamimport org.w3c.dom.Document;
47171095Ssamimport org.w3c.dom.Element;
48173139Srwatsonimport org.w3c.dom.Node;
49173139Srwatsonimport org.w3c.dom.NodeList;
50171095Ssamimport org.xml.sax.InputSource;
51171095Ssam
52171095Ssam/*
53171095Ssam * @summary Test for the methods of Element Interface
54171095Ssam */
55171095Ssampublic class ElementTest extends JAXPFileBaseTest {
56171095Ssam    @Test
57171095Ssam    public void testGetAttributeNS() throws Exception {
58171095Ssam        Document document = createDOMWithNS("ElementSample01.xml");
59171095Ssam        Element elemNode = (Element) document.getElementsByTagName("book").item(0);
60171095Ssam        String s = elemNode.getAttributeNS("urn:BooksAreUs.org:BookInfo", "category");
61171095Ssam        assertEquals(s, "research");
62173139Srwatson    }
63171095Ssam
64171095Ssam    @Test
65171095Ssam    public void testGetAttributeNodeNS() throws Exception {
66171095Ssam        Document document = createDOMWithNS("ElementSample01.xml");
67171095Ssam        Element elemNode = (Element) document.getElementsByTagName("book").item(0);
68171095Ssam        Attr attr = elemNode.getAttributeNodeNS("urn:BooksAreUs.org:BookInfo", "category");
69171095Ssam        assertEquals(attr.getValue(), "research");
70171095Ssam
71171095Ssam    }
72173139Srwatson
73171095Ssam    /*
74171095Ssam     * Test getAttributeNode to get a Attr and then remove it successfully by
75171095Ssam     * removeAttributeNode.
76171095Ssam     */
77171095Ssam    @Test
78171095Ssam    public void testRemoveAttributeNode() throws Exception {
79171095Ssam        Document document = createDOMWithNS("ElementSample01.xml");
80171095Ssam        Element elemNode = (Element) document.getElementsByTagName("book").item(1);
81171095Ssam        Attr attr = elemNode.getAttributeNode("category1");
82173139Srwatson        assertEquals(attr.getValue(), "research");
83171095Ssam
84171095Ssam        assertEquals(elemNode.getTagName(), "book");
85171095Ssam        elemNode.removeAttributeNode(attr);
86171095Ssam        assertEquals(elemNode.getAttribute("category1"), "");
87171095Ssam    }
88173139Srwatson
89173139Srwatson    /*
90173139Srwatson     * Test removing an Attribute Node with removeAttributeNS(String
91173139Srwatson     * namespaceURI, String localName).
92173139Srwatson     */
93173139Srwatson    @Test
94173139Srwatson    public void testRemoveAttributeNS() throws Exception {
95173139Srwatson        final String nsURI = "urn:BooksAreUs.org:BookInfo";
96173139Srwatson        final String localName = "category";
97173139Srwatson        Document document = createDOMWithNS("ElementSample01.xml");
98173139Srwatson        Element elemNode = (Element) document.getElementsByTagName("book").item(0);
99173139Srwatson        elemNode.removeAttributeNS(nsURI, localName);
100173139Srwatson
101173139Srwatson        assertNull(elemNode.getAttributeNodeNS(nsURI, localName));
102173139Srwatson    }
103173139Srwatson
104173139Srwatson    /*
105173139Srwatson     * Test getFirstChild and getLastChild.
106173139Srwatson     */
107173139Srwatson    @Test
108171095Ssam    public void testGetChild() throws Exception {
109171095Ssam        Document document = createDOMWithNS("ElementSample01.xml");
110171095Ssam        Element elemNode = (Element) document.getElementsByTagName("b:aaa").item(0);
111171095Ssam        elemNode.normalize();
112173139Srwatson        Node firstChild = elemNode.getFirstChild();
113173139Srwatson        Node lastChild = elemNode.getLastChild();
114173139Srwatson        assertEquals(firstChild.getNodeValue(), "fjfjf");
115173139Srwatson        assertEquals(lastChild.getNodeValue(), "fjfjf");
116173139Srwatson    }
117173139Srwatson
118173139Srwatson    /*
119173139Srwatson     * Test setAttributeNode with an Attr from createAttribute.
120173139Srwatson     */
121173139Srwatson    @Test
122173139Srwatson    public void testSetAttributeNode() throws Exception {
123173139Srwatson        final String attrName = "myAttr";
124173139Srwatson        final String attrValue = "attrValue";
125173139Srwatson        Document document = createDOM("ElementSample02.xml");
126173139Srwatson        Element elemNode = document.createElement("pricetag2");
127173139Srwatson        Attr myAttr = document.createAttribute(attrName);
128171095Ssam        myAttr.setValue(attrValue);
129173139Srwatson
130173139Srwatson        assertNull(elemNode.setAttributeNode(myAttr));
131173139Srwatson        assertEquals(elemNode.getAttribute(attrName), attrValue);
132171095Ssam    }
133171095Ssam
134171095Ssam    @DataProvider(name = "attribute")
135171095Ssam    public Object[][] getAttributeData() {
136171095Ssam        return new Object[][] {
137171095Ssam                { "thisisname", "thisisitsvalue" },
138171095Ssam                { "style", "font-Family" } };
139171095Ssam    }
140171095Ssam
141171095Ssam    @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}
254