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.SAXException; 40 import org.xml.sax.SAXParseException; 41 42 /* 43 * @test 44 * @bug 8220818 8176447 45 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest 46 * @run testng/othervm validation.ValidationTest 47 * @summary Runs validations with schemas and sources 48 */ 49 @Listeners({jaxp.library.FilePolicy.class}) 50 public class ValidationTest { 51 static final String FILE_PATH = "files/"; 52 /* 53 DataProvider: valid xml 54 */ 55 @DataProvider(name = "valid") 56 Object[][] getValid() { 57 return new Object[][]{ 58 {"JDK8220818a.xsd", "JDK8220818a_Valid.xml"}, 59 {"JDK8220818a.xsd", "JDK8220818a_Valid1.xml"}, 60 {"JDK8220818b.xsd", "JDK8220818b_Valid.xml"}, 61 }; 62 } 63 64 /* 65 DataProvider: invalid xml 66 */ 67 @DataProvider(name = "invalid") 68 Object[][] getInvalid() { 69 return new Object[][]{ 70 {"JDK8220818a.xsd", "JDK8220818a_Invalid.xml"}, 71 {"JDK8220818b.xsd", "JDK8220818b_Invalid.xml"}, 72 }; 73 } 74 75 /* 76 DataProvider: uniqueness 77 */ 78 @DataProvider(name = "uniqueness") 79 Object[][] getUniqueData() { 80 return new Object[][]{ 81 {"JDK8176447a.xsd", "JDK8176447a.xml"}, 82 {"JDK8176447b.xsd", "JDK8176447b.xml"}, 83 }; 84 } 85 86 @Test(dataProvider = "invalid", expectedExceptions = SAXParseException.class) 87 public void testValidateRefType(String xsd, String xml) throws Exception { 88 validate(xsd, xml); 89 } 90 91 @Test(dataProvider = "valid") 92 public void testValidateRefType1(String xsd, String xml) throws Exception { 93 validate(xsd, xml); 94 } 95 96 /** 97 * @bug 8176447 98 * Verifies that the uniqueness constraint is checked. 99 * @param xsd the XSD 100 * @param xml the XML 101 * @throws Exception expected when the uniqueness constraint is validated 102 * correctly. 103 */ 104 @Test(dataProvider = "uniqueness", expectedExceptions = SAXException.class) 105 public void testUnique(String xsd, String xml) throws Exception { 106 validate(xsd, xml); 107 } 108 109 private void validate(String xsd, String xml) throws Exception { 110 final SchemaFactory schemaFactory = SchemaFactory.newInstance( 111 XMLConstants.W3C_XML_SCHEMA_NS_URI); 112 final Schema schema = schemaFactory.newSchema( 113 new File(getClass().getResource(FILE_PATH + xsd).getFile())); 114 final Validator validator = schema.newValidator(); 115 validator.validate(new StreamSource( 116 new File(getClass().getResource(FILE_PATH + xml).getFile()))); 117 } 118 119 }