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