test/jaxp/javax/xml/jaxp/unittest/validation/ValidationTest.java
changeset 54672 a43d6467317d
child 55688 0e1bc587472c
child 58678 9cf78a70fa4f
equal deleted inserted replaced
54671:41339a468716 54672:a43d6467317d
       
     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.net.URL;
       
    29 
       
    30 import javax.xml.XMLConstants;
       
    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 import org.testng.annotations.DataProvider;
       
    36 
       
    37 import org.testng.annotations.Listeners;
       
    38 import org.testng.annotations.Test;
       
    39 import org.xml.sax.SAXParseException;
       
    40 
       
    41 /*
       
    42  * @test
       
    43  * @bug 8220818
       
    44  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
       
    45  * @run testng/othervm validation.ValidationTest
       
    46  * @summary Runs validations with schemas and sources
       
    47  */
       
    48 @Listeners({jaxp.library.FilePolicy.class})
       
    49 public class ValidationTest {
       
    50     static final String FILE_PATH = "files/";
       
    51     /*
       
    52      DataProvider: valid xml
       
    53      */
       
    54     @DataProvider(name = "valid")
       
    55     Object[][] getValid() {
       
    56         return new Object[][]{
       
    57             {"JDK8220818a.xsd", "JDK8220818a_Valid.xml"},
       
    58             {"JDK8220818a.xsd", "JDK8220818a_Valid1.xml"},
       
    59             {"JDK8220818b.xsd", "JDK8220818b_Valid.xml"},
       
    60         };
       
    61     }
       
    62 
       
    63     /*
       
    64      DataProvider: invalid xml
       
    65      */
       
    66     @DataProvider(name = "invalid")
       
    67     Object[][] getInvalid() {
       
    68         return new Object[][]{
       
    69             {"JDK8220818a.xsd", "JDK8220818a_Invalid.xml"},
       
    70             {"JDK8220818b.xsd", "JDK8220818b_Invalid.xml"},
       
    71         };
       
    72     }
       
    73 
       
    74     @Test(dataProvider = "invalid", expectedExceptions = SAXParseException.class)
       
    75     public void testValidateRefType(String xsd, String xml) throws Exception {
       
    76         validate(xsd, xml);
       
    77     }
       
    78 
       
    79     @Test(dataProvider = "valid")
       
    80     public void testValidateRefType1(String xsd, String xml) throws Exception {
       
    81         validate(xsd, xml);
       
    82     }
       
    83 
       
    84     private void validate(String xsd, String xml) throws Exception {
       
    85         final SchemaFactory schemaFactory = SchemaFactory.newInstance(
       
    86                 XMLConstants.W3C_XML_SCHEMA_NS_URI);
       
    87         final Schema schema = schemaFactory.newSchema(
       
    88                 new File(getClass().getResource(FILE_PATH + xsd).getFile()));
       
    89         final Validator validator = schema.newValidator();
       
    90         validator.validate(new StreamSource(
       
    91                 new File(getClass().getResource(FILE_PATH + xml).getFile())));
       
    92     }
       
    93 }