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 import java.io.IOException;
  25 import java.nio.file.Files;
  26 import java.nio.file.Path;
  27 import java.nio.file.Paths;
  28 import java.util.ArrayList;
  29 import java.util.List;
  30 import java.util.stream.Collectors;
  31 import java.util.stream.Stream;
  32 
  33 import tests.Helper;
  34 import tests.JImageGenerator;
  35 import tests.Result;
  36 
  37 /*
  38  * @test
  39  * @summary Test custom plugin
  40  * @author Jean-Francois Denise
  41  * @library ../lib
  42  * @modules java.base/jdk.internal.jimage
  43  *          jdk.jdeps/com.sun.tools.classfile
  44  *          jdk.jlink/jdk.tools.jlink
  45  *          jdk.jlink/jdk.tools.jlink.internal
  46  *          jdk.jlink/jdk.tools.jmod
  47  *          jdk.jlink/jdk.tools.jimage
  48  *          jdk.compiler
  49  * @build tests.*
  50  * @run main/othervm CustomPluginTest
  51  */
  52 
  53 public class CustomPluginTest {
  54 
  55     public static void main(String[] args) throws Exception {
  56         new CustomPluginTest().test();
  57     }
  58 
  59     private void test() throws Exception {
  60         Helper helper = Helper.newHelper();
  61         if (helper == null) {
  62             System.err.println("Test not run");
  63             return;
  64         }
  65         helper.generateDefaultModules();
  66         Path jmod = registerServices(helper);
  67         Path pluginModulePath = jmod.getParent();
  68 
  69         testHelloProvider(helper, pluginModulePath);
  70         testCustomPlugins(helper, pluginModulePath);
  71         testHelp(pluginModulePath);
  72     }
  73 
  74     private void testHelp(Path pluginModulePath) {
  75         Result result = JImageGenerator.getJLinkTask()
  76                 .option("--help")
  77                 .pluginModulePath(pluginModulePath)
  78                 .call();
  79         result.assertSuccess();
  80         String output = result.getMessage();
  81         List<String> plugins = new ArrayList<>();
  82         String[] lines = output.split("\n");
  83         for (String s : lines) {
  84             if (s.startsWith(" --custom-image-plugin") || s.startsWith(" custom-image-plugin") ||
  85                     s.startsWith(" --custom-resource-plugin") || s.startsWith(" custom-resource-plugin")) {
  86                 plugins.add(s);
  87             }
  88         }
  89         if (plugins.size() != 4) {
  90             System.err.println(output);
  91             throw new AssertionError("Expected two plugins " + plugins);
  92         }
  93         for (int i = 0; i < plugins.size(); i += 2) {
  94             String[] ss = plugins.get(i).trim().split(" +");
  95             String pluginName = ss[0].substring(2, ss[0].lastIndexOf('-'));
  96             assertEquals("--" + pluginName + "-option", ss[0], output);
  97             assertEquals(pluginName + "-argument", ss[1], output);
  98             assertEquals(pluginName + "-description", plugins.get(i + 1).trim(), output);
  99         }
 100     }
 101 
 102     private static void assertEquals(String expected, String actual, String message) {
 103         if (!expected.equals(actual)) {
 104             System.err.println(message);
 105             throw new AssertionError("Expected: " + expected + ", got: " + actual);
 106         }
 107     }
 108 
 109     private void testCustomPlugins(Helper helper, Path pluginModulePath) {
 110         Result result = JImageGenerator.getJLinkTask()
 111                 .option("--list-plugins")
 112                 .pluginModulePath(pluginModulePath)
 113                 .output(helper.createNewImageDir("customplugin"))
 114                 .call();
 115         if (result.getExitCode() != 0) {
 116             System.err.println(result.getMessage());
 117             throw new AssertionError("jlink crashed: " + result.getExitCode());
 118         }
 119         List<String> customPlugins = Stream.of(result.getMessage().split("\n"))
 120                 .filter(s -> s.startsWith("Plugin Name:"))
 121                 .filter(s -> s.contains("custom"))
 122                 .collect(Collectors.toList());
 123         if (customPlugins.size() != 2) {
 124             System.err.println(result.getMessage());
 125             throw new AssertionError("Found plugins: " + customPlugins);
 126         }
 127     }
 128 
 129     private Path registerServices(Helper helper) throws IOException {
 130         String name = "customplugin";
 131         Path src = Paths.get(System.getProperty("test.src")).resolve(name);
 132         Path classes = helper.getJmodClassesDir().resolve(name);
 133         JImageGenerator.compile(src, classes, "-XaddExports:jdk.jlink/jdk.tools.jlink.internal=customplugin");
 134         return JImageGenerator.getJModTask()
 135                 .addClassPath(classes)
 136                 .jmod(helper.getJmodDir().resolve(name + ".jmod"))
 137                 .create().assertSuccess();
 138     }
 139 
 140     private void testHelloProvider(Helper helper, Path pluginModulePath) throws IOException {
 141         Path pluginFile = Paths.get("customplugin.txt");
 142         if (Files.exists(pluginFile)) {
 143             throw new AssertionError("Custom plugin output file already exists");
 144         }
 145         String customplugin = "customplugin";
 146         {
 147             // Add the path but not the option, plugin musn't be called
 148             JImageGenerator.getJLinkTask()
 149                     .modulePath(helper.defaultModulePath())
 150                     .pluginModulePath(pluginModulePath)
 151                     .output(helper.createNewImageDir(customplugin))
 152                     .addMods(customplugin)
 153                     .call().assertSuccess();
 154         }
 155 
 156         if (Files.exists(pluginFile)) {
 157             throw new AssertionError("Custom plugin output file exists, plugin "
 158                     + " called although shouldn't have been");
 159         }
 160 
 161         { // Add the path and the option, plugin should be called.
 162             JImageGenerator.getJLinkTask()
 163                     .modulePath(helper.defaultModulePath())
 164                     .addMods(customplugin)
 165                     .pluginModulePath(pluginModulePath)
 166                     .output(helper.createNewImageDir(customplugin))
 167                     .option("--hello")
 168                     .call().assertSuccess();
 169         }
 170 
 171         if (!Files.exists(pluginFile)) {
 172             throw new AssertionError("Custom plugin not called");
 173         }
 174     }
 175 }