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