Path: blob/master/test/jdk/tools/jlink/basic/BasicTest.java
41152 views
/*1* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @summary Basic test of jlink to create jmods and images26* @author Andrei Eremeev27* @library /test/lib28* @modules java.base/jdk.internal.module29* jdk.jlink30* jdk.compiler31* @build jdk.test.lib.process.ProcessTools32* jdk.test.lib.process.OutputAnalyzer33* jdk.test.lib.compiler.CompilerUtils34* jdk.test.lib.util.JarUtils35* @run main BasicTest36*/3738import java.io.File;39import java.io.PrintWriter;40import java.nio.file.Files;41import java.nio.file.Path;42import java.nio.file.Paths;43import java.util.ArrayList;44import java.util.Collections;45import java.util.List;46import java.util.spi.ToolProvider;4748import jdk.test.lib.compiler.CompilerUtils;49import jdk.test.lib.process.OutputAnalyzer;50import jdk.test.lib.process.ProcessTools;51import jdk.test.lib.util.JarUtils;5253public class BasicTest {54static final ToolProvider JMOD_TOOL = ToolProvider.findFirst("jmod")55.orElseThrow(() ->56new RuntimeException("jmod tool not found")57);5859static final ToolProvider JLINK_TOOL = ToolProvider.findFirst("jlink")60.orElseThrow(() ->61new RuntimeException("jlink tool not found")62);6364private final String TEST_MODULE = "test";65private final Path jdkHome = Paths.get(System.getProperty("test.jdk"));66private final Path jdkMods = jdkHome.resolve("jmods");67private final Path testSrc = Paths.get(System.getProperty("test.src"));68private final Path src = testSrc.resolve("src").resolve(TEST_MODULE);69private final Path classes = Paths.get("classes");70private final Path jmods = Paths.get("jmods");71private final Path jars = Paths.get("jars");7273public static void main(String[] args) throws Throwable {74new BasicTest().run();75}7677public void run() throws Throwable {78if (Files.notExists(jdkMods)) {79return;80}8182if (!CompilerUtils.compile(src, classes)) {83throw new AssertionError("Compilation failure. See log.");84}8586Files.createDirectories(jmods);87Files.createDirectories(jars);88Path jarfile = jars.resolve("test.jar");89JarUtils.createJarFile(jarfile, classes);9091Path image = Paths.get("mysmallimage");92runJmod(jarfile.toString(), TEST_MODULE, true);93runJlink(image, TEST_MODULE, "--compress", "2", "--launcher", "foo=" + TEST_MODULE);94execute(image, "foo");9596Files.delete(jmods.resolve(TEST_MODULE + ".jmod"));9798image = Paths.get("myimage");99runJmod(classes.toString(), TEST_MODULE, true);100runJlink(image, TEST_MODULE, "--launcher", "bar=" + TEST_MODULE);101execute(image, "bar");102Files.delete(jmods.resolve(TEST_MODULE + ".jmod"));103104image = Paths.get("myimage2");105runJmod(classes.toString(), TEST_MODULE, false /* no ModuleMainClass! */);106// specify main class in --launcher command line107runJlink(image, TEST_MODULE, "--launcher", "bar2=" + TEST_MODULE + "/jdk.test.Test");108execute(image, "bar2");109Files.delete(jmods.resolve(TEST_MODULE + ".jmod"));110111image = Paths.get("myadder");112runJmod(classes.toString(), TEST_MODULE, false /* no ModuleMainClass! */);113// specify main class in --launcher command line114runJlink(image, TEST_MODULE, "--launcher", "adder=" + TEST_MODULE + "/jdk.test.Adder");115addAndCheck(image, "adder");116}117118private void addAndCheck(Path image, String scriptName) throws Throwable {119String cmd = image.resolve("bin").resolve(scriptName).toString();120OutputAnalyzer analyzer;121if (System.getProperty("os.name").startsWith("Windows")) {122analyzer = ProcessTools.executeProcess("sh.exe", cmd, "12", "8", "7", "--", "foo bar");123} else {124analyzer = ProcessTools.executeProcess(cmd, "12", "8", "7", "--", "foo bar");125}126if (analyzer.getExitValue() != 27) {127throw new AssertionError("Image invocation failed: expected 27, rc=" + analyzer.getExitValue());128}129// last argument contains space and should be properly quoted.130analyzer.stdoutShouldContain("Num args: 5");131}132133private void execute(Path image, String scriptName) throws Throwable {134String cmd = image.resolve("bin").resolve(scriptName).toString();135OutputAnalyzer analyzer;136if (System.getProperty("os.name").startsWith("Windows")) {137analyzer = ProcessTools.executeProcess("sh.exe", cmd, "1", "2", "3");138} else {139analyzer = ProcessTools.executeProcess(cmd, "1", "2", "3");140}141if (analyzer.getExitValue() != 0) {142throw new AssertionError("Image invocation failed: rc=" + analyzer.getExitValue());143}144}145146private void runJlink(Path image, String modName, String... options) {147List<String> args = new ArrayList<>();148Collections.addAll(args,149"--module-path", jdkMods + File.pathSeparator + jmods,150"--add-modules", modName,151"--output", image.toString());152Collections.addAll(args, options);153154PrintWriter pw = new PrintWriter(System.out);155int rc = JLINK_TOOL.run(pw, pw, args.toArray(new String[args.size()]));156if (rc != 0) {157throw new AssertionError("Jlink failed: rc = " + rc);158}159}160161private void runJmod(String cp, String modName, boolean main) {162int rc;163if (main) {164rc = JMOD_TOOL.run(System.out, System.out, new String[] {165"create",166"--class-path", cp,167"--module-version", "1.0",168"--main-class", "jdk.test.Test",169jmods.resolve(modName + ".jmod").toString()170});171} else {172rc = JMOD_TOOL.run(System.out, System.out, new String[] {173"create",174"--class-path", cp,175"--module-version", "1.0",176jmods.resolve(modName + ".jmod").toString(),177});178}179180if (rc != 0) {181throw new AssertionError("Jmod failed: rc = " + rc);182}183}184}185186187