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