|
| 1 | +/* |
| 2 | + * Copyright (c) 2019, 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 | + |
| 24 | +package sax; |
| 25 | + |
| 26 | +import java.io.File; |
| 27 | +import java.io.StringReader; |
| 28 | +import javax.xml.parsers.SAXParser; |
| 29 | +import javax.xml.parsers.SAXParserFactory; |
| 30 | +import org.testng.Assert; |
| 31 | +import org.testng.annotations.DataProvider; |
| 32 | +import org.testng.annotations.Listeners; |
| 33 | +import org.testng.annotations.Test; |
| 34 | +import org.xml.sax.ContentHandler; |
| 35 | +import org.xml.sax.InputSource; |
| 36 | +import org.xml.sax.SAXException; |
| 37 | +import org.xml.sax.XMLReader; |
| 38 | +import org.xml.sax.helpers.DefaultHandler; |
| 39 | + |
| 40 | + |
| 41 | +/* |
| 42 | + * @test |
| 43 | + * @bug 8230814 |
| 44 | + * @run testng sax.DeclarationTest |
| 45 | + * @summary Test SAX Parser's handling of XML Declarations. |
| 46 | + */ |
| 47 | +public class DeclarationTest { |
| 48 | + static String SRC_DIR = System.getProperty("test.src"); |
| 49 | + final static String XML_NO_DECLARATION = "<a>abc</a>"; |
| 50 | + final static String XML_NO_STANDALONE = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?><a>abc</a>"; |
| 51 | + final static String XML_STANDALONE_N = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"no\" ?><a>abc</a>"; |
| 52 | + final static String XML_STANDALONE_Y = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"yes\" ?><a>abc</a>"; |
| 53 | + |
| 54 | + /** |
| 55 | + * Provides XML strings for testing XML declaration. |
| 56 | + * |
| 57 | + * Fields: |
| 58 | + * XML string, expected version, encoding and standalone strings |
| 59 | + * |
| 60 | + * @return data array for the test |
| 61 | + * @throws Exception |
| 62 | + */ |
| 63 | + @DataProvider(name = "default") |
| 64 | + public Object[][] forDefaultHandler() throws Exception { |
| 65 | + return new Object[][] { |
| 66 | + { XML_NO_DECLARATION, null, null, null}, |
| 67 | + { XML_NO_STANDALONE, null, null, null}, |
| 68 | + { XML_STANDALONE_Y, null, null, null}, |
| 69 | + { XML_STANDALONE_N, null, null, null}, |
| 70 | + }; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Provides XML strings for testing XML declaration. |
| 75 | + * |
| 76 | + * Fields: |
| 77 | + * XML string, expected version, encoding and standalone strings |
| 78 | + * |
| 79 | + * @return data array for the test |
| 80 | + * @throws Exception |
| 81 | + */ |
| 82 | + @DataProvider(name = "sax-data") |
| 83 | + public Object[][] xmlSAXData() throws Exception { |
| 84 | + return new Object[][] { |
| 85 | + { XML_NO_DECLARATION, null, null, null}, |
| 86 | + { XML_NO_STANDALONE, "1.0", "ISO-8859-1", null}, |
| 87 | + { XML_STANDALONE_Y, "1.0", "ISO-8859-1", "yes"}, |
| 88 | + { XML_STANDALONE_N, "1.0", "ISO-8859-1", "no"}, |
| 89 | + }; |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + /** |
| 94 | + * Provides XML files for testing XML declaration. |
| 95 | + * |
| 96 | + * Fields: |
| 97 | + * Source files, expected version, encoding and standalone strings |
| 98 | + * |
| 99 | + * @return data array for the test |
| 100 | + * @throws Exception |
| 101 | + */ |
| 102 | + @DataProvider(name = "sax-data-files") |
| 103 | + public Object[][] xmlSAXDataFiles() throws Exception { |
| 104 | + return new Object[][] { |
| 105 | + //the source contains no declaration |
| 106 | + { new File(SRC_DIR + "/../transform/SourceTest.xml"), null, null, null}, |
| 107 | + //<?xml version="1.0" encoding="UTF-8"?> |
| 108 | + { new File(SRC_DIR + "/toys.xml"), "1.0", "UTF-8", null}, |
| 109 | + //<?xml version="1.0" encoding="UTF-8" standalone="no"?> |
| 110 | + { new File(SRC_DIR + "/../dom/ElementTraversal.xml"), "1.0", "UTF-8", "no"}, |
| 111 | + // <?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 112 | + { new File(SRC_DIR + "/../validation/Bug6449797.xsd"), "1.0", "UTF-8", "yes"}, |
| 113 | + //<?xml version="1.0" standalone="no" ?> |
| 114 | + { new File(SRC_DIR + "/../transform/5368141.xml"), "1.0", null, "no"}, |
| 115 | + |
| 116 | + //<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?> |
| 117 | + { new File(SRC_DIR + "/../transform/Bug6206491.xml"), "1.0", "ISO-8859-1", "no"}, |
| 118 | + //<?xml version="1.0" encoding="ISO-8859-1"?> |
| 119 | + { new File(SRC_DIR + "/../transform/Bug6206491.xsl"), "1.0", "ISO-8859-1", null}, |
| 120 | + |
| 121 | + }; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Verifies that the default handler does nothing. |
| 126 | + * @param xml xml string |
| 127 | + * @param version expected version string |
| 128 | + * @param encoding expected encoding string |
| 129 | + * @param standalone expected standalone string |
| 130 | + * @throws Exception if the test fails |
| 131 | + */ |
| 132 | + @Test(dataProvider = "default") |
| 133 | + public void testDefault(String xml, String version, String encoding, String standalone) |
| 134 | + throws Exception { |
| 135 | + DefaultImpl h = new DefaultImpl(); |
| 136 | + parseAndVerify(xml, h, version, encoding, standalone); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Verifies that the SAX Parser returns the information of XML declaration |
| 141 | + * through the ContentHandler interface. |
| 142 | + * @param xml xml string |
| 143 | + * @param version expected version string |
| 144 | + * @param encoding expected encoding string |
| 145 | + * @param standalone expected standalone string |
| 146 | + * @throws Exception if the test fails |
| 147 | + */ |
| 148 | + @Test(dataProvider = "sax-data") |
| 149 | + public void test(String xml, String version, String encoding, String standalone) |
| 150 | + throws Exception { |
| 151 | + NewMethodImpl h = new NewMethodImpl(); |
| 152 | + parseAndVerify(xml, h, version, encoding, standalone); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Verifies that the SAX Parser returns the information of XML declaration |
| 157 | + * through the ContentHandler interface. |
| 158 | + * @param xml xml files |
| 159 | + * @param version expected version string |
| 160 | + * @param encoding expected encoding string |
| 161 | + * @param standalone expected standalone string |
| 162 | + * @throws Exception if the test fails |
| 163 | + */ |
| 164 | + @Test(dataProvider = "sax-data-files") |
| 165 | + public void testFiles(File xml, String version, String encoding, String standalone) |
| 166 | + throws Exception { |
| 167 | + SAXParser parser = SAXParserFactory.newDefaultInstance().newSAXParser(); |
| 168 | + NewMethodImpl h = new NewMethodImpl(); |
| 169 | + parser.parse(xml, h); |
| 170 | + Assert.assertEquals(h.version, version); |
| 171 | + Assert.assertEquals(h.encoding, encoding); |
| 172 | + Assert.assertEquals(h.standalone, standalone); |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Verifies the ContentHandler's XML Declaration feature by parsing an XML |
| 177 | + * string content. |
| 178 | + * @param xml xml string |
| 179 | + * @param version expected version string |
| 180 | + * @param encoding expected encoding string |
| 181 | + * @param standalone expected standalone string |
| 182 | + * @throws Exception if the test fails |
| 183 | + */ |
| 184 | + private void parseAndVerify(String xml, DefaultImpl h, |
| 185 | + String version, String encoding, String standalone) |
| 186 | + throws Exception { |
| 187 | + XMLReader r = SAXParserFactory.newDefaultInstance().newSAXParser().getXMLReader(); |
| 188 | + r.setContentHandler(h); |
| 189 | + r.parse(new InputSource(new StringReader(xml))); |
| 190 | + Assert.assertEquals(h.version, version); |
| 191 | + Assert.assertEquals(h.encoding, encoding); |
| 192 | + Assert.assertEquals(h.standalone, standalone); |
| 193 | + } |
| 194 | + |
| 195 | + class DefaultImpl extends DefaultHandler{ |
| 196 | + boolean startDocumentInvoked = false; |
| 197 | + String version, encoding, standalone; |
| 198 | + |
| 199 | + public void startDocument() throws SAXException { |
| 200 | + super.startDocument(); |
| 201 | + startDocumentInvoked = true; |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + class NewMethodImpl extends DefaultImpl { |
| 206 | + |
| 207 | + public void startDocument() throws SAXException { |
| 208 | + super.startDocument(); |
| 209 | + } |
| 210 | + |
| 211 | + @Override |
| 212 | + public void declaration(String version, String encoding, String standalone) |
| 213 | + throws SAXException |
| 214 | + { |
| 215 | + super.declaration(version, encoding, standalone); |
| 216 | + Assert.assertTrue(startDocumentInvoked, "declaration follows startDocument"); |
| 217 | + this.version = version; |
| 218 | + this.encoding = encoding; |
| 219 | + this.standalone = standalone; |
| 220 | + } |
| 221 | + } |
| 222 | +} |
0 commit comments