1 /*
   2  * Copyright (c) 2019, 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 
  27 import java.io.File;
  28 import java.io.FileInputStream;
  29 import javax.xml.XMLConstants;
  30 import javax.xml.parsers.SAXParserFactory;
  31 import javax.xml.stream.XMLInputFactory;
  32 import javax.xml.stream.XMLStreamReader;
  33 import javax.xml.stream.events.XMLEvent;
  34 import javax.xml.transform.Source;
  35 import javax.xml.transform.sax.SAXSource;
  36 import javax.xml.transform.stream.StreamSource;
  37 import javax.xml.validation.Schema;
  38 import javax.xml.validation.SchemaFactory;
  39 import javax.xml.validation.Validator;
  40 import org.testng.annotations.DataProvider;
  41 import org.testng.annotations.Listeners;
  42 import org.testng.annotations.Test;
  43 import org.xml.sax.Attributes;
  44 import org.xml.sax.InputSource;
  45 import org.xml.sax.SAXException;
  46 import org.xml.sax.SAXParseException;
  47 import org.xml.sax.XMLFilter;
  48 import org.xml.sax.helpers.XMLFilterImpl;
  49 
  50 /*
  51  * @test
  52  * @bug 8220818 8176447
  53  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  54  * @run testng/othervm validation.ValidationTest
  55  * @summary Runs validations with schemas and sources
  56  */
  57 @Listeners({jaxp.library.FilePolicy.class})
  58 public class ValidationTest {
  59     static final String FILE_PATH = "files/";
  60     /*
  61      DataProvider: valid xml
  62      */
  63     @DataProvider(name = "valid")
  64     Object[][] getValid() {
  65         return new Object[][]{
  66             {"JDK8220818a.xsd", "JDK8220818a_Valid.xml"},
  67             {"JDK8220818a.xsd", "JDK8220818a_Valid1.xml"},
  68             {"JDK8220818b.xsd", "JDK8220818b_Valid.xml"},
  69         };
  70     }
  71 
  72     /*
  73      DataProvider: invalid xml
  74      */
  75     @DataProvider(name = "invalid")
  76     Object[][] getInvalid() {
  77         return new Object[][]{
  78             {"JDK8220818a.xsd", "JDK8220818a_Invalid.xml"},
  79             {"JDK8220818b.xsd", "JDK8220818b_Invalid.xml"},
  80         };
  81     }
  82 
  83     /*
  84      DataProvider: uniqueness
  85      */
  86     @DataProvider(name = "uniqueness")
  87     Object[][] getUniqueData() {
  88         return new Object[][]{
  89             {"JDK8176447a.xsd", "JDK8176447a.xml"},
  90             {"JDK8176447b.xsd", "JDK8176447b.xml"},
  91         };
  92     }
  93 
  94     @Test(dataProvider = "invalid", expectedExceptions = SAXParseException.class)
  95     public void testValidateRefType(String xsd, String xml) throws Exception {
  96         validate(xsd, xml);
  97     }
  98 
  99     @Test(dataProvider = "valid")
 100     public void testValidateRefType1(String xsd, String xml) throws Exception {
 101         validate(xsd, xml);
 102     }
 103 
 104     /**
 105      * @bug 8176447
 106      * Verifies that the uniqueness constraint is checked.
 107      * @param xsd the XSD
 108      * @param xml the XML
 109      * @throws Exception expected when the uniqueness constraint is validated
 110      * correctly.
 111      */
 112     @Test(dataProvider = "uniqueness", expectedExceptions = SAXException.class)
 113     public void testUnique(String xsd, String xml) throws Exception {
 114         validate(xsd, xml);
 115     }
 116 
 117     /**
 118      * @bug 8068376
 119      * Verifies that validation performs normally with externally provided string
 120      * parameters.
 121      * @throws Exception if the test fails
 122      */
 123     @Test
 124     public void testJDK8068376() throws Exception {
 125 
 126         String xsdFile = getClass().getResource(FILE_PATH + "JDK8068376.xsd").getFile();
 127         String xmlFile = getClass().getResource(FILE_PATH + "JDK8068376.xml").getFile();
 128         String targetNamespace = getTargetNamespace(xsdFile);
 129 
 130         XMLFilter namespaceFilter = new XMLFilterImpl(SAXParserFactory.newDefaultNSInstance().newSAXParser().getXMLReader()) {
 131         @Override
 132         public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
 133             uri = targetNamespace; // overwriting the uri with our own choice
 134             super.startElement(uri, localName, qName, atts);
 135         }
 136         };
 137 
 138         Source xmlSource = new SAXSource(namespaceFilter, new InputSource(xmlFile));
 139         Source schemaSource = new StreamSource(xsdFile);
 140         validate(schemaSource, xmlSource);
 141 
 142     }
 143 
 144     private static String getTargetNamespace(String xsdFile) throws Exception {
 145         XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new FileInputStream(xsdFile));
 146         while (reader.hasNext()) {
 147             int event = reader.next();
 148 
 149             // Get the root element's "targetNamespace" attribute
 150             if (event == XMLEvent.START_ELEMENT) {
 151                 // validation fails before patch
 152                 String value = reader.getAttributeValue(null, "targetNamespace"); // fails validation
 153                 // validation passes due to a reference comparison in the original code
 154                 // String value = "mynamespace";
 155                 return value;
 156             }
 157         }
 158         return null;
 159     }
 160 
 161     private void validate(String xsd, String xml) throws Exception {
 162         final SchemaFactory schemaFactory = SchemaFactory.newInstance(
 163                 XMLConstants.W3C_XML_SCHEMA_NS_URI);
 164         final Schema schema = schemaFactory.newSchema(
 165                 new File(getClass().getResource(FILE_PATH + xsd).getFile()));
 166         final Validator validator = schema.newValidator();
 167         validator.validate(new StreamSource(
 168                 new File(getClass().getResource(FILE_PATH + xml).getFile())));
 169     }
 170 
 171     private void validate(Source xsd, Source xml) throws Exception {
 172         final SchemaFactory schemaFactory = SchemaFactory.newInstance(
 173                 XMLConstants.W3C_XML_SCHEMA_NS_URI);
 174         final Schema schema = schemaFactory.newSchema(xsd);
 175         final Validator validator = schema.newValidator();
 176         validator.validate(xml);
 177     }
 178 
 179 }