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