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.StringReader; 27 28 import javax.xml.parsers.SAXParserFactory; 29 import javax.xml.transform.stream.StreamSource; 30 import javax.xml.validation.Schema; 31 import javax.xml.validation.SchemaFactory; 32 import javax.xml.validation.TypeInfoProvider; 33 import javax.xml.validation.ValidatorHandler; 34 35 import org.testng.Assert; 36 import org.testng.annotations.Listeners; 37 import org.testng.annotations.Test; 38 import org.xml.sax.InputSource; 39 import org.xml.sax.SAXException; 40 import org.xml.sax.XMLReader; 41 import org.xml.sax.helpers.DefaultHandler; 42 43 /* 44 * @test 45 * @bug 4969732 46 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest 47 * @run testng/othervm -DrunSecMngr=true validation.Bug4969732 48 * @run testng/othervm validation.Bug4969732 49 * @summary Test TypeInfoProvider.getElementTypeInfo() throws IllegalStateException 50 * in case the method is not called from method startElement() or endElement(). 51 */ 52 @Listeners({jaxp.library.BasePolicy.class}) 53 public class Bug4969732 { 54 55 public static final String XSD = "<?xml version='1.0'?>\n" + "<schema xmlns='http://www.w3.org/2001/XMLSchema'\n" + " xmlns:test='jaxp13_test'\n" 56 + " targetNamespace='jaxp13_test'\n" + " elementFormDefault='qualified'>\n" + " <element name='test' type='string'/>\n" 57 + "</schema>\n"; 58 59 public static final String XML = "<?xml version='1.0'?>\n" + "<ns:test xmlns:ns='jaxp13_test'>1234abc</ns:test>\n"; 60 61 private ValidatorHandler createValidatorHandler(String xsd) throws SAXException { 62 SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); 63 64 StringReader reader = new StringReader(xsd); 65 StreamSource xsdSource = new StreamSource(reader); 66 67 Schema schema = schemaFactory.newSchema(xsdSource); 68 return schema.newValidatorHandler(); 69 } 70 71 private XMLReader createXMLReader() throws Exception { 72 SAXParserFactory parserFactory = SAXParserFactory.newInstance(); 73 parserFactory.setNamespaceAware(true); 74 75 return parserFactory.newSAXParser().getXMLReader(); 76 } 77 78 private void parse(XMLReader xmlReader, String xml) throws Exception { 79 StringReader reader = new StringReader(xml); 80 InputSource inSource = new InputSource(reader); 81 82 xmlReader.parse(inSource); 83 } 84 85 @Test 86 public void test1() throws Exception { 87 XMLReader xmlReader = createXMLReader(); 88 final ValidatorHandler validatorHandler = createValidatorHandler(XSD); 89 xmlReader.setContentHandler(validatorHandler); 90 91 DefaultHandler handler = new DefaultHandler() { 92 public void characters(char[] ch, int start, int length) throws SAXException { 93 TypeInfoProvider infoProvider = null; 94 synchronized (validatorHandler) { 95 infoProvider = validatorHandler.getTypeInfoProvider(); 96 } 97 if (infoProvider == null) { 98 Assert.fail("Can't obtain TypeInfo object."); 99 } 100 101 try { 102 infoProvider.getElementTypeInfo(); 103 Assert.fail("IllegalStateException was not thrown."); 104 } catch (IllegalStateException e) { 105 // as expected 106 System.out.println("OK"); 107 } 108 } 109 }; 110 validatorHandler.setContentHandler(handler); 111 112 parse(xmlReader, XML); 113 } 114 }