1 /* 2 * Copyright (c) 2002, 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 package test.astro; 24 25 import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI; 26 import static test.astro.AstroConstants.ASTROCAT; 27 import static test.astro.AstroConstants.JAXP_SCHEMA_LANGUAGE; 28 import static test.astro.AstroConstants.JAXP_SCHEMA_SOURCE; 29 30 import java.io.File; 31 32 import javax.xml.parsers.ParserConfigurationException; 33 import javax.xml.parsers.SAXParser; 34 import javax.xml.parsers.SAXParserFactory; 35 36 import jaxp.library.JAXPFileBaseTest; 37 38 import org.testng.annotations.Test; 39 import org.xml.sax.SAXException; 40 import org.xml.sax.helpers.DefaultHandler; 41 42 /* 43 * @summary test parser sets schema related properties to do validation 44 */ 45 public class SchemaValidationTest extends JAXPFileBaseTest { 46 /* 47 * Only set the schemaLanguage, without setting schemaSource. It should 48 * work. 49 */ 50 @Test 51 public void testSchemaValidation() throws Exception { 52 SAXParser sp = getValidatingParser(); 53 sp.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI); 54 sp.parse(new File(ASTROCAT), new DefaultHandler()); 55 } 56 57 /* 58 * Test SAXException shall be thrown if schemaSource is set but 59 * schemaLanguage is not set. 60 */ 61 @Test(expectedExceptions = SAXException.class) 62 public void testSchemaValidationNeg() throws Exception { 63 SAXParser sp = getValidatingParser(); 64 sp.setProperty(JAXP_SCHEMA_SOURCE, "catalog.xsd"); 65 sp.parse(new File(ASTROCAT), new DefaultHandler()); 66 } 67 68 private SAXParser getValidatingParser() throws ParserConfigurationException, SAXException { 69 SAXParserFactory spf = SAXParserFactory.newInstance(); 70 spf.setNamespaceAware(true); 71 spf.setValidating(true); 72 return spf.newSAXParser(); 73 } 74 }