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.IOException;
28import java.io.InputStreamReader;
29
30import javax.xml.parsers.ParserConfigurationException;
31import javax.xml.parsers.SAXParserFactory;
32import javax.xml.transform.stream.StreamSource;
33import javax.xml.validation.Schema;
34import javax.xml.validation.SchemaFactory;
35import javax.xml.validation.TypeInfoProvider;
36import javax.xml.validation.ValidatorHandler;
37
38import org.testng.Assert;
39import org.testng.annotations.Listeners;
40import org.testng.annotations.Test;
41import org.w3c.dom.TypeInfo;
42import org.xml.sax.Attributes;
43import org.xml.sax.InputSource;
44import org.xml.sax.SAXException;
45import org.xml.sax.XMLReader;
46import org.xml.sax.helpers.DefaultHandler;
47
48/*
49 * @test
50 * @bug 6509668
51 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
52 * @run testng/othervm -DrunSecMngr=true validation.Bug6509668
53 * @run testng/othervm validation.Bug6509668
54 * @summary Test TypeInfoProvider.getElementTypeInfo() for union type when startElement and endElement.
55 */
56@Listeners({jaxp.library.BasePolicy.class})
57public class Bug6509668 {
58
59    public static final String XSD = "<?xml version='1.0'?>\n" + "<schema xmlns='http://www.w3.org/2001/XMLSchema'\n"
60            + "  xmlns:ns='http://example.org/jaxp13_test'\n" + "    targetNamespace='http://example.org/jaxp13_test'\n" + "    elementFormDefault='qualified'>\n"
61            + "  <simpleType name='intOrString'>\n" + "    <union memberTypes='int string'/>\n" + "  </simpleType>\n"
62            + "  <element name='test' type='ns:intOrString'/>\n" + "</schema>\n";
63
64    public static final String XML = "<?xml version='1.0'?>\n" + "<ns:test xmlns:ns='http://example.org/jaxp13_test'>abc</ns:test>\n";
65
66    private ValidatorHandler createValidatorHandler(String xsd) throws SAXException {
67        SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
68
69        InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(xsd.getBytes()));
70        StreamSource xsdSource = new StreamSource(reader);
71
72        Schema schema = schemaFactory.newSchema(xsdSource);
73        return schema.newValidatorHandler();
74    }
75
76    private XMLReader createXMLReader() throws ParserConfigurationException, SAXException {
77        SAXParserFactory parserFactory = SAXParserFactory.newInstance();
78        if (!parserFactory.isNamespaceAware()) {
79            parserFactory.setNamespaceAware(true);
80        }
81
82        return parserFactory.newSAXParser().getXMLReader();
83    }
84
85    private void parse(XMLReader xmlReader, String xml) throws SAXException, IOException {
86        InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(xml.getBytes()));
87        InputSource inSource = new InputSource(reader);
88
89        xmlReader.parse(inSource);
90    }
91
92    @Test
93    public void testGetElementTypeInfo() throws ParserConfigurationException, SAXException, IOException {
94        XMLReader xmlReader;
95        xmlReader = createXMLReader();
96
97        final ValidatorHandler validatorHandler;
98        validatorHandler = createValidatorHandler(XSD);
99
100        xmlReader.setContentHandler(validatorHandler);
101
102        DefaultHandler handler = new DefaultHandler() {
103            public void startElement(String uri, String localName, String qName, Attributes attr) throws SAXException {
104                TypeInfoProvider infoProvider = null;
105                synchronized (validatorHandler) {
106                    infoProvider = validatorHandler.getTypeInfoProvider();
107                }
108                if (infoProvider == null) {
109                    throw new SAXException("Can't obtain TypeInfoProvider object.");
110                }
111
112                try {
113                    TypeInfo typeInfo = infoProvider.getElementTypeInfo();
114                    Assert.assertEquals(typeInfo.getTypeName(), "intOrString");
115                } catch (IllegalStateException e) {
116                    System.out.println(e);
117                    throw new SAXException("Unexpected IllegalStateException was thrown.");
118                }
119            }
120
121            public void endElement(String uri, String localName, String qName) throws SAXException {
122                TypeInfoProvider infoProvider = null;
123                synchronized (validatorHandler) {
124                    infoProvider = validatorHandler.getTypeInfoProvider();
125                }
126                if (infoProvider == null) {
127                    throw new SAXException("Can't obtain TypeInfoProvider object.");
128                }
129
130                try {
131                    TypeInfo typeInfo = infoProvider.getElementTypeInfo();
132                    Assert.assertEquals(typeInfo.getTypeName(), "string");
133                } catch (IllegalStateException e) {
134                    System.out.println(e);
135                    throw new SAXException("Unexpected IllegalStateException was thrown.");
136                }
137            }
138        };
139        validatorHandler.setContentHandler(handler);
140
141        parse(xmlReader, XML);
142    }
143}
144