test/jaxp/javax/xml/jaxp/unittest/dom/DocumentTest.java
author joehw
Wed, 28 Nov 2018 10:30:15 -0800
changeset 52721 732bec44c89e
child 54628 dcb78d2f07e5
permissions -rw-r--r--
8213117: adoptNode corrupts attribute values Reviewed-by: lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52721
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
     1
/*
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
     4
 *
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
     7
 * published by the Free Software Foundation.
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
     8
 *
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    13
 * accompanied this code).
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    14
 *
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    18
 *
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    21
 * questions.
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    22
 */
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    23
package dom;
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    24
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    25
import java.io.ByteArrayInputStream;
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    26
import java.io.IOException;
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    27
import java.io.InputStream;
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    28
import javax.xml.parsers.DocumentBuilder;
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    29
import javax.xml.parsers.DocumentBuilderFactory;
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    30
import org.testng.Assert;
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    31
import org.testng.annotations.Listeners;
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    32
import org.testng.annotations.Test;
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    33
import org.w3c.dom.Document;
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    34
import org.w3c.dom.Element;
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    35
import org.w3c.dom.Node;
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    36
import org.xml.sax.SAXException;
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    37
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    38
/*
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    39
 * @test
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    40
 * @bug 8213117
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    41
 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    42
 * @run testng dom.DocumentTest
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    43
 * @summary Tests functionalities for Document.
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    44
 */
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    45
@Listeners({jaxp.library.BasePolicy.class})
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    46
public class DocumentTest {
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    47
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    48
    private static final String XML1 = "<root><oldNode oldAttrib1=\"old value 1\" oldAttrib2=\"old value 2\"></oldNode></root>";
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    49
    private static final String XML2 = "<root><newNode newAttrib=\"new value\"></newNode></root>";
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    50
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    51
    /**
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    52
     * Verifies the adoptNode method. Before a node from a deferred DOM can be
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    53
     * adopted, it needs to be fully expanded.
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    54
     */
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    55
    @Test
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    56
    public void testAdoptNode() throws Exception {
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    57
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    58
        DocumentBuilder builder = DocumentBuilderFactory.newInstance()
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    59
                .newDocumentBuilder();
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    60
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    61
        Document doc1 = getDocument(builder, XML1);
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    62
        Document doc2 = getDocument(builder, XML2);
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    63
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    64
        Element newNode = (Element) doc2.getFirstChild().getFirstChild();
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    65
        Element replacementNode = (Element) doc1.adoptNode(newNode);
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    66
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    67
        Node oldNode = doc1.getFirstChild().getFirstChild();
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    68
        doc1.getDocumentElement().replaceChild(replacementNode, oldNode);
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    69
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    70
        String attrValue = doc1.getFirstChild().getFirstChild().getAttributes()
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    71
                .getNamedItem("newAttrib").getNodeValue();
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    72
        Assert.assertEquals(attrValue, "new value");
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    73
    }
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    74
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    75
    private static Document getDocument(DocumentBuilder builder, String xml) throws SAXException, IOException {
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    76
        InputStream a = new ByteArrayInputStream(xml.getBytes());
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    77
        Document out = builder.parse(a);
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    78
        return out;
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    79
    }
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    80
732bec44c89e 8213117: adoptNode corrupts attribute values
joehw
parents:
diff changeset
    81
}