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 
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.BufferedReader;
41 import java.io.IOException;
42 import java.io.InputStreamReader;
43 import java.nio.file.Paths;
44 import java.util.ArrayList;
45 import java.util.Arrays;
46 import java.util.HashMap;
47 import java.util.List;
48 import java.util.Map;
49 
50 import javax.xml.catalog.CatalogResolver;
51 
52 import org.testng.Assert;
53 import org.testng.annotations.Test;
54 
55 /*
56  * @test
57  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/isolatedjdk
58  * @run shell/timeout=600 ../IsolatedJDK.sh JAXP_PROPS
59  * @run testng catalog.PropertiesTest
60  * @run shell/timeout=600 ../IsolatedJDK.sh JAXP_PROPS remove
61  * @summary This test case tests if the properties FILES, DEFER, PREFER,
62  *          RESOLVE in jaxp.properties and system properties are used.
63  *          It needs to run in a copied JDK as it modifies the JDK's
64  *          jaxp.properties file.
65  * @bug 8077931
66  */
67 public class PropertiesTest {
68 
69     private static final String CATALOG_PROPERTIES = "properties.xml";
70 
71     @Test
72     /*
73      * Run main in a child process as it will modify the JDK.
74      */
test()75     public void test() throws Exception {
76         // get required properties and do some assertions
77         String javaclasspath = System.getProperty("java.class.path");
78         Assert.assertNotNull(javaclasspath, "Test class path is null");
79         String testclasspath = System.getProperty("test.class.path");
80         Assert.assertNotNull(testclasspath, "Test class path is null");
81         String testsourcepath = System.getProperty("test.src");
82         Assert.assertNotNull(testsourcepath, "Test source path is null");
83 
84         // start the child process
85         List<String> testCall = new ArrayList<>(6);
86         testCall.add(Paths.get("ISOLATED_JDK_JAXP_PROPS", "/bin", "java").toString());
87         testCall.add("-cp");
88         testCall.add(javaclasspath);
89         testCall.add("-Dtest.class.path=" + testclasspath);
90         testCall.add("-Dtest.src=" + testsourcepath);
91         testCall.add("catalog.PropertiesTest");
92         System.out.println("Starting child process: " + Arrays.toString(testCall.toArray()));
93         Process test = new ProcessBuilder(testCall).start();
94 
95         // wait for it to finish
96         boolean interrupted = false;
97         do {
98             try {
99                 test.waitFor();
100                 interrupted = false;
101             } catch (InterruptedException ie) {
102                 interrupted = true;
103             }
104         } while (interrupted);
105 
106         // trace system.out of child process
107         System.out.println("Proccess Out:");
108         BufferedReader br = new BufferedReader(new InputStreamReader(test.getInputStream()));
109         String line;
110         while ((line = br.readLine()) != null) {
111             System.out.println(line);
112         }
113         br.close();
114 
115         // trace system.err of child process
116         System.out.println("Proccess Err:");
117         br = new BufferedReader(new InputStreamReader(test.getErrorStream()));
118         while ((line = br.readLine()) != null) {
119             System.out.println(line);
120         }
121         br.close();
122 
123         // trace exit value and assert 0
124         int exitValue = test.exitValue();
125         System.out.println("Process Exit code: " + exitValue);
126         Assert.assertEquals(exitValue, 0, "PropertiesTest returned nonzero exit code.");
127     }
128 
main(String[] args)129     public static void main(String[] args) throws Exception {
130         System.out.println("testJAXPProperties started");
131         testJAXPProperties();
132         System.out.println("testJAXPProperties ended");
133 
134         System.out.println("testSystemProperties started");
135         testSystemProperties();
136         System.out.println("testSystemProperties ended");
137 
138         System.out.println("Test passed");
139     }
140 
141     /*
142      * Tests how jaxp.properties affects the resolution.
143      * Be careful: This test modifies jaxp.properties in the used JDK.
144      */
testJAXPProperties()145     private static void testJAXPProperties() throws IOException {
146         generateJAXPProps(createJAXPPropsContent());
147         testProperties();
148         deleteJAXPProps();
149     }
150 
151     /*
152      * Tests how system properties affects the resolution.
153      */
testSystemProperties()154     private static void testSystemProperties() {
155         setSystemProperties();
156         testProperties();
157     }
158 
testProperties()159     private static void testProperties() {
160         testPropertiesOnEntityResolver();
161         testPropertiesOnUriResolver();
162     }
163 
testPropertiesOnEntityResolver()164     private static void testPropertiesOnEntityResolver() {
165         CatalogResolver entityResolver = catalogResolver((String[]) null);
166         entityResolver.resolveEntity("-//REMOTE//DTD DOCDUMMY XML//EN",
167                 "http://remote/sys/dtd/docDummy.dtd");
168         "http://local/base/dtd/docSys.dtd".equals(
169                 entityResolver.resolveEntity("-//REMOTE//DTD DOC XML//EN",
170                         "http://remote/dtd/doc.dtd").getSystemId());
171     }
172 
testPropertiesOnUriResolver()173     private static void testPropertiesOnUriResolver() {
174         CatalogResolver uriResolver = catalogUriResolver((String[]) null);
175         uriResolver.resolve("http://remote/uri/dtd/docDummy.dtd", null);
176         "http://local/base/dtd/docURI.dtd".equals(uriResolver.resolve(
177                 "http://remote/dtd/doc.dtd", null).getSystemId());
178     }
179 
180     // The properties in jaxp.properties don't use default values
createJAXPPropsContent()181     private static String createJAXPPropsContent() {
182         Map<String, String> props = new HashMap<>();
183         props.put(FEATURE_FILES, getCatalogPath(CATALOG_PROPERTIES).toString());
184         props.put(FEATURE_DEFER, DEFER_FALSE);
185         props.put(FEATURE_PREFER, PREFER_SYSTEM);
186         props.put(FEATURE_RESOLVE, RESOLVE_CONTINUE);
187         return createPropsContent(props);
188     }
189 
190     // The system properties don't use default values
setSystemProperties()191     private static void setSystemProperties() {
192         System.setProperty(FEATURE_FILES, getCatalogPath(CATALOG_PROPERTIES).toString());
193         System.setProperty(FEATURE_DEFER, DEFER_FALSE);
194         System.setProperty(FEATURE_PREFER, PREFER_SYSTEM);
195         System.setProperty(FEATURE_RESOLVE, RESOLVE_CONTINUE);
196     }
197 }
198