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 javax.xml.XMLConstants; 27import javax.xml.parsers.DocumentBuilder; 28import javax.xml.parsers.DocumentBuilderFactory; 29import javax.xml.transform.Source; 30import javax.xml.transform.dom.DOMSource; 31import javax.xml.transform.stream.StreamSource; 32import javax.xml.validation.Schema; 33import javax.xml.validation.SchemaFactory; 34import javax.xml.validation.Validator; 35 36import org.testng.Assert; 37import org.testng.annotations.Listeners; 38import org.testng.annotations.Test; 39import org.w3c.dom.Document; 40import org.xml.sax.SAXException; 41 42/* 43 * @test 44 * @bug 6526547 45 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest 46 * @run testng/othervm -DrunSecMngr=true validation.Bug6526547 47 * @run testng/othervm validation.Bug6526547 48 * @summary Test document parsed without setting NamespaceAware can be validated with a Schema. 49 */ 50@Listeners({jaxp.library.FilePolicy.class}) 51public class Bug6526547 { 52 53 @Test 54 public void test() { 55 try { 56 // parse an XML document into a DOM tree 57 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 58 DocumentBuilder parser = dbf.newDocumentBuilder(); 59 Assert.assertFalse(parser.isNamespaceAware()); 60 Document document = parser.parse(getClass().getResourceAsStream("Bug6526547.xml")); 61 62 // create a SchemaFactory capable of understanding WXS schemas 63 SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 64 65 // load a WXS schema, represented by a Schema instance 66 Source schemaFile = new StreamSource(getClass().getResourceAsStream("Bug6526547.xsd")); 67 Schema schema = factory.newSchema(schemaFile); 68 69 // create a Validator instance, which can be used to validate an 70 // instance document 71 Validator validator = schema.newValidator(); 72 73 // validate the DOM tree 74 try { 75 validator.validate(new DOMSource(document)); 76 } catch (SAXException e) { 77 e.printStackTrace(); 78 Assert.fail("Document is reported as invalid but it is not!"); 79 } 80 } catch (Exception e) { 81 Assert.fail("Unable to configure validator"); 82 } 83 } 84} 85