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.InputStreamReader;
  28 
  29 import javax.xml.parsers.SAXParserFactory;
  30 import javax.xml.transform.stream.StreamSource;
  31 import javax.xml.validation.Schema;
  32 import javax.xml.validation.SchemaFactory;
  33 import javax.xml.validation.Validator;
  34 import javax.xml.validation.ValidatorHandler;
  35 
  36 import org.testng.annotations.BeforeMethod;
  37 import org.testng.annotations.Listeners;
  38 import org.testng.annotations.Test;
  39 import org.xml.sax.InputSource;
  40 import org.xml.sax.XMLReader;
  41 import org.xml.sax.helpers.DefaultHandler;
  42 
  43 /*
  44  * @bug 5011500
  45  * @summary Test ValidatorHanlder and Validator can work for the xml document.
  46  */
  47 @Listeners({jaxp.library.BasePolicy.class})
  48 public class Bug5011500 {
  49 
  50     public static final String XSD = "<?xml version='1.0'?>\n" + "<schema xmlns='http://www.w3.org/2001/XMLSchema'\n" + "        xmlns:test='jaxp13_test'\n"
  51             + "        targetNamespace='jaxp13_test'>\n" + "    <element name='test'>\n" + "        <complexType>\n" + "            <sequence>\n"
  52             + "                <element name='child' type='string'/>\n" + "            </sequence>\n" + "            <attribute name='id' type='ID'/>\n"
  53             + "        </complexType>\n" + "    </element>\n" + "</schema>\n";
  54 
  55     public static final String XML = "<?xml version='1.0'?>\n" + "<?test v01?>\n" + "<ns:test xmlns:ns='jaxp13_test' id='i001'>\n"
  56             + "  <child>123abc</child>\n" + "</ns:test>\n";
  57 
  58     private ValidatorHandler validatorHandler;
  59     private Validator validator;
  60 
  61     private XMLReader createXMLReader() throws Exception {
  62         SAXParserFactory parserFactory = SAXParserFactory.newInstance();
  63         parserFactory.setNamespaceAware(true);
  64 
  65         return parserFactory.newSAXParser().getXMLReader();
  66     }
  67 
  68     private void parse(XMLReader xmlReader, String xml) throws Exception {
  69         InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(xml.getBytes()));
  70         InputSource inSource = new InputSource(reader);
  71 
  72         xmlReader.parse(inSource);
  73     }
  74 
  75     @BeforeMethod
  76     public void setUp() throws Exception {
  77         SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
  78 
  79         InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(XSD.getBytes()));
  80         StreamSource xsdSource = new StreamSource(reader);
  81 
  82         Schema schema = schemaFactory.newSchema(xsdSource);
  83 
  84         this.validatorHandler = schema.newValidatorHandler();
  85         this.validator = schema.newValidator();
  86     }
  87 
  88     @Test
  89     public void test1() throws Exception {
  90         DefaultHandler contentHandler = new DefaultHandler();
  91         validatorHandler.setContentHandler(contentHandler);
  92         validatorHandler.setErrorHandler(contentHandler);
  93 
  94         XMLReader xmlReader = createXMLReader();
  95         xmlReader.setContentHandler(validatorHandler);
  96         parse(xmlReader, XML);
  97     }
  98 
  99     @Test
 100     public void test2() throws Exception {
 101         InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(XML.getBytes()));
 102         StreamSource xmlSource = new StreamSource(reader);
 103 
 104         validator.validate(xmlSource);
 105     }
 106 }