Bug5011500.java revision 997:540334ae53fe
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.ByteArrayInputStream;
27import java.io.InputStreamReader;
28
29import javax.xml.parsers.SAXParserFactory;
30import javax.xml.transform.stream.StreamSource;
31import javax.xml.validation.Schema;
32import javax.xml.validation.SchemaFactory;
33import javax.xml.validation.Validator;
34import javax.xml.validation.ValidatorHandler;
35
36import org.testng.annotations.BeforeMethod;
37import org.testng.annotations.Listeners;
38import org.testng.annotations.Test;
39import org.xml.sax.InputSource;
40import org.xml.sax.XMLReader;
41import org.xml.sax.helpers.DefaultHandler;
42
43/*
44 * @test
45 * @bug 5011500
46 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
47 * @run testng/othervm -DrunSecMngr=true validation.Bug5011500
48 * @run testng/othervm validation.Bug5011500
49 * @summary Test ValidatorHanlder and Validator can work for the xml document.
50 */
51@Listeners({jaxp.library.BasePolicy.class})
52public class Bug5011500 {
53
54    public static final String XSD = "<?xml version='1.0'?>\n" + "<schema xmlns='http://www.w3.org/2001/XMLSchema'\n" + "        xmlns:test='jaxp13_test'\n"
55            + "        targetNamespace='jaxp13_test'>\n" + "    <element name='test'>\n" + "        <complexType>\n" + "            <sequence>\n"
56            + "                <element name='child' type='string'/>\n" + "            </sequence>\n" + "            <attribute name='id' type='ID'/>\n"
57            + "        </complexType>\n" + "    </element>\n" + "</schema>\n";
58
59    public static final String XML = "<?xml version='1.0'?>\n" + "<?test v01?>\n" + "<ns:test xmlns:ns='jaxp13_test' id='i001'>\n"
60            + "  <child>123abc</child>\n" + "</ns:test>\n";
61
62    private ValidatorHandler validatorHandler;
63    private Validator validator;
64
65    private XMLReader createXMLReader() throws Exception {
66        SAXParserFactory parserFactory = SAXParserFactory.newInstance();
67        parserFactory.setNamespaceAware(true);
68
69        return parserFactory.newSAXParser().getXMLReader();
70    }
71
72    private void parse(XMLReader xmlReader, String xml) throws Exception {
73        InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(xml.getBytes()));
74        InputSource inSource = new InputSource(reader);
75
76        xmlReader.parse(inSource);
77    }
78
79    @BeforeMethod
80    public void setUp() throws Exception {
81        SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
82
83        InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(XSD.getBytes()));
84        StreamSource xsdSource = new StreamSource(reader);
85
86        Schema schema = schemaFactory.newSchema(xsdSource);
87
88        this.validatorHandler = schema.newValidatorHandler();
89        this.validator = schema.newValidator();
90    }
91
92    @Test
93    public void test1() throws Exception {
94        DefaultHandler contentHandler = new DefaultHandler();
95        validatorHandler.setContentHandler(contentHandler);
96        validatorHandler.setErrorHandler(contentHandler);
97
98        XMLReader xmlReader = createXMLReader();
99        xmlReader.setContentHandler(validatorHandler);
100        parse(xmlReader, XML);
101    }
102
103    @Test
104    public void test2() throws Exception {
105        InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(XML.getBytes()));
106        StreamSource xmlSource = new StreamSource(reader);
107
108        validator.validate(xmlSource);
109    }
110}
111