|
1 /* |
|
2 * Copyright (c) 2018, 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 dom; |
|
24 |
|
25 import java.io.ByteArrayInputStream; |
|
26 import java.io.IOException; |
|
27 import java.io.InputStream; |
|
28 import javax.xml.parsers.DocumentBuilder; |
|
29 import javax.xml.parsers.DocumentBuilderFactory; |
|
30 import org.testng.Assert; |
|
31 import org.testng.annotations.Listeners; |
|
32 import org.testng.annotations.Test; |
|
33 import org.w3c.dom.Document; |
|
34 import org.w3c.dom.Element; |
|
35 import org.w3c.dom.Node; |
|
36 import org.xml.sax.SAXException; |
|
37 |
|
38 /* |
|
39 * @test |
|
40 * @bug 8213117 |
|
41 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest |
|
42 * @run testng dom.DocumentTest |
|
43 * @summary Tests functionalities for Document. |
|
44 */ |
|
45 @Listeners({jaxp.library.BasePolicy.class}) |
|
46 public class DocumentTest { |
|
47 |
|
48 private static final String XML1 = "<root><oldNode oldAttrib1=\"old value 1\" oldAttrib2=\"old value 2\"></oldNode></root>"; |
|
49 private static final String XML2 = "<root><newNode newAttrib=\"new value\"></newNode></root>"; |
|
50 |
|
51 /** |
|
52 * Verifies the adoptNode method. Before a node from a deferred DOM can be |
|
53 * adopted, it needs to be fully expanded. |
|
54 */ |
|
55 @Test |
|
56 public void testAdoptNode() throws Exception { |
|
57 |
|
58 DocumentBuilder builder = DocumentBuilderFactory.newInstance() |
|
59 .newDocumentBuilder(); |
|
60 |
|
61 Document doc1 = getDocument(builder, XML1); |
|
62 Document doc2 = getDocument(builder, XML2); |
|
63 |
|
64 Element newNode = (Element) doc2.getFirstChild().getFirstChild(); |
|
65 Element replacementNode = (Element) doc1.adoptNode(newNode); |
|
66 |
|
67 Node oldNode = doc1.getFirstChild().getFirstChild(); |
|
68 doc1.getDocumentElement().replaceChild(replacementNode, oldNode); |
|
69 |
|
70 String attrValue = doc1.getFirstChild().getFirstChild().getAttributes() |
|
71 .getNamedItem("newAttrib").getNodeValue(); |
|
72 Assert.assertEquals(attrValue, "new value"); |
|
73 } |
|
74 |
|
75 private static Document getDocument(DocumentBuilder builder, String xml) throws SAXException, IOException { |
|
76 InputStream a = new ByteArrayInputStream(xml.getBytes()); |
|
77 Document out = builder.parse(a); |
|
78 return out; |
|
79 } |
|
80 |
|
81 } |