1 /* |
|
2 * Copyright (c) 2015, 2019, 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 |
|
25 /** |
|
26 * @test |
|
27 * @requires vm.cds |
|
28 * @modules java.base/jdk.internal.misc |
|
29 * jdk.jartool/sun.tools.jar |
|
30 * @library ../.. |
|
31 * @library /test/lib |
|
32 * @compile ../../test-classes/Hello.java |
|
33 * @run driver OverrideTests |
|
34 * @summary AppCDS tests for overriding archived classes with -p and --upgrade-module-path |
|
35 */ |
|
36 |
|
37 /* |
|
38 * This test consists of 4 tests: |
|
39 * 1. Archive PLATFORM class and override with --upgrade-module-path. |
|
40 * 2. Archive PLATFORM class and override with -p. |
|
41 * 3. Archive APP class and override with --upgrade-module-path. |
|
42 * 4. Archive App class and override with -p. |
|
43 * For all 4 tests, the class is instantiatied and toString() is called |
|
44 * to check whether the archived version or the override version was instantiatied. |
|
45 * For tests 1 and 3, the overridden version should be instantiatied. |
|
46 * For tests 2 and 4, the archived version should be instantiated. |
|
47 * |
|
48 * This test uses the same test helper class in all 4 cases. It is located in |
|
49 * src/test/jdk/test/Main.java. It will be invoked once for each test cases, |
|
50 * with parameters to the test determining how it is run and what the |
|
51 * expected result is. See Main.java for a description of these 3 arguments. |
|
52 */ |
|
53 |
|
54 import java.io.File; |
|
55 import java.nio.file.Files; |
|
56 import java.nio.file.Path; |
|
57 import java.nio.file.Paths; |
|
58 |
|
59 import jdk.test.lib.Asserts; |
|
60 import jdk.test.lib.cds.CDSOptions; |
|
61 import jdk.test.lib.cds.CDSTestUtils; |
|
62 import jdk.test.lib.process.OutputAnalyzer; |
|
63 import jdk.test.lib.process.ProcessTools; |
|
64 |
|
65 |
|
66 public class OverrideTests { |
|
67 private static final String TEST_SRC = System.getProperty("test.src"); |
|
68 private static final Path SRC_DIR = Paths.get(TEST_SRC, "src"); |
|
69 private static final Path MODS_DIR = Paths.get("mods"); |
|
70 private static String appJar; |
|
71 // the module that is upgraded |
|
72 private static final String[] UPGRADED_MODULES = {"jdk.compiler", "java.net.http"}; |
|
73 private static final Path[] UPGRADEDMODS_DIR = {Paths.get("upgradedmod1"), Paths.get("upgradedmod2")}; |
|
74 |
|
75 // the test module |
|
76 private static final String TEST_MODULE = "test"; |
|
77 private static final String MAIN_CLASS = "jdk.test.Main"; |
|
78 |
|
79 // test classes to archive. These are both in UPGRADED_MODULES |
|
80 private static final String APP_ARCHIVE_CLASS = "com/sun/tools/javac/Main"; |
|
81 private static final String PLATFORM_ARCHIVE_CLASS = "java/net/http/HttpTimeoutException"; |
|
82 private static final String[] ARCHIVE_CLASSES = {APP_ARCHIVE_CLASS, PLATFORM_ARCHIVE_CLASS}; |
|
83 private static String testArchiveName; |
|
84 |
|
85 |
|
86 public static void main(String[] args) throws Exception { |
|
87 appJar = JarBuilder.getOrCreateHelloJar(); |
|
88 OverrideTests tests = new OverrideTests(); |
|
89 tests.compileModulesAndDumpArchive(); |
|
90 tests.testAppClassOverriding(); |
|
91 tests.testPlatformClassOverriding(); |
|
92 } |
|
93 |
|
94 void compileModulesAndDumpArchive() throws Exception { |
|
95 boolean compiled; |
|
96 // javac -d upgradedmods/$upgradedMod src/$upgradedMod/** |
|
97 int i = 0; |
|
98 for (String upgradedMod : UPGRADED_MODULES) { |
|
99 compiled = CompilerUtils.compile( |
|
100 SRC_DIR.resolve(UPGRADED_MODULES[i]), |
|
101 UPGRADEDMODS_DIR[i].resolve(UPGRADED_MODULES[i]) |
|
102 ); |
|
103 Asserts.assertTrue(compiled, UPGRADED_MODULES[i] + " did not compile"); |
|
104 i++; |
|
105 } |
|
106 |
|
107 // javac -d mods/test --upgrade-module-path upgradedmods ... |
|
108 compiled = CompilerUtils.compile( |
|
109 SRC_DIR.resolve(TEST_MODULE), |
|
110 MODS_DIR.resolve(TEST_MODULE), |
|
111 "--upgrade-module-path", UPGRADEDMODS_DIR[0].toString() + |
|
112 System.getProperty("path.separator") + UPGRADEDMODS_DIR[1].toString() |
|
113 ); |
|
114 Asserts.assertTrue(compiled, TEST_MODULE + " did not compile"); |
|
115 |
|
116 // dump the archive with jdk.compiler and java.net.http classes in the class list |
|
117 OutputAnalyzer output = TestCommon.dump(appJar, TestCommon.list(ARCHIVE_CLASSES)); |
|
118 TestCommon.checkDump(output); |
|
119 // Make sure all the classes where successfully archived. |
|
120 for (String archiveClass : ARCHIVE_CLASSES) { |
|
121 output.shouldNotContain("Preload Warning: Cannot find " + archiveClass); |
|
122 } |
|
123 |
|
124 testArchiveName = TestCommon.getCurrentArchiveName(); |
|
125 } |
|
126 |
|
127 /** |
|
128 * APP Class Overriding Tests |
|
129 * |
|
130 * Archive APP class com.sun.tools.javac.Main from module jdk.compiler. |
|
131 * -At run time, upgrade module jdk.compiler using --upgrade-module-path. |
|
132 * Class.forname(Main) MUST NOT load the archived Main. |
|
133 * -At run time, module jdk.compiler also exists in --module-path. |
|
134 * Class.forname(Main) MUST load the archived Main. |
|
135 */ |
|
136 public void testAppClassOverriding() throws Exception { |
|
137 testClassOverriding(APP_ARCHIVE_CLASS, "app"); |
|
138 } |
|
139 |
|
140 /** |
|
141 * PLATFORM Class Overriding Tests |
|
142 * |
|
143 * Archive PLATFORM class java.net.http.HttpTimeoutException from module java.net.http. |
|
144 * -At run time, upgrade module java.net.http using --upgrade-module-path. |
|
145 * Class.forname(HttpTimeoutException) MUST NOT load the archived HttpTimeoutException. |
|
146 * -At run time, module java.net.http also exists in --module-path. |
|
147 * Class.forname(HttpTimeoutException) MUST load the archived HttpTimeoutException. |
|
148 */ |
|
149 public void testPlatformClassOverriding() throws Exception { |
|
150 testClassOverriding(PLATFORM_ARCHIVE_CLASS, "platform"); |
|
151 } |
|
152 |
|
153 /** |
|
154 * Run the test twice. Once with upgrade module on --upgrade-module-path and once with it on -p. |
|
155 * Only modules defined to the PlatformClassLoader are upgradeable. |
|
156 * Modules defined to the AppClassLoader are not upgradeble; we expect the |
|
157 * FindException to be thrown. |
|
158 */ |
|
159 void testClassOverriding(String archiveClass, String loaderName) throws Exception { |
|
160 String mid = TEST_MODULE + "/" + MAIN_CLASS; |
|
161 OutputAnalyzer output; |
|
162 boolean isAppLoader = loaderName.equals("app"); |
|
163 int upgradeModIdx = isAppLoader ? 0 : 1; |
|
164 String expectedException = "java.lang.module.FindException: Unable to compute the hash"; |
|
165 String prefix[] = new String[3]; |
|
166 prefix[0] = "-Djava.class.path=" + appJar; |
|
167 prefix[1] = "--add-modules"; |
|
168 prefix[2] = "java.net.http"; |
|
169 |
|
170 // Run the test with --upgrade-module-path set to alternate location of archiveClass |
|
171 // The alternate version of archiveClass SHOULD be found. |
|
172 TestCommon.runWithModules(prefix, |
|
173 UPGRADEDMODS_DIR[upgradeModIdx].toString(), |
|
174 MODS_DIR.toString(), |
|
175 mid, |
|
176 archiveClass, loaderName, "true") // last 3 args passed to test |
|
177 .ifNoMappingFailure(out -> out.shouldContain(expectedException)); |
|
178 |
|
179 // Now run this same test again, but this time without AppCDS. Behavior should be the same. |
|
180 CDSOptions opts = (new CDSOptions()) |
|
181 .addPrefix(prefix) |
|
182 .setArchiveName(testArchiveName).setUseVersion(false) |
|
183 .addSuffix("--upgrade-module-path", UPGRADEDMODS_DIR[upgradeModIdx].toString(), |
|
184 "-p", MODS_DIR.toString(), "-m", mid) |
|
185 .addSuffix(archiveClass, loaderName, "true"); |
|
186 |
|
187 output = CDSTestUtils.runWithArchive(opts); |
|
188 |
|
189 try { |
|
190 output.shouldContain(expectedException); |
|
191 } catch (Exception e) { |
|
192 TestCommon.checkCommonExecExceptions(output, e); |
|
193 } |
|
194 |
|
195 // Run the test with -p set to alternate location of archiveClass. |
|
196 // The alternate version of archiveClass SHOULD NOT be found. |
|
197 TestCommon.runWithModules( |
|
198 prefix, |
|
199 null, |
|
200 UPGRADEDMODS_DIR[upgradeModIdx].toString() + java.io.File.pathSeparator + MODS_DIR.toString(), |
|
201 mid, |
|
202 archiveClass, loaderName, "false") // last 3 args passed to test |
|
203 .assertNormalExit(); |
|
204 |
|
205 // Now run this same test again, but this time without AppCDS. Behavior should be the same. |
|
206 opts = (new CDSOptions()) |
|
207 .addPrefix(prefix) |
|
208 .setArchiveName(testArchiveName).setUseVersion(false) |
|
209 .addSuffix("-p", MODS_DIR.toString(), "-m", mid) |
|
210 .addSuffix(archiveClass, loaderName, "false"); // params to the test class |
|
211 |
|
212 OutputAnalyzer out = CDSTestUtils.runWithArchive(opts); |
|
213 if (!CDSTestUtils.isUnableToMap(out)) |
|
214 out.shouldHaveExitValue(0); |
|
215 } |
|
216 } |
|