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