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, "-XaddExports:jdk.jlink/jdk.tools.jlink.internal=customplugin"); 97 return JImageGenerator.getJModTask() 98 .addClassPath(classes) 99 .jmod(helper.getJmodDir().resolve(name + ".jmod")) 100 .create().assertSuccess(); 101 } 102 103 private void testHelloProvider(Helper helper, Path pluginModulePath) throws IOException { 104 Path pluginFile = Paths.get("customplugin.txt"); 105 if (Files.exists(pluginFile)) { 106 throw new AssertionError("Custom plugin output file already exists"); 107 } 108 String customplugin = "customplugin"; 109 { 110 // Add the path but not the option, plugin musn't be called 111 JImageGenerator.getJLinkTask() 112 .modulePath(helper.defaultModulePath()) 113 .pluginModulePath(pluginModulePath) 114 .output(helper.createNewImageDir(customplugin)) 115 .addMods(customplugin) 116 .call().assertSuccess(); 117 } 118 119 if (Files.exists(pluginFile)) { 120 throw new AssertionError("Custom plugin output file exists, plugin " 121 + " called although shouldn't have been"); 122 } 123 124 { // Add the path and the option, plugin should be called. 125 JImageGenerator.getJLinkTask() 126 .modulePath(helper.defaultModulePath()) 127 .addMods(customplugin) 128 .pluginModulePath(pluginModulePath) 129 .output(helper.createNewImageDir(customplugin)) 130 .option("--hello") 131 .call().assertSuccess(); 132 } 133 134 if (!Files.exists(pluginFile)) { 135 throw new AssertionError("Custom plugin not called"); 136 } 137 } 138 }