jdk/test/tools/jlink/plugins/PluginsNegativeTest.java
author alanb
Fri, 07 Apr 2017 08:05:54 +0000
changeset 44545 83b611b88ac8
parent 43185 d75d9ff8d4e7
permissions -rw-r--r--
8177530: Module system implementation refresh (4/2017) Reviewed-by: mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
 /*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
 * @test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
 * @summary Negative test for ImagePluginStack.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
 * @author Andrei Eremeev
43185
d75d9ff8d4e7 8171380: Remove all exports from jdk.jlink
chegar
parents: 41559
diff changeset
    28
 * @modules jdk.jlink/jdk.tools.jlink
d75d9ff8d4e7 8171380: Remove all exports from jdk.jlink
chegar
parents: 41559
diff changeset
    29
 *          jdk.jlink/jdk.tools.jlink.internal
d75d9ff8d4e7 8171380: Remove all exports from jdk.jlink
chegar
parents: 41559
diff changeset
    30
 *          jdk.jlink/jdk.tools.jlink.plugin
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
 * @run main/othervm PluginsNegativeTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.util.ArrayList;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.util.Collections;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.util.List;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.util.Map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import jdk.tools.jlink.internal.ImagePluginConfiguration;
41559
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents: 39834
diff changeset
    39
import jdk.tools.jlink.internal.Jlink;
5b33f33df1fb 8168091: jlink should check security permission early when programmatic access is used
sundar
parents: 39834
diff changeset
    40
import jdk.tools.jlink.internal.Jlink.PluginsConfiguration;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import jdk.tools.jlink.internal.PluginRepository;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import jdk.tools.jlink.internal.ImagePluginStack;
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
    43
import jdk.tools.jlink.internal.ResourcePoolManager;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import jdk.tools.jlink.plugin.Plugin;
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
    45
import jdk.tools.jlink.plugin.ResourcePool;
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
    46
import jdk.tools.jlink.plugin.ResourcePoolBuilder;
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
    47
import jdk.tools.jlink.plugin.ResourcePoolEntry;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
public class PluginsNegativeTest {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
    public static void main(String[] args) throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
        new PluginsNegativeTest().test();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
    public void test() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
        testDuplicateBuiltInProviders();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
        testUnknownProvider();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
        PluginRepository.registerPlugin(new CustomPlugin("plugin"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
        testEmptyInputResource();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
        testEmptyOutputResource();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
    private void testDuplicateBuiltInProviders() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
        List<Plugin> javaPlugins = new ArrayList<>();
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43185
diff changeset
    65
        javaPlugins.addAll(PluginRepository.getPlugins(ModuleLayer.boot()));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
        for (Plugin javaPlugin : javaPlugins) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
            System.out.println("Registered plugin: " + javaPlugin.getName());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
        for (Plugin javaPlugin : javaPlugins) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
            String pluginName = javaPlugin.getName();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
                PluginRepository.registerPlugin(new CustomPlugin(pluginName));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
                try {
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43185
diff changeset
    74
                    PluginRepository.getPlugin(pluginName, ModuleLayer.boot());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
                    throw new AssertionError("Exception is not thrown for duplicate plugin: " + pluginName);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
                } catch (Exception ignored) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
            } finally {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
                PluginRepository.unregisterPlugin(pluginName);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
    private void testUnknownProvider() {
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43185
diff changeset
    85
        if (PluginRepository.getPlugin("unknown", ModuleLayer.boot()) != null) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
            throw new AssertionError("Exception expected for unknown plugin name");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
    private static Plugin createPlugin(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
        return Jlink.newPlugin(name, Collections.emptyMap(), null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
    private void testEmptyOutputResource() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
        List<Plugin> plugins = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
        plugins.add(createPlugin("plugin"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
        ImagePluginStack stack = ImagePluginConfiguration.parseConfiguration(new PluginsConfiguration(plugins,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
                null, null));
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
    99
        ResourcePoolManager inResources = new ResourcePoolManager();
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   100
        inResources.add(ResourcePoolEntry.create("/aaa/bbb/A", new byte[10]));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
            stack.visitResources(inResources);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
            throw new AssertionError("Exception expected when output resource is empty");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
        } catch (Exception ignored) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
    private void testEmptyInputResource() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
        List<Plugin> plugins = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
        plugins.add(createPlugin("plugin"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
        ImagePluginStack stack = ImagePluginConfiguration.parseConfiguration(new PluginsConfiguration(plugins,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
                null, null));
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   113
        ResourcePoolManager inResources = new ResourcePoolManager();
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   114
        ResourcePool outResources = stack.visitResources(inResources);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
        if (!outResources.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
            throw new AssertionError("Output resource is not empty");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
39321
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 39129
diff changeset
   120
    public static class CustomPlugin implements Plugin {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
        private final String name;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
        CustomPlugin(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
            this.name = name;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
        @Override
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   129
        public ResourcePool transform(ResourcePool inResources, ResourcePoolBuilder outResources) {
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   130
            // don't add anything to the builder
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 39321
diff changeset
   131
            return outResources.build();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
        public String getName() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
            return name;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
        public String getDescription() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
            return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
        public void configure(Map<String, String> config) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
}