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").get();
  53     static final ToolProvider JLINK_TOOL = ToolProvider.findFirst("jlink").get();
  54 
  55     private final Path jdkHome = Paths.get(System.getProperty("test.jdk"));
  56     private final Path jdkMods = jdkHome.resolve("jmods");
  57     private final Path testSrc = Paths.get(System.getProperty("test.src"));
  58     private final Path src = testSrc.resolve("src");
  59     private final Path classes = Paths.get("classes");
  60     private final Path jmods = Paths.get("jmods");
  61     private final Path jars = Paths.get("jars");
  62 
  63     public static void main(String[] args) throws Throwable {
  64         new BasicTest().run();
  65     }
  66 
  67     public void run() throws Throwable {
  68         if (Files.notExists(jdkMods)) {
  69             return;
  70         }
  71 
  72         if (!CompilerUtils.compile(src, classes)) {
  73             throw new AssertionError("Compilation failure. See log.");
  74         }
  75 
  76         String modName = "test";
  77         Files.createDirectories(jmods);
  78         Files.createDirectories(jars);
  79         Path jarfile = jars.resolve("test.jar");
  80         JarUtils.createJarFile(jarfile, classes);
  81 
  82         Path image = Paths.get("mysmallimage");
  83         runJmod(jarfile.toString(), modName);
  84         runJlink(image, modName, "--compress", "2");
  85         execute(image, modName);
  86 
  87         Files.delete(jmods.resolve(modName + ".jmod"));
  88 
  89         image = Paths.get("myimage");
  90         runJmod(classes.toString(), modName);
  91         runJlink(image, modName);
  92         execute(image, modName);
  93     }
  94 
  95     private void execute(Path image, String moduleName) throws Throwable {
  96         String cmd = image.resolve("bin").resolve(moduleName).toString();
  97         OutputAnalyzer analyzer;
  98         if (System.getProperty("os.name").startsWith("Windows")) {
  99             analyzer = ProcessTools.executeProcess("sh.exe", cmd, "1", "2", "3");
 100         } else {
 101             analyzer = ProcessTools.executeProcess(cmd, "1", "2", "3");
 102         }
 103         if (analyzer.getExitValue() != 0) {
 104             throw new AssertionError("Image invocation failed: rc=" + analyzer.getExitValue());
 105         }
 106     }
 107 
 108     private void runJlink(Path image, String modName, String... options) {
 109         List<String> args = new ArrayList<>();
 110         Collections.addAll(args,
 111                 "--module-path", jdkMods + File.pathSeparator + jmods,
 112                 "--add-modules", modName,
 113                 "--output", image.toString());
 114         Collections.addAll(args, options);
 115 
 116         PrintWriter pw = new PrintWriter(System.out);
 117         int rc = JLINK_TOOL.run(pw, pw, args.toArray(new String[args.size()]));
 118         if (rc != 0) {
 119             throw new AssertionError("Jlink failed: rc = " + rc);
 120         }
 121     }
 122 
 123     private void runJmod(String cp, String modName) {
 124         int rc = JMOD_TOOL.run(System.out, System.out, new String[] {
 125                 "create",
 126                 "--class-path", cp,
 127                 "--module-version", "1.0",
 128                 "--main-class", "jdk.test.Test",
 129                 jmods.resolve(modName + ".jmod").toString(),
 130         });
 131         if (rc != 0) {
 132             throw new AssertionError("Jmod failed: rc = " + rc);
 133         }
 134     }
 135 }