1 /* 2 * Copyright (c) 2014, 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 javax.xml.validation.tck; 25 26 import java.io.IOException; 27 28 import javax.xml.XMLConstants; 29 import javax.xml.parsers.SAXParser; 30 import javax.xml.parsers.SAXParserFactory; 31 import javax.xml.transform.stream.StreamSource; 32 import javax.xml.validation.Schema; 33 import javax.xml.validation.SchemaFactory; 34 import javax.xml.validation.Validator; 35 36 import org.testng.Assert; 37 import org.testng.annotations.Test; 38 import org.xml.sax.SAXException; 39 import org.xml.sax.SAXParseException; 40 import org.xml.sax.helpers.DefaultHandler; 41 42 /* 43 * @bug 6974551 44 * @summary Test Validation for SAXParser can expose whitespace facet for xs:anySimpleType. 45 */ 46 public class Bug6974551Test { 47 static final String SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; 48 static final String SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource"; 49 50 static String _xml = Bug6974551Test.class.getResource("Bug6974551.xml").getPath(); 51 static String _xsd = Bug6974551Test.class.getResource("Bug6974551.xsd").getPath(); 52 53 @Test 54 public void testSAX() { 55 try { 56 Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(new StreamSource(_xsd)); 57 SAXParserFactory spf = SAXParserFactory.newInstance(); 58 spf.setNamespaceAware(true); 59 spf.setValidating(true); 60 spf.setSchema(schema); 61 SAXParser parser = spf.newSAXParser(); 62 MyErrorHandler errorHandler = new MyErrorHandler(); 63 parser.parse(_xml, errorHandler); 64 if (!errorHandler.errorOccured) { 65 Assert.fail("should report error"); 66 } 67 } catch (Exception e) { 68 System.out.println(e.getMessage()); 69 } 70 } 71 72 @Test 73 public void testValidationAPI() { 74 try { 75 SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 76 77 Schema schema = schemaFactory.newSchema(new StreamSource(_xsd)); 78 Validator validator = schema.newValidator(); 79 validator.validate(new StreamSource(_xml)); 80 81 Assert.fail("should report error"); 82 } catch (SAXException e) { 83 // expected, pass 84 System.out.println(e.getMessage()); 85 } catch (IOException e) { 86 Assert.fail(e.getMessage()); 87 System.out.println(e.getMessage()); 88 } 89 } 90 91 class MyErrorHandler extends DefaultHandler { 92 93 public boolean errorOccured = false; 94 95 public void error(SAXParseException e) throws SAXException { 96 97 System.err.println("Error: " + "[[" + e.getPublicId() + "]" + "[" + e.getSystemId() + "]]" + "[[" + e.getLineNumber() + "]" + "[" 98 + e.getColumnNumber() + "]] " + e); 99 100 errorOccured = true; 101 } 102 103 public void fatalError(SAXParseException e) throws SAXException { 104 105 System.err.println("Fatal Error: " + e); 106 107 errorOccured = true; 108 } 109 110 public void warning(SAXParseException e) throws SAXException { 111 112 System.err.println("Warning: " + e); 113 114 errorOccured = true; 115 } 116 } 117 118 }