test/jdk/tools/jlink/plugins/PluginsNegativeTest.java
changeset 47216 71c04702a3d5
parent 44545 83b611b88ac8
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     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.util.ArrayList;
       
    34 import java.util.Collections;
       
    35 import java.util.List;
       
    36 import java.util.Map;
       
    37 
       
    38 import jdk.tools.jlink.internal.ImagePluginConfiguration;
       
    39 import jdk.tools.jlink.internal.Jlink;
       
    40 import jdk.tools.jlink.internal.Jlink.PluginsConfiguration;
       
    41 import jdk.tools.jlink.internal.PluginRepository;
       
    42 import jdk.tools.jlink.internal.ImagePluginStack;
       
    43 import jdk.tools.jlink.internal.ResourcePoolManager;
       
    44 import jdk.tools.jlink.plugin.Plugin;
       
    45 import jdk.tools.jlink.plugin.ResourcePool;
       
    46 import jdk.tools.jlink.plugin.ResourcePoolBuilder;
       
    47 import jdk.tools.jlink.plugin.ResourcePoolEntry;
       
    48 
       
    49 public 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(ModuleLayer.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, ModuleLayer.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", ModuleLayer.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         ResourcePoolManager inResources = new ResourcePoolManager();
       
   100         inResources.add(ResourcePoolEntry.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         ResourcePoolManager inResources = new ResourcePoolManager();
       
   114         ResourcePool outResources = 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 Plugin {
       
   121 
       
   122         private final String name;
       
   123 
       
   124         CustomPlugin(String name) {
       
   125             this.name = name;
       
   126         }
       
   127 
       
   128         @Override
       
   129         public ResourcePool transform(ResourcePool inResources, ResourcePoolBuilder outResources) {
       
   130             // don't add anything to the builder
       
   131             return outResources.build();
       
   132         }
       
   133 
       
   134         @Override
       
   135         public String getName() {
       
   136             return name;
       
   137         }
       
   138 
       
   139         @Override
       
   140         public String getDescription() {
       
   141             return null;
       
   142         }
       
   143 
       
   144         @Override
       
   145         public void configure(Map<String, String> config) {
       
   146 
       
   147         }
       
   148     }
       
   149 }