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