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.FileWriter;
  28 
  29 import javax.xml.XMLConstants;
  30 import javax.xml.stream.XMLInputFactory;
  31 import javax.xml.stream.XMLOutputFactory;
  32 import javax.xml.stream.XMLStreamReader;
  33 import javax.xml.transform.Result;
  34 import javax.xml.transform.Source;
  35 import javax.xml.transform.stax.StAXSource;
  36 import javax.xml.validation.Schema;
  37 import javax.xml.validation.SchemaFactory;
  38 import javax.xml.validation.Validator;
  39 
  40 import org.testng.Assert;
  41 import org.testng.annotations.Test;
  42 
  43 /*
  44  * @bug 6708840
  45  * @summary Test Validator can process StAXSource.
  46  */
  47 public class CR6708840Test {
  48 
  49     @Test
  50     public final void testStream() {
  51         try {
  52             SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
  53             Schema schemaGrammar = schemaFactory.newSchema(new File(getClass().getResource("gMonths.xsd").getFile()));
  54 
  55             Validator schemaValidator = schemaGrammar.newValidator();
  56             Source xmlSource = new javax.xml.transform.stream.StreamSource(new File(CR6708840Test.class.getResource("gMonths.xml").toURI()));
  57             schemaValidator.validate(xmlSource);
  58 
  59         } catch (NullPointerException ne) {
  60             Assert.fail("NullPointerException when result is not specified.");
  61         } catch (Exception e) {
  62             Assert.fail(e.getMessage());
  63             e.printStackTrace();
  64         }
  65     }
  66 
  67     /**
  68      * refer to http://forums.java.net/jive/thread.jspa?threadID=41626&tstart=0
  69      */
  70     @Test
  71     public final void testStAX() {
  72         try {
  73             XMLInputFactory xmlif = XMLInputFactory.newInstance();
  74 
  75             // XMLStreamReader staxReader =
  76             // xmlif.createXMLStreamReader((Source)new
  77             // StreamSource(getClass().getResource("Forum31576.xml").getFile()));
  78             XMLStreamReader staxReader = xmlif.createXMLStreamReader(this.getClass().getResourceAsStream("gMonths.xml"));
  79 
  80             SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
  81             Schema schemaGrammar = schemaFactory.newSchema(new File(getClass().getResource("gMonths.xsd").getFile()));
  82 
  83             Validator schemaValidator = schemaGrammar.newValidator();
  84 
  85             Source staxSrc = new StAXSource(staxReader);
  86             schemaValidator.validate(staxSrc);
  87 
  88             while (staxReader.hasNext()) {
  89                 int eventType = staxReader.next();
  90                 System.out.println("Event of type: " + eventType);
  91             }
  92         } catch (NullPointerException ne) {
  93             Assert.fail("NullPointerException when result is not specified.");
  94         } catch (Exception e) {
  95             Assert.fail(e.getMessage());
  96             e.printStackTrace();
  97         }
  98     }
  99 
 100     /**
 101      * workaround before the fix: provide a result
 102      */
 103     @Test
 104     public final void testStAXWResult() {
 105         try {
 106             XMLInputFactory xmlif = XMLInputFactory.newInstance();
 107 
 108             // XMLStreamReader staxReader =
 109             // xmlif.createXMLStreamReader((Source)new
 110             // StreamSource(getClass().getResource("Forum31576.xml").getFile()));
 111             XMLStreamReader staxReader = xmlif.createXMLStreamReader(this.getClass().getResourceAsStream("gMonths.xml"));
 112 
 113             SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
 114             Schema schemaGrammar = schemaFactory.newSchema(new File(getClass().getResource("gMonths.xsd").getFile()));
 115 
 116             Validator schemaValidator = schemaGrammar.newValidator();
 117 
 118             Source staxSrc = new StAXSource(staxReader);
 119             File resultFile = new File("gMonths.result.xml");
 120             if (resultFile.exists()) {
 121                 resultFile.delete();
 122             }
 123 
 124             Result xmlResult = new javax.xml.transform.stax.StAXResult(XMLOutputFactory.newInstance().createXMLStreamWriter(new FileWriter(resultFile)));
 125             schemaValidator.validate(staxSrc, xmlResult);
 126 
 127             while (staxReader.hasNext()) {
 128                 int eventType = staxReader.next();
 129                 System.out.println("Event of type: " + eventType);
 130             }
 131         } catch (Exception e) {
 132             Assert.fail(e.getMessage());
 133             e.printStackTrace();
 134         }
 135     }
 136 }