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