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