test/jaxp/javax/xml/jaxp/unittest/sax/DeclarationTest.java
author joehw
Thu, 19 Sep 2019 16:53:17 +0000
changeset 58230 0ff1aeedc338
permissions -rw-r--r--
8230814: Enable SAX ContentHandler to handle XML Declaration Reviewed-by: lancea, dfuchs, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58230
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
     1
/*
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
     4
 *
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
     7
 * published by the Free Software Foundation.
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
     8
 *
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    13
 * accompanied this code).
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    14
 *
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    18
 *
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    21
 * questions.
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    22
 */
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    23
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    24
package sax;
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    25
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    26
import java.io.File;
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    27
import java.io.StringReader;
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    28
import javax.xml.parsers.SAXParser;
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    29
import javax.xml.parsers.SAXParserFactory;
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    30
import org.testng.Assert;
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    31
import org.testng.annotations.DataProvider;
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    32
import org.testng.annotations.Listeners;
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    33
import org.testng.annotations.Test;
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    34
import org.xml.sax.ContentHandler;
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    35
import org.xml.sax.InputSource;
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    36
import org.xml.sax.SAXException;
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    37
import org.xml.sax.XMLReader;
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    38
import org.xml.sax.helpers.DefaultHandler;
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    39
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    40
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    41
/*
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    42
 * @test
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    43
 * @bug 8230814
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    44
 * @run testng sax.DeclarationTest
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    45
 * @summary Test SAX Parser's handling of XML Declarations.
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    46
 */
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    47
public class DeclarationTest {
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    48
    static String SRC_DIR = System.getProperty("test.src");
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    49
    final static String XML_NO_DECLARATION = "<a>abc</a>";
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    50
    final static String XML_NO_STANDALONE = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?><a>abc</a>";
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    51
    final static String XML_STANDALONE_N = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"no\" ?><a>abc</a>";
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    52
    final static String XML_STANDALONE_Y = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"yes\" ?><a>abc</a>";
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    53
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    54
    /**
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    55
     * Provides XML strings for testing XML declaration.
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    56
     *
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    57
     * Fields:
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    58
     * XML string, expected version, encoding and standalone strings
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    59
     *
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    60
     * @return data array for the test
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    61
     * @throws Exception
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    62
     */
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    63
    @DataProvider(name = "default")
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    64
    public Object[][] forDefaultHandler() throws Exception {
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    65
        return new Object[][] {
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    66
            { XML_NO_DECLARATION, null, null, null},
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    67
            { XML_NO_STANDALONE, null, null, null},
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    68
            { XML_STANDALONE_Y, null, null, null},
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    69
            { XML_STANDALONE_N, null, null, null},
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    70
        };
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    71
    }
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    72
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    73
    /**
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    74
     * Provides XML strings for testing XML declaration.
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    75
     *
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    76
     * Fields:
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    77
     * XML string, expected version, encoding and standalone strings
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    78
     *
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    79
     * @return data array for the test
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    80
     * @throws Exception
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    81
     */
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    82
    @DataProvider(name = "sax-data")
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    83
    public Object[][] xmlSAXData() throws Exception {
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    84
        return new Object[][] {
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    85
            { XML_NO_DECLARATION, null, null, null},
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    86
            { XML_NO_STANDALONE, "1.0", "ISO-8859-1", null},
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    87
            { XML_STANDALONE_Y, "1.0", "ISO-8859-1", "yes"},
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    88
            { XML_STANDALONE_N, "1.0", "ISO-8859-1", "no"},
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    89
        };
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    90
    }
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    91
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    92
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    93
    /**
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    94
     * Provides XML files for testing XML declaration.
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    95
     *
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    96
     * Fields:
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    97
     * Source files, expected version, encoding and standalone strings
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    98
     *
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
    99
     * @return data array for the test
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   100
     * @throws Exception
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   101
     */
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   102
    @DataProvider(name = "sax-data-files")
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   103
    public Object[][] xmlSAXDataFiles() throws Exception {
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   104
        return new Object[][] {
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   105
            //the source contains no declaration
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   106
            { new File(SRC_DIR + "/../transform/SourceTest.xml"), null, null, null},
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   107
            //<?xml version="1.0" encoding="UTF-8"?>
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   108
            { new File(SRC_DIR + "/toys.xml"), "1.0", "UTF-8", null},
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   109
            //<?xml version="1.0" encoding="UTF-8" standalone="no"?>
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   110
            { new File(SRC_DIR + "/../dom/ElementTraversal.xml"), "1.0", "UTF-8", "no"},
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   111
            // <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   112
            { new File(SRC_DIR + "/../validation/Bug6449797.xsd"), "1.0", "UTF-8", "yes"},
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   113
            //<?xml version="1.0" standalone="no" ?>
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   114
            { new File(SRC_DIR + "/../transform/5368141.xml"), "1.0", null, "no"},
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   115
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   116
            //<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   117
            { new File(SRC_DIR + "/../transform/Bug6206491.xml"), "1.0", "ISO-8859-1", "no"},
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   118
            //<?xml version="1.0" encoding="ISO-8859-1"?>
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   119
            { new File(SRC_DIR + "/../transform/Bug6206491.xsl"), "1.0", "ISO-8859-1", null},
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   120
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   121
        };
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   122
    }
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   123
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   124
    /**
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   125
     * Verifies that the default handler does nothing.
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   126
     * @param xml xml string
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   127
     * @param version expected version string
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   128
     * @param encoding expected encoding string
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   129
     * @param standalone expected standalone string
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   130
     * @throws Exception if the test fails
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   131
     */
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   132
    @Test(dataProvider = "default")
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   133
    public void testDefault(String xml, String version, String encoding, String standalone)
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   134
            throws Exception {
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   135
        DefaultImpl h = new DefaultImpl();
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   136
        parseAndVerify(xml, h, version, encoding, standalone);
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   137
    }
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   138
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   139
    /**
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   140
     * Verifies that the SAX Parser returns the information of XML declaration
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   141
     * through the ContentHandler interface.
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   142
     * @param xml xml string
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   143
     * @param version expected version string
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   144
     * @param encoding expected encoding string
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   145
     * @param standalone expected standalone string
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   146
     * @throws Exception if the test fails
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   147
     */
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   148
    @Test(dataProvider = "sax-data")
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   149
    public void test(String xml, String version, String encoding, String standalone)
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   150
            throws Exception {
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   151
        NewMethodImpl h = new NewMethodImpl();
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   152
        parseAndVerify(xml, h, version, encoding, standalone);
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   153
    }
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   154
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   155
    /**
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   156
     * Verifies that the SAX Parser returns the information of XML declaration
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   157
     * through the ContentHandler interface.
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   158
     * @param xml xml files
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   159
     * @param version expected version string
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   160
     * @param encoding expected encoding string
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   161
     * @param standalone expected standalone string
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   162
     * @throws Exception if the test fails
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   163
     */
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   164
    @Test(dataProvider = "sax-data-files")
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   165
    public void testFiles(File xml, String version, String encoding, String standalone)
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   166
            throws Exception {
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   167
        SAXParser parser = SAXParserFactory.newDefaultInstance().newSAXParser();
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   168
        NewMethodImpl h = new NewMethodImpl();
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   169
        parser.parse(xml, h);
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   170
        Assert.assertEquals(h.version, version);
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   171
        Assert.assertEquals(h.encoding, encoding);
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   172
        Assert.assertEquals(h.standalone, standalone);
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   173
    }
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   174
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   175
    /**
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   176
     * Verifies the ContentHandler's XML Declaration feature by parsing an XML
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   177
     * string content.
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   178
     * @param xml xml string
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   179
     * @param version expected version string
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   180
     * @param encoding expected encoding string
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   181
     * @param standalone expected standalone string
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   182
     * @throws Exception if the test fails
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   183
     */
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   184
    private void parseAndVerify(String xml, DefaultImpl h,
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   185
            String version, String encoding, String standalone)
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   186
            throws Exception {
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   187
        XMLReader r = SAXParserFactory.newDefaultInstance().newSAXParser().getXMLReader();
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   188
        r.setContentHandler(h);
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   189
        r.parse(new InputSource(new StringReader(xml)));
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   190
        Assert.assertEquals(h.version, version);
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   191
        Assert.assertEquals(h.encoding, encoding);
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   192
        Assert.assertEquals(h.standalone, standalone);
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   193
    }
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   194
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   195
    class DefaultImpl extends DefaultHandler{
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   196
        boolean startDocumentInvoked = false;
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   197
        String version, encoding, standalone;
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   198
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   199
        public void startDocument() throws SAXException {
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   200
            super.startDocument();
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   201
            startDocumentInvoked = true;
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   202
        }
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   203
    }
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   204
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   205
    class NewMethodImpl extends DefaultImpl {
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   206
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   207
        public void startDocument() throws SAXException {
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   208
            super.startDocument();
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   209
        }
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   210
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   211
        @Override
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   212
        public void declaration(String version, String encoding, String standalone)
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   213
                throws SAXException
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   214
        {
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   215
            super.declaration(version, encoding, standalone);
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   216
            Assert.assertTrue(startDocumentInvoked, "declaration follows startDocument");
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   217
            this.version = version;
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   218
            this.encoding = encoding;
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   219
            this.standalone = standalone;
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   220
        }
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   221
    }
0ff1aeedc338 8230814: Enable SAX ContentHandler to handle XML Declaration
joehw
parents:
diff changeset
   222
}