1 /*
   2  * Copyright (c) 2015, 2016, 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 static catalog.CatalogTestUtils.DEFER_FALSE;
  27 import static catalog.CatalogTestUtils.FEATURE_DEFER;
  28 import static catalog.CatalogTestUtils.FEATURE_FILES;
  29 import static catalog.CatalogTestUtils.FEATURE_PREFER;
  30 import static catalog.CatalogTestUtils.FEATURE_RESOLVE;
  31 import static catalog.CatalogTestUtils.PREFER_SYSTEM;
  32 import static catalog.CatalogTestUtils.RESOLVE_CONTINUE;
  33 import static catalog.CatalogTestUtils.catalogResolver;
  34 import static catalog.CatalogTestUtils.catalogUriResolver;
  35 import static catalog.CatalogTestUtils.createPropsContent;
  36 import static catalog.CatalogTestUtils.deleteJAXPProps;
  37 import static catalog.CatalogTestUtils.generateJAXPProps;
  38 import static catalog.CatalogTestUtils.getCatalogPath;
  39 
  40 import java.io.IOException;
  41 import java.util.HashMap;
  42 import java.util.Map;
  43 
  44 import javax.xml.catalog.CatalogResolver;
  45 
  46 /*
  47  * This case tests if the properties FILES, DEFER, PREFER, RESOLVE in
  48  * jaxp.properties and system properties could be cared.
  49  */
  50 public class PropertiesTest {
  51 
  52     private static final String CATALOG_PROPERTIES = "properties.xml";
  53 
  54     public static void main(String[] args) throws Exception {
  55         System.out.println("testJAXPProperties started");
  56         testJAXPProperties();
  57         System.out.println("testJAXPProperties ended");
  58 
  59         System.out.println("testSystemProperties started");
  60         testSystemProperties();
  61         System.out.println("testSystemProperties ended");
  62 
  63         System.out.println("Test passed");
  64     }
  65 
  66     /*
  67      * Tests how does jaxp.properties affects the resolution.
  68      */
  69     private static void testJAXPProperties() throws IOException {
  70         generateJAXPProps(createJAXPPropsContent());
  71         testProperties();
  72         deleteJAXPProps();
  73     }
  74 
  75     /*
  76      * Tests how does system properties affects the resolution.
  77      */
  78     private static void testSystemProperties() {
  79         setSystemProperties();
  80         testProperties();
  81     }
  82 
  83     private static void testProperties() {
  84         testPropertiesOnEntityResolver();
  85         testPropertiesOnUriResolver();
  86     }
  87 
  88     private static void testPropertiesOnEntityResolver() {
  89         CatalogResolver entityResolver = catalogResolver((String[]) null);
  90         entityResolver.resolveEntity("-//REMOTE//DTD DOCDUMMY XML//EN",
  91                 "http://remote/sys/dtd/docDummy.dtd");
  92         "http://local/base/dtd/docSys.dtd".equals(
  93                 entityResolver.resolveEntity("-//REMOTE//DTD DOC XML//EN",
  94                         "http://remote/dtd/doc.dtd").getSystemId());
  95     }
  96 
  97     private static void testPropertiesOnUriResolver() {
  98         CatalogResolver uriResolver = catalogUriResolver((String[]) null);
  99         uriResolver.resolve("http://remote/uri/dtd/docDummy.dtd", null);
 100         "http://local/base/dtd/docURI.dtd".equals(uriResolver.resolve(
 101                 "http://remote/dtd/doc.dtd", null).getSystemId());
 102     }
 103 
 104     // The properties in jaxp.properties don't use default values
 105     private static String createJAXPPropsContent() {
 106         Map<String, String> props = new HashMap<>();
 107         props.put(FEATURE_FILES, getCatalogPath(CATALOG_PROPERTIES));
 108         props.put(FEATURE_DEFER, DEFER_FALSE);
 109         props.put(FEATURE_PREFER, PREFER_SYSTEM);
 110         props.put(FEATURE_RESOLVE, RESOLVE_CONTINUE);
 111         return createPropsContent(props);
 112     }
 113 
 114     // The system properties don't use default values
 115     private static void setSystemProperties() {
 116         System.setProperty(FEATURE_FILES, getCatalogPath(CATALOG_PROPERTIES));
 117         System.setProperty(FEATURE_DEFER, DEFER_FALSE);
 118         System.setProperty(FEATURE_PREFER, PREFER_SYSTEM);
 119         System.setProperty(FEATURE_RESOLVE, RESOLVE_CONTINUE);
 120     }
 121 }