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