1 /*
   2  * Copyright (c) 2015, 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 package catalog;
  24 
  25 import java.io.IOException;
  26 import javax.xml.catalog.CatalogFeatures;
  27 import javax.xml.catalog.CatalogFeatures.Feature;
  28 import javax.xml.catalog.CatalogManager;
  29 import javax.xml.catalog.CatalogResolver;
  30 import javax.xml.parsers.ParserConfigurationException;
  31 import javax.xml.parsers.SAXParser;
  32 import javax.xml.parsers.SAXParserFactory;
  33 import static jaxp.library.JAXPTestUtilities.getPathByClassName;
  34 import org.testng.Assert;
  35 import org.testng.annotations.DataProvider;
  36 import org.testng.annotations.Test;
  37 import org.w3c.dom.Element;
  38 import org.xml.sax.Attributes;
  39 import org.xml.sax.ErrorHandler;
  40 import org.xml.sax.SAXException;
  41 import org.xml.sax.XMLReader;
  42 import org.xml.sax.ext.DefaultHandler2;
  43 
  44 /*
  45  * @bug 8081248
  46  * @summary Tests basic Catalog functions.
  47  */
  48 
  49 public class CatalogTest {
  50     /*
  51        Tests basic catalog feature by using a CatalogResolver instance to
  52     resolve a DTD reference to a locally specified DTD file. If the resolution
  53     is successful, the Handler shall return the value of the entity reference
  54     that matches the expected value.
  55      */
  56     @Test(dataProvider = "catalog")
  57     public void testCatalogResolver(String test, String expected, String catalogFile, String xml, SAXParser saxParser) {
  58         String catalog = null;
  59         if (catalogFile != null) {
  60             catalog = getClass().getResource(catalogFile).getFile();
  61         }
  62         String url = getClass().getResource(xml).getFile();
  63         try {
  64             CatalogResolver cr = CatalogManager.catalogResolver(null, catalog);
  65             XMLReader reader = saxParser.getXMLReader();
  66             reader.setEntityResolver(cr);
  67             MyHandler handler = new MyHandler(saxParser);
  68             reader.setContentHandler(handler);
  69             reader.parse(url);
  70             System.out.println(test + ": expected [" + expected + "] <> actual [" + handler.getResult() + "]");
  71             Assert.assertEquals(handler.getResult(), expected);
  72         } catch (SAXException | IOException e) {
  73             Assert.fail(e.getMessage());
  74         }
  75     }
  76 
  77     /*
  78        Verifies that when there's no match, in this case only an invalid
  79     catalog is provided, the resolver will throw an exception by default.
  80     */
  81     @Test
  82     public void testInvalidCatalog() {
  83         String catalog = getClass().getResource("catalog_invalid.xml").getFile();
  84 
  85         String test = "testInvalidCatalog";
  86         try {
  87             CatalogResolver resolver = CatalogManager.catalogResolver(null, catalog);
  88             String actualSystemId = resolver.resolveEntity(null, "http://remote/xml/dtd/sys/alice/docAlice.dtd").getSystemId();
  89         } catch (Exception e) {
  90             String msg = e.getMessage();
  91             if (msg != null) {
  92                 if (msg.contains("No match found for publicId")) {
  93                     Assert.assertEquals(msg, "No match found for publicId 'null' and systemId 'http://remote/xml/dtd/sys/alice/docAlice.dtd'.");
  94                     System.out.println(test + ": expected [No match found for publicId 'null' and systemId 'http://remote/xml/dtd/sys/alice/docAlice.dtd'.]");
  95                     System.out.println("actual [" + msg + "]");
  96                 }
  97             }
  98         }
  99     }
 100 
 101     /*
 102        Verifies that if resolve is "ignore", an empty InputSource will be returned
 103     when there's no match. The systemId is then null.
 104     */
 105     @Test
 106     public void testIgnoreInvalidCatalog() {
 107         String catalog = getClass().getResource("catalog_invalid.xml").getFile();
 108         CatalogFeatures f = CatalogFeatures.builder()
 109                 .with(Feature.FILES, catalog)
 110                 .with(Feature.PREFER, "public")
 111                 .with(Feature.DEFER, "true")
 112                 .with(Feature.RESOLVE, "ignore")
 113                 .build();
 114 
 115         String test = "testInvalidCatalog";
 116         try {
 117             CatalogResolver resolver = CatalogManager.catalogResolver(f, "");
 118             String actualSystemId = resolver.resolveEntity(null, "http://remote/xml/dtd/sys/alice/docAlice.dtd").getSystemId();
 119             System.out.println("testIgnoreInvalidCatalog: expected [null]");
 120             System.out.println("testIgnoreInvalidCatalog: expected [null]");
 121             System.out.println("actual [" + actualSystemId + "]");
 122             Assert.assertEquals(actualSystemId, null);
 123         } catch (Exception e) {
 124             Assert.fail(e.getMessage());
 125         }
 126     }
 127 
 128 
 129     /*
 130        DataProvider: provides test name, expected string, the catalog, and XML
 131        document.
 132      */
 133     @DataProvider(name = "catalog")
 134     Object[][] getCatalog() {
 135         return new Object[][]{
 136             {"testSystem", "Test system entry", "catalog.xml", "system.xml", getParser()},
 137             {"testRewriteSystem", "Test rewritesystem entry", "catalog.xml", "rewritesystem.xml", getParser()},
 138             {"testRewriteSystem1", "Test rewritesystem entry", "catalog.xml", "rewritesystem1.xml", getParser()},
 139             {"testSystemSuffix", "Test systemsuffix entry", "catalog.xml", "systemsuffix.xml", getParser()},
 140             {"testDelegateSystem", "Test delegatesystem entry", "catalog.xml", "delegatesystem.xml", getParser()},
 141             {"testPublic", "Test public entry", "catalog.xml", "public.xml", getParser()},
 142             {"testDelegatePublic", "Test delegatepublic entry", "catalog.xml", "delegatepublic.xml", getParser()},
 143         };
 144     }
 145 
 146     SAXParser getParser() {
 147         SAXParser saxParser = null;
 148         try {
 149             SAXParserFactory factory = SAXParserFactory.newInstance();
 150             factory.setNamespaceAware(true);
 151             saxParser = factory.newSAXParser();
 152         } catch (ParserConfigurationException | SAXException e) {
 153         }
 154 
 155         return saxParser;
 156     }
 157 
 158 
 159     /**
 160      * SAX handler
 161      */
 162     public class MyHandler extends DefaultHandler2 implements ErrorHandler {
 163 
 164         StringBuilder textContent = new StringBuilder();
 165         SAXParser saxParser;
 166 
 167         MyHandler(SAXParser saxParser) {
 168             textContent.setLength(0);
 169             this.saxParser = saxParser;
 170         }
 171 
 172         String getResult() {
 173             return textContent.toString();
 174         }
 175 
 176         @Override
 177         public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
 178             textContent.delete(0, textContent.length());
 179             try {
 180                 System.out.println("Element: " + uri + ":" + localName + " " + qName);
 181             } catch (Exception e) {
 182                 throw new SAXException(e);
 183             }
 184 
 185         }
 186 
 187         @Override
 188         public void characters(char ch[], int start, int length) throws SAXException {
 189             textContent.append(ch, start, length);
 190         }
 191     }
 192 }