1 /** 2 * Copyright (c) 2015, 2016, 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 * @test 26 * @summary Basic test of jlink to create jmods and images 27 * @author Andrei Eremeev 28 * @library /lib/testlibrary 29 * @modules java.base/jdk.internal.module 30 * jdk.jlink 31 * jdk.compiler 32 * @build jdk.testlibrary.ProcessTools 33 * jdk.testlibrary.OutputAnalyzer 34 * JarUtils CompilerUtils 35 * @run main BasicTest 36 */ 37 38 import java.io.File; 39 import java.io.PrintWriter; 40 import java.nio.file.Files; 41 import java.nio.file.Path; 42 import java.nio.file.Paths; 43 import java.util.ArrayList; 44 import java.util.Collections; 45 import java.util.List; 46 import java.util.spi.ToolProvider; 47 48 import jdk.testlibrary.OutputAnalyzer; 49 import jdk.testlibrary.ProcessTools; 50 51 public class BasicTest { 52 static final ToolProvider JMOD_TOOL = ToolProvider.findFirst("jmod") 53 .orElseThrow(() -> 54 new RuntimeException("jmod tool not found") 55 ); 56 57 static final ToolProvider JLINK_TOOL = ToolProvider.findFirst("jlink") 58 .orElseThrow(() -> 59 new RuntimeException("jlink tool not found") 60 ); 61 62 private final String TEST_MODULE = "test"; 63 private final Path jdkHome = Paths.get(System.getProperty("test.jdk")); 64 private final Path jdkMods = jdkHome.resolve("jmods"); 65 private final Path testSrc = Paths.get(System.getProperty("test.src")); 66 private final Path src = testSrc.resolve("src").resolve(TEST_MODULE); 67 private final Path classes = Paths.get("classes"); 68 private final Path jmods = Paths.get("jmods"); 69 private final Path jars = Paths.get("jars"); 70 71 public static void main(String[] args) throws Throwable { 72 new BasicTest().run(); 73 } 74 75 public void run() throws Throwable { 76 if (Files.notExists(jdkMods)) { 77 return; 78 } 79 80 if (!CompilerUtils.compile(src, classes)) { 81 throw new AssertionError("Compilation failure. See log."); 82 } 83 84 Files.createDirectories(jmods); 85 Files.createDirectories(jars); 86 Path jarfile = jars.resolve("test.jar"); 87 JarUtils.createJarFile(jarfile, classes); 88 89 Path image = Paths.get("mysmallimage"); 90 runJmod(jarfile.toString(), TEST_MODULE, true); 91 runJlink(image, TEST_MODULE, "--compress", "2", "--launcher", "foo=" + TEST_MODULE); 92 execute(image, "foo"); 93 94 Files.delete(jmods.resolve(TEST_MODULE + ".jmod")); 95 96 image = Paths.get("myimage"); 97 runJmod(classes.toString(), TEST_MODULE, true); 98 runJlink(image, TEST_MODULE, "--launcher", "bar=" + TEST_MODULE); 99 execute(image, "bar"); 100 101 Files.delete(jmods.resolve(TEST_MODULE + ".jmod")); 102 103 image = Paths.get("myimage2"); 104 runJmod(classes.toString(), TEST_MODULE, false /* no ModuleMainClass! */); 105 // specify main class in --launcher command line 106 runJlink(image, TEST_MODULE, "--launcher", "bar2=" + TEST_MODULE + "/jdk.test.Test"); 107 execute(image, "bar2"); 108 109 } 110 111 private void execute(Path image, String scriptName) throws Throwable { 112 String cmd = image.resolve("bin").resolve(scriptName).toString(); 113 OutputAnalyzer analyzer; 114 if (System.getProperty("os.name").startsWith("Windows")) { 115 analyzer = ProcessTools.executeProcess("sh.exe", cmd, "1", "2", "3"); 116 } else { 117 analyzer = ProcessTools.executeProcess(cmd, "1", "2", "3"); 118 } 119 if (analyzer.getExitValue() != 0) { 120 throw new AssertionError("Image invocation failed: rc=" + analyzer.getExitValue()); 121 } 122 } 123 124 private void runJlink(Path image, String modName, String... options) { 125 List<String> args = new ArrayList<>(); 126 Collections.addAll(args, 127 "--module-path", jdkMods + File.pathSeparator + jmods, 128 "--add-modules", modName, 129 "--output", image.toString()); 130 Collections.addAll(args, options); 131 132 PrintWriter pw = new PrintWriter(System.out); 133 int rc = JLINK_TOOL.run(pw, pw, args.toArray(new String[args.size()])); 134 if (rc != 0) { 135 throw new AssertionError("Jlink failed: rc = " + rc); 136 } 137 } 138 139 private void runJmod(String cp, String modName, boolean main) { 140 int rc; 141 if (main) { 142 rc = JMOD_TOOL.run(System.out, System.out, new String[] { 143 "create", 144 "--class-path", cp, 145 "--module-version", "1.0", 146 "--main-class", "jdk.test.Test", 147 jmods.resolve(modName + ".jmod").toString() 148 }); 149 } else { 150 rc = JMOD_TOOL.run(System.out, System.out, new String[] { 151 "create", 152 "--class-path", cp, 153 "--module-version", "1.0", 154 jmods.resolve(modName + ".jmod").toString(), 155 }); 156 } 157 158 if (rc != 0) { 159 throw new AssertionError("Jmod failed: rc = " + rc); 160 } 161 } 162 }