|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, 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 | +import java.nio.file.Files; |
| 25 | +import java.nio.file.Path; |
| 26 | +import java.nio.file.Paths; |
| 27 | +import java.util.Arrays; |
| 28 | +import java.util.spi.ToolProvider; |
| 29 | +import java.util.stream.Collectors; |
| 30 | +import java.util.stream.Stream; |
| 31 | + |
| 32 | +import jdk.test.lib.compiler.CompilerUtils; |
| 33 | +import jdk.test.lib.util.FileUtils; |
| 34 | + |
| 35 | +import static jdk.test.lib.process.ProcessTools.*; |
| 36 | + |
| 37 | +import org.junit.jupiter.api.Test; |
| 38 | +import org.junit.jupiter.api.BeforeAll; |
| 39 | +import static org.junit.jupiter.api.Assertions.*; |
| 40 | + |
| 41 | +/** |
| 42 | + * @test |
| 43 | + * @bug 8322809 |
| 44 | + * @library /test/lib |
| 45 | + * @modules jdk.compiler jdk.jlink |
| 46 | + * @build jdk.test.lib.compiler.CompilerUtils |
| 47 | + * jdk.test.lib.process.ProcessTools |
| 48 | + * jdk.test.lib.util.FileUtils |
| 49 | + * ModuleMainClassTest |
| 50 | + * @run junit ModuleMainClassTest |
| 51 | + */ |
| 52 | + |
| 53 | +public class ModuleMainClassTest { |
| 54 | + private static final String JAVA_HOME = System.getProperty("java.home"); |
| 55 | + private static final String TEST_SRC = System.getProperty("test.src"); |
| 56 | + |
| 57 | + private static final Path SRC_DIR = Path.of(TEST_SRC, "src"); |
| 58 | + private static final Path MODS_DIR = Path.of("mods"); |
| 59 | + private static final Path JMODS_DIR = Path.of("jmods"); |
| 60 | + |
| 61 | + private static final Path IMAGE = Path.of("image"); |
| 62 | + |
| 63 | + // the module names are sorted by the plugin and so these names cover |
| 64 | + // the cases that are before and after the elements of `jdk.*` modules |
| 65 | + // with main classes |
| 66 | + private static String[] modules = new String[] {"com.foo", "net.foo"}; |
| 67 | + |
| 68 | + private static boolean hasJmods() { |
| 69 | + if (!Files.exists(Paths.get(JAVA_HOME, "jmods"))) { |
| 70 | + System.err.println("Test skipped. NO jmods directory"); |
| 71 | + return false; |
| 72 | + } |
| 73 | + return true; |
| 74 | + } |
| 75 | + |
| 76 | + @BeforeAll |
| 77 | + public static void compileAll() throws Throwable { |
| 78 | + if (!hasJmods()) return; |
| 79 | + |
| 80 | + for (String mn : modules) { |
| 81 | + Path msrc = SRC_DIR.resolve(mn); |
| 82 | + assertTrue(CompilerUtils.compile(msrc, MODS_DIR, |
| 83 | + "--module-source-path", SRC_DIR.toString(), |
| 84 | + "--add-exports", "java.base/jdk.internal.module=" + mn)); |
| 85 | + } |
| 86 | + |
| 87 | + if (Files.exists(IMAGE)) { |
| 88 | + FileUtils.deleteFileTreeUnchecked(IMAGE); |
| 89 | + } |
| 90 | + |
| 91 | + // create JMOD files |
| 92 | + Files.createDirectories(JMODS_DIR); |
| 93 | + Stream.of(modules).forEach(mn -> |
| 94 | + assertTrue(jmod("create", |
| 95 | + "--class-path", MODS_DIR.resolve(mn).toString(), |
| 96 | + "--main-class", mn + ".Main", |
| 97 | + JMODS_DIR.resolve(mn + ".jmod").toString()) == 0) |
| 98 | + ); |
| 99 | + |
| 100 | + // the run-time image created will have 4 modules with main classes |
| 101 | + createImage(IMAGE, "com.foo"); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void testComFoo() throws Exception { |
| 106 | + if (!hasJmods()) return; |
| 107 | + |
| 108 | + Path java = IMAGE.resolve("bin").resolve("java"); |
| 109 | + assertTrue(executeProcess(java.toString(), |
| 110 | + "-m", "com.foo") |
| 111 | + .outputTo(System.out) |
| 112 | + .errorTo(System.out) |
| 113 | + .getExitValue() == 0); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void testNetFoo() throws Exception { |
| 118 | + if (!hasJmods()) return; |
| 119 | + |
| 120 | + Path java = IMAGE.resolve("bin").resolve("java"); |
| 121 | + assertTrue(executeProcess(java.toString(), |
| 122 | + "-m", "net.foo") |
| 123 | + .outputTo(System.out) |
| 124 | + .errorTo(System.out) |
| 125 | + .getExitValue() == 0); |
| 126 | + } |
| 127 | + |
| 128 | + static final ToolProvider JLINK_TOOL = ToolProvider.findFirst("jlink") |
| 129 | + .orElseThrow(() -> new RuntimeException("jlink tool not found")); |
| 130 | + |
| 131 | + static final ToolProvider JMOD_TOOL = ToolProvider.findFirst("jmod") |
| 132 | + .orElseThrow(() -> new RuntimeException("jmod tool not found")); |
| 133 | + |
| 134 | + private static void createImage(Path outputDir, String... modules) throws Throwable { |
| 135 | + assertTrue(JLINK_TOOL.run(System.out, System.out, |
| 136 | + "--output", outputDir.toString(), |
| 137 | + "--add-modules", Arrays.stream(modules).collect(Collectors.joining(",")), |
| 138 | + "--module-path", JMODS_DIR.toString()) == 0); |
| 139 | + } |
| 140 | + |
| 141 | + private static int jmod(String... options) { |
| 142 | + System.out.println("jmod " + Arrays.asList(options)); |
| 143 | + return JMOD_TOOL.run(System.out, System.out, options); |
| 144 | + } |
| 145 | +} |
1 commit comments
openjdk-notifier[bot] commentedon Dec 2, 2024
Review
Issues