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