Bug6520131.java revision 997:540334ae53fe
114965Sjoerg/*
214965Sjoerg * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
314965Sjoerg * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
414965Sjoerg *
514965Sjoerg * This code is free software; you can redistribute it and/or modify it
614965Sjoerg * under the terms of the GNU General Public License version 2 only, as
714965Sjoerg * published by the Free Software Foundation.
814965Sjoerg *
914965Sjoerg * This code is distributed in the hope that it will be useful, but WITHOUT
1014965Sjoerg * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1114965Sjoerg * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1214965Sjoerg * version 2 for more details (a copy is included in the LICENSE file that
1314965Sjoerg * accompanied this code).
1414965Sjoerg *
1514965Sjoerg * You should have received a copy of the GNU General Public License version
1614965Sjoerg * 2 along with this work; if not, write to the Free Software Foundation,
1714965Sjoerg * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1814965Sjoerg *
1914965Sjoerg * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2014965Sjoerg * or visit www.oracle.com if you need additional information or have any
2114965Sjoerg * questions.
2214965Sjoerg */
2314965Sjoerg
2414965Sjoergpackage dom;
2514965Sjoerg
2650476Speterimport javax.xml.parsers.DocumentBuilder;
2714965Sjoergimport javax.xml.parsers.DocumentBuilderFactory;
2814965Sjoergimport javax.xml.parsers.ParserConfigurationException;
29240506Seadler
30206622Suqsimport org.testng.Assert;
3114965Sjoergimport org.testng.annotations.Listeners;
3214965Sjoergimport org.testng.annotations.Test;
3314965Sjoergimport org.w3c.dom.DOMConfiguration;
3414965Sjoergimport org.w3c.dom.DOMError;
3514965Sjoergimport org.w3c.dom.DOMErrorHandler;
3684306Sruimport org.w3c.dom.Document;
3714965Sjoergimport org.w3c.dom.Element;
3814965Sjoergimport org.w3c.dom.Text;
3914965Sjoerg
4014965Sjoerg/*
4114965Sjoerg * @test
4214965Sjoerg * @bug 6520131
43131530Sru * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
44131530Sru * @run testng/othervm -DrunSecMngr=true dom.Bug6520131
4568962Sru * @run testng/othervm dom.Bug6520131
4614965Sjoerg * @summary Test DOMErrorHandler reports an error for invalid character.
4714965Sjoerg */
4814965Sjoerg@Listeners({jaxp.library.BasePolicy.class})
4914965Sjoergpublic class Bug6520131 {
5014965Sjoerg
5114965Sjoerg    @Test
5214965Sjoerg    public void test() {
5314965Sjoerg        String string = new String("\u0001");
54131530Sru
55131530Sru        try {
5614965Sjoerg            // create document
5714965Sjoerg            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
5814965Sjoerg            DocumentBuilder documentBuilder = dbf.newDocumentBuilder();
5914965Sjoerg            Document document = documentBuilder.newDocument();
6014965Sjoerg
6114965Sjoerg            DOMConfiguration domConfig = document.getDomConfig();
62131530Sru            domConfig.setParameter("well-formed", Boolean.TRUE);
63131530Sru            domConfig.setParameter("error-handler", new DOMErrorHandler() {
6414965Sjoerg                public boolean handleError(DOMError e) {
6514965Sjoerg                    throw new RuntimeException(e.getMessage());
6614965Sjoerg                }
6714965Sjoerg            });
6814965Sjoerg
6914965Sjoerg            // add text element
7014965Sjoerg            Element textElement = document.createElementNS("", "Text");
71131530Sru            Text text = document.createTextNode(string);
72131530Sru            textElement.appendChild(text);
7314965Sjoerg            document.appendChild(textElement);
7414965Sjoerg
75131530Sru            // normalize document
76131530Sru            document.normalizeDocument();
7714965Sjoerg
7814965Sjoerg            Assert.fail("Invalid character exception not thrown");
79131530Sru        } catch (ParserConfigurationException e) {
80131530Sru            Assert.fail("Unable to configure parser");
81131530Sru        } catch (RuntimeException e) {
82131530Sru            // This exception is expected!
83141846Sru        }
8414965Sjoerg    }
85131530Sru}
86131530Sru