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