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