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