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.URIResolver;
  30 import javax.xml.transform.dom.DOMSource;
  31 import javax.xml.transform.sax.SAXSource;
  32 import javax.xml.transform.stax.StAXSource;
  33 import javax.xml.transform.stream.StreamSource;
  34 import org.testng.annotations.AfterClass;
  35 import org.testng.annotations.BeforeClass;
  36 import org.testng.annotations.DataProvider;
  37 import org.testng.annotations.Test;
  38 import org.w3c.dom.ls.LSResourceResolver;
  39 import org.xml.sax.InputSource;
  40 
  41 /**
  42  * @bug 8158084 8162435 8162438 8162442
  43  * @summary verifies the overriding over of the USE_CATALOG feature. Extending
  44  * CatalogSupport tests, the USE_CATALOG is turned off system-wide, however,
  45  * a JAXP processor may decide to use Catalog by enabling it through the factory
  46  * or a processor created by the factory.
  47  */
  48 
  49 /**
  50  *
  51  * Test notes:
  52  * Same set of data as in CatalogSupport without the ones with resolvers.
  53  * The set-use-catalog is set to true.
  54  *
  55  * @author huizhe.wang@oracle.com
  56  */
  57 public class CatalogSupport4 extends CatalogSupportBase {
  58     /*
  59      * Initializing fields
  60      */
  61     @BeforeClass
  62     public void setUpClass() throws Exception {
  63         setUp();
  64         //turn off USE_CATALOG system-wide
  65         System.setProperty(SP_USE_CATALOG, "false");
  66     }
  67 
  68     @AfterClass
  69     public void tearDownClass() throws Exception {
  70         System.clearProperty(SP_USE_CATALOG);
  71     }
  72 
  73     /*
  74        Verifies the Catalog support on SAXParser.
  75     */
  76     @Test(dataProvider = "data_SAXA")
  77     public void testSAXA(boolean setUseCatalog, boolean useCatalog, String catalog,
  78             String 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_SAXA")
  86     public void testXMLReaderA(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_XIA")
  95     public void testXIncludeA(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_DOMA")
 104     public void testDOMA(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_SchemaA")
 114     public void testValidationA(boolean setUseCatalog, boolean useCatalog,
 115             String catalog, String xsd, LSResourceResolver resolver)
 116             throws Exception {
 117 
 118         testValidation(setUseCatalog, useCatalog, catalog, xsd, resolver) ;
 119     }
 120 
 121     /*
 122        @bug 8158084 8162435 8162438 these tests also verifies the fix for 8162435 and 8162438
 123        Verifies the Catalog support on the Schema Validator.
 124     */
 125     @Test(dataProvider = "data_ValidatorA")
 126     public void testValidatorA(boolean setUseCatalog1, boolean setUseCatalog2, boolean useCatalog,
 127             Source source, LSResourceResolver resolver1, LSResourceResolver resolver2,
 128             String catalog1, String catalog2)
 129             throws Exception {
 130         testValidator(setUseCatalog1, setUseCatalog2, useCatalog, source,
 131                 resolver1, resolver2, catalog1, catalog2);
 132     }
 133 
 134     /*
 135        Verifies the Catalog support on resolving DTD, xsl import and include in
 136     XSL files.
 137     */
 138     @Test(dataProvider = "data_XSLA")
 139     public void testXSLImportA(boolean setUseCatalog, boolean useCatalog, String catalog,
 140             SAXSource xsl, StreamSource xml, URIResolver resolver, String expected)
 141             throws Exception {
 142 
 143         testXSLImport(setUseCatalog, useCatalog, catalog, xsl, xml, resolver, expected);
 144     }
 145 
 146     /*
 147        @bug 8158084 8162442
 148        Verifies the Catalog support on resolving DTD, xsl import and include in
 149     XSL files.
 150     */
 151     @Test(dataProvider = "data_XSLA")
 152     public void testXSLImportWTemplatesA(boolean setUseCatalog, boolean useCatalog,
 153             String catalog, SAXSource xsl, StreamSource xml, URIResolver resolver, String expected)
 154             throws Exception {
 155         testXSLImportWTemplates(setUseCatalog, useCatalog, catalog, xsl, xml, resolver, expected);
 156     }
 157 
 158     /*
 159        DataProvider: for testing the SAX parser
 160        Data: set use_catalog, use_catalog, catalog file, xml file, handler, expected result string
 161      */
 162     @DataProvider(name = "data_SAXA")
 163     Object[][] getDataSAX() {
 164         return new Object[][]{
 165             {true, true, xml_catalog, xml_system, new MyHandler(elementInSystem), expectedWCatalog},
 166         };
 167     }
 168 
 169     /*
 170        DataProvider: for testing XInclude
 171        Data: set use_catalog, use_catalog, catalog file, xml file, handler, expected result string
 172      */
 173     @DataProvider(name = "data_XIA")
 174     Object[][] getDataXI() {
 175         return new Object[][]{
 176             {true, true, xml_catalog, xml_xInclude, new MyHandler(elementInXISimple), contentInUIutf8Catalog},
 177         };
 178     }
 179 
 180     /*
 181        DataProvider: for testing DOM parser
 182        Data: set use_catalog, use_catalog, catalog file, xml file, handler, expected result string
 183      */
 184     @DataProvider(name = "data_DOMA")
 185     Object[][] getDataDOM() {
 186         return new Object[][]{
 187             {true, true, xml_catalog, xml_system, new MyHandler(elementInSystem), expectedWCatalog},
 188         };
 189     }
 190 
 191     MyEntityHandler getMyEntityHandler(String elementName, String[] systemIds, InputSource... returnValues) {
 192        return new MyEntityHandler(systemIds, returnValues, elementName);
 193     }
 194 
 195     /*
 196        DataProvider: for testing Schema validation
 197        Data: set use_catalog, use_catalog, catalog file, xsd file, a LSResourceResolver
 198      */
 199     @DataProvider(name = "data_SchemaA")
 200     Object[][] getDataSchema() {
 201         return new Object[][]{
 202             // for resolving DTD in xsd
 203             {true, true, xml_catalog, xsd_xmlSchema, null},
 204             // for resolving xsd import
 205             {true, true, xml_catalog, xsd_xmlSchema_import, null},
 206             // for resolving xsd include
 207             {true, true, xml_catalog, xsd_include_company, null},
 208         };
 209     }
 210 
 211     /*
 212        DataProvider: for testing Schema Validator
 213        Data: source, resolver1, resolver2, catalog1, a catalog2
 214      */
 215     @DataProvider(name = "data_ValidatorA")
 216     Object[][] getDataValidator() {
 217         DOMSource ds = getDOMSource(xml_val_test, xml_val_test_id, true, true, xml_catalog);
 218 
 219         SAXSource ss = new SAXSource(new InputSource(xml_val_test));
 220         ss.setSystemId(xml_val_test_id);
 221 
 222         StAXSource stax = getStaxSource(xml_val_test, xml_val_test_id);
 223         StAXSource stax1 = getStaxSource(xml_val_test, xml_val_test_id);
 224 
 225         StreamSource source = new StreamSource(new File(xml_val_test));
 226 
 227         return new Object[][]{
 228             // use catalog
 229             {true, false, true, ds, null, null, xml_catalog, null},
 230             {false, true, true, ds, null, null, null, xml_catalog},
 231             {true, false, true, ss, null, null, xml_catalog, null},
 232             {false, true, true, ss, null, null, null, xml_catalog},
 233             {true, false, true, stax, null, null, xml_catalog, null},
 234             {false, true, true, stax1, null, null, null, xml_catalog},
 235             {true, false, true, source, null, null, xml_catalog, null},
 236             {false, true, true, source, null, null, null, xml_catalog},
 237         };
 238     }
 239 
 240     /*
 241        DataProvider: for testing XSL import and include
 242        Data: set use_catalog, use_catalog, catalog file, xsl file, xml file, a URIResolver, expected result
 243      */
 244     @DataProvider(name = "data_XSLA")
 245     Object[][] getDataXSL() {
 246         // XSLInclude.xsl has one import XSLImport_html.xsl and two includes,
 247         // XSLInclude_header.xsl and XSLInclude_footer.xsl;
 248         SAXSource xslSourceDTD = new SAXSource(new InputSource(new StringReader(xsl_includeDTD)));
 249         StreamSource xmlSourceDTD = new StreamSource(new StringReader(xml_xslDTD));
 250 
 251         SAXSource xslDocSource = new SAXSource(new InputSource(new File(xsl_doc).toURI().toASCIIString()));
 252         StreamSource xmlDocSource = new StreamSource(new File(xml_doc));
 253         return new Object[][]{
 254             // for resolving DTD, import and include in xsl
 255             {true, true, xml_catalog, xslSourceDTD, xmlSourceDTD, null, ""},
 256             // for resolving reference by the document function
 257             {true, true, xml_catalog, xslDocSource, xmlDocSource, null, "Resolved by a catalog"},
 258         };
 259     }
 260 }