PluginsNegativeTest.java revision 14869:f0bdc4ddb086
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 /* 25 * @test 26 * @summary Negative test for ImagePluginStack. 27 * @author Andrei Eremeev 28 * @modules jdk.jlink/jdk.tools.jlink.internal 29 * jdk.jlink/jdk.tools.jlink 30 * @run main/othervm PluginsNegativeTest 31 */ 32import java.lang.reflect.Layer; 33import java.util.ArrayList; 34import java.util.Collections; 35import java.util.List; 36import java.util.Map; 37 38import jdk.tools.jlink.internal.ImagePluginConfiguration; 39import jdk.tools.jlink.internal.PluginRepository; 40import jdk.tools.jlink.internal.ImagePluginStack; 41import jdk.tools.jlink.internal.ModulePoolImpl; 42import jdk.tools.jlink.Jlink; 43import jdk.tools.jlink.Jlink.PluginsConfiguration; 44import jdk.tools.jlink.plugin.Plugin; 45import jdk.tools.jlink.plugin.ModuleEntry; 46import jdk.tools.jlink.plugin.ModulePool; 47import jdk.tools.jlink.plugin.TransformerPlugin; 48 49public class PluginsNegativeTest { 50 51 public static void main(String[] args) throws Exception { 52 new PluginsNegativeTest().test(); 53 } 54 55 public void test() throws Exception { 56 testDuplicateBuiltInProviders(); 57 testUnknownProvider(); 58 PluginRepository.registerPlugin(new CustomPlugin("plugin")); 59 testEmptyInputResource(); 60 testEmptyOutputResource(); 61 } 62 63 private void testDuplicateBuiltInProviders() { 64 List<Plugin> javaPlugins = new ArrayList<>(); 65 javaPlugins.addAll(PluginRepository.getPlugins(Layer.boot())); 66 for (Plugin javaPlugin : javaPlugins) { 67 System.out.println("Registered plugin: " + javaPlugin.getName()); 68 } 69 for (Plugin javaPlugin : javaPlugins) { 70 String pluginName = javaPlugin.getName(); 71 try { 72 PluginRepository.registerPlugin(new CustomPlugin(pluginName)); 73 try { 74 PluginRepository.getPlugin(pluginName, Layer.boot()); 75 throw new AssertionError("Exception is not thrown for duplicate plugin: " + pluginName); 76 } catch (Exception ignored) { 77 } 78 } finally { 79 PluginRepository.unregisterPlugin(pluginName); 80 } 81 } 82 } 83 84 private void testUnknownProvider() { 85 if (PluginRepository.getPlugin("unknown", Layer.boot()) != null) { 86 throw new AssertionError("Exception expected for unknown plugin name"); 87 } 88 } 89 90 private static Plugin createPlugin(String name) { 91 return Jlink.newPlugin(name, Collections.emptyMap(), null); 92 } 93 94 private void testEmptyOutputResource() throws Exception { 95 List<Plugin> plugins = new ArrayList<>(); 96 plugins.add(createPlugin("plugin")); 97 ImagePluginStack stack = ImagePluginConfiguration.parseConfiguration(new PluginsConfiguration(plugins, 98 null, null)); 99 ModulePoolImpl inResources = new ModulePoolImpl(); 100 inResources.add(ModuleEntry.create("/aaa/bbb/A", new byte[10])); 101 try { 102 stack.visitResources(inResources); 103 throw new AssertionError("Exception expected when output resource is empty"); 104 } catch (Exception ignored) { 105 } 106 } 107 108 private void testEmptyInputResource() throws Exception { 109 List<Plugin> plugins = new ArrayList<>(); 110 plugins.add(createPlugin("plugin")); 111 ImagePluginStack stack = ImagePluginConfiguration.parseConfiguration(new PluginsConfiguration(plugins, 112 null, null)); 113 ModulePoolImpl inResources = new ModulePoolImpl(); 114 ModulePoolImpl outResources = (ModulePoolImpl) stack.visitResources(inResources); 115 if (!outResources.isEmpty()) { 116 throw new AssertionError("Output resource is not empty"); 117 } 118 } 119 120 public static class CustomPlugin implements TransformerPlugin { 121 122 private final String name; 123 124 CustomPlugin(String name) { 125 this.name = name; 126 } 127 128 @Override 129 public void visit(ModulePool inResources, ModulePool outResources) { 130 // do nothing 131 } 132 133 @Override 134 public String getName() { 135 return name; 136 } 137 138 @Override 139 public String getDescription() { 140 return null; 141 } 142 143 @Override 144 public void configure(Map<String, String> config) { 145 146 } 147 } 148} 149