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.getSystemProperty;
  27 import static jaxp.library.JAXPTestUtilities.setSystemProperty;
  28 
  29 import java.io.File;
  30 import java.io.IOException;
  31 import java.io.StringReader;
  32 import javax.xml.transform.Source;
  33 import javax.xml.transform.TransformerException;
  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 import org.xml.sax.SAXException;
  48 import org.xml.sax.SAXParseException;
  49 
  50 /*
  51  * @test
  52  * @bug 8158084 8162438 8162442 8163535
  53  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  54  * @run testng/othervm -DrunSecMngr=true catalog.CatalogSupport3
  55  * @run testng/othervm catalog.CatalogSupport3
  56  * @summary extends CatalogSupport tests, verifies that the use of the Catalog may
  57  * be disabled through the API property.
  58  */
  59 
  60 /**
  61  * For all of the JAXP processors that support the Catalog, the use of the Catalog
  62  * is turned on by default. It can then be turned off through the API and the
  63  * System property.
  64  *
  65  * @author huizhe.wang@oracle.com
  66  */
  67 @Listeners({jaxp.library.FilePolicy.class, jaxp.library.NetAccessPolicy.class})
  68 public class CatalogSupport3 extends CatalogSupportBase {
  69     static final String TTIMEOUTREAD = "sun.net.client.defaultReadTimeout";
  70     static final String TIMEOUTCONNECT = "sun.net.client.defaultConnectTimeout";
  71     static String timeoutRead = getSystemProperty(TTIMEOUTREAD);
  72     static String timeoutConnect = getSystemProperty(TIMEOUTCONNECT);
  73     /*
  74      * Initializing fields
  75      */
  76     @BeforeClass
  77     public void setUpClass() throws Exception {
  78         setUp();
  79         timeoutRead = getSystemProperty(TTIMEOUTREAD);
  80         timeoutConnect = getSystemProperty(TIMEOUTCONNECT);
  81         setSystemProperty(TTIMEOUTREAD, "1000");
  82         setSystemProperty(TIMEOUTCONNECT, "1000");
  83     }
  84 
  85     @AfterClass
  86     public void tearDownClass() throws Exception {
  87         setSystemProperty(TIMEOUTCONNECT, "-1");
  88         setSystemProperty(TTIMEOUTREAD, "-1");
  89     }
  90 
  91     /*
  92        Verifies the Catalog support on SAXParser.
  93     */
  94     @Test(dataProvider = "data_SAXC", expectedExceptions = IOException.class)
  95     public void testSAXC(boolean setUseCatalog, boolean useCatalog, String catalog,
  96             String xml, MyHandler handler, String expected) throws Exception {
  97         testSAX(setUseCatalog, useCatalog, catalog, xml, handler, expected);
  98     }
  99 
 100     /*
 101        Verifies the Catalog support on XMLReader.
 102     */
 103     @Test(dataProvider = "data_SAXC", expectedExceptions = IOException.class)
 104     public void testXMLReaderC(boolean setUseCatalog, boolean useCatalog, String catalog,
 105             String xml, MyHandler handler, String expected) throws Exception {
 106         testXMLReader(setUseCatalog, useCatalog, catalog, xml, handler, expected);
 107     }
 108 
 109     /*
 110        Verifies the Catalog support on XInclude.
 111     */
 112     @Test(dataProvider = "data_XIC", expectedExceptions = SAXParseException.class)
 113     public void testXIncludeC(boolean setUseCatalog, boolean useCatalog, String catalog,
 114             String xml, MyHandler handler, String expected) throws Exception {
 115         testXInclude(setUseCatalog, useCatalog, catalog, xml, handler, expected);
 116     }
 117 
 118     /*
 119        Verifies the Catalog support on DOM parser.
 120     */
 121     @Test(dataProvider = "data_DOMC", expectedExceptions = IOException.class)
 122     public void testDOMC(boolean setUseCatalog, boolean useCatalog, String catalog,
 123             String xml, MyHandler handler, String expected) throws Exception {
 124         testDOM(setUseCatalog, useCatalog, catalog, xml, handler, expected);
 125     }
 126 
 127     /*
 128        Verifies the Catalog support on resolving DTD, xsd import and include in
 129     Schema files.
 130     */
 131     @Test(dataProvider = "data_SchemaC", expectedExceptions = SAXParseException.class)
 132     public void testValidationC(boolean setUseCatalog, boolean useCatalog, String catalog,
 133             String xsd, LSResourceResolver resolver)
 134             throws Exception {
 135         testValidation(setUseCatalog, useCatalog, catalog, xsd, resolver) ;
 136     }
 137 
 138     /*
 139        @bug 8158084 8162438 these tests also verifies the fix for 8162438
 140        Verifies the Catalog support on the Schema Validator.
 141     */
 142     @Test(dataProvider = "data_ValidatorC", expectedExceptions = {SAXException.class, IOException.class})
 143     public void testValidatorC(boolean setUseCatalog1, boolean setUseCatalog2, boolean useCatalog,
 144             Source source, LSResourceResolver resolver1, LSResourceResolver resolver2,
 145             String catalog1, String catalog2)
 146             throws Exception {
 147         testValidator(setUseCatalog1, setUseCatalog2, useCatalog, source,
 148                 resolver1, resolver2, catalog1, catalog2);
 149     }
 150 
 151     /*
 152        Verifies the Catalog support on resolving DTD, xsl import and include in
 153     XSL files.
 154     */
 155     @Test(dataProvider = "data_XSLC", expectedExceptions = TransformerException.class)
 156     public void testXSLImportC(boolean setUseCatalog, boolean useCatalog, String catalog, SAXSource xsl, StreamSource xml,
 157         URIResolver resolver, String expected) 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_XSLC", expectedExceptions = TransformerException.class)
 168     public void testXSLImportWTemplatesC(boolean setUseCatalog, boolean useCatalog, String catalog,
 169             SAXSource xsl, StreamSource xml,
 170         URIResolver resolver, String expected) 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_SAXC")
 179     public Object[][] getDataSAXC() {
 180         return new Object[][]{
 181             {true, false, xml_catalog, xml_system, new MyHandler(elementInSystem), expectedWCatalog}
 182 
 183         };
 184     }
 185 
 186     /*
 187        DataProvider: for testing XInclude
 188        Data: set use_catalog, use_catalog, catalog file, xml file, handler, expected result string
 189      */
 190     @DataProvider(name = "data_XIC")
 191     public Object[][] getDataXIC() {
 192         return new Object[][]{
 193             {true, false, xml_catalog, xml_xInclude, new MyHandler(elementInXISimple), contentInUIutf8Catalog},
 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_DOMC")
 202     public Object[][] getDataDOMC() {
 203         return new Object[][]{
 204             {true, false, xml_catalog, xml_system, new MyHandler(elementInSystem), expectedWCatalog}
 205         };
 206     }
 207 
 208     /*
 209        DataProvider: for testing Schema validation
 210        Data: set use_catalog, use_catalog, catalog file, xsd file, a LSResourceResolver
 211      */
 212     @DataProvider(name = "data_SchemaC")
 213     public Object[][] getDataSchemaC() {
 214 
 215         return new Object[][]{
 216             // for resolving DTD in xsd
 217             {true, false, xml_catalog, xsd_xmlSchema, null},
 218             // for resolving xsd import
 219             {true, false, xml_catalog, xsd_xmlSchema_import, null},
 220             // for resolving xsd include
 221             {true, false, xml_catalog, xsd_include_company, null}
 222         };
 223     }
 224 
 225 
 226     /*
 227        DataProvider: for testing Schema Validator
 228        Data: source, resolver1, resolver2, catalog1, a catalog2
 229      */
 230     @DataProvider(name = "data_ValidatorC")
 231     public Object[][] getDataValidator() {
 232         DOMSource ds = getDOMSource(xml_val_test, xml_val_test_id, false, true, xml_catalog);
 233 
 234         SAXSource ss = new SAXSource(new InputSource(xml_val_test));
 235         ss.setSystemId(xml_val_test_id);
 236 
 237         StAXSource stax = getStaxSource(xml_val_test, xml_val_test_id);
 238         StAXSource stax1 = getStaxSource(xml_val_test, xml_val_test_id);
 239 
 240         StreamSource source = new StreamSource(new File(xml_val_test));
 241 
 242         return new Object[][]{
 243             // use catalog disabled through factory
 244             {true, false, false, ds, null, null, xml_catalog, null},
 245             {true, false, false, ds, null, null, null, xml_catalog},
 246             {true, false, false, ss, null, null, xml_catalog, null},
 247             {true, false, false, ss, null, null, null, xml_catalog},
 248             {true, false, false, stax, null, null, xml_catalog, null},
 249             {true, false, false, stax1, null, null, null, xml_catalog},
 250             {true, false, false, source, null, null, xml_catalog, null},
 251             {true, false, false, source, null, null, null, xml_catalog},
 252             // use catalog disabled through validatory
 253             {false, true, false, ds, null, null, xml_catalog, null},
 254             {false, true, false, ds, null, null, null, xml_catalog},
 255             {false, true, false, ss, null, null, xml_catalog, null},
 256             {false, true, false, ss, null, null, null, xml_catalog},
 257             {false, true, false, stax, null, null, xml_catalog, null},
 258             {false, true, false, stax1, null, null, null, xml_catalog},
 259             {false, true, false, source, null, null, xml_catalog, null},
 260             {false, true, false, source, null, null, null, xml_catalog},
 261         };
 262     }
 263 
 264     /*
 265        DataProvider: for testing XSL import and include
 266        Data: set use_catalog, use_catalog, catalog file, xsl file, xml file, a URIResolver, expected
 267      */
 268     @DataProvider(name = "data_XSLC")
 269     public Object[][] getDataXSLC() {
 270         SAXSource xslSourceDTD = new SAXSource(new InputSource(new StringReader(xsl_includeDTD)));
 271         StreamSource xmlSourceDTD = new StreamSource(new StringReader(xml_xslDTD));
 272 
 273         SAXSource xslDocSource = new SAXSource(new InputSource(new File(xsl_doc).toURI().toASCIIString()));
 274         StreamSource xmlDocSource = new StreamSource(new File(xml_doc));
 275         return new Object[][]{
 276             // for resolving DTD, import and include in xsl
 277             {true, false, xml_catalog, xslSourceDTD, xmlSourceDTD, null, ""},
 278             // for resolving reference by the document function
 279             {true, false, xml_catalog, xslDocSource, xmlDocSource, null, "Resolved by a catalog"},
 280         };
 281     }
 282 }