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