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 * @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 testHelp(pluginModulePath); 71 } 72 73 private void testHelp(Path pluginModulePath) { 74 Result result = JImageGenerator.getJLinkTask() 75 .option("--help") 76 .pluginModulePath(pluginModulePath) 77 .call(); 78 result.assertSuccess(); 79 String output = result.getMessage(); 80 List<String> plugins = new ArrayList<>(); 81 String[] lines = output.split("\n"); 82 for (String s : lines) { 83 if (s.startsWith(" --custom-image-plugin") || s.startsWith(" custom-image-plugin") || 84 s.startsWith(" --custom-resource-plugin") || s.startsWith(" custom-resource-plugin")) { 85 plugins.add(s); 86 } 87 } 88 if (plugins.size() != 4) { 89 System.err.println(output); 90 throw new AssertionError("Expected two plugins " + plugins); 91 } 92 for (int i = 0; i < plugins.size(); i += 2) { 93 String[] ss = plugins.get(i).trim().split(" +"); 94 String pluginName = ss[0].substring(2, ss[0].lastIndexOf('-')); 95 assertEquals("--" + pluginName + "-option", ss[0], output); 96 assertEquals(pluginName + "-argument", ss[1], output); 97 assertEquals(pluginName + "-description", plugins.get(i + 1).trim(), output); 98 } 99 } 100 101 private static void assertEquals(String expected, String actual, String message) { 102 if (!expected.equals(actual)) { 103 System.err.println(message); 104 throw new AssertionError("Expected: " + expected + ", got: " + actual); 105 } 106 } 107 108 private void testCustomPlugins(Helper helper, Path pluginModulePath) { 109 Result result = JImageGenerator.getJLinkTask() 110 .option("--list-plugins") 111 .pluginModulePath(pluginModulePath) 112 .output(helper.createNewImageDir("customplugin")) 113 .call(); 114 if (result.getExitCode() != 0) { 115 System.err.println(result.getMessage()); 116 throw new AssertionError("jlink crashed: " + result.getExitCode()); 117 } 118 List<String> customPlugins = Stream.of(result.getMessage().split("\n")) 119 .filter(s -> s.startsWith("Plugin Name:")) 120 .filter(s -> s.contains("custom")) 121 .collect(Collectors.toList()); 122 if (customPlugins.size() != 2) { 123 System.err.println(result.getMessage()); 124 throw new AssertionError("Found plugins: " + customPlugins); 125 } 126 } 127 128 private Path registerServices(Helper helper) throws IOException { 129 String name = "customplugin"; 130 Path src = Paths.get(System.getProperty("test.src")).resolve(name); 131 Path classes = helper.getJmodClassesDir().resolve(name); 132 JImageGenerator.compile(src, classes, "-XaddExports:jdk.jlink/jdk.tools.jlink.internal=customplugin"); 133 return JImageGenerator.getJModTask() 134 .addClassPath(classes) 135 .jmod(helper.getJmodDir().resolve(name + ".jmod")) 136 .create().assertSuccess(); 137 } 138 139 private void testHelloProvider(Helper helper, Path pluginModulePath) throws IOException { 140 Path pluginFile = Paths.get("customplugin.txt"); 141 if (Files.exists(pluginFile)) { 142 throw new AssertionError("Custom plugin output file already exists"); 143 } 144 String customplugin = "customplugin"; 145 { 146 // Add the path but not the option, plugin musn't be called 147 JImageGenerator.getJLinkTask() 148 .modulePath(helper.defaultModulePath()) 149 .pluginModulePath(pluginModulePath) 150 .output(helper.createNewImageDir(customplugin)) 151 .addMods(customplugin) 152 .call().assertSuccess(); 153 } 154 155 if (Files.exists(pluginFile)) { 156 throw new AssertionError("Custom plugin output file exists, plugin " 157 + " called although shouldn't have been"); 158 } 159 160 { // Add the path and the option, plugin should be called. 161 JImageGenerator.getJLinkTask() 162 .modulePath(helper.defaultModulePath()) 163 .addMods(customplugin) 164 .pluginModulePath(pluginModulePath) 165 .output(helper.createNewImageDir(customplugin)) 166 .option("--hello") 167 .call().assertSuccess(); 168 } 169 170 if (!Files.exists(pluginFile)) { 171 throw new AssertionError("Custom plugin not called"); 172 } 173 } 174 }