Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/xml/jaxp/parsers/8073385/BadExceptionMessageTest.java
38860 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 807338526* @summary test that invalid XML character exception string contains27* information about character value, element and attribute names28* @run testng/othervm BadExceptionMessageTest29*/3031import java.io.StringReader;32import java.util.Locale;33import javax.xml.parsers.DocumentBuilderFactory;34import javax.xml.parsers.DocumentBuilder;35import org.xml.sax.SAXException;36import org.xml.sax.InputSource;3738import org.testng.annotations.AfterClass;39import org.testng.annotations.BeforeClass;40import org.testng.annotations.DataProvider;41import org.testng.annotations.Test;42import static org.testng.Assert.assertTrue;4344public class BadExceptionMessageTest {4546private Locale defLoc;4748@BeforeClass49private void setup() {50defLoc = Locale.getDefault();51Locale.setDefault(Locale.ENGLISH);52}5354@AfterClass55private void cleanup() {56Locale.setDefault(defLoc);57}5859@DataProvider(name = "illegalCharactersData")60public static Object[][] illegalCharactersData() {61return new Object[][]{62{0x00},63{0xFFFE},64{0xFFFF}65};66}6768@Test(dataProvider = "illegalCharactersData")69public void test(int character) throws Exception {70// Construct the XML document as a String71int[] cps = new int[]{character};72String txt = new String(cps, 0, cps.length);73String inxml = "<topElement attTest=\'" + txt + "\'/>";74String exceptionText = "NO EXCEPTION OBSERVED";75String hexString = "0x" + Integer.toHexString(character);7677DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();78dbf.setNamespaceAware(true);79dbf.setValidating(false);80DocumentBuilder db = dbf.newDocumentBuilder();81InputSource isrc = new InputSource(new StringReader(inxml));8283try {84db.parse(isrc);85} catch (SAXException e) {86exceptionText = e.toString();87}88System.out.println("Got Exception:" + exceptionText);89assertTrue(exceptionText.contains("attribute \"attTest\""));90assertTrue(exceptionText.contains("element is \"topElement\""));91assertTrue(exceptionText.contains("Unicode: " + hexString));92}93}949596