1 /* 2 * Copyright (c) 2014, 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 javax.xml.validation.tck; 25 26 import java.io.File; 27 import java.io.IOException; 28 29 import javax.xml.XMLConstants; 30 import javax.xml.parsers.ParserConfigurationException; 31 import javax.xml.parsers.SAXParser; 32 import javax.xml.parsers.SAXParserFactory; 33 import javax.xml.transform.stream.StreamSource; 34 import javax.xml.validation.Schema; 35 import javax.xml.validation.SchemaFactory; 36 import javax.xml.validation.Validator; 37 38 import org.testng.Assert; 39 import org.testng.annotations.Test; 40 import org.xml.sax.ErrorHandler; 41 import org.xml.sax.SAXException; 42 import org.xml.sax.SAXNotRecognizedException; 43 import org.xml.sax.SAXNotSupportedException; 44 import org.xml.sax.SAXParseException; 45 import org.xml.sax.helpers.DefaultHandler; 46 47 /* 48 * @bug 6963468 49 * @summary Test Validation allows element a is a union type and element b specifies a as its substitution group and b type is or is derived from one of the member types of the union. 50 */ 51 public class Bug6963468Test { 52 static final String SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; 53 static final String SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource"; 54 55 @Test 56 public void test() { 57 try { 58 SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 59 schemaFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); 60 61 Schema schema = schemaFactory.newSchema(new StreamSource(Bug6963468Test.class.getResourceAsStream("Bug6963468.xsd"))); 62 63 Validator validator = schema.newValidator(); 64 validator.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); 65 validator.setErrorHandler(new ErrorHandler() { 66 public void error(SAXParseException exception) throws SAXException { 67 exception.printStackTrace(); 68 } 69 70 public void fatalError(SAXParseException exception) throws SAXException { 71 exception.printStackTrace(); 72 } 73 74 public void warning(SAXParseException exception) throws SAXException { 75 exception.printStackTrace(); 76 } 77 }); 78 79 validator.validate(new StreamSource(Bug6963468Test.class.getResourceAsStream("Bug6963468.xml"))); 80 81 } catch (SAXException e) { 82 System.out.println(e.getMessage()); 83 // fail(e.getMessage()); 84 85 } catch (IOException e) { 86 e.printStackTrace(); 87 System.out.println(e.getMessage()); 88 // fail(e.getMessage()); 89 } 90 } 91 92 @Test 93 public void testInstance() throws ParserConfigurationException, SAXException, IOException { 94 System.out.println(Bug6963468Test.class.getResource("Bug6963468.xsd").getPath()); 95 File schemaFile = new File(Bug6963468Test.class.getResource("Bug6963468.xsd").getPath()); 96 SAXParser parser = createParser(schemaFile); 97 98 try { 99 parser.parse(Bug6963468Test.class.getResource("Bug6963468.xml").getPath(), new DefaultHandler()); 100 } catch (SAXException e) { 101 e.printStackTrace(); 102 Assert.fail("Fatal Error: " + strException(e)); 103 } 104 105 } 106 107 protected SAXParser createParser(File schema) throws ParserConfigurationException, SAXException { 108 109 // create and initialize the parser 110 SAXParserFactory spf = SAXParserFactory.newInstance(); 111 spf.setNamespaceAware(true); 112 spf.setValidating(true); 113 SAXParser parser = spf.newSAXParser(); 114 parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); 115 116 // set schemaLocation if possible 117 try { 118 parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", schema); 119 } catch (SAXNotRecognizedException e) { 120 System.out.println("Warning: Property 'http://java.sun.com/xml/jaxp/properties/schemaSource' is not recognized."); 121 } catch (SAXNotSupportedException e) { 122 System.out.println("Warning: Property 'http://java.sun.com/xml/jaxp/properties/schemaSource' is not supported."); 123 } 124 125 return parser; 126 } 127 128 protected static String strException(Exception ex) { 129 StringBuffer sb = new StringBuffer(); 130 131 while (ex != null) { 132 if (ex instanceof SAXParseException) { 133 SAXParseException e = (SAXParseException) ex; 134 sb.append("" + e.getSystemId() + "(" + e.getLineNumber() + "," + e.getColumnNumber() + "): " + e.getMessage()); 135 ex = e.getException(); 136 } else { 137 sb.append(ex); 138 ex = null; 139 } 140 } 141 return sb.toString(); 142 } 143 144 }