|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Red Hat, Inc. |
| 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 | +import java.io.ByteArrayOutputStream; |
| 25 | +import java.io.PrintWriter; |
| 26 | +import java.nio.charset.StandardCharsets; |
| 27 | +import java.nio.file.Path; |
| 28 | +import java.util.List; |
| 29 | +import java.util.regex.Pattern; |
| 30 | +import java.util.spi.ToolProvider; |
| 31 | +import java.util.stream.Stream; |
| 32 | + |
| 33 | +import jdk.test.lib.Platform; |
| 34 | +import jdk.test.lib.process.OutputAnalyzer; |
| 35 | +import jdk.test.lib.process.ProcessTools; |
| 36 | +import jdk.tools.jlink.internal.LinkableRuntimeImage; |
| 37 | +import tests.Helper; |
| 38 | +import tests.Result; |
| 39 | + |
| 40 | +/* |
| 41 | + * @test |
| 42 | + * @summary Test ALL-MODULE-PATH option |
| 43 | + * @bug 8345259 8345573 |
| 44 | + * @requires (vm.compMode != "Xcomp" & os.maxMemory >= 2g) |
| 45 | + * @library ../lib /test/lib |
| 46 | + * @enablePreview |
| 47 | + * @modules java.base/jdk.internal.jimage |
| 48 | + * jdk.jlink/jdk.tools.jlink.internal |
| 49 | + * jdk.jlink/jdk.tools.jlink.plugin |
| 50 | + * jdk.jlink/jdk.tools.jimage |
| 51 | + * @run main/othervm -Xmx1g AllModulePathTest |
| 52 | + */ |
| 53 | +public class AllModulePathTest { |
| 54 | + private static final ToolProvider JLINK_TOOL = ToolProvider.findFirst("jlink") |
| 55 | + .orElseThrow(() -> |
| 56 | + new RuntimeException("jlink tool not found") |
| 57 | + ); |
| 58 | + |
| 59 | + private final Helper helper; |
| 60 | + |
| 61 | + public AllModulePathTest(Helper helper) { |
| 62 | + this.helper = helper; |
| 63 | + } |
| 64 | + |
| 65 | + private void noModulePath() { |
| 66 | + Path targetPath = helper.createNewImageDir("all-mod-path-no-mod-path"); |
| 67 | + List<String> allArgs = List.of("--add-modules", "ALL-MODULE-PATH", |
| 68 | + "--output", targetPath.toString()); |
| 69 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 70 | + PrintWriter out = new PrintWriter(baos); |
| 71 | + ByteArrayOutputStream berrOs = new ByteArrayOutputStream(); |
| 72 | + PrintWriter err = new PrintWriter(berrOs); |
| 73 | + JLINK_TOOL.run(out, err, allArgs.toArray(new String[] {})); |
| 74 | + OutputAnalyzer analyzer = new OutputAnalyzer(new String(baos.toByteArray(), StandardCharsets.UTF_8), |
| 75 | + new String(berrOs.toByteArray(), StandardCharsets.UTF_8)); |
| 76 | + analyzer.stdoutShouldContain("Error"); |
| 77 | + analyzer.stdoutShouldContain("ALL-MODULE-PATH requires --module-path option"); |
| 78 | + } |
| 79 | + |
| 80 | + private void modulePathWithLimitMods() throws Exception { |
| 81 | + Path targetPath = helper.createNewImageDir("all-mods-limit-mods"); |
| 82 | + String moduleName = "com.baz.runtime"; |
| 83 | + Result result = helper.generateDefaultJModule(moduleName, "jdk.jfr"); |
| 84 | + Path customModulePath = result.getFile().getParent(); |
| 85 | + List<String> allArgs = List.of("--add-modules", "ALL-MODULE-PATH", |
| 86 | + "--limit-modules", "jdk.jfr", // A dependency of com.baz.runtime |
| 87 | + "--module-path", customModulePath.toString(), |
| 88 | + "--output", targetPath.toString()); |
| 89 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 90 | + PrintWriter out = new PrintWriter(baos); |
| 91 | + ByteArrayOutputStream berrOs = new ByteArrayOutputStream(); |
| 92 | + PrintWriter err = new PrintWriter(berrOs); |
| 93 | + JLINK_TOOL.run(out, err, allArgs.toArray(new String[] {})); |
| 94 | + OutputAnalyzer analyzer = new OutputAnalyzer(new String(baos.toByteArray(), StandardCharsets.UTF_8), |
| 95 | + new String(berrOs.toByteArray(), StandardCharsets.UTF_8)); |
| 96 | + analyzer.shouldBeEmpty(); |
| 97 | + List<String> expected = List.of("java.base", "jdk.jfr"); |
| 98 | + verifyListModules(targetPath, expected); |
| 99 | + } |
| 100 | + |
| 101 | + private void modulePath() throws Exception { |
| 102 | + Path targetPath = helper.createNewImageDir("all-mod-path-w-mod-path"); |
| 103 | + String moduleName = "com.foo.runtime"; |
| 104 | + Result result = helper.generateDefaultJModule(moduleName, "jdk.jfr"); |
| 105 | + Path customModulePath = result.getFile().getParent(); |
| 106 | + List<String> allArgs = List.of("--add-modules", "ALL-MODULE-PATH", |
| 107 | + "--module-path", customModulePath.toString(), |
| 108 | + "--output", targetPath.toString(), |
| 109 | + "--verbose"); |
| 110 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 111 | + PrintWriter out = new PrintWriter(baos); |
| 112 | + ByteArrayOutputStream berrOs = new ByteArrayOutputStream(); |
| 113 | + PrintWriter err = new PrintWriter(berrOs); |
| 114 | + JLINK_TOOL.run(out, err, allArgs.toArray(new String[] {})); |
| 115 | + OutputAnalyzer analyzer = new OutputAnalyzer(new String(baos.toByteArray(), StandardCharsets.UTF_8), |
| 116 | + new String(berrOs.toByteArray(), StandardCharsets.UTF_8)); |
| 117 | + analyzer.stderrShouldBeEmpty(); |
| 118 | + analyzer.stdoutShouldContain(moduleName); |
| 119 | + analyzer.stdoutShouldContain("java.base"); |
| 120 | + analyzer.stdoutShouldContain("jdk.jfr"); |
| 121 | + // Verify the output image's modules |
| 122 | + List<String> expected = List.of(moduleName, "java.base", "jdk.jfr"); |
| 123 | + verifyListModules(targetPath, expected); |
| 124 | + } |
| 125 | + |
| 126 | + private void verifyListModules(Path targetPath, List<String> expected) throws Exception { |
| 127 | + String jlink = "java" + (Platform.isWindows() ? ".exe" : ""); |
| 128 | + Path javaExe = targetPath.resolve(Path.of("bin"), Path.of(jlink)); |
| 129 | + List<String> listMods = List.of(javaExe.toString(), "--list-modules"); |
| 130 | + OutputAnalyzer out = ProcessTools.executeCommand(listMods.toArray(new String[] {})); |
| 131 | + if (out.getExitValue() != 0) { |
| 132 | + throw new AssertionError("java --list-modules failed"); |
| 133 | + } |
| 134 | + List<String> actual = Stream.of(out.getStdout().split(Pattern.quote(System.lineSeparator()))) |
| 135 | + .map(s -> { return s.split("@")[0]; }) |
| 136 | + .sorted() |
| 137 | + .toList(); |
| 138 | + if (!expected.equals(actual)) { |
| 139 | + throw new RuntimeException("Unexpected list of modules: " + actual + " expected: " + expected); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + public static void main(String[] args) throws Exception { |
| 144 | + boolean linkableRuntime = LinkableRuntimeImage.isLinkableRuntime(); |
| 145 | + Helper helper = Helper.newHelper(linkableRuntime); |
| 146 | + if (helper == null) { |
| 147 | + System.err.println("Test not run"); |
| 148 | + return; |
| 149 | + } |
| 150 | + AllModulePathTest test = new AllModulePathTest(helper); |
| 151 | + test.noModulePath(); |
| 152 | + test.modulePath(); |
| 153 | + test.modulePathWithLimitMods(); |
| 154 | + } |
| 155 | +} |
0 commit comments