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