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.stream.XMLResolver;
  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 8166220
  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 XMLStreamReader.
 118     */
 119     @Test(dataProvider = "data_StAXA")
 120     public void testStAXA(boolean setUseCatalog, boolean useCatalog, String catalog,
 121             String xml, XMLResolver resolver, String expected) throws Exception {
 122         testStAX(setUseCatalog, useCatalog, catalog, xml, resolver, expected);
 123     }
 124 
 125     /*
 126        Verifies the Catalog support on resolving DTD, xsd import and include in
 127     Schema files.
 128     */
 129     @Test(dataProvider = "data_SchemaA")
 130     public void testValidationA(boolean setUseCatalog, boolean useCatalog,
 131             String catalog, String xsd, LSResourceResolver resolver)
 132             throws Exception {
 133 
 134         testValidation(setUseCatalog, useCatalog, catalog, xsd, resolver) ;
 135     }
 136 
 137     /*
 138        @bug 8158084 8162438 these tests also verifies the fix for 8162438
 139        Verifies the Catalog support on the Schema Validator.
 140     */
 141     @Test(dataProvider = "data_ValidatorA")
 142     public void testValidatorA(boolean setUseCatalog1, boolean setUseCatalog2, boolean useCatalog,
 143             Source source, LSResourceResolver resolver1, LSResourceResolver resolver2,
 144             String catalog1, String catalog2)
 145             throws Exception {
 146         testValidator(setUseCatalog1, setUseCatalog2, useCatalog, source,
 147                 resolver1, resolver2, catalog1, catalog2);
 148     }
 149 
 150     /*
 151        Verifies the Catalog support on resolving DTD, xsl import and include in
 152     XSL files.
 153     */
 154     @Test(dataProvider = "data_XSLA")
 155     public void testXSLImportA(boolean setUseCatalog, boolean useCatalog, String catalog,
 156             SAXSource xsl, StreamSource xml, URIResolver resolver, String expected)
 157             throws Exception {
 158 
 159         testXSLImport(setUseCatalog, useCatalog, catalog, xsl, xml, resolver, expected);
 160     }
 161 
 162     /*
 163        @bug 8158084 8162442
 164        Verifies the Catalog support on resolving DTD, xsl import and include in
 165     XSL files.
 166     */
 167     @Test(dataProvider = "data_XSLA")
 168     public void testXSLImportWTemplatesA(boolean setUseCatalog, boolean useCatalog,
 169             String catalog, SAXSource xsl, StreamSource xml, URIResolver resolver, String expected)
 170             throws Exception {
 171         testXSLImportWTemplates(setUseCatalog, useCatalog, catalog, xsl, xml, resolver, expected);
 172     }
 173 
 174     /*
 175        DataProvider: for testing the SAX parser
 176        Data: set use_catalog, use_catalog, catalog file, xml file, handler, expected result string
 177      */
 178     @DataProvider(name = "data_SAXA")
 179     public Object[][] getDataSAX() {
 180         String[] systemIds = {"system.xsd"};
 181         InputSource[] returnValues = {new InputSource(new StringReader(dtd_systemResolved))};
 182         MyEntityHandler entityHandler = new MyEntityHandler(systemIds, returnValues, elementInSystem);
 183         return new Object[][]{
 184             {false, true, xml_catalog, xml_system, new MyHandler(elementInSystem), expectedWCatalog},
 185             {false, true, xml_catalog, xml_system, entityHandler, expectedWResolver},
 186             {true, true, xml_catalog, xml_system, entityHandler, expectedWResolver}
 187         };
 188     }
 189 
 190     /*
 191        DataProvider: for testing XInclude
 192        Data: set use_catalog, use_catalog, catalog file, xml file, handler, expected result string
 193      */
 194     @DataProvider(name = "data_XIA")
 195     public Object[][] getDataXI() {
 196         String[] systemIds = {"XI_simple.xml"};
 197         InputSource[] returnValues = {new InputSource(xml_xIncludeSimple)};
 198         MyEntityHandler entityHandler = new MyEntityHandler(systemIds, returnValues, elementInXISimple);
 199         return new Object[][]{
 200             {false, true, xml_catalog, xml_xInclude, new MyHandler(elementInXISimple), contentInUIutf8Catalog},
 201             {false, true, xml_catalog, xml_xInclude, entityHandler, contentInXIutf8},
 202             {true, true, xml_catalog, xml_xInclude, entityHandler, contentInXIutf8}
 203         };
 204     }
 205 
 206     /*
 207        DataProvider: for testing DOM parser
 208        Data: set use_catalog, use_catalog, catalog file, xml file, handler, expected result string
 209      */
 210     @DataProvider(name = "data_DOMA")
 211     public Object[][] getDataDOM() {
 212         String[] systemIds = {"system.xsd"};
 213         InputSource[] returnValues = {new InputSource(new StringReader(dtd_systemResolved))};
 214         MyEntityHandler entityHandler = new MyEntityHandler(systemIds, returnValues, elementInSystem);
 215         return new Object[][]{
 216             {false, true, xml_catalog, xml_system, new MyHandler(elementInSystem), expectedWCatalog},
 217             {false, true, xml_catalog, xml_system, getMyEntityHandler(elementInSystem, systemIds,
 218                     new InputSource(new StringReader(dtd_systemResolved))), expectedWResolver},
 219             {true, true, xml_catalog, xml_system, getMyEntityHandler(elementInSystem, systemIds,
 220                     new InputSource(new StringReader(dtd_systemResolved))), expectedWResolver}
 221         };
 222     }
 223 
 224     /*
 225        DataProvider: for testing the StAX parser
 226        Data: set use_catalog, use_catalog, catalog file, xml file, handler, expected result string
 227      */
 228     @DataProvider(name = "data_StAXA")
 229     public Object[][] getDataStAX() {
 230 
 231         return new Object[][]{
 232             {false, true, xml_catalog, xml_system, null, expectedWCatalog},
 233             {false, true, xml_catalog, xml_system, null, expectedWResolver},
 234             {true, true, xml_catalog, xml_system, null, expectedWResolver}
 235         };
 236     }
 237 
 238     MyEntityHandler getMyEntityHandler(String elementName, String[] systemIds, InputSource... returnValues) {
 239        return new MyEntityHandler(systemIds, returnValues, elementName);
 240     }
 241 
 242     /*
 243        DataProvider: for testing Schema validation
 244        Data: set use_catalog, use_catalog, catalog file, xsd file, a LSResourceResolver
 245      */
 246     @DataProvider(name = "data_SchemaA")
 247     public Object[][] getDataSchema() {
 248         String[] systemIds = {"pathto/XMLSchema.dtd", "datatypes.dtd"};
 249         XmlInput[] returnValues = {new XmlInput(null, dtd_xmlSchema, null), new XmlInput(null, dtd_datatypes, null)};
 250         LSResourceResolver resolver = new SourceResolver(null, systemIds, returnValues);
 251 
 252         String[] systemIds1 = {"xml.xsd"};
 253         XmlInput[] returnValues1 = {new XmlInput(null, xsd_xml, null)};
 254         LSResourceResolver resolverImport = new SourceResolver(null, systemIds1, returnValues1);
 255 
 256         String[] systemIds2 = {"XSDInclude_person.xsd", "XSDInclude_product.xsd"};
 257         XmlInput[] returnValues2 = {new XmlInput(null, xsd_include_person, null),
 258                         new XmlInput(null, xsd_include_product, null)};
 259         LSResourceResolver resolverInclude = new SourceResolver(null, systemIds2, returnValues2);
 260 
 261         return new Object[][]{
 262             // for resolving DTD in xsd
 263             {false, true, xml_catalog, xsd_xmlSchema, null},
 264             {false, true, xml_bogus_catalog, xsd_xmlSchema, resolver},
 265             {true, true, xml_bogus_catalog, xsd_xmlSchema, resolver},
 266             // for resolving xsd import
 267             {false, true, xml_catalog, xsd_xmlSchema_import, null},
 268             {false, true, xml_bogus_catalog, xsd_xmlSchema_import, resolverImport},
 269             {true, true, xml_bogus_catalog, xsd_xmlSchema_import, resolverImport},
 270             // for resolving xsd include
 271             {false, true, xml_catalog, xsd_include_company, null},
 272             {false, true, xml_bogus_catalog, xsd_include_company, resolverInclude},
 273             {true, true, xml_bogus_catalog, xsd_include_company, resolverInclude}
 274         };
 275     }
 276 
 277     /*
 278        DataProvider: for testing Schema Validator
 279        Data: source, resolver1, resolver2, catalog1, a catalog2
 280      */
 281     @DataProvider(name = "data_ValidatorA")
 282     public Object[][] getDataValidator() {
 283         DOMSource ds = getDOMSource(xml_val_test, xml_val_test_id, false, true, xml_catalog);
 284 
 285         SAXSource ss = new SAXSource(new InputSource(xml_val_test));
 286         ss.setSystemId(xml_val_test_id);
 287 
 288         StAXSource stax = getStaxSource(xml_val_test, xml_val_test_id, false, true, xml_catalog);
 289         StAXSource stax1 = getStaxSource(xml_val_test, xml_val_test_id, false, true, xml_catalog);
 290 
 291         StreamSource source = new StreamSource(new File(xml_val_test));
 292 
 293         String[] systemIds = {"system.dtd", "val_test.xsd"};
 294         XmlInput[] returnValues = {new XmlInput(null, dtd_system, null), new XmlInput(null, xsd_val_test, null)};
 295         LSResourceResolver resolver = new SourceResolver(null, systemIds, returnValues);
 296 
 297         StAXSource stax2 = getStaxSource(xml_val_test, xml_val_test_id, false, true, xml_catalog);
 298 
 299         return new Object[][]{
 300             // use catalog
 301             {false, false, true, ds, null, null, xml_catalog, null},
 302             {false, false, true, ds, null, null, null, xml_catalog},
 303             {false, false, true, ss, null, null, xml_catalog, null},
 304             {false, false, true, ss, null, null, null, xml_catalog},
 305             {false, false, true, stax, null, null, xml_catalog, null},
 306             {false, false, true, stax1, null, null, null, xml_catalog},
 307             {false, false, true, source, null, null, xml_catalog, null},
 308             {false, false, true, source, null, null, null, xml_catalog},
 309             // use resolver
 310             {false, false, true, ds, resolver, resolver, xml_bogus_catalog, xml_bogus_catalog},
 311             {false, false, true, ss, resolver, resolver, xml_bogus_catalog, xml_bogus_catalog},
 312             {false, false, true, stax2, resolver, resolver, xml_bogus_catalog, xml_bogus_catalog},
 313             {false, false, true, source, resolver, resolver, xml_bogus_catalog, xml_bogus_catalog}
 314         };
 315     }
 316 
 317     /*
 318        DataProvider: for testing XSL import and include
 319        Data: set use_catalog, use_catalog, catalog file, xsl file, xml file, a URIResolver, expected result
 320      */
 321     @DataProvider(name = "data_XSLA")
 322     public Object[][] getDataXSL() {
 323         // XSLInclude.xsl has one import XSLImport_html.xsl and two includes,
 324         // XSLInclude_header.xsl and XSLInclude_footer.xsl;
 325         String[] hrefs = {"XSLImport_html.xsl", "XSLInclude_header.xsl", "XSLInclude_footer.xsl"};
 326         Source[] returnValues = {new StreamSource(xsl_import_html),
 327                         new StreamSource(xsl_include_header),
 328                         new StreamSource(xsl_include_footer)};
 329         URIResolver resolver = new XslResolver(hrefs, returnValues);
 330         SAXSource xslSourceDTD = new SAXSource(new InputSource(new StringReader(xsl_includeDTD)));
 331         StreamSource xmlSourceDTD = new StreamSource(new StringReader(xml_xslDTD));
 332 
 333         String[] hrefs1 = {"pathto/DocFunc2.xml"};
 334         Source[] returnValues1 = {new StreamSource(xml_doc2)};
 335         URIResolver docResolver = new XslResolver(hrefs1, returnValues1);
 336         SAXSource xslDocSource = new SAXSource(new InputSource(new File(xsl_doc).toURI().toASCIIString()));
 337         StreamSource xmlDocSource = new StreamSource(new File(xml_doc));
 338         return new Object[][]{
 339             // for resolving DTD, import and include in xsl
 340             {false, true, xml_catalog, xslSourceDTD, xmlSourceDTD, null, ""},
 341             {false, true, xml_bogus_catalog, new SAXSource(new InputSource(new StringReader(xsl_include))),
 342                 new StreamSource(new StringReader(xml_xsl)), resolver, ""},
 343             {true, true, xml_bogus_catalog, new SAXSource(new InputSource(new StringReader(xsl_include))),
 344                 new StreamSource(new StringReader(xml_xsl)), resolver, ""},
 345             // for resolving reference by the document function
 346             {false, true, xml_catalog, xslDocSource, xmlDocSource, null, "Resolved by a catalog"},
 347             {false, true, xml_bogus_catalog, xslDocSource, xmlDocSource, docResolver, "Resolved by a resolver"},
 348             {true, true, xml_bogus_catalog, xslDocSource, xmlDocSource, docResolver, "Resolved by a resolver"}
 349         };
 350     }
 351 }