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