1/* 2 * Copyright (c) 2015, 2017, 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 24package catalog; 25 26import java.io.File; 27import java.io.IOException; 28import java.net.URI; 29import java.nio.file.Files; 30import java.nio.file.Path; 31import java.nio.file.Paths; 32import java.util.Map; 33import java.util.stream.Collectors; 34import java.util.stream.Stream; 35import javax.xml.catalog.CatalogFeatures; 36import javax.xml.catalog.CatalogManager; 37import javax.xml.catalog.CatalogResolver; 38import jaxp.library.JAXPTestUtilities; 39 40/* 41 * Utilities for testing XML Catalog API. 42 */ 43final 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 private CatalogTestUtils() { } 73 74 /* ********** create resolver ********** */ 75 76 /* 77 * Creates CatalogResolver with a set of catalogs. 78 */ 79 static CatalogResolver catalogResolver(String... catalogName) { 80 return catalogResolver(CatalogFeatures.defaults(), catalogName); 81 } 82 83 /* 84 * Creates CatalogResolver with a feature and a set of catalogs. 85 */ 86 static CatalogResolver catalogResolver(CatalogFeatures features, 87 String... catalogName) { 88 return (catalogName == null) ? 89 CatalogManager.catalogResolver(features) : 90 CatalogManager.catalogResolver(features, getCatalogPaths(catalogName)); 91 } 92 93 /* 94 * Creates catalogUriResolver with a set of catalogs. 95 */ 96 static CatalogResolver catalogUriResolver(String... catalogName) { 97 return catalogUriResolver(CatalogFeatures.defaults(), catalogName); 98 } 99 100 /* 101 * Creates catalogUriResolver with a feature and a set of catalogs. 102 */ 103 static CatalogResolver catalogUriResolver( 104 CatalogFeatures features, String... catalogName) { 105 return (catalogName == null) ? 106 CatalogManager.catalogResolver(features) : 107 CatalogManager.catalogResolver(features, getCatalogPaths(catalogName)); 108 } 109 110 // Gets the paths of the specified catalogs. 111 private static URI[] getCatalogPaths(String... catalogNames) { 112 return catalogNames == null 113 ? null 114 : Stream.of(catalogNames).map( 115 catalogName -> getCatalogPath(catalogName)).collect( 116 Collectors.toList()).toArray(new URI[0]); 117 } 118 119 // Gets the paths of the specified catalogs. 120 static URI getCatalogPath(String catalogName) { 121 return catalogName == null 122 ? null 123 : Paths.get(JAXPTestUtilities.getPathByClassName(CatalogTestUtils.class, "catalogFiles") 124 + catalogName).toUri(); 125 } 126 127 /* ********** jaxp.properties ********** */ 128 129 /* 130 * Generates jaxp.properties with the specified content, 131 * takes a backup if possible. 132 */ 133 static void generateJAXPProps(String content) throws IOException { 134 Path filePath = getJAXPPropsPath(); 135 Path bakPath = filePath.resolveSibling(JAXP_PROPS_BAK); 136 System.out.println("Creating new file " + filePath + 137 ", saving old version to " + bakPath + "."); 138 if (Files.exists(filePath) && !Files.exists(bakPath)) { 139 Files.move(filePath, bakPath); 140 } 141 142 Files.write(filePath, content.getBytes()); 143 } 144 145 /* 146 * Deletes jaxp.properties, restoring backup if possible. 147 */ 148 static void deleteJAXPProps() throws IOException { 149 Path filePath = getJAXPPropsPath(); 150 Path bakPath = filePath.resolveSibling(JAXP_PROPS_BAK); 151 System.out.println("Removing file " + filePath + 152 ", restoring old version from " + bakPath + "."); 153 Files.delete(filePath); 154 if (Files.exists(bakPath)) { 155 Files.move(bakPath, filePath); 156 } 157 } 158 159 /* 160 * Gets the path of jaxp.properties. 161 */ 162 private static Path getJAXPPropsPath() { 163 return Paths.get(System.getProperty("java.home") + File.separator 164 + "conf" + File.separator + JAXP_PROPS); 165 } 166 167 /* 168 * Creates the content of properties file with the specified 169 * property-value pairs. 170 */ 171 static String createPropsContent(Map<String, String> props) { 172 return props.entrySet().stream().map( 173 entry -> String.format("%s=%s%n", entry.getKey(), 174 entry.getValue())).reduce( 175 (line1, line2) -> line1 + line2).get(); 176 } 177} 178