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 24 package catalog; 25 26 import javax.xml.catalog.CatalogResolver; 27 import javax.xml.catalog.CatalogUriResolver; 28 29 import org.testng.Assert; 30 31 /* 32 * Utilities for checking catalog resolution. 33 */ 34 class ResolutionChecker { 35 36 /* ********** Checks normal resolution ********** */ 37 38 /* 39 * Checks the resolution result for specified external identifier. 40 */ 41 static void checkExtIdResolution(CatalogResolver resolver, 42 String publicId, String systemId, String matchedUri) { 43 Assert.assertEquals( 44 resolver.resolveEntity(publicId, getNotSpecified(systemId)).getSystemId(), 45 matchedUri); 46 } 47 48 /* 49 * Checks the resolution result for specified system identifier. 50 */ 51 static void checkSysIdResolution(CatalogResolver resolver, 52 String systemId, String matchedUri) { 53 checkExtIdResolution(resolver, null, systemId, matchedUri); 54 } 55 56 /* 57 * Checks the resolution result for specified public identifier. 58 */ 59 static void checkPubIdResolution(CatalogResolver resolver, 60 String publicId, String matchedUri) { 61 checkExtIdResolution(resolver, publicId, null, matchedUri); 62 } 63 64 /* 65 * Checks the resolution result for specified URI references 66 * with the specified base location. 67 */ 68 static void checkUriResolution(CatalogUriResolver resolver, 69 String href, String base, String matchedUri) { 70 Assert.assertEquals(resolver.resolve(href, base).getSystemId(), 71 matchedUri); 72 } 73 74 /* 75 * Checks the resolution result for specified URI references. 76 */ 77 static void checkUriResolution(CatalogUriResolver resolver, 78 String href, String matchedUri) { 79 checkUriResolution(resolver, href, null, matchedUri); 80 } 81 82 /* ********** Checks no match is found ********** */ 83 84 /* 85 * With strict resolution, if no match is found, 86 * CatalogResolver should throw CatalogException. 87 */ 88 static void checkNoMatch(CatalogResolver resolver) { 89 resolver.resolveEntity("-//EXTID//DTD NOMATCH DOCNOMATCH XML//EN", 90 "http://extId/noMatch/docNoMatch.dtd"); 91 } 92 93 /* 94 * With strict resolution, if no match is found, 95 * CatalogUriResolver should throw CatalogException. 96 */ 97 static void checkNoMatch(CatalogUriResolver resolver) { 98 resolver.resolve("http://uri/noMatch/docNoMatch.dtd", getNotSpecified(null)); 99 } 100 101 /* ********** Checks expected exception ********** */ 102 103 /* 104 * Checks the expected exception during the resolution for specified 105 * external identifier. 106 */ 107 static <T extends Throwable> void expectExceptionOnExtId( 108 CatalogResolver resolver, String publicId, String systemId, 109 Class<T> expectedExceptionClass) { 110 expectThrows(expectedExceptionClass, () -> { 111 resolver.resolveEntity(publicId, getNotSpecified(systemId)); 112 }); 113 } 114 115 /* 116 * Checks the expected exception during the resolution for specified 117 * system identifier. 118 */ 119 static <T extends Throwable> void expectExceptionOnSysId( 120 CatalogResolver resolver, String systemId, 121 Class<? extends Throwable> expectedExceptionClass) { 122 expectExceptionOnExtId(resolver, null, systemId, 123 expectedExceptionClass); 124 } 125 126 /* 127 * Checks the expected exception during the resolution for specified 128 * public identifier. 129 */ 130 static <T extends Throwable> void expectExceptionOnPubId( 131 CatalogResolver resolver, String publicId, 132 Class<T> expectedExceptionClass) { 133 expectExceptionOnExtId(resolver, publicId, null, 134 expectedExceptionClass); 135 } 136 137 /* 138 * Checks the expected exception during the resolution for specified 139 * URI reference with a specified base location. 140 */ 141 static <T extends Throwable> void expectExceptionOnUri( 142 CatalogUriResolver resolver, String href, String base, 143 Class<T> expectedExceptionClass) { 144 expectThrows(expectedExceptionClass, () -> { 145 resolver.resolve(href, base); 146 }); 147 } 148 149 /* 150 * Checks the expected exception during the resolution for specified 151 * URI reference without any specified base location. 152 */ 153 static <T extends Throwable> void expectExceptionOnUri( 154 CatalogUriResolver resolver, String href, 155 Class<T> expectedExceptionClass) { 156 expectExceptionOnUri(resolver, href, null, expectedExceptionClass); 157 } 158 159 // The TestNG distribution in current JTREG build doesn't support 160 // method Assert.expectThrows(). 161 private static <T extends Throwable> T expectThrows(Class<T> throwableClass, 162 ThrowingRunnable runnable) { 163 try { 164 runnable.run(); 165 } catch (Throwable t) { 166 if (throwableClass.isInstance(t)) { 167 return throwableClass.cast(t); 168 } else { 169 String mismatchMessage = String.format( 170 "Expected %s to be thrown, but %s was thrown", 171 throwableClass.getSimpleName(), 172 t.getClass().getSimpleName()); 173 174 throw new AssertionError(mismatchMessage, t); 175 } 176 } 177 178 String message = String.format( 179 "Expected %s to be thrown, but nothing was thrown", 180 throwableClass.getSimpleName()); 181 throw new AssertionError(message); 182 } 183 184 /* 185 * SystemId can never be null in XML. For publicId tests, if systemId is null, 186 * it will be considered as not-specified instead. A non-existent systemId 187 * is returned to make sure there's no match by the systemId. 188 */ 189 private static String getNotSpecified(String systemId) { 190 if (systemId == null) { 191 return "not-specified-systemId.dtd"; 192 } 193 return systemId; 194 } 195 196 private interface ThrowingRunnable { 197 void run() throws Throwable; 198 } 199 }