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