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