1/*
2 * Copyright (c) 2014, 2016, 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
24package validation;
25
26import java.io.IOException;
27import java.io.StringReader;
28
29import javax.xml.parsers.ParserConfigurationException;
30import javax.xml.parsers.SAXParserFactory;
31import javax.xml.transform.stream.StreamSource;
32import javax.xml.validation.Schema;
33import javax.xml.validation.SchemaFactory;
34import javax.xml.validation.ValidatorHandler;
35
36import org.testng.Assert;
37import org.testng.annotations.Listeners;
38import org.testng.annotations.Test;
39import org.xml.sax.InputSource;
40import org.xml.sax.SAXException;
41import org.xml.sax.XMLReader;
42import org.xml.sax.helpers.DefaultHandler;
43
44/*
45 * @test
46 * @bug 4969042
47 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
48 * @run testng/othervm -DrunSecMngr=true validation.Bug4969042
49 * @run testng/othervm validation.Bug4969042
50 * @summary Test ValidationHandler shall invoke ignorableWhitespace() of the
51 * user-defined ContentHandler once the validator detects any ignorable whitespaces.
52 */
53@Listeners({jaxp.library.BasePolicy.class})
54public class Bug4969042 {
55
56    public static final String XSD = "<?xml version='1.0'?>\n" + "<schema xmlns='http://www.w3.org/2001/XMLSchema'\n" + "        xmlns:test='jaxp13_test'\n"
57            + "        targetNamespace='jaxp13_test'\n" + "        elementFormDefault='qualified'>\n" + "    <element name='test'>\n"
58            + "        <complexType>\n" + "            <sequence>\n" + "                <element name='child' type='string'/>\n" + "            </sequence>\n"
59            + "        </complexType>\n" + "    </element>\n" + "</schema>\n";
60
61    public static final String XML = "<?xml version='1.0'?>\n" + "<ns:test xmlns:ns='jaxp13_test'>\n" + "  <ns:child>\n" + "      123abc\n" + "  </ns:child>\n"
62            + "</ns:test>\n";
63
64    private ValidatorHandler createValidatorHandler(String xsd) throws SAXException {
65        SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
66
67        StringReader reader = new StringReader(xsd);
68        StreamSource xsdSource = new StreamSource(reader);
69
70        Schema schema = schemaFactory.newSchema(xsdSource);
71        return schema.newValidatorHandler();
72    }
73
74    private XMLReader createXMLReader() throws ParserConfigurationException, SAXException {
75        SAXParserFactory parserFactory = SAXParserFactory.newInstance();
76        if (!parserFactory.isNamespaceAware()) {
77            parserFactory.setNamespaceAware(true);
78        }
79
80        return parserFactory.newSAXParser().getXMLReader();
81    }
82
83    private void parse(XMLReader xmlReader, String xml) throws SAXException, IOException {
84        StringReader reader = new StringReader(xml);
85        InputSource inSource = new InputSource(reader);
86
87        xmlReader.parse(inSource);
88    }
89
90    @Test
91    public void test() throws SAXException, ParserConfigurationException, IOException {
92        XMLReader xmlReader = createXMLReader();
93        ValidatorHandler validatorHandler = createValidatorHandler(XSD);
94        xmlReader.setContentHandler(validatorHandler);
95
96        final boolean[] invoked = { false, false };
97        DefaultHandler contentHandler = new DefaultHandler() {
98            public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
99                invoked[0] = true;
100            }
101
102            public void characters(char[] ch, int start, int length) throws SAXException {
103                invoked[1] = true;
104            }
105        };
106        validatorHandler.setContentHandler(contentHandler);
107
108        parse(xmlReader, XML);
109
110        Assert.assertTrue(invoked[0], "Method ignorableWhitespace() was not invoked.");
111        Assert.assertTrue(invoked[1], "Method characters() was not invoked.");
112    }
113}
114