< prev index next >

test/javax/xml/jaxp/unittest/validation/ValidatorTest.java

Print this page


   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) {


   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) {


< prev index next >