Path: blob/master/test/jdk/tools/jlink/plugins/PluginsNegativeTest.java
41149 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @summary Negative test for ImagePluginStack.26* @author Andrei Eremeev27* @modules jdk.jlink/jdk.tools.jlink28* jdk.jlink/jdk.tools.jlink.internal29* jdk.jlink/jdk.tools.jlink.plugin30* @run main/othervm PluginsNegativeTest31*/32import java.util.ArrayList;33import java.util.Collections;34import java.util.List;35import java.util.Map;3637import jdk.tools.jlink.internal.ImagePluginConfiguration;38import jdk.tools.jlink.internal.Jlink;39import jdk.tools.jlink.internal.Jlink.PluginsConfiguration;40import jdk.tools.jlink.internal.PluginRepository;41import jdk.tools.jlink.internal.ImagePluginStack;42import jdk.tools.jlink.internal.ResourcePoolManager;43import jdk.tools.jlink.plugin.Plugin;44import jdk.tools.jlink.plugin.ResourcePool;45import jdk.tools.jlink.plugin.ResourcePoolBuilder;46import jdk.tools.jlink.plugin.ResourcePoolEntry;4748public class PluginsNegativeTest {4950public static void main(String[] args) throws Exception {51new PluginsNegativeTest().test();52}5354public void test() throws Exception {55testDuplicateBuiltInProviders();56testUnknownProvider();57PluginRepository.registerPlugin(new CustomPlugin("plugin"));58testEmptyInputResource();59testEmptyOutputResource();60}6162private void testDuplicateBuiltInProviders() {63List<Plugin> javaPlugins = new ArrayList<>();64javaPlugins.addAll(PluginRepository.getPlugins(ModuleLayer.boot()));65for (Plugin javaPlugin : javaPlugins) {66System.out.println("Registered plugin: " + javaPlugin.getName());67}68for (Plugin javaPlugin : javaPlugins) {69String pluginName = javaPlugin.getName();70try {71PluginRepository.registerPlugin(new CustomPlugin(pluginName));72try {73PluginRepository.getPlugin(pluginName, ModuleLayer.boot());74throw new AssertionError("Exception is not thrown for duplicate plugin: " + pluginName);75} catch (Exception ignored) {76}77} finally {78PluginRepository.unregisterPlugin(pluginName);79}80}81}8283private void testUnknownProvider() {84if (PluginRepository.getPlugin("unknown", ModuleLayer.boot()) != null) {85throw new AssertionError("Exception expected for unknown plugin name");86}87}8889private static Plugin createPlugin(String name) {90return Jlink.newPlugin(name, Collections.emptyMap(), null);91}9293private void testEmptyOutputResource() throws Exception {94List<Plugin> plugins = new ArrayList<>();95plugins.add(createPlugin("plugin"));96ImagePluginStack stack = ImagePluginConfiguration.parseConfiguration(new PluginsConfiguration(plugins,97null, null));98ResourcePoolManager inResources = new ResourcePoolManager();99inResources.add(ResourcePoolEntry.create("/aaa/bbb/A", new byte[10]));100try {101stack.visitResources(inResources);102throw new AssertionError("Exception expected when output resource is empty");103} catch (Exception ignored) {104}105}106107private void testEmptyInputResource() throws Exception {108List<Plugin> plugins = new ArrayList<>();109plugins.add(createPlugin("plugin"));110ImagePluginStack stack = ImagePluginConfiguration.parseConfiguration(new PluginsConfiguration(plugins,111null, null));112ResourcePoolManager inResources = new ResourcePoolManager();113ResourcePool outResources = stack.visitResources(inResources);114if (!outResources.isEmpty()) {115throw new AssertionError("Output resource is not empty");116}117}118119public static class CustomPlugin implements Plugin {120121private final String name;122123CustomPlugin(String name) {124this.name = name;125}126127@Override128public ResourcePool transform(ResourcePool inResources, ResourcePoolBuilder outResources) {129// don't add anything to the builder130return outResources.build();131}132133@Override134public String getName() {135return name;136}137138@Override139public String getDescription() {140return null;141}142143@Override144public void configure(Map<String, String> config) {145146}147}148}149150151