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 } | 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 testModuleVerification(helper, pluginModulePath); 71 } 72 73 private void testCustomPlugins(Helper helper, Path pluginModulePath) { 74 Result result = JImageGenerator.getJLinkTask() 75 .option("--list-plugins") 76 .pluginModulePath(pluginModulePath) 77 .output(helper.createNewImageDir("customplugin")) 78 .call(); 79 if (result.getExitCode() != 0) { 80 System.err.println(result.getMessage()); 81 throw new AssertionError("jlink crashed: " + result.getExitCode()); 82 } 83 List<String> customPlugins = Stream.of(result.getMessage().split("\n")) 84 .filter(s -> s.startsWith("Plugin Name:")) 85 .filter(s -> s.contains("custom")) 86 .collect(Collectors.toList()); 87 if (customPlugins.size() != 1) { 88 System.err.println(result.getMessage()); 89 throw new AssertionError("Found plugins: " + customPlugins); 90 } 91 } 92 93 private Path registerServices(Helper helper) throws IOException { 94 String name = "customplugin"; 95 Path src = Paths.get(System.getProperty("test.src")).resolve(name); 96 Path classes = helper.getJmodClassesDir().resolve(name); 97 JImageGenerator.compile(src, classes); 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 140 private void testModuleVerification(Helper helper, Path pluginModulePath) throws IOException { 141 { 142 // dependent module missing check 143 String moduleName = "bar"; // 8147491 144 Path jmodFoo = helper.generateDefaultJModule("foo").assertSuccess(); 145 Path jmodBar = helper.generateDefaultJModule(moduleName, "foo").assertSuccess(); 146 // rogue filter removes "foo" module resources which are 147 // required by "bar" module. Module checks after plugin 148 // application should detect and report error. 149 JImageGenerator.getJLinkTask() 150 .modulePath(helper.defaultModulePath()) 151 .pluginModulePath(pluginModulePath) 152 .output(helper.createNewImageDir(moduleName)) 153 .addMods(moduleName) 154 .option("--rogue-filter") 155 .option("/foo/") 156 .call() 157 .assertFailure("java.lang.module.ResolutionException"); 158 } 159 160 { 161 // package exported by one module used as concealed package 162 // in another module. But, module-info.class is not updated! 163 String moduleName = "jdk.scripting.nashorn"; 164 JImageGenerator.getJLinkTask() 165 .modulePath(helper.defaultModulePath()) 166 .pluginModulePath(pluginModulePath) 167 .output(helper.createNewImageDir(moduleName)) 168 .addMods(moduleName) 169 // "java.logging" includes a package 'javax.script' 170 // which is an exported package from "java.scripting" module! 171 // module-info.class of java.logging left "as is". 172 .option("--rogue-adder") 173 .option("/java.logging/javax/script/Foo.class") 174 .call() 175 .assertFailure( 176 "Module java.logging's descriptor returns inconsistent package set"); 177 } 178 } 179 } |