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.internal
  45  *          jdk.jlink/jdk.tools.jmod
  46  *          jdk.jlink/jdk.tools.jimage
  47  *          jdk.compiler
  48  * @build tests.*
  49  * @run main/othervm CustomPluginTest
  50  */
  51 
  52 public class CustomPluginTest {
  53 
  54     public static void main(String[] args) throws Exception {
  55         new CustomPluginTest().test();
  56     }
  57 
  58     private void test() throws Exception {
  59         Helper helper = Helper.newHelper();
  60         if (helper == null) {
  61             System.err.println("Test not run");
  62             return;
  63         }
  64         helper.generateDefaultModules();
  65         Path jmod = registerServices(helper);
  66         Path pluginModulePath = jmod.getParent();
  67 
  68         testHelloProvider(helper, pluginModulePath);
  69         testCustomPlugins(helper, pluginModulePath);
  70     }
  71 
  72     private void testCustomPlugins(Helper helper, Path pluginModulePath) {
  73         Result result = JImageGenerator.getJLinkTask()
  74                 .option("--list-plugins")
  75                 .pluginModulePath(pluginModulePath)
  76                 .output(helper.createNewImageDir("customplugin"))
  77                 .call();
  78         if (result.getExitCode() != 0) {
  79             System.err.println(result.getMessage());
  80             throw new AssertionError("jlink crashed: " + result.getExitCode());
  81         }
  82         List<String> customPlugins = Stream.of(result.getMessage().split("\n"))
  83                 .filter(s -> s.startsWith("Plugin Name:"))
  84                 .filter(s -> s.contains("custom"))
  85                 .collect(Collectors.toList());
  86         if (customPlugins.size() != 1) {
  87             System.err.println(result.getMessage());
  88             throw new AssertionError("Found plugins: " + customPlugins);
  89         }
  90     }
  91 
  92     private Path registerServices(Helper helper) throws IOException {
  93         String name = "customplugin";
  94         Path src = Paths.get(System.getProperty("test.src")).resolve(name);
  95         Path classes = helper.getJmodClassesDir().resolve(name);
  96         JImageGenerator.compile(src, classes,
  97                                 "--add-exports", "jdk.jlink/jdk.tools.jlink.internal=customplugin");
  98         return JImageGenerator.getJModTask()
  99                 .addClassPath(classes)
 100                 .jmod(helper.getJmodDir().resolve(name + ".jmod"))
 101                 .create().assertSuccess();
 102     }
 103 
 104     private void testHelloProvider(Helper helper, Path pluginModulePath) throws IOException {
 105         Path pluginFile = Paths.get("customplugin.txt");
 106         if (Files.exists(pluginFile)) {
 107             throw new AssertionError("Custom plugin output file already exists");
 108         }
 109         String customplugin = "customplugin";
 110         {
 111             // Add the path but not the option, plugin musn't be called
 112             JImageGenerator.getJLinkTask()
 113                     .modulePath(helper.defaultModulePath())
 114                     .pluginModulePath(pluginModulePath)
 115                     .output(helper.createNewImageDir(customplugin))
 116                     .addMods(customplugin)
 117                     .call().assertSuccess();
 118         }
 119 
 120         if (Files.exists(pluginFile)) {
 121             throw new AssertionError("Custom plugin output file exists, plugin "
 122                     + " called although shouldn't have been");
 123         }
 124 
 125         { // Add the path and the option, plugin should be called.
 126             JImageGenerator.getJLinkTask()
 127                     .modulePath(helper.defaultModulePath())
 128                     .addMods(customplugin)
 129                     .pluginModulePath(pluginModulePath)
 130                     .output(helper.createNewImageDir(customplugin))
 131                     .option("--hello")
 132                     .call().assertSuccess();
 133         }
 134 
 135         if (!Files.exists(pluginFile)) {
 136             throw new AssertionError("Custom plugin not called");
 137         }
 138     }
 139 }