1 /*
2  * Copyright (c) 2015, 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 /**
25  * @test
26  * @bug 8073385
27  * @summary test that invalid XML character exception string contains
28  *     information about character value, element and attribute names
29  * @run testng/othervm BadExceptionMessageTest
30  */
31 
32 import java.io.StringReader;
33 import java.util.Locale;
34 import javax.xml.parsers.DocumentBuilderFactory;
35 import javax.xml.parsers.DocumentBuilder;
36 import org.xml.sax.SAXException;
37 import org.xml.sax.InputSource;
38 
39 import org.testng.annotations.AfterClass;
40 import org.testng.annotations.BeforeClass;
41 import org.testng.annotations.DataProvider;
42 import org.testng.annotations.Test;
43 import static org.testng.Assert.assertTrue;
44 
45 public class BadExceptionMessageTest {
46 
47     private Locale defLoc;
48 
49     @BeforeClass
setup()50     private void setup() {
51         defLoc = Locale.getDefault();
52         Locale.setDefault(Locale.ENGLISH);
53     }
54 
55     @AfterClass
cleanup()56     private void cleanup() {
57         Locale.setDefault(defLoc);
58     }
59 
60     @DataProvider(name = "illegalCharactersData")
illegalCharactersData()61     public static Object[][] illegalCharactersData() {
62         return new Object[][]{
63             {0x00},
64             {0xFFFE},
65             {0xFFFF}
66         };
67     }
68 
69     @Test(dataProvider = "illegalCharactersData")
test(int character)70     public void test(int character) throws Exception {
71         // Construct the XML document as a String
72         int[] cps = new int[]{character};
73         String txt = new String(cps, 0, cps.length);
74         String inxml = "<topElement attTest=\'" + txt + "\'/>";
75         String exceptionText = "NO EXCEPTION OBSERVED";
76         String hexString = "0x" + Integer.toHexString(character);
77 
78         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
79         dbf.setNamespaceAware(true);
80         dbf.setValidating(false);
81         DocumentBuilder db = dbf.newDocumentBuilder();
82         InputSource isrc = new InputSource(new StringReader(inxml));
83 
84         try {
85             db.parse(isrc);
86         } catch (SAXException e) {
87             exceptionText = e.toString();
88         }
89         System.out.println("Got Exception:" + exceptionText);
90         assertTrue(exceptionText.contains("attribute \"attTest\""));
91         assertTrue(exceptionText.contains("element is \"topElement\""));
92         assertTrue(exceptionText.contains("Unicode: " + hexString));
93     }
94 }
95