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.catalog.CatalogFeatures.Feature;
  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 import org.testng.annotations.AfterClass;
  36 import org.testng.annotations.BeforeClass;
  37 import org.testng.annotations.DataProvider;
  38 import org.testng.annotations.Test;
  39 import org.w3c.dom.ls.LSResourceResolver;
  40 import org.xml.sax.InputSource;
  41 
  42 /*
  43  * @bug 8158084 8162435 8162438 8162442
  44  * @summary extends CatalogSupport, verifies that the catalog file can be set
  45  * using the System property.
  46  */
  47 
  48 /**
  49  * The name of a System property in javax.xml.catalog is the same as that of the
  50  * property, and can be read through CatalogFeatures.Feature.
  51  *
  52  * @author huizhe.wang@oracle.com
  53  */
  54 public class CatalogSupport1 extends CatalogSupportBase {
  55     /*
  56      * Initializing fields
  57      */
  58     @BeforeClass
  59     public void setUpClass() throws Exception {
  60         setUp();
  61         System.setProperty(Feature.FILES.getPropertyName(), xml_catalog);
  62     }
  63 
  64     @AfterClass
  65     public void tearDownClass() throws Exception {
  66         System.clearProperty(Feature.FILES.getPropertyName());
  67     }
  68 
  69     /*
  70        Verifies the Catalog support on SAXParser.
  71     */
  72     @Test(dataProvider = "data_SAXC")
  73     public void testSAXC(boolean setUseCatalog, boolean useCatalog, String catalog, String xml, MyHandler handler, String expected) throws Exception {
  74         testSAX(setUseCatalog, useCatalog, catalog, xml, handler, expected);
  75     }
  76 
  77     /*
  78        Verifies the Catalog support on XMLReader.
  79     */
  80     @Test(dataProvider = "data_SAXC")
  81     public void testXMLReaderC(boolean setUseCatalog, boolean useCatalog, String catalog, String xml, MyHandler handler, String expected) throws Exception {
  82         testXMLReader(setUseCatalog, useCatalog, catalog, xml, handler, expected);
  83     }
  84 
  85     /*
  86        Verifies the Catalog support on XInclude.
  87     */
  88     @Test(dataProvider = "data_XIC")
  89     public void testXIncludeC(boolean setUseCatalog, boolean useCatalog, String catalog, String xml, MyHandler handler, String expected) throws Exception {
  90         testXInclude(setUseCatalog, useCatalog, catalog, xml, handler, expected);
  91     }
  92 
  93     /*
  94        Verifies the Catalog support on DOM parser.
  95     */
  96     @Test(dataProvider = "data_DOMC")
  97     public void testDOMC(boolean setUseCatalog, boolean useCatalog, String catalog, String xml, MyHandler handler, String expected) throws Exception {
  98         testDOM(setUseCatalog, useCatalog, catalog, xml, handler, expected);
  99     }
 100 
 101     /*
 102        Verifies the Catalog support on resolving DTD, xsd import and include in
 103     Schema files.
 104     */
 105     @Test(dataProvider = "data_SchemaC")
 106     public void testValidationC(boolean setUseCatalog, boolean useCatalog, String catalog, String xsd, LSResourceResolver resolver)
 107             throws Exception {
 108 
 109         testValidation(setUseCatalog, useCatalog, catalog, xsd, resolver) ;
 110     }
 111 
 112     /*
 113        @bug 8158084 8162435 8162438 these tests also verifies the fix for 8162435 and 8162438
 114        Verifies the Catalog support on the Schema Validator.
 115     */
 116     @Test(dataProvider = "data_ValidatorC")
 117     public void testValidatorA(boolean setUseCatalog1, boolean setUseCatalog2, boolean useCatalog,
 118             Source source, LSResourceResolver resolver1, LSResourceResolver resolver2,
 119             String catalog1, String catalog2)
 120             throws Exception {
 121         testValidator(setUseCatalog1, setUseCatalog2, useCatalog, source,
 122                 resolver1, resolver2, catalog1, catalog2);
 123     }
 124 
 125     /*
 126        Verifies the Catalog support on resolving DTD, xsl import and include in
 127     XSL files.
 128     */
 129     @Test(dataProvider = "data_XSLC")
 130     public void testXSLImportC(boolean setUseCatalog, boolean useCatalog, String catalog, SAXSource xsl, StreamSource xml,
 131         URIResolver resolver, String expected) throws Exception {
 132 
 133         testXSLImport(setUseCatalog, useCatalog, catalog, xsl, xml, resolver, expected);
 134     }
 135 
 136     /*
 137        @bug 8158084 8162442
 138        Verifies the Catalog support on resolving DTD, xsl import and include in
 139     XSL files.
 140     */
 141     @Test(dataProvider = "data_XSLC")
 142     public void testXSLImportWTemplatesC(boolean setUseCatalog, boolean useCatalog, String catalog, SAXSource xsl, StreamSource xml,
 143         URIResolver resolver, String expected) throws Exception {
 144         testXSLImportWTemplates(setUseCatalog, useCatalog, catalog, xsl, xml, resolver, expected);
 145     }
 146 
 147     /*
 148        DataProvider: for testing the SAX parser
 149        Data: set use_catalog, use_catalog, catalog file, xml file, handler, expected result string
 150      */
 151     @DataProvider(name = "data_SAXC")
 152     Object[][] getDataSAXC() {
 153         return new Object[][]{
 154             {false, true, null, xml_system, new MyHandler(elementInSystem), expectedWCatalog}
 155 
 156         };
 157     }
 158 
 159     /*
 160        DataProvider: for testing XInclude
 161        Data: set use_catalog, use_catalog, catalog file, xml file, handler, expected result string
 162      */
 163     @DataProvider(name = "data_XIC")
 164     Object[][] getDataXIC() {
 165         return new Object[][]{
 166             {false, true, null, xml_xInclude, new MyHandler(elementInXISimple), contentInUIutf8Catalog},
 167         };
 168     }
 169 
 170     /*
 171        DataProvider: for testing DOM parser
 172        Data: set use_catalog, use_catalog, catalog file, xml file, handler, expected result string
 173      */
 174     @DataProvider(name = "data_DOMC")
 175     Object[][] getDataDOMC() {
 176         return new Object[][]{
 177             {false, true, null, xml_system, new MyHandler(elementInSystem), expectedWCatalog}
 178         };
 179     }
 180 
 181     /*
 182        DataProvider: for testing Schema validation
 183        Data: set use_catalog, use_catalog, catalog file, xsd file, a LSResourceResolver
 184      */
 185     @DataProvider(name = "data_SchemaC")
 186     Object[][] getDataSchemaC() {
 187 
 188         return new Object[][]{
 189             // for resolving DTD in xsd
 190             {false, true, null, xsd_xmlSchema, null},
 191             // for resolving xsd import
 192             {false, true, null, xsd_xmlSchema_import, null},
 193             // for resolving xsd include
 194             {false, true, null, xsd_include_company, null}
 195         };
 196     }
 197 
 198 
 199     /*
 200        DataProvider: for testing Schema Validator
 201        Data: source, resolver1, resolver2, catalog1, a catalog2
 202      */
 203     @DataProvider(name = "data_ValidatorC")
 204     Object[][] getDataValidator() {
 205         DOMSource ds = getDOMSource(xml_val_test, xml_val_test_id, false, true, null);
 206 
 207         SAXSource ss = new SAXSource(new InputSource(xml_val_test));
 208         ss.setSystemId(xml_val_test_id);
 209 
 210         StAXSource stax = getStaxSource(xml_val_test, xml_val_test_id);
 211         StAXSource stax1 = getStaxSource(xml_val_test, xml_val_test_id);
 212 
 213         StreamSource source = new StreamSource(new File(xml_val_test));
 214 
 215         String[] systemIds = {"system.dtd", "val_test.xsd"};
 216         XmlInput[] returnValues = {new XmlInput(null, dtd_system, null), new XmlInput(null, xsd_val_test, null)};
 217         LSResourceResolver resolver = new SourceResolver(null, systemIds, returnValues);
 218 
 219         StAXSource stax2 = getStaxSource(xml_val_test, xml_val_test_id);
 220         StAXSource stax3 = getStaxSource(xml_val_test, xml_val_test_id);
 221 
 222         return new Object[][]{
 223             // use catalog
 224             {false, false, true, ds, null, null, null, null},
 225             {false, false, true, ds, null, null, null, null},
 226             {false, false, true, ss, null, null, null, null},
 227             {false, false, true, ss, null, null, null, null},
 228             {false, false, true, stax, null, null, null, null},
 229             {false, false, true, stax1, null, null, null, null},
 230             {false, false, true, source, null, null, null, null},
 231             {false, false, true, source, null, null, null, null},
 232             // use resolver
 233             {false, false, true, ds, resolver, null, xml_bogus_catalog, null},
 234             {false, false, true, ds, null, resolver, null, xml_bogus_catalog},
 235             {false, false, true, ss, resolver, null, xml_bogus_catalog, null},
 236             {false, false, true, ss, null, resolver, null, xml_bogus_catalog},
 237             {false, false, true, stax2, resolver, null, xml_bogus_catalog, null},
 238             {false, false, true, stax3, null, resolver, null, xml_bogus_catalog},
 239             {false, false, true, source, resolver, null, xml_bogus_catalog, null},
 240             {false, false, true, source, null, resolver, null, xml_bogus_catalog}
 241         };
 242     }
 243 
 244     /*
 245        DataProvider: for testing XSL import and include
 246        Data: set use_catalog, use_catalog, catalog file, xsl file, xml file, a URIResolver, expected
 247      */
 248     @DataProvider(name = "data_XSLC")
 249     Object[][] getDataXSLC() {
 250         SAXSource xslSourceDTD = new SAXSource(new InputSource(new StringReader(xsl_includeDTD)));
 251         StreamSource xmlSourceDTD = new StreamSource(new StringReader(xml_xslDTD));
 252 
 253         SAXSource xslDocSource = new SAXSource(new InputSource(new File(xsl_doc).toURI().toASCIIString()));
 254         StreamSource xmlDocSource = new StreamSource(new File(xml_doc));
 255         return new Object[][]{
 256             // for resolving DTD, import and include in xsl
 257             {false, true, null, xslSourceDTD, xmlSourceDTD, null, ""},
 258             // for resolving reference by the document function
 259             {false, true, null, xslDocSource, xmlDocSource, null, "Resolved by a catalog"},
 260         };
 261     }
 262 
 263 }