1 /* 2 * Copyright (c) 2016, 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 import java.io.File; 27 import java.io.StringReader; 28 import java.util.ArrayList; 29 import java.util.List; 30 import javax.xml.XMLConstants; 31 import javax.xml.transform.stream.StreamSource; 32 import javax.xml.validation.SchemaFactory; 33 import org.testng.Assert; 34 import org.testng.annotations.Listeners; 35 import org.testng.annotations.Test; 36 import org.xml.sax.ErrorHandler; 37 import org.xml.sax.SAXException; 38 import org.xml.sax.SAXParseException; 39 40 /* 41 * @test 42 * @bug 8149915 8222991 43 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest 44 * @run testng/othervm validation.SchemaTest 45 * @summary Test Schema creation 46 */ 47 @Listeners({jaxp.library.FilePolicy.class}) 48 public class SchemaTest { 49 /** 50 * Verifies that an over-the-limit value of an enumeration is caught as a 51 * warning. 52 * @throws Exception if the test fails 53 */ 54 @Test 55 public void testSchema() throws Exception { 56 String xsd = "<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n" + 57 " <xsd:simpleType name=\"PaymentStatus\">\n" + 58 " <xsd:restriction base=\"xsd:string\">\n" + 59 " <xsd:maxLength value=\"15\"/>\n" + 60 " <xsd:enumeration value=\"AWAIT_PAY_INFO\"/>\n" + 61 " <xsd:enumeration value=\"AWAIT_AUTH\"/>\n" + 62 " <xsd:enumeration value=\"REQUESTED_AUTH\"/>\n" + 63 " <xsd:enumeration value=\"REQUESTED_CHARGE\"/>\n" + 64 " <xsd:enumeration value=\"PAID\"/>\n" + 65 " </xsd:restriction>\n" + 66 " </xsd:simpleType> \n" + 67 "</xsd:schema>"; 68 SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 69 final List<SAXParseException> exceptions = new ArrayList<>(); 70 71 factory.setErrorHandler(new ErrorHandler() 72 { 73 @Override 74 public void warning(SAXParseException exception) throws SAXException 75 { 76 exceptions.add(exception); 77 } 78 79 @Override 80 public void fatalError(SAXParseException exception) throws SAXException 81 {} 82 83 @Override 84 public void error(SAXParseException exception) throws SAXException 85 {} 86 }); 87 factory.newSchema(new StreamSource(new StringReader(xsd))); 88 Assert.assertTrue(exceptions.get(0).toString().contains("FacetsContradict"), 89 "Report warning when the maxLength limit is exceeded in an enumeration"); 90 } 91 92 /* 93 * @bug 8149915 94 * Verifies that the annotation validator is initialized with the security manager for schema 95 * creation with http://apache.org/xml/features/validate-annotations=true. 96 */ 97 @Test 98 public void testValidation() throws Exception { 99 SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 100 factory.setFeature("http://apache.org/xml/features/validate-annotations", true); 101 factory.newSchema(new File(getClass().getResource("Bug8149915.xsd").getFile())); 102 } 103 }