1 /*
   2  * Copyright (c) 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 
  24 package catalog;
  25 
  26 import java.io.File;
  27 import java.io.StringReader;
  28 import javax.xml.transform.Source;
  29 import javax.xml.transform.TransformerException;
  30 import javax.xml.transform.URIResolver;
  31 import javax.xml.transform.dom.DOMSource;
  32 import javax.xml.transform.sax.SAXSource;
  33 import javax.xml.transform.stax.StAXSource;
  34 import javax.xml.transform.stream.StreamSource;
  35 import org.testng.annotations.AfterClass;
  36 import org.testng.annotations.BeforeClass;
  37 import org.testng.annotations.DataProvider;
  38 import org.testng.annotations.Listeners;
  39 import org.testng.annotations.Test;
  40 import org.w3c.dom.ls.LSResourceResolver;
  41 import org.xml.sax.InputSource;
  42 import org.xml.sax.SAXException;
  43 
  44 /*
  45  * @test
  46  * @bug 8158084 8163232
  47  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  48  * @run testng/othervm -DrunSecMngr=true catalog.CatalogSupport5
  49  * @run testng/othervm catalog.CatalogSupport5
  50  * @summary extends CatalogSupport tests, verifies that when errors occur,
  51  * relevant checked Exceptions are returned.
  52  */
  53 /**
  54  * The CatalogResolver will throw CatalogException when there is no match and
  55  * the resolve property is strict. The Exception should be caught with the existing
  56  * mechanisms so that the checked Exception corresponding to the process can be
  57  * returned.
  58  *
  59  * @author huizhe.wang@oracle.com
  60  */
  61 @Listeners({jaxp.library.FilePolicy.class, jaxp.library.NetAccessPolicy.class})
  62 public class CatalogSupport5 extends CatalogSupportBase {
  63 
  64     /*
  65      * Initializing fields
  66      */
  67     @BeforeClass
  68     public void setUpClass() throws Exception {
  69         setUp();
  70     }
  71 
  72 
  73     /*
  74        Verifies the Catalog support on SAXParser.
  75     */
  76     @Test(dataProvider = "data_SAXC", expectedExceptions = SAXException.class)
  77     public void testSAXC(boolean setUseCatalog, boolean useCatalog, String catalog, String
  78             xml, MyHandler handler, String expected) throws Exception {
  79         testSAX(setUseCatalog, useCatalog, catalog, xml, handler, expected);
  80     }
  81 
  82     /*
  83        Verifies the Catalog support on XMLReader.
  84     */
  85     @Test(dataProvider = "data_SAXC", expectedExceptions = SAXException.class)
  86     public void testXMLReaderC(boolean setUseCatalog, boolean useCatalog, String catalog,
  87             String xml, MyHandler handler, String expected) throws Exception {
  88         testXMLReader(setUseCatalog, useCatalog, catalog, xml, handler, expected);
  89     }
  90 
  91     /*
  92        Verifies the Catalog support on XInclude.
  93     */
  94     @Test(dataProvider = "data_XIC", expectedExceptions = SAXException.class)
  95     public void testXIncludeC(boolean setUseCatalog, boolean useCatalog, String catalog,
  96             String xml, MyHandler handler, String expected) throws Exception {
  97         testXInclude(setUseCatalog, useCatalog, catalog, xml, handler, expected);
  98     }
  99 
 100     /*
 101        Verifies the Catalog support on DOM parser.
 102     */
 103     @Test(dataProvider = "data_DOMC", expectedExceptions = SAXException.class)
 104     public void testDOMC(boolean setUseCatalog, boolean useCatalog, String catalog,
 105             String xml, MyHandler handler, String expected) throws Exception {
 106         testDOM(setUseCatalog, useCatalog, catalog, xml, handler, expected);
 107     }
 108 
 109     /*
 110        Verifies the Catalog support on resolving DTD, xsd import and include in
 111     Schema files.
 112     */
 113     @Test(dataProvider = "data_SchemaC", expectedExceptions = SAXException.class)
 114     public void testValidationC(boolean setUseCatalog, boolean useCatalog, String catalog,
 115             String xsd, LSResourceResolver resolver)
 116             throws Exception {
 117         testValidation(setUseCatalog, useCatalog, catalog, xsd, resolver) ;
 118     }
 119 
 120     @Test(dataProvider = "data_ValidatorC", expectedExceptions = SAXException.class)
 121     public void testValidatorC(boolean setUseCatalog1, boolean setUseCatalog2, boolean useCatalog,
 122             Source source, LSResourceResolver resolver1, LSResourceResolver resolver2,
 123             String catalog1, String catalog2)
 124             throws Exception {
 125         testValidator(setUseCatalog1, setUseCatalog2, useCatalog, source,
 126                 resolver1, resolver2, catalog1, catalog2);
 127     }
 128 
 129     /*
 130        Verifies the Catalog support on resolving DTD, xsl import and include in
 131     XSL files.
 132     */
 133     @Test(dataProvider = "data_XSLC", expectedExceptions = TransformerException.class)
 134     public void testXSLImportC(boolean setUseCatalog, boolean useCatalog, String catalog,
 135             SAXSource xsl, StreamSource xml, URIResolver resolver, String expected) throws Exception {
 136 
 137         testXSLImport(setUseCatalog, useCatalog, catalog, xsl, xml, resolver, expected);
 138     }
 139 
 140     /*
 141        @bug 8158084 8162442
 142        Verifies the Catalog support on resolving DTD, xsl import and include in
 143     XSL files.
 144     */
 145     @Test(dataProvider = "data_XSLC", expectedExceptions = TransformerException.class)
 146     public void testXSLImportWTemplatesC(boolean setUseCatalog, boolean useCatalog, String catalog,
 147             SAXSource xsl, StreamSource xml, URIResolver resolver, String expected) throws Exception {
 148         testXSLImportWTemplates(setUseCatalog, useCatalog, catalog, xsl, xml, resolver, expected);
 149     }
 150 
 151     /*
 152        DataProvider: for testing the SAX parser
 153        Data: set use_catalog, use_catalog, catalog file, xml file, handler, expected result string
 154      */
 155     @DataProvider(name = "data_SAXC")
 156     public Object[][] getDataSAXC() {
 157         return new Object[][]{
 158             {false, true, xml_bogus_catalog, xml_system, new MyHandler(elementInSystem), expectedWCatalog}
 159 
 160         };
 161     }
 162 
 163     /*
 164        DataProvider: for testing XInclude
 165        Data: set use_catalog, use_catalog, catalog file, xml file, handler, expected result string
 166      */
 167     @DataProvider(name = "data_XIC")
 168     public Object[][] getDataXIC() {
 169         return new Object[][]{
 170             {false, true, xml_bogus_catalog, xml_xInclude, new MyHandler(elementInXISimple), contentInUIutf8Catalog},
 171         };
 172     }
 173 
 174     /*
 175        DataProvider: for testing DOM parser
 176        Data: set use_catalog, use_catalog, catalog file, xml file, handler, expected result string
 177      */
 178     @DataProvider(name = "data_DOMC")
 179     public Object[][] getDataDOMC() {
 180         return new Object[][]{
 181             {false, true, xml_bogus_catalog, xml_system, new MyHandler(elementInSystem), expectedWCatalog}
 182         };
 183     }
 184 
 185     /*
 186        DataProvider: for testing Schema validation
 187        Data: set use_catalog, use_catalog, catalog file, xsd file, a LSResourceResolver
 188      */
 189     @DataProvider(name = "data_SchemaC")
 190     public Object[][] getDataSchemaC() {
 191 
 192         return new Object[][]{
 193             // for resolving DTD in xsd
 194             {false, true, xml_bogus_catalog, xsd_xmlSchema, null},
 195             // for resolving xsd import
 196             {false, true, xml_bogus_catalog, xsd_xmlSchema_import, null},
 197             // for resolving xsd include
 198             {false, true, xml_bogus_catalog, xsd_include_company, null}
 199         };
 200     }
 201 
 202     /*
 203        DataProvider: for testing Schema Validator
 204        Data: setUseCatalog1, setUseCatalog2, useCatalog, source, resolver1, resolver2,
 205              catalog1, catalog2
 206      */
 207     @DataProvider(name = "data_ValidatorC")
 208     public Object[][] getDataValidator() {
 209         DOMSource ds = getDOMSource(xml_val_test, xml_val_test_id, true, true, xml_catalog);
 210 
 211         SAXSource ss = new SAXSource(new InputSource(xml_val_test));
 212         ss.setSystemId(xml_val_test_id);
 213 
 214         StAXSource stax = getStaxSource(xml_val_test, xml_val_test_id);
 215         StAXSource stax1 = getStaxSource(xml_val_test, xml_val_test_id);
 216 
 217         StreamSource source = new StreamSource(new File(xml_val_test));
 218 
 219         return new Object[][]{
 220             // use catalog
 221             {false, false, true, ds, null, null, xml_bogus_catalog, null},
 222             {false, false, true, ds, null, null, null, xml_bogus_catalog},
 223             {false, false, true, ss, null, null, xml_bogus_catalog, null},
 224             {false, false, true, ss, null, null, null, xml_bogus_catalog},
 225             {false, false, true, stax, null, null, xml_bogus_catalog, null},
 226             {false, false, true, stax1, null, null, null, xml_bogus_catalog},
 227             {false, false, true, source, null, null, xml_bogus_catalog, null},
 228             {false, false, true, source, null, null, null, xml_bogus_catalog},
 229         };
 230     }
 231 
 232     /*
 233        DataProvider: for testing XSL import and include
 234        Data: set use_catalog, use_catalog, catalog file, xsl file, xml file, a URIResolver, expected
 235      */
 236     @DataProvider(name = "data_XSLC")
 237     public Object[][] getDataXSLC() {
 238         SAXSource xslSourceDTD = new SAXSource(new InputSource(new StringReader(xsl_includeDTD)));
 239         StreamSource xmlSourceDTD = new StreamSource(new StringReader(xml_xslDTD));
 240 
 241         SAXSource xslDocSource = new SAXSource(new InputSource(new File(xsl_doc).toURI().toASCIIString()));
 242         StreamSource xmlDocSource = new StreamSource(new File(xml_doc));
 243         return new Object[][]{
 244             // for resolving DTD, import and include in xsl
 245             {false, true, xml_bogus_catalog, xslSourceDTD, xmlSourceDTD, null, ""},
 246             // for resolving reference by the document function
 247             {false, true, xml_bogus_catalog, xslDocSource, xmlDocSource, null, "Resolved by a catalog"},
 248         };
 249     }
 250 }