jdk/test/tools/jlink/CustomPluginTest.java
author mchung
Wed, 10 Aug 2016 15:51:25 -0700
changeset 40261 86a49ba76f52
parent 36511 9d0388c6b336
child 40556 27f68990ba9d
permissions -rw-r--r--
8136930: Simplify use of module-system options by custom launchers Reviewed-by: alanb, ksrini, henryjen, sundar
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, 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
import java.io.IOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
import java.nio.file.Files;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
import java.nio.file.Paths;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.util.ArrayList;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.util.List;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.util.stream.Collectors;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.util.stream.Stream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import tests.Helper;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import tests.JImageGenerator;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import tests.Result;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
 * @test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
 * @summary Test custom plugin
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
 * @author Jean-Francois Denise
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
 * @library ../lib
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
 * @modules java.base/jdk.internal.jimage
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
 *          jdk.jdeps/com.sun.tools.classfile
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
 *          jdk.jlink/jdk.tools.jlink.internal
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
 *          jdk.jlink/jdk.tools.jmod
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
 *          jdk.jlink/jdk.tools.jimage
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
 *          jdk.compiler
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
 * @build tests.*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
 * @run main/othervm CustomPluginTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
public class CustomPluginTest {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
    public static void main(String[] args) throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
        new CustomPluginTest().test();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
    private void test() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
        Helper helper = Helper.newHelper();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
        if (helper == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
            System.err.println("Test not run");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
            return;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
        helper.generateDefaultModules();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
        Path jmod = registerServices(helper);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
        Path pluginModulePath = jmod.getParent();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
        testHelloProvider(helper, pluginModulePath);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
        testCustomPlugins(helper, pluginModulePath);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
    private void testCustomPlugins(Helper helper, Path pluginModulePath) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
        Result result = JImageGenerator.getJLinkTask()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
                .option("--list-plugins")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
                .pluginModulePath(pluginModulePath)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
                .output(helper.createNewImageDir("customplugin"))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
                .call();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
        if (result.getExitCode() != 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
            System.err.println(result.getMessage());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
            throw new AssertionError("jlink crashed: " + result.getExitCode());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
        List<String> customPlugins = Stream.of(result.getMessage().split("\n"))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
                .filter(s -> s.startsWith("Plugin Name:"))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
                .filter(s -> s.contains("custom"))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
                .collect(Collectors.toList());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
        if (customPlugins.size() != 1) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
            System.err.println(result.getMessage());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
            throw new AssertionError("Found plugins: " + customPlugins);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
    private Path registerServices(Helper helper) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
        String name = "customplugin";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
        Path src = Paths.get(System.getProperty("test.src")).resolve(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
        Path classes = helper.getJmodClassesDir().resolve(name);
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 36511
diff changeset
    96
        JImageGenerator.compile(src, classes,
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 36511
diff changeset
    97
                                "--add-exports", "jdk.jlink/jdk.tools.jlink.internal=customplugin");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
        return JImageGenerator.getJModTask()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
                .addClassPath(classes)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
                .jmod(helper.getJmodDir().resolve(name + ".jmod"))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
                .create().assertSuccess();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
    private void testHelloProvider(Helper helper, Path pluginModulePath) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
        Path pluginFile = Paths.get("customplugin.txt");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
        if (Files.exists(pluginFile)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
            throw new AssertionError("Custom plugin output file already exists");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
        String customplugin = "customplugin";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
        {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
            // Add the path but not the option, plugin musn't be called
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
            JImageGenerator.getJLinkTask()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
                    .modulePath(helper.defaultModulePath())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
                    .pluginModulePath(pluginModulePath)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
                    .output(helper.createNewImageDir(customplugin))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
                    .addMods(customplugin)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
                    .call().assertSuccess();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
        if (Files.exists(pluginFile)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
            throw new AssertionError("Custom plugin output file exists, plugin "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
                    + " called although shouldn't have been");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
        { // Add the path and the option, plugin should be called.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
            JImageGenerator.getJLinkTask()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
                    .modulePath(helper.defaultModulePath())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
                    .addMods(customplugin)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
                    .pluginModulePath(pluginModulePath)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
                    .output(helper.createNewImageDir(customplugin))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
                    .option("--hello")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
                    .call().assertSuccess();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
        if (!Files.exists(pluginFile)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
            throw new AssertionError("Custom plugin not called");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
}