Bug6946312Test.java revision 779:2b61bfcaa586
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
24package validation;
25
26import java.io.IOException;
27import java.io.InputStream;
28import java.io.StringReader;
29
30import javax.xml.parsers.ParserConfigurationException;
31import javax.xml.parsers.SAXParser;
32import javax.xml.parsers.SAXParserFactory;
33import javax.xml.transform.stream.StreamSource;
34import javax.xml.validation.Schema;
35import javax.xml.validation.SchemaFactory;
36
37import org.testng.Assert;
38import org.testng.annotations.Test;
39import org.xml.sax.Attributes;
40import org.xml.sax.ContentHandler;
41import org.xml.sax.InputSource;
42import org.xml.sax.Locator;
43import org.xml.sax.SAXException;
44import org.xml.sax.XMLReader;
45
46/*
47 * @bug 6946312
48 * @summary Test XML parser shall callback to ContentHandler when receiving characters data.
49 */
50public class Bug6946312Test {
51    static final String SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
52    static final String SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
53    String xmlSchema = "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n" + "<xs:element name=\"root\">\n" + "<xs:complexType>\n"
54            + "<xs:sequence>\n" + "<xs:any namespace=\"##any\"  processContents=\"skip\"/>\n" + "</xs:sequence>\n" + "</xs:complexType>\n" + "</xs:element>\n"
55            + "</xs:schema>";
56
57    boolean charEvent = false;
58
59    @Test
60    public void test() throws SAXException, ParserConfigurationException, IOException {
61        Schema schema = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema").newSchema(new StreamSource(new StringReader(xmlSchema)));
62
63        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
64        saxParserFactory.setNamespaceAware(true);
65        saxParserFactory.setSchema(schema);
66        // saxParserFactory.setFeature("http://java.sun.com/xml/schema/features/report-ignored-element-content-whitespace",
67        // true);
68
69        SAXParser saxParser = saxParserFactory.newSAXParser();
70
71        XMLReader xmlReader = saxParser.getXMLReader();
72
73        xmlReader.setContentHandler(new MyContentHandler());
74
75        // InputStream input =
76        // ClassLoader.getSystemClassLoader().getResourceAsStream("test/test.xml");
77
78        InputStream input = getClass().getResourceAsStream("Bug6946312.xml");
79        System.out.println("Parse InputStream:");
80        xmlReader.parse(new InputSource(input));
81        if (!charEvent) {
82            Assert.fail("missing character event");
83        }
84    }
85
86    public class MyContentHandler implements ContentHandler {
87        public void characters(char[] ch, int start, int length) {
88            charEvent = true;
89            System.out.println("Characters called: " + new String(ch, start, length));
90        }
91
92        public void endDocument() throws SAXException {
93        }
94
95        public void endElement(String arg0, String arg1, String arg2) throws SAXException {
96        }
97
98        public void endPrefixMapping(String arg0) throws SAXException {
99        }
100
101        public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
102            System.out.println("ignorableWhitespace called: " + new String(ch, start, length));
103        }
104
105        public void processingInstruction(String arg0, String arg1) throws SAXException {
106        }
107
108        public void setDocumentLocator(Locator arg0) {
109        }
110
111        public void skippedEntity(String arg0) throws SAXException {
112        }
113
114        public void startDocument() throws SAXException {
115        }
116
117        public void startElement(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException {
118        }
119
120        public void startPrefixMapping(String arg0, String arg1) throws SAXException {
121        }
122    }
123
124}
125