|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, 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 | + * @summary Test that CDS archive can be loaded if the archive is in a non-JVM variant directory. |
| 28 | + * @bug 8353504 |
| 29 | + * @requires vm.cds |
| 30 | + * @requires vm.flagless |
| 31 | + * @requires vm.flavor == "server" |
| 32 | + * @comment This test doesn't work on Windows because it depends on symlinks |
| 33 | + * @requires os.family != "windows" |
| 34 | + * @library /test/lib appcds |
| 35 | + * @run driver NonJVMVariantLocation |
| 36 | + */ |
| 37 | + |
| 38 | +import java.io.File; |
| 39 | +import java.io.IOException; |
| 40 | +import java.nio.file.Files; |
| 41 | +import java.nio.file.Paths; |
| 42 | +import java.util.List; |
| 43 | +import jdk.test.lib.cds.CDSTestUtils; |
| 44 | +import jdk.test.lib.process.OutputAnalyzer; |
| 45 | + |
| 46 | +public class NonJVMVariantLocation { |
| 47 | + public static void main(String[] args) throws Exception { |
| 48 | + String java_home_src = System.getProperty("java.home"); |
| 49 | + String java_home_dst = CDSTestUtils.getOutputDir() + File.separator + "moved_jdk"; |
| 50 | + String homeJava = java_home_src + File.separator + "bin" + File.separator + "java"; |
| 51 | + String dstJava = java_home_dst + File.separator + "bin" + File.separator + "java"; |
| 52 | + |
| 53 | + CDSTestUtils.clone(new File(java_home_src), new File(java_home_dst)); |
| 54 | + System.out.println("============== Cloned JDK at " + java_home_dst); |
| 55 | + |
| 56 | + // Replace "server" with "release" in jvm.cfg. |
| 57 | + // The jvm.cfg is parsed by the java launcher in java.c. |
| 58 | + String locDir = java_home_dst + File.separator + "lib"; |
| 59 | + String jvmCfg = locDir + File.separator + "jvm.cfg"; |
| 60 | + String serverDir = "server"; |
| 61 | + String releaseDir = "release"; |
| 62 | + replaceTextInFile(jvmCfg, serverDir, releaseDir); |
| 63 | + |
| 64 | + // Rename "server" dir to "release" dir. |
| 65 | + CDSTestUtils.rename(new File(locDir + File.separator + serverDir), |
| 66 | + new File(locDir + File.separator + releaseDir)); |
| 67 | + |
| 68 | + // Test runtime with cloned JDK |
| 69 | + { |
| 70 | + ProcessBuilder pb = CDSTestUtils.makeBuilder(dstJava, |
| 71 | + "-Xshare:on", |
| 72 | + "-Xlog:cds", |
| 73 | + "-version"); |
| 74 | + OutputAnalyzer out = TestCommon.executeAndLog(pb, "exec-dst"); |
| 75 | + out.shouldHaveExitValue(0) |
| 76 | + .shouldMatch(".info..cds. Opened shared archive file.*classes.*\\.jsa"); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private static void replaceTextInFile(String filePath, String oldText, String newText) { |
| 81 | + try { |
| 82 | + List<String> lines = Files.readAllLines(Paths.get(filePath)); |
| 83 | + lines.replaceAll(line -> line.replace(oldText, newText)); |
| 84 | + Files.write(Paths.get(filePath), lines); |
| 85 | + } catch (IOException e) { |
| 86 | + e.printStackTrace(); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments