Path: blob/master/test/jdk/tools/jlink/DefaultProviderTest.java
41144 views
/*1* Copyright (c) 2015, 2018, 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*/2223import java.nio.file.Path;24import java.util.ArrayList;25import java.util.Collections;26import java.util.EnumSet;27import java.util.HashMap;28import java.util.List;29import java.util.Map;30import java.util.Set;31import jdk.tools.jlink.internal.PluginRepository;32import jdk.tools.jlink.plugin.Plugin;33import jdk.tools.jlink.plugin.PluginException;34import jdk.tools.jlink.plugin.ResourcePool;35import jdk.tools.jlink.plugin.ResourcePoolBuilder;36import tests.Helper;3738/*39* @test40* @summary Test plugins enabled by default41* @author Jean-Francois Denise42* @requires vm.compMode != "Xcomp"43* @library ../lib44* @modules java.base/jdk.internal.jimage45* jdk.jdeps/com.sun.tools.classfile46* jdk.jlink/jdk.tools.jlink.internal47* jdk.jlink/jdk.tools.jlink.plugin48* jdk.jlink/jdk.tools.jmod49* jdk.jlink/jdk.tools.jimage50* jdk.compiler51* @build tests.*52* @run main/othervm DefaultProviderTest53*/54public class DefaultProviderTest {55private static final String NAME = "disable-toto";56private final static Map<String, Object> expectedOptions = new HashMap<>();5758static {59expectedOptions.put("disable-toto", "false");60expectedOptions.put("option1", "value1");61expectedOptions.put("option2", "value2");62}6364private static class Custom implements Plugin {65private boolean enabled = true;6667@Override68public Set<State> getState() {69return enabled ? EnumSet.of(State.AUTO_ENABLED, State.FUNCTIONAL)70: EnumSet.of(State.DISABLED);71}7273@Override74public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out) {75if (!enabled) {76throw new PluginException(NAME + " was set");77}7879DefaultProviderTest.isNewPluginsCalled = true;80in.transformAndCopy(content -> {81return content;82}, out);8384return out.build();85}8687@Override88public String getName() {89return NAME;90}9192@Override93public String getDescription() {94return NAME;95}9697@Override98public boolean hasArguments() {99return true;100}101102@Override103public void configure(Map<String, String> config) {104if (config.containsKey(NAME)) {105enabled = !Boolean.parseBoolean(config.get(NAME));106}107108if (enabled) {109DefaultProviderTest.receivedOptions = config;110} else {111DefaultProviderTest.receivedOptions = null;112}113}114}115116private static boolean isNewPluginsCalled;117private static Map<String, String> receivedOptions;118119private static void reset() {120isNewPluginsCalled = false;121receivedOptions = null;122}123124public static void main(String[] args) throws Exception {125Helper helper = Helper.newHelper();126if (helper == null) {127System.err.println("Test not run");128return;129}130helper.generateDefaultModules();131test(helper, new Custom());132}133134private static void test(Helper helper, Plugin plugin) throws Exception {135PluginRepository.registerPlugin(plugin);136137{138String[] userOptions = {};139Path imageDir = helper.generateDefaultImage(userOptions, "composite2").assertSuccess();140helper.checkImage(imageDir, "composite2", null, null);141if (!isNewPluginsCalled) {142throw new Exception("Should have been called");143}144reset();145}146147{148String[] userOptions = {"--disable-toto=false:option1=value1:option2=value2"};149Path imageDir = helper.generateDefaultImage(userOptions, "composite2").assertSuccess();150helper.checkImage(imageDir, "composite2", null, null);151if (!isNewPluginsCalled) {152throw new Exception("Should have been called");153}154if (!receivedOptions.equals(expectedOptions)) {155throw new Exception("Optional options " + receivedOptions + " are not expected one "156+ expectedOptions);157}158System.err.println("OPTIONS " + receivedOptions);159reset();160}161162{163String[] userOptions = {"--disable-toto=true:option1=value1"};164Path imageDir = helper.generateDefaultImage(userOptions, "composite2").assertSuccess();165helper.checkImage(imageDir, "composite2", null, null);166if (isNewPluginsCalled) {167throw new Exception("Should not have been called");168}169if (receivedOptions != null) {170throw new Exception("Optional options are not expected");171}172reset();173}174}175}176177178