jdk/test/tools/jlink/basic/BasicTest.java
author alanb
Thu, 17 Mar 2016 19:04:16 +0000
changeset 36511 9d0388c6b336
child 40261 86a49ba76f52
permissions -rw-r--r--
8142968: Module System implementation Summary: Initial integration of JEP 200, JEP 260, JEP 261, and JEP 282 Reviewed-by: alanb, mchung, naoto, rriggs, psandoz, plevart, mullan, ascarpino, vinnie, prr, sherman, dfuchs, mhaupt Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, chris.hegarty@oracle.com, alexandr.scherbatiy@oracle.com, amy.lu@oracle.com, calvin.cheung@oracle.com, daniel.fuchs@oracle.com, erik.joelsson@oracle.com, harold.seigel@oracle.com, jaroslav.bachorik@oracle.com, jean-francois.denise@oracle.com, jan.lahoda@oracle.com, james.laskey@oracle.com, lois.foltan@oracle.com, miroslav.kos@oracle.com, huaming.li@oracle.com, sean.mullan@oracle.com, naoto.sato@oracle.com, masayoshi.okutsu@oracle.com, peter.levart@gmail.com, philip.race@oracle.com, claes.redestad@oracle.com, sergey.bylokhov@oracle.com, alexandre.iline@oracle.com, volker.simonis@gmail.com, staffan.larsen@oracle.com, stuart.marks@oracle.com, semyon.sadetsky@oracle.com, serguei.spitsyn@oracle.com, sundararajan.athijegannathan@oracle.com, valerie.peng@oracle.com, vincent.x.ryan@oracle.com, weijun.wang@oracle.com, yuri.nesterenko@oracle.com, yekaterina.kantserova@oracle.com, alexander.kulyakhtin@oracle.com, felix.yang@oracle.com, andrei.eremeev@oracle.com, frank.yuan@oracle.com, sergei.pikalev@oracle.com, sibabrata.sahoo@oracle.com, tiantian.du@oracle.com, sha.jiang@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
 * @test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
 * @summary Basic test of jlink to create jmods and images
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
 * @author Andrei Eremeev
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
 * @library /lib/testlibrary
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
 * @modules java.base/jdk.internal.module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
 *          jdk.jlink/jdk.tools.jlink.internal
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
 *          jdk.jlink/jdk.tools.jmod
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
 *          jdk.compiler
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
 * @build jdk.testlibrary.ProcessTools
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
 *        jdk.testlibrary.OutputAnalyzer
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
 *        JarUtils CompilerUtils
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
 * @run main BasicTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.io.File;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.io.PrintWriter;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.nio.file.Files;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.nio.file.Paths;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import java.util.ArrayList;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import java.util.Collections;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import java.util.List;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import jdk.testlibrary.OutputAnalyzer;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
import jdk.testlibrary.ProcessTools;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
public class BasicTest {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
    private final Path jdkHome = Paths.get(System.getProperty("test.jdk"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
    private final Path jdkMods = jdkHome.resolve("jmods");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
    private final Path testSrc = Paths.get(System.getProperty("test.src"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
    private final Path src = testSrc.resolve("src");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
    private final Path classes = Paths.get("classes");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
    private final Path jmods = Paths.get("jmods");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
    private final Path jars = Paths.get("jars");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
    public static void main(String[] args) throws Throwable {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
        new BasicTest().run();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
    public void run() throws Throwable {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
        if (Files.notExists(jdkMods)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
            return;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
        if (!CompilerUtils.compile(src, classes)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
            throw new AssertionError("Compilation failure. See log.");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
        String modName = "test";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
        Files.createDirectories(jmods);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
        Files.createDirectories(jars);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
        Path jarfile = jars.resolve("test.jar");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
        JarUtils.createJarFile(jarfile, classes);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
        Path image = Paths.get("mysmallimage");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
        runJmod(jarfile.toString(), modName);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
        runJlink(image, modName, "--compress", "2");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
        execute(image, modName);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
        Files.delete(jmods.resolve(modName + ".jmod"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
        image = Paths.get("myimage");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
        runJmod(classes.toString(), modName);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
        runJlink(image, modName);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
        execute(image, modName);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
    private void execute(Path image, String moduleName) throws Throwable {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
        String cmd = image.resolve("bin").resolve(moduleName).toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
        OutputAnalyzer analyzer;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
        if (System.getProperty("os.name").startsWith("Windows")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
            analyzer = ProcessTools.executeProcess("sh.exe", cmd, "1", "2", "3");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
            analyzer = ProcessTools.executeProcess(cmd, "1", "2", "3");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
        if (analyzer.getExitValue() != 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
            throw new AssertionError("Image invocation failed: rc=" + analyzer.getExitValue());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
    private void runJlink(Path image, String modName, String... options) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
        List<String> args = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
        Collections.addAll(args,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
                "--modulepath", jdkMods + File.pathSeparator + jmods,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
                "--addmods", modName,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
                "--output", image.toString());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
        Collections.addAll(args, options);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
        int rc = jdk.tools.jlink.internal.Main.run(args.toArray(new String[args.size()]), new PrintWriter(System.out));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
        if (rc != 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
            throw new AssertionError("Jlink failed: rc = " + rc);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
    private void runJmod(String cp, String modName) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
        int rc = jdk.tools.jmod.Main.run(new String[] {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
                "create",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
                "--class-path", cp,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
                "--module-version", "1.0",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
                "--main-class", "jdk.test.Test",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
                jmods.resolve(modName + ".jmod").toString(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
        }, System.out);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
        if (rc != 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
            throw new AssertionError("Jmod failed: rc = " + rc);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
}