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