1 /*
   2  * Copyright (c) 2014, 2016, 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 static jaxp.library.JAXPTestUtilities.runWithTmpPermission;
  27 
  28 import java.io.File;
  29 import java.io.FileInputStream;
  30 import java.io.FileWriter;
  31 import java.util.PropertyPermission;
  32 
  33 import javax.xml.XMLConstants;
  34 import javax.xml.stream.XMLEventReader;
  35 import javax.xml.stream.XMLInputFactory;
  36 import javax.xml.stream.XMLOutputFactory;
  37 import javax.xml.transform.Result;
  38 import javax.xml.transform.Source;
  39 import javax.xml.transform.stax.StAXResult;
  40 import javax.xml.validation.Schema;
  41 import javax.xml.validation.SchemaFactory;
  42 import javax.xml.validation.Validator;
  43 
  44 import org.testng.Assert;
  45 import org.testng.annotations.Listeners;
  46 import org.testng.annotations.Test;
  47 import org.xml.sax.ErrorHandler;
  48 
  49 /*
  50  * @test
  51  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  52  * @run testng/othervm -DrunSecMngr=true validation.ValidatorTest
  53  * @run testng/othervm validation.ValidatorTest
  54  * @summary Test Validator.validate(Source, Result).
  55  */
  56 @Listeners({jaxp.library.FilePolicy.class})
  57 public class ValidatorTest {
  58 
  59     @Test
  60     public void testValidateStAX() {
  61 
  62         File resultFile = null;
  63         try {
  64             resultFile = new File("stax.result");
  65             if (resultFile.exists()) {
  66                 resultFile.delete();
  67             }
  68 
  69             Result xmlResult = new javax.xml.transform.stax.StAXResult(XMLOutputFactory.newInstance().createXMLStreamWriter(new FileWriter(resultFile)));
  70             Source xmlSource = new javax.xml.transform.stax.StAXSource(getXMLEventReader("toys.xml"));
  71             validate("toys.xsd", xmlSource, xmlResult);
  72 
  73             ((StAXResult) xmlResult).getXMLStreamWriter().close();
  74             Assert.assertTrue(resultFile.exists(), "result file is not created");
  75 
  76         } catch (Exception ex) {
  77             ex.printStackTrace();
  78             Assert.fail("Exception : " + ex.getMessage());
  79         } finally {
  80             if (resultFile != null && resultFile.exists()) {
  81                 resultFile.delete();
  82             }
  83         }
  84     }
  85 
  86     @Test
  87     public void testValidateStream() {
  88 
  89         File resultFile = null;
  90         try {
  91             resultFile = new File("stax.result");
  92             if (resultFile.exists()) {
  93                 resultFile.delete();
  94             }
  95             // Validate this instance document against the
  96             // Instance document supplied
  97             File resultAlias = resultFile;
  98             Result xmlResult = runWithTmpPermission(() -> new javax.xml.transform.stream.StreamResult(
  99                     resultAlias), new PropertyPermission("user.dir", "read"));
 100             Source xmlSource = new javax.xml.transform.stream.StreamSource(new File(ValidatorTest.class.getResource("toys.xml").toURI()));
 101 
 102             validate("toys.xsd", xmlSource, xmlResult);
 103             Assert.assertTrue(resultFile.exists(), "result file is not created");
 104         } catch (Exception ex) {
 105             ex.printStackTrace();
 106             Assert.fail("Exception : " + ex.getMessage());
 107         } finally {
 108             if (resultFile != null && resultFile.exists()) {
 109                 resultFile.delete();
 110             }
 111         }
 112     }
 113 
 114     @Test
 115     public void testValidateGMonth() {
 116 
 117         // test valid gMonths
 118         File resultFile = null;
 119         try {
 120             resultFile = new File("gMonths.result.xml");
 121             if (resultFile.exists()) {
 122                 resultFile.delete();
 123             }
 124 
 125             // Validate this instance document against the
 126             // Instance document supplied
 127             File resultAlias = resultFile;
 128             Result xmlResult = runWithTmpPermission(() -> new javax.xml.transform.stream.StreamResult(
 129                     resultAlias), new PropertyPermission("user.dir", "read"));
 130             Source xmlSource = new javax.xml.transform.stream.StreamSource(new File(ValidatorTest.class.getResource("gMonths.xml").toURI()));
 131 
 132             validate("gMonths.xsd", xmlSource, xmlResult);
 133 
 134             Assert.assertTrue(resultFile.exists(), "result file is not created");
 135         } catch (Exception ex) {
 136             ex.printStackTrace();
 137             Assert.fail("Exception : " + ex.getMessage());
 138         } finally {
 139             if (resultFile != null && resultFile.exists()) {
 140                 resultFile.delete();
 141             }
 142         }
 143 
 144         // test invalid gMonths
 145         File invalidResultFile = null;
 146         try {
 147             invalidResultFile = new File("gMonths-invalid.result.xml");
 148             if (invalidResultFile.exists()) {
 149                 invalidResultFile.delete();
 150             }
 151 
 152             // Validate this instance document against the
 153             // Instance document supplied
 154             Result xmlResult = new javax.xml.transform.stream.StreamResult(resultFile);
 155             Source xmlSource = new javax.xml.transform.stream.StreamSource(new File(ValidatorTest.class.getResource("gMonths-invalid.xml").toURI()));
 156 
 157             validate("gMonths.xsd", xmlSource, xmlResult);
 158 
 159             // should have failed with an Exception due to invalid gMonths
 160             Assert.fail("invalid gMonths were accepted as valid in " + ValidatorTest.class.getResource("gMonths-invalid.xml").toURI());
 161         } catch (Exception ex) {
 162             // expected failure
 163             System.out.println("Expected failure: " + ex.toString());
 164         } finally {
 165             if (invalidResultFile != null && invalidResultFile.exists()) {
 166                 invalidResultFile.delete();
 167             }
 168         }
 169     }
 170 
 171     private void validate(final String xsdFile, final Source src, final Result result) throws Exception {
 172         try {
 173             SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
 174             Schema schema = sf.newSchema(new File(ValidatorTest.class.getResource(xsdFile).toURI()));
 175 
 176             // Get a Validator which can be used to validate instance document
 177             // against this grammar.
 178             Validator validator = schema.newValidator();
 179             ErrorHandler eh = new ErrorHandlerImpl();
 180             validator.setErrorHandler(eh);
 181 
 182             // Validate this instance document against the
 183             // Instance document supplied
 184             validator.validate(src, result);
 185         } catch (Exception ex) {
 186             throw ex;
 187         }
 188     }
 189 
 190     private XMLEventReader getXMLEventReader(final String filename) {
 191 
 192         XMLInputFactory xmlif = null;
 193         XMLEventReader xmlr = null;
 194         try {
 195             xmlif = XMLInputFactory.newInstance();
 196             xmlif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
 197             xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
 198             xmlif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
 199             xmlif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
 200 
 201             // FileInputStream fis = new FileInputStream(filename);
 202             FileInputStream fis = new FileInputStream(new File(ValidatorTest.class.getResource(filename).toURI()));
 203             xmlr = xmlif.createXMLEventReader(filename, fis);
 204         } catch (Exception ex) {
 205             ex.printStackTrace();
 206             Assert.fail("Exception : " + ex.getMessage());
 207         }
 208         return xmlr;
 209     }
 210 }