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 java.io.File; 27 import java.io.IOException; 28 import java.nio.file.Files; 29 import java.nio.file.Path; 30 import java.nio.file.Paths; 31 import java.util.Map; 32 import java.util.stream.Collectors; 33 import java.util.stream.Stream; 34 35 import javax.xml.catalog.CatalogFeatures; 36 import javax.xml.catalog.CatalogManager; 37 import javax.xml.catalog.CatalogResolver; 38 import javax.xml.catalog.CatalogUriResolver; 39 40 /* 41 * Utilities for testing XML Catalog API. 42 */ 43 final class CatalogTestUtils { 44 45 /* catalog files */ 46 static final String CATALOG_PUBLIC = "public.xml"; 47 static final String CATALOG_SYSTEM = "system.xml"; 48 static final String CATALOG_URI = "uri.xml"; 49 50 /* features */ 51 static final String FEATURE_FILES = "javax.xml.catalog.files"; 52 static final String FEATURE_PREFER = "javax.xml.catalog.prefer"; 53 static final String FEATURE_DEFER = "javax.xml.catalog.defer"; 54 static final String FEATURE_RESOLVE = "javax.xml.catalog.resolve"; 55 56 /* values of prefer feature */ 57 static final String PREFER_SYSTEM = "system"; 58 static final String PREFER_PUBLIC = "public"; 59 60 /* values of defer feature */ 61 static final String DEFER_TRUE = "true"; 62 static final String DEFER_FALSE = "false"; 63 64 /* values of resolve feature */ 65 static final String RESOLVE_STRICT = "strict"; 66 static final String RESOLVE_CONTINUE = "continue"; 67 static final String RESOLVE_IGNORE = "ignore"; 68 69 private static final String JAXP_PROPS = "jaxp.properties"; 70 private static final String JAXP_PROPS_BAK = JAXP_PROPS + ".bak"; 71 72 /* 73 * Force using slash as File separator as we always use cygwin to test in 74 * Windows platform. 75 */ 76 private static final String FILE_SEP = "/"; 77 78 private CatalogTestUtils() { } 79 80 /* ********** create resolver ********** */ 81 82 /* 83 * Creates CatalogResolver with a set of catalogs. 84 */ 85 static CatalogResolver catalogResolver(String... catalogName) { 86 return catalogResolver(CatalogFeatures.defaults(), catalogName); 87 } 88 89 /* 90 * Creates CatalogResolver with a feature and a set of catalogs. 91 */ 92 static CatalogResolver catalogResolver(CatalogFeatures features, 93 String... catalogName) { 94 return (catalogName == null) ? 95 CatalogManager.catalogResolver(features) : 96 CatalogManager.catalogResolver(features, getCatalogPaths(catalogName)); 97 } 98 99 /* 100 * Creates catalogUriResolver with a set of catalogs. 101 */ 102 static CatalogUriResolver catalogUriResolver(String... catalogName) { 103 return catalogUriResolver(CatalogFeatures.defaults(), catalogName); 104 } 105 106 /* 107 * Creates catalogUriResolver with a feature and a set of catalogs. 108 */ 109 static CatalogUriResolver catalogUriResolver( 110 CatalogFeatures features, String... catalogName) { 111 return (catalogName == null) ? 112 CatalogManager.catalogUriResolver(features) : 113 CatalogManager.catalogUriResolver(features, getCatalogPaths(catalogName)); 114 } 115 116 // Gets the paths of the specified catalogs. 117 private static String[] getCatalogPaths(String... catalogNames) { 118 return catalogNames == null 119 ? null 120 : Stream.of(catalogNames).map( 121 catalogName -> getCatalogPath(catalogName)).collect( 122 Collectors.toList()).toArray(new String[0]); 123 } 124 125 // Gets the paths of the specified catalogs. 126 static String getCatalogPath(String catalogName) { 127 return catalogName == null 128 ? null 129 : getPathByClassName(CatalogTestUtils.class, "catalogFiles") 130 + catalogName; 131 } 132 133 /* 134 * Acquire a full path string by given class name and relative path string. 135 */ 136 private static String getPathByClassName(Class<?> clazz, 137 String relativeDir) { 138 String packageName = FILE_SEP 139 + clazz.getPackage().getName().replaceAll("[.]", FILE_SEP); 140 String javaSourcePath = System.getProperty("test.src").replaceAll( 141 "\\" + File.separator, FILE_SEP) + packageName + FILE_SEP; 142 String normalizedPath = Paths.get(javaSourcePath, 143 relativeDir).normalize().toAbsolutePath().toString(); 144 return normalizedPath.replace("\\", FILE_SEP) + FILE_SEP; 145 } 146 147 /* ********** jaxp.properties ********** */ 148 149 /* 150 * Generates the jaxp.properties with the specified content. 151 */ 152 static void generateJAXPProps(String content) throws IOException { 153 Path filePath = getJAXPPropsPath(); 154 Path bakPath = filePath.resolveSibling(JAXP_PROPS_BAK); 155 if (Files.exists(filePath) && !Files.exists(bakPath)) { 156 Files.move(filePath, bakPath); 157 } 158 159 Files.write(filePath, content.getBytes()); 160 } 161 162 /* 163 * Deletes the jaxp.properties. 164 */ 165 static void deleteJAXPProps() throws IOException { 166 Path filePath = getJAXPPropsPath(); 167 Files.delete(filePath); 168 Path bakFilePath = filePath.resolveSibling(JAXP_PROPS_BAK); 169 if (Files.exists(bakFilePath)) { 170 Files.move(bakFilePath, filePath); 171 } 172 } 173 174 /* 175 * Gets the path of jaxp.properties. 176 */ 177 private static Path getJAXPPropsPath() { 178 return Paths.get(System.getProperty("java.home") + File.separator 179 + "conf" + File.separator + JAXP_PROPS); 180 } 181 182 /* 183 * Creates the content of properties file with the specified 184 * property-value pairs. 185 */ 186 static String createPropsContent(Map<String, String> props) { 187 return props.entrySet().stream().map( 188 entry -> String.format("%s=%s%n", entry.getKey(), 189 entry.getValue())).reduce( 190 (line1, line2) -> line1 + line2).get(); 191 } 192 }