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