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