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