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 
  29 import javax.xml.transform.Source;
  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 
  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 
  43 /**
  44  * @test
  45  * @bug 8158084 8162438 8162442
  46  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  47  * @run testng/othervm -DrunSecMngr=true catalog.CatalogSupport
  48  * @run testng/othervm catalog.CatalogSupport
  49  * @summary verifies the use of Catalog in SAX/DOM/StAX/Validation/Transform.
  50  * The two main scenarios for all processors are:
  51  * A custom resolver is used whether or not there's a Catalog;
  52  * A Catalog is used when there's no custom resolver, and the USE_CATALOG
  53  * is true (which is the case by default).
  54  */
  55 
  56 /**
  57  * Support Catalog:
  58  * With this patch, the Catalog features are supported by all of the JAXP processors.
  59  * The support is enabled by default. Using Catalog is as simple as setting a
  60  * path to a catalog, through the API, or System property, or jaxp.properties.
  61  *
  62  * Test notes:
  63  * For all DataProviders, the 1st and 2nd columns determine whether to set USE_CATALOG
  64  * through the API and to use Catalog. When a custom resolver is specified, these
  65  * settings should not affect the operation, thus the tests are repeated for both
  66  * false and true.
  67  *
  68  * @author huizhe.wang@oracle.com
  69  */
  70 @Listeners({jaxp.library.FilePolicy.class, jaxp.library.NetAccessPolicy.class})
  71 public class CatalogSupport extends CatalogSupportBase {
  72     /*
  73      * Initializing fields
  74      */
  75     @BeforeClass
  76     public void setUpClass() throws Exception {
  77         setUp();
  78     }
  79 
  80     /*
  81        Verifies the Catalog support on SAXParser.
  82     */
  83     @Test(dataProvider = "data_SAXA")
  84     public void testSAXA(boolean setUseCatalog, boolean useCatalog, String catalog,
  85             String xml, MyHandler handler, String expected) throws Exception {
  86         testSAX(setUseCatalog, useCatalog, catalog, xml, handler, expected);
  87     }
  88 
  89     /*
  90        Verifies the Catalog support on XMLReader.
  91     */
  92     @Test(dataProvider = "data_SAXA")
  93     public void testXMLReaderA(boolean setUseCatalog, boolean useCatalog, String catalog,
  94             String xml, MyHandler handler, String expected) throws Exception {
  95         testXMLReader(setUseCatalog, useCatalog, catalog, xml, handler, expected);
  96     }
  97 
  98     /*
  99        Verifies the Catalog support on XInclude.
 100     */
 101     @Test(dataProvider = "data_XIA")
 102     public void testXIncludeA(boolean setUseCatalog, boolean useCatalog, String catalog,
 103             String xml, MyHandler handler, String expected) throws Exception {
 104         testXInclude(setUseCatalog, useCatalog, catalog, xml, handler, expected);
 105     }
 106 
 107     /*
 108        Verifies the Catalog support on DOM parser.
 109     */
 110     @Test(dataProvider = "data_DOMA")
 111     public void testDOMA(boolean setUseCatalog, boolean useCatalog, String catalog,
 112             String xml, MyHandler handler, String expected) throws Exception {
 113         testDOM(setUseCatalog, useCatalog, catalog, xml, handler, expected);
 114     }
 115 
 116     /*
 117        Verifies the Catalog support on resolving DTD, xsd import and include in
 118     Schema files.
 119     */
 120     @Test(dataProvider = "data_SchemaA")
 121     public void testValidationA(boolean setUseCatalog, boolean useCatalog,
 122             String catalog, String xsd, LSResourceResolver resolver)
 123             throws Exception {
 124 
 125         testValidation(setUseCatalog, useCatalog, catalog, xsd, resolver) ;
 126     }
 127 
 128     /*
 129        @bug 8158084 8162438 these tests also verifies the fix for 8162438
 130        Verifies the Catalog support on the Schema Validator.
 131     */
 132     @Test(dataProvider = "data_ValidatorA")
 133     public void testValidatorA(boolean setUseCatalog1, boolean setUseCatalog2, boolean useCatalog,
 134             Source source, LSResourceResolver resolver1, LSResourceResolver resolver2,
 135             String catalog1, String catalog2)
 136             throws Exception {
 137         testValidator(setUseCatalog1, setUseCatalog2, useCatalog, source,
 138                 resolver1, resolver2, catalog1, catalog2);
 139     }
 140 
 141     /*
 142        Verifies the Catalog support on resolving DTD, xsl import and include in
 143     XSL files.
 144     */
 145     @Test(dataProvider = "data_XSLA")
 146     public void testXSLImportA(boolean setUseCatalog, boolean useCatalog, String catalog,
 147             SAXSource xsl, StreamSource xml, URIResolver resolver, String expected)
 148             throws Exception {
 149 
 150         testXSLImport(setUseCatalog, useCatalog, catalog, xsl, xml, resolver, expected);
 151     }
 152 
 153     /*
 154        @bug 8158084 8162442
 155        Verifies the Catalog support on resolving DTD, xsl import and include in
 156     XSL files.
 157     */
 158     @Test(dataProvider = "data_XSLA")
 159     public void testXSLImportWTemplatesA(boolean setUseCatalog, boolean useCatalog,
 160             String catalog, SAXSource xsl, StreamSource xml, URIResolver resolver, String expected)
 161             throws Exception {
 162         testXSLImportWTemplates(setUseCatalog, useCatalog, catalog, xsl, xml, resolver, expected);
 163     }
 164 
 165     /*
 166        DataProvider: for testing the SAX parser
 167        Data: set use_catalog, use_catalog, catalog file, xml file, handler, expected result string
 168      */
 169     @DataProvider(name = "data_SAXA")
 170     public Object[][] getDataSAX() {
 171         String[] systemIds = {"system.xsd"};
 172         InputSource[] returnValues = {new InputSource(new StringReader(dtd_systemResolved))};
 173         MyEntityHandler entityHandler = new MyEntityHandler(systemIds, returnValues, elementInSystem);
 174         return new Object[][]{
 175             {false, true, xml_catalog, xml_system, new MyHandler(elementInSystem), expectedWCatalog},
 176             {false, true, xml_catalog, xml_system, entityHandler, expectedWResolver},
 177             {true, true, xml_catalog, xml_system, entityHandler, expectedWResolver}
 178         };
 179     }
 180 
 181     /*
 182        DataProvider: for testing XInclude
 183        Data: set use_catalog, use_catalog, catalog file, xml file, handler, expected result string
 184      */
 185     @DataProvider(name = "data_XIA")
 186     public Object[][] getDataXI() {
 187         String[] systemIds = {"XI_simple.xml"};
 188         InputSource[] returnValues = {new InputSource(xml_xIncludeSimple)};
 189         MyEntityHandler entityHandler = new MyEntityHandler(systemIds, returnValues, elementInXISimple);
 190         return new Object[][]{
 191             {false, true, xml_catalog, xml_xInclude, new MyHandler(elementInXISimple), contentInUIutf8Catalog},
 192             {false, true, xml_catalog, xml_xInclude, entityHandler, contentInXIutf8},
 193             {true, true, xml_catalog, xml_xInclude, entityHandler, contentInXIutf8}
 194         };
 195     }
 196 
 197     /*
 198        DataProvider: for testing DOM parser
 199        Data: set use_catalog, use_catalog, catalog file, xml file, handler, expected result string
 200      */
 201     @DataProvider(name = "data_DOMA")
 202     public Object[][] getDataDOM() {
 203         String[] systemIds = {"system.xsd"};
 204         InputSource[] returnValues = {new InputSource(new StringReader(dtd_systemResolved))};
 205         MyEntityHandler entityHandler = new MyEntityHandler(systemIds, returnValues, elementInSystem);
 206         return new Object[][]{
 207             {false, true, xml_catalog, xml_system, new MyHandler(elementInSystem), expectedWCatalog},
 208             {false, true, xml_catalog, xml_system, getMyEntityHandler(elementInSystem, systemIds,
 209                     new InputSource(new StringReader(dtd_systemResolved))), expectedWResolver},
 210             {true, true, xml_catalog, xml_system, getMyEntityHandler(elementInSystem, systemIds,
 211                     new InputSource(new StringReader(dtd_systemResolved))), expectedWResolver}
 212         };
 213     }
 214 
 215     MyEntityHandler getMyEntityHandler(String elementName, String[] systemIds, InputSource... returnValues) {
 216        return new MyEntityHandler(systemIds, returnValues, elementName);
 217     }
 218 
 219     /*
 220        DataProvider: for testing Schema validation
 221        Data: set use_catalog, use_catalog, catalog file, xsd file, a LSResourceResolver
 222      */
 223     @DataProvider(name = "data_SchemaA")
 224     public Object[][] getDataSchema() {
 225         String[] systemIds = {"pathto/XMLSchema.dtd", "datatypes.dtd"};
 226         XmlInput[] returnValues = {new XmlInput(null, dtd_xmlSchema, null), new XmlInput(null, dtd_datatypes, null)};
 227         LSResourceResolver resolver = new SourceResolver(null, systemIds, returnValues);
 228 
 229         String[] systemIds1 = {"xml.xsd"};
 230         XmlInput[] returnValues1 = {new XmlInput(null, xsd_xml, null)};
 231         LSResourceResolver resolverImport = new SourceResolver(null, systemIds1, returnValues1);
 232 
 233         String[] systemIds2 = {"XSDInclude_person.xsd", "XSDInclude_product.xsd"};
 234         XmlInput[] returnValues2 = {new XmlInput(null, xsd_include_person, null),
 235                         new XmlInput(null, xsd_include_product, null)};
 236         LSResourceResolver resolverInclude = new SourceResolver(null, systemIds2, returnValues2);
 237 
 238         return new Object[][]{
 239             // for resolving DTD in xsd
 240             {false, true, xml_catalog, xsd_xmlSchema, null},
 241             {false, true, xml_bogus_catalog, xsd_xmlSchema, resolver},
 242             {true, true, xml_bogus_catalog, xsd_xmlSchema, resolver},
 243             // for resolving xsd import
 244             {false, true, xml_catalog, xsd_xmlSchema_import, null},
 245             {false, true, xml_bogus_catalog, xsd_xmlSchema_import, resolverImport},
 246             {true, true, xml_bogus_catalog, xsd_xmlSchema_import, resolverImport},
 247             // for resolving xsd include
 248             {false, true, xml_catalog, xsd_include_company, null},
 249             {false, true, xml_bogus_catalog, xsd_include_company, resolverInclude},
 250             {true, true, xml_bogus_catalog, xsd_include_company, resolverInclude}
 251         };
 252     }
 253 
 254     /*
 255        DataProvider: for testing Schema Validator
 256        Data: source, resolver1, resolver2, catalog1, a catalog2
 257      */
 258     @DataProvider(name = "data_ValidatorA")
 259     public Object[][] getDataValidator() {
 260         DOMSource ds = getDOMSource(xml_val_test, xml_val_test_id, false, true, xml_catalog);
 261 
 262         SAXSource ss = new SAXSource(new InputSource(xml_val_test));
 263         ss.setSystemId(xml_val_test_id);
 264 
 265         StAXSource stax = getStaxSource(xml_val_test, xml_val_test_id);
 266         StAXSource stax1 = getStaxSource(xml_val_test, xml_val_test_id);
 267 
 268         StreamSource source = new StreamSource(new File(xml_val_test));
 269 
 270         String[] systemIds = {"system.dtd", "val_test.xsd"};
 271         XmlInput[] returnValues = {new XmlInput(null, dtd_system, null), new XmlInput(null, xsd_val_test, null)};
 272         LSResourceResolver resolver = new SourceResolver(null, systemIds, returnValues);
 273 
 274         StAXSource stax2 = getStaxSource(xml_val_test, xml_val_test_id);
 275         StAXSource stax3 = getStaxSource(xml_val_test, xml_val_test_id);
 276 
 277         return new Object[][]{
 278             // use catalog
 279             {false, false, true, ds, null, null, xml_catalog, null},
 280             {false, false, true, ds, null, null, null, xml_catalog},
 281             {false, false, true, ss, null, null, xml_catalog, null},
 282             {false, false, true, ss, null, null, null, xml_catalog},
 283             {false, false, true, stax, null, null, xml_catalog, null},
 284             {false, false, true, stax1, null, null, null, xml_catalog},
 285             {false, false, true, source, null, null, xml_catalog, null},
 286             {false, false, true, source, null, null, null, xml_catalog},
 287             // use resolver
 288             {false, false, true, ds, resolver, resolver, xml_bogus_catalog, xml_bogus_catalog},
 289             {false, false, true, ss, resolver, resolver, xml_bogus_catalog, xml_bogus_catalog},
 290             {false, false, true, stax2, resolver, resolver, xml_bogus_catalog, xml_bogus_catalog},
 291             {false, false, true, source, resolver, resolver, xml_bogus_catalog, xml_bogus_catalog}
 292         };
 293     }
 294 
 295     /*
 296        DataProvider: for testing XSL import and include
 297        Data: set use_catalog, use_catalog, catalog file, xsl file, xml file, a URIResolver, expected result
 298      */
 299     @DataProvider(name = "data_XSLA")
 300     public Object[][] getDataXSL() {
 301         // XSLInclude.xsl has one import XSLImport_html.xsl and two includes,
 302         // XSLInclude_header.xsl and XSLInclude_footer.xsl;
 303         String[] hrefs = {"XSLImport_html.xsl", "XSLInclude_header.xsl", "XSLInclude_footer.xsl"};
 304         Source[] returnValues = {new StreamSource(xsl_import_html),
 305                         new StreamSource(xsl_include_header),
 306                         new StreamSource(xsl_include_footer)};
 307         URIResolver resolver = new XslResolver(hrefs, returnValues);
 308         SAXSource xslSourceDTD = new SAXSource(new InputSource(new StringReader(xsl_includeDTD)));
 309         StreamSource xmlSourceDTD = new StreamSource(new StringReader(xml_xslDTD));
 310 
 311         String[] hrefs1 = {"pathto/DocFunc2.xml"};
 312         Source[] returnValues1 = {new StreamSource(xml_doc2)};
 313         URIResolver docResolver = new XslResolver(hrefs1, returnValues1);
 314         SAXSource xslDocSource = new SAXSource(new InputSource(new File(xsl_doc).toURI().toASCIIString()));
 315         StreamSource xmlDocSource = new StreamSource(new File(xml_doc));
 316         return new Object[][]{
 317             // for resolving DTD, import and include in xsl
 318             {false, true, xml_catalog, xslSourceDTD, xmlSourceDTD, null, ""},
 319             {false, true, xml_bogus_catalog, new SAXSource(new InputSource(new StringReader(xsl_include))),
 320                 new StreamSource(new StringReader(xml_xsl)), resolver, ""},
 321             {true, true, xml_bogus_catalog, new SAXSource(new InputSource(new StringReader(xsl_include))),
 322                 new StreamSource(new StringReader(xml_xsl)), resolver, ""},
 323             // for resolving reference by the document function
 324             {false, true, xml_catalog, xslDocSource, xmlDocSource, null, "Resolved by a catalog"},
 325             {false, true, xml_bogus_catalog, xslDocSource, xmlDocSource, docResolver, "Resolved by a resolver"},
 326             {true, true, xml_bogus_catalog, xslDocSource, xmlDocSource, docResolver, "Resolved by a resolver"}
 327         };
 328     }
 329 }