DefaultProviderTest.java revision 14869:f0bdc4ddb086
180709Sjake/*
280709Sjake * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
380709Sjake * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
480709Sjake *
580709Sjake * This code is free software; you can redistribute it and/or modify it
680709Sjake * under the terms of the GNU General Public License version 2 only, as
780709Sjake * published by the Free Software Foundation.
880709Sjake *
980709Sjake * This code is distributed in the hope that it will be useful, but WITHOUT
1080709Sjake * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1180709Sjake * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1280709Sjake * version 2 for more details (a copy is included in the LICENSE file that
1380709Sjake * accompanied this code).
1480709Sjake *
1580709Sjake * You should have received a copy of the GNU General Public License version
1680709Sjake * 2 along with this work; if not, write to the Free Software Foundation,
1780709Sjake * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1880709Sjake *
1980709Sjake * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2080709Sjake * or visit www.oracle.com if you need additional information or have any
2180709Sjake * questions.
2280709Sjake */
2380709Sjake
2480709Sjakeimport java.nio.file.Path;
2580709Sjakeimport java.util.ArrayList;
2680709Sjakeimport java.util.Collections;
2780709Sjakeimport java.util.EnumSet;
2880709Sjakeimport java.util.HashMap;
2980709Sjakeimport java.util.List;
3080709Sjakeimport java.util.Map;
3180709Sjakeimport java.util.Set;
3280709Sjakeimport jdk.tools.jlink.internal.PluginRepository;
3380709Sjakeimport jdk.tools.jlink.plugin.Plugin;
3480709Sjakeimport jdk.tools.jlink.plugin.PluginException;
3580709Sjakeimport jdk.tools.jlink.plugin.ModulePool;
3680709Sjakeimport jdk.tools.jlink.plugin.TransformerPlugin;
3780709Sjakeimport tests.Helper;
3880709Sjake
3980709Sjake/*
4080709Sjake * @test
4180709Sjake * @summary Test plugins enabled by default
4280709Sjake * @author Jean-Francois Denise
4381176Sjake * @library ../lib
4480709Sjake * @modules java.base/jdk.internal.jimage
4580709Sjake *          jdk.jdeps/com.sun.tools.classfile
4680709Sjake *          jdk.jlink/jdk.tools.jlink.internal
4780709Sjake *          jdk.jlink/jdk.tools.jmod
4880709Sjake *          jdk.jlink/jdk.tools.jimage
4980709Sjake *          jdk.compiler
5080709Sjake * @build tests.*
5180709Sjake * @run main/othervm DefaultProviderTest
5280709Sjake */
5380709Sjakepublic class DefaultProviderTest {
5480709Sjake    private static final String NAME = "disable-toto";
5580709Sjake    private final static Map<String, Object> expectedOptions = new HashMap<>();
5680709Sjake
5780709Sjake    static {
5880709Sjake        expectedOptions.put("disable-toto", "false");
5980709Sjake        expectedOptions.put("option1", "value1");
6080709Sjake        expectedOptions.put("option2", "value2");
6180709Sjake    }
6280709Sjake
6380709Sjake    private static class Custom implements TransformerPlugin {
6480709Sjake        private boolean enabled = true;
6580709Sjake
6680709Sjake        @Override
6780709Sjake        public Set<State> getState() {
6880709Sjake             return enabled ? EnumSet.of(State.AUTO_ENABLED, State.FUNCTIONAL)
6980709Sjake                : EnumSet.of(State.DISABLED);
7081176Sjake        }
7181176Sjake
7281176Sjake        @Override
7381176Sjake        public void visit(ModulePool in, ModulePool out) {
7481176Sjake            if (!enabled) {
7581176Sjake                throw new PluginException(NAME + " was set");
7681176Sjake            }
7781176Sjake
7881176Sjake            DefaultProviderTest.isNewPluginsCalled = true;
7981176Sjake            in.transformAndCopy(content -> {
8081176Sjake                return content;
8181176Sjake            }, out);
8281176Sjake        }
8381176Sjake
8481176Sjake        @Override
8580709Sjake        public String getName() {
8680709Sjake            return NAME;
8780709Sjake        }
8880709Sjake
8980709Sjake        @Override
9080709Sjake        public String getDescription() {
9180709Sjake            return NAME;
9280709Sjake        }
9380709Sjake
9480709Sjake        @Override
9580709Sjake        public boolean hasArguments() {
9680709Sjake            return true;
9780709Sjake        }
9880709Sjake
9981176Sjake        @Override
10081176Sjake        public void configure(Map<String, String> config) {
10180709Sjake            if (config.containsKey(NAME)) {
10280709Sjake                enabled = !Boolean.parseBoolean(config.get(NAME));
10380709Sjake            }
10480709Sjake
10580709Sjake            if (enabled) {
10680709Sjake                DefaultProviderTest.receivedOptions = config;
10780709Sjake            } else {
10880709Sjake                DefaultProviderTest.receivedOptions = null;
10980709Sjake            }
11080709Sjake        }
11180709Sjake    }
11280709Sjake
11380709Sjake    private static boolean isNewPluginsCalled;
11480709Sjake    private static Map<String, String> receivedOptions;
11580709Sjake
11680709Sjake    private static void reset() {
11780709Sjake        isNewPluginsCalled = false;
11880709Sjake        receivedOptions = null;
11980709Sjake    }
12080709Sjake
12180709Sjake    public static void main(String[] args) throws Exception {
12280709Sjake        Helper helper = Helper.newHelper();
12380709Sjake        if (helper == null) {
12480709Sjake            System.err.println("Test not run");
12580709Sjake            return;
12680709Sjake        }
12780709Sjake        helper.generateDefaultModules();
12880709Sjake        test(helper, new Custom());
12980709Sjake    }
13080709Sjake
13180709Sjake    private static void test(Helper helper, Plugin plugin) throws Exception {
13280709Sjake        PluginRepository.registerPlugin(plugin);
13380709Sjake
13480709Sjake        {
13580709Sjake            String[] userOptions = {};
13680709Sjake            Path imageDir = helper.generateDefaultImage(userOptions, "composite2").assertSuccess();
13780709Sjake            helper.checkImage(imageDir, "composite2", null, null);
13880709Sjake            if (!isNewPluginsCalled) {
13980709Sjake                throw new Exception("Should have been called");
14080709Sjake            }
14180709Sjake            reset();
14280709Sjake        }
14380709Sjake
14480709Sjake        {
14580709Sjake            String[] userOptions = {"--disable-toto=false:option1=value1:option2=value2"};
14680709Sjake            Path imageDir = helper.generateDefaultImage(userOptions, "composite2").assertSuccess();
14780709Sjake            helper.checkImage(imageDir, "composite2", null, null);
14880709Sjake            if (!isNewPluginsCalled) {
14980709Sjake                throw new Exception("Should have been called");
15080709Sjake            }
15180709Sjake            if (!receivedOptions.equals(expectedOptions)) {
15280709Sjake                throw new Exception("Optional options " + receivedOptions + " are not expected one "
15380709Sjake                        + expectedOptions);
15480709Sjake            }
15580709Sjake            System.err.println("OPTIONS " + receivedOptions);
15680709Sjake            reset();
15780709Sjake        }
15880709Sjake
15980709Sjake        {
16080709Sjake            String[] userOptions = {"--disable-toto=true:option1=value1"};
16180709Sjake            Path imageDir = helper.generateDefaultImage(userOptions, "composite2").assertSuccess();
16280709Sjake            helper.checkImage(imageDir, "composite2", null, null);
16380709Sjake            if (isNewPluginsCalled) {
16480709Sjake                throw new Exception("Should not have been called");
16580709Sjake            }
166            if (receivedOptions != null) {
167                throw new Exception("Optional options are not expected");
168            }
169            reset();
170        }
171    }
172}
173