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