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