jaxp/test/javax/xml/jaxp/unittest/org/w3c/dom/ls/LSSerializerTest.java
author joehw
Mon, 08 Jun 2015 21:59:07 -0700
changeset 31104 79a0696bbd7c
parent 30707 47b636cd6456
child 31284 e9b8469adb9a
permissions -rw-r--r--
8080906: Develop test for Xerces Update: DOM L3 Serializer Reviewed-by: lancea, joehw Contributed-by: frank.yuan@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27830
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
     1
/*
30707
47b636cd6456 8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents: 27830
diff changeset
     2
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
27830
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
     4
 *
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
     7
 * published by the Free Software Foundation.
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
     8
 *
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    13
 * accompanied this code).
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    14
 *
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    18
 *
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    21
 * questions.
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    22
 */
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    23
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    24
package org.w3c.dom.ls;
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    25
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    26
import java.io.IOException;
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    27
import java.io.OutputStream;
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    28
import java.io.StringReader;
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    29
import java.io.Writer;
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    30
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    31
import javax.xml.parsers.DocumentBuilder;
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    32
import javax.xml.parsers.DocumentBuilderFactory;
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    33
import javax.xml.parsers.ParserConfigurationException;
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    34
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    35
import org.testng.Assert;
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    36
import org.testng.annotations.Test;
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    37
import org.w3c.dom.DOMConfiguration;
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    38
import org.w3c.dom.DOMError;
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    39
import org.w3c.dom.DOMErrorHandler;
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    40
import org.w3c.dom.DOMImplementation;
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    41
import org.w3c.dom.Document;
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    42
import org.xml.sax.InputSource;
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    43
import org.xml.sax.SAXException;
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    44
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    45
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    46
/*
31104
79a0696bbd7c 8080906: Develop test for Xerces Update: DOM L3 Serializer
joehw
parents: 30707
diff changeset
    47
 * @bug 6439439 8080906
27830
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    48
 * @summary Test LSSerializer.
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    49
 */
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    50
public class LSSerializerTest {
30707
47b636cd6456 8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents: 27830
diff changeset
    51
    private static final String DOM_FORMAT_PRETTY_PRINT = "format-pretty-print";
27830
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    52
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    53
    class DOMErrorHandlerImpl implements DOMErrorHandler {
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    54
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    55
        boolean NoOutputSpecifiedErrorReceived = false;
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    56
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    57
        public boolean handleError(final DOMError error) {
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    58
            // consume "no-output-specified" errors
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    59
            if ("no-output-specified".equalsIgnoreCase(error.getType())) {
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    60
                NoOutputSpecifiedErrorReceived = true;
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    61
                return true;
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    62
            }
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    63
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    64
            // unexpected error
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    65
            Assert.fail("Unexpected Error Type: " + error.getType() + " @ (" + error.getLocation().getLineNumber() + ", "
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    66
                    + error.getLocation().getColumnNumber() + ")" + ", " + error.getMessage());
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    67
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    68
            return false;
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    69
        }
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    70
    }
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    71
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    72
    class Output implements LSOutput {
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    73
        public OutputStream getByteStream() {
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    74
            return null;
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    75
        }
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    76
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    77
        public void setByteStream(final OutputStream byteStream) {
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    78
        }
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    79
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    80
        public Writer getCharacterStream() {
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    81
            return null;
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    82
        }
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    83
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    84
        public void setCharacterStream(final Writer characterStream) {
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    85
        }
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    86
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    87
        public String getSystemId() {
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    88
            return null;
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    89
        }
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    90
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    91
        public void setSystemId(final String systemId) {
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    92
        }
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    93
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    94
        public String getEncoding() {
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    95
            return "UTF8";
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    96
        }
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    97
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    98
        public void setEncoding(final String encoding) {
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
    99
        }
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   100
    }
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   101
31104
79a0696bbd7c 8080906: Develop test for Xerces Update: DOM L3 Serializer
joehw
parents: 30707
diff changeset
   102
    /*
79a0696bbd7c 8080906: Develop test for Xerces Update: DOM L3 Serializer
joehw
parents: 30707
diff changeset
   103
     * @bug 8080906
79a0696bbd7c 8080906: Develop test for Xerces Update: DOM L3 Serializer
joehw
parents: 30707
diff changeset
   104
     * It will fail in a Jigsaw build until JDK-8080266 is fixed.
79a0696bbd7c 8080906: Develop test for Xerces Update: DOM L3 Serializer
joehw
parents: 30707
diff changeset
   105
     */
79a0696bbd7c 8080906: Develop test for Xerces Update: DOM L3 Serializer
joehw
parents: 30707
diff changeset
   106
    @Test
79a0696bbd7c 8080906: Develop test for Xerces Update: DOM L3 Serializer
joehw
parents: 30707
diff changeset
   107
    public void testDefaultLSSerializer() throws Exception {
79a0696bbd7c 8080906: Develop test for Xerces Update: DOM L3 Serializer
joehw
parents: 30707
diff changeset
   108
        DOMImplementationLS domImpl = (DOMImplementationLS) DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation();
79a0696bbd7c 8080906: Develop test for Xerces Update: DOM L3 Serializer
joehw
parents: 30707
diff changeset
   109
        LSSerializer lsSerializer = domImpl.createLSSerializer();
79a0696bbd7c 8080906: Develop test for Xerces Update: DOM L3 Serializer
joehw
parents: 30707
diff changeset
   110
        Assert.assertTrue(lsSerializer.getClass().getName().endsWith("dom3.LSSerializerImpl"));
79a0696bbd7c 8080906: Develop test for Xerces Update: DOM L3 Serializer
joehw
parents: 30707
diff changeset
   111
    }
79a0696bbd7c 8080906: Develop test for Xerces Update: DOM L3 Serializer
joehw
parents: 30707
diff changeset
   112
27830
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   113
    @Test
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   114
    public void testDOMErrorHandler() {
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   115
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   116
        final String XML_DOCUMENT = "<?xml version=\"1.0\"?>" + "<hello>" + "world" + "</hello>";
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   117
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   118
        StringReader stringReader = new StringReader(XML_DOCUMENT);
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   119
        InputSource inputSource = new InputSource(stringReader);
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   120
        Document doc = null;
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   121
        try {
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   122
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   123
            // LSSerializer defaults to Namespace processing
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   124
            // so parsing must also
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   125
            documentBuilderFactory.setNamespaceAware(true);
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   126
            DocumentBuilder parser = documentBuilderFactory.newDocumentBuilder();
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   127
            doc = parser.parse(inputSource);
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   128
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   129
        } catch (Throwable e) {
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   130
            e.printStackTrace();
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   131
            Assert.fail(e.toString());
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   132
        }
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   133
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   134
        DOMImplementation impl = doc.getImplementation();
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   135
        DOMImplementationLS implLS = (DOMImplementationLS) impl.getFeature("LS", "3.0");
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   136
        LSSerializer writer = implLS.createLSSerializer();
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   137
        DOMErrorHandlerImpl eh = new DOMErrorHandlerImpl();
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   138
        writer.getDomConfig().setParameter("error-handler", eh);
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   139
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   140
        boolean serialized = false;
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   141
        try {
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   142
            serialized = writer.write(doc, new Output());
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   143
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   144
            // unexpected success
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   145
            Assert.fail("Serialized without raising an LSException due to " + "'no-output-specified'.");
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   146
        } catch (LSException lsException) {
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   147
            // expected exception
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   148
            System.out.println("Expected LSException: " + lsException.toString());
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   149
            // continue processing
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   150
        }
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   151
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   152
        Assert.assertFalse(serialized, "Expected writer.write(doc, new Output()) == false");
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   153
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   154
        Assert.assertTrue(eh.NoOutputSpecifiedErrorReceived, "'no-output-specified' error was expected");
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   155
    }
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   156
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   157
    @Test
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   158
    public void testFormatPrettyPrint() {
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   159
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   160
        final String XML_DOCUMENT = "<?xml version=\"1.0\" encoding=\"UTF-16\"?>\n" + "<hello>" + "world" + "<child><children/><children/></child>"
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   161
                + "</hello>";
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   162
        /**JDK-8035467
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   163
         * no newline in default output
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   164
         */
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   165
        final String XML_DOCUMENT_DEFAULT_PRINT =
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   166
                "<?xml version=\"1.0\" encoding=\"UTF-16\"?>"
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   167
                + "<hello>"
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   168
                + "world"
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   169
                + "<child><children/><children/></child>"
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   170
                + "</hello>";
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   171
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   172
        final String XML_DOCUMENT_PRETTY_PRINT = "<?xml version=\"1.0\" encoding=\"UTF-16\"?>" + "<hello>" + "world" + "<child>" + "\n" + "        "
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   173
                + "<children/>" + "\n" + "        " + "<children/>" + "\n" + "    " + "</child>" + "\n" + "</hello>" + "\n";
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   174
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   175
        // it all begins with a Document
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   176
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   177
        DocumentBuilder documentBuilder = null;
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   178
        try {
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   179
            documentBuilder = documentBuilderFactory.newDocumentBuilder();
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   180
        } catch (ParserConfigurationException parserConfigurationException) {
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   181
            parserConfigurationException.printStackTrace();
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   182
            Assert.fail(parserConfigurationException.toString());
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   183
        }
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   184
        Document document = null;
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   185
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   186
        StringReader stringReader = new StringReader(XML_DOCUMENT);
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   187
        InputSource inputSource = new InputSource(stringReader);
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   188
        try {
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   189
            document = documentBuilder.parse(inputSource);
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   190
        } catch (SAXException saxException) {
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   191
            saxException.printStackTrace();
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   192
            Assert.fail(saxException.toString());
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   193
        } catch (IOException ioException) {
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   194
            ioException.printStackTrace();
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   195
            Assert.fail(ioException.toString());
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   196
        }
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   197
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   198
        // query DOM Interfaces to get to a LSSerializer
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   199
        DOMImplementation domImplementation = documentBuilder.getDOMImplementation();
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   200
        DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation;
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   201
        LSSerializer lsSerializer = domImplementationLS.createLSSerializer();
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   202
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   203
        // get configuration
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   204
        DOMConfiguration domConfiguration = lsSerializer.getDomConfig();
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   205
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   206
        // query current configuration
30707
47b636cd6456 8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents: 27830
diff changeset
   207
        Boolean defaultFormatPrettyPrint = (Boolean) domConfiguration.getParameter(DOM_FORMAT_PRETTY_PRINT);
47b636cd6456 8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents: 27830
diff changeset
   208
        Boolean canSetFormatPrettyPrintFalse = (Boolean) domConfiguration.canSetParameter(DOM_FORMAT_PRETTY_PRINT, Boolean.FALSE);
47b636cd6456 8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents: 27830
diff changeset
   209
        Boolean canSetFormatPrettyPrintTrue = (Boolean) domConfiguration.canSetParameter(DOM_FORMAT_PRETTY_PRINT, Boolean.TRUE);
27830
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   210
30707
47b636cd6456 8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents: 27830
diff changeset
   211
        System.out.println(DOM_FORMAT_PRETTY_PRINT + " default/can set false/can set true = " + defaultFormatPrettyPrint + "/"
27830
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   212
                + canSetFormatPrettyPrintFalse + "/" + canSetFormatPrettyPrintTrue);
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   213
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   214
        // test values
30707
47b636cd6456 8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents: 27830
diff changeset
   215
        Assert.assertEquals(defaultFormatPrettyPrint, Boolean.FALSE, "Default value of " + DOM_FORMAT_PRETTY_PRINT + " should be " + Boolean.FALSE);
27830
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   216
30707
47b636cd6456 8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents: 27830
diff changeset
   217
        Assert.assertEquals(canSetFormatPrettyPrintFalse, Boolean.TRUE, "Can set " + DOM_FORMAT_PRETTY_PRINT + " to " + Boolean.FALSE + " should be "
27830
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   218
                + Boolean.TRUE);
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   219
30707
47b636cd6456 8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents: 27830
diff changeset
   220
        Assert.assertEquals(canSetFormatPrettyPrintTrue, Boolean.TRUE, "Can set " + DOM_FORMAT_PRETTY_PRINT + " to " + Boolean.TRUE + " should be "
27830
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   221
                + Boolean.TRUE);
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   222
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   223
        // get default serialization
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   224
        String prettyPrintDefault = lsSerializer.writeToString(document);
30707
47b636cd6456 8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents: 27830
diff changeset
   225
        System.out.println("(default) " + DOM_FORMAT_PRETTY_PRINT + "==" + (Boolean) domConfiguration.getParameter(DOM_FORMAT_PRETTY_PRINT)
27830
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   226
                + ": \n\"" + prettyPrintDefault + "\"");
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   227
30707
47b636cd6456 8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents: 27830
diff changeset
   228
        Assert.assertEquals(XML_DOCUMENT_DEFAULT_PRINT, prettyPrintDefault, "Invalid serialization with default value, " + DOM_FORMAT_PRETTY_PRINT + "=="
47b636cd6456 8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents: 27830
diff changeset
   229
                + (Boolean) domConfiguration.getParameter(DOM_FORMAT_PRETTY_PRINT));
27830
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   230
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   231
        // configure LSSerializer to not format-pretty-print
30707
47b636cd6456 8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents: 27830
diff changeset
   232
        domConfiguration.setParameter(DOM_FORMAT_PRETTY_PRINT, Boolean.FALSE);
27830
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   233
        String prettyPrintFalse = lsSerializer.writeToString(document);
30707
47b636cd6456 8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents: 27830
diff changeset
   234
        System.out.println("(FALSE) " + DOM_FORMAT_PRETTY_PRINT + "==" + (Boolean) domConfiguration.getParameter(DOM_FORMAT_PRETTY_PRINT)
27830
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   235
                + ": \n\"" + prettyPrintFalse + "\"");
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   236
30707
47b636cd6456 8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents: 27830
diff changeset
   237
        Assert.assertEquals(XML_DOCUMENT_DEFAULT_PRINT, prettyPrintFalse, "Invalid serialization with FALSE value, " + DOM_FORMAT_PRETTY_PRINT + "=="
47b636cd6456 8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents: 27830
diff changeset
   238
                + (Boolean) domConfiguration.getParameter(DOM_FORMAT_PRETTY_PRINT));
27830
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   239
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   240
        // configure LSSerializer to format-pretty-print
30707
47b636cd6456 8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents: 27830
diff changeset
   241
        domConfiguration.setParameter(DOM_FORMAT_PRETTY_PRINT, Boolean.TRUE);
27830
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   242
        String prettyPrintTrue = lsSerializer.writeToString(document);
30707
47b636cd6456 8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents: 27830
diff changeset
   243
        System.out.println("(TRUE) " + DOM_FORMAT_PRETTY_PRINT + "==" + (Boolean) domConfiguration.getParameter(DOM_FORMAT_PRETTY_PRINT)
27830
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   244
                + ": \n\"" + prettyPrintTrue + "\"");
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   245
30707
47b636cd6456 8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents: 27830
diff changeset
   246
        Assert.assertEquals(XML_DOCUMENT_PRETTY_PRINT, prettyPrintTrue, "Invalid serialization with TRUE value, " + DOM_FORMAT_PRETTY_PRINT + "=="
47b636cd6456 8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents: 27830
diff changeset
   247
                + (Boolean) domConfiguration.getParameter(DOM_FORMAT_PRETTY_PRINT));
27830
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   248
    }
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   249
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   250
    @Test
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   251
    public void testXML11() {
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   252
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   253
        /**
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   254
         * XML 1.1 document to parse.
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   255
         */
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   256
        final String XML11_DOCUMENT = "<?xml version=\"1.1\" encoding=\"UTF-16\"?>\n" + "<hello>" + "world" + "<child><children/><children/></child>"
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   257
                + "</hello>";
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   258
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   259
        /**JDK-8035467
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   260
         * no newline in default output
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   261
         */
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   262
        final String XML11_DOCUMENT_OUTPUT =
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   263
                "<?xml version=\"1.1\" encoding=\"UTF-16\"?>"
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   264
                + "<hello>"
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   265
                + "world"
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   266
                + "<child><children/><children/></child>"
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   267
                + "</hello>";
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   268
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   269
        // it all begins with a Document
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   270
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   271
        DocumentBuilder documentBuilder = null;
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   272
        try {
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   273
            documentBuilder = documentBuilderFactory.newDocumentBuilder();
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   274
        } catch (ParserConfigurationException parserConfigurationException) {
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   275
            parserConfigurationException.printStackTrace();
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   276
            Assert.fail(parserConfigurationException.toString());
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   277
        }
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   278
        Document document = null;
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   279
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   280
        StringReader stringReader = new StringReader(XML11_DOCUMENT);
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   281
        InputSource inputSource = new InputSource(stringReader);
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   282
        try {
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   283
            document = documentBuilder.parse(inputSource);
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   284
        } catch (SAXException saxException) {
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   285
            saxException.printStackTrace();
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   286
            Assert.fail(saxException.toString());
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   287
        } catch (IOException ioException) {
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   288
            ioException.printStackTrace();
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   289
            Assert.fail(ioException.toString());
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   290
        }
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   291
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   292
        // query DOM Interfaces to get to a LSSerializer
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   293
        DOMImplementation domImplementation = documentBuilder.getDOMImplementation();
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   294
        DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation;
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   295
        LSSerializer lsSerializer = domImplementationLS.createLSSerializer();
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   296
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   297
        // get default serialization
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   298
        String defaultSerialization = lsSerializer.writeToString(document);
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   299
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   300
        System.out.println("XML 1.1 serialization = \"" + defaultSerialization + "\"");
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   301
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   302
        // output should == input
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   303
        Assert.assertEquals(XML11_DOCUMENT_OUTPUT, defaultSerialization, "Invalid serialization of XML 1.1 document: ");
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   304
    }
85b0d46b0104 8043084: XML JAXP unittest co-location
joehw
parents:
diff changeset
   305
}