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