DefaultProviderTest.java revision 14919:cc2b9f4a15cb
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
24import java.nio.file.Path;
25import java.util.ArrayList;
26import java.util.Collections;
27import java.util.EnumSet;
28import java.util.HashMap;
29import java.util.List;
30import java.util.Map;
31import java.util.Set;
32import jdk.tools.jlink.internal.PluginRepository;
33import jdk.tools.jlink.plugin.Plugin;
34import jdk.tools.jlink.plugin.PluginException;
35import jdk.tools.jlink.plugin.ModulePool;
36import tests.Helper;
37
38/*
39 * @test
40 * @summary Test plugins enabled by default
41 * @author Jean-Francois Denise
42 * @library ../lib
43 * @modules java.base/jdk.internal.jimage
44 *          jdk.jdeps/com.sun.tools.classfile
45 *          jdk.jlink/jdk.tools.jlink.internal
46 *          jdk.jlink/jdk.tools.jmod
47 *          jdk.jlink/jdk.tools.jimage
48 *          jdk.compiler
49 * @build tests.*
50 * @run main/othervm DefaultProviderTest
51 */
52public class DefaultProviderTest {
53    private static final String NAME = "disable-toto";
54    private final static Map<String, Object> expectedOptions = new HashMap<>();
55
56    static {
57        expectedOptions.put("disable-toto", "false");
58        expectedOptions.put("option1", "value1");
59        expectedOptions.put("option2", "value2");
60    }
61
62    private static class Custom implements Plugin {
63        private boolean enabled = true;
64
65        @Override
66        public Set<State> getState() {
67             return enabled ? EnumSet.of(State.AUTO_ENABLED, State.FUNCTIONAL)
68                : EnumSet.of(State.DISABLED);
69        }
70
71        @Override
72        public void visit(ModulePool in, ModulePool out) {
73            if (!enabled) {
74                throw new PluginException(NAME + " was set");
75            }
76
77            DefaultProviderTest.isNewPluginsCalled = true;
78            in.transformAndCopy(content -> {
79                return content;
80            }, out);
81        }
82
83        @Override
84        public String getName() {
85            return NAME;
86        }
87
88        @Override
89        public String getDescription() {
90            return NAME;
91        }
92
93        @Override
94        public boolean hasArguments() {
95            return true;
96        }
97
98        @Override
99        public void configure(Map<String, String> config) {
100            if (config.containsKey(NAME)) {
101                enabled = !Boolean.parseBoolean(config.get(NAME));
102            }
103
104            if (enabled) {
105                DefaultProviderTest.receivedOptions = config;
106            } else {
107                DefaultProviderTest.receivedOptions = null;
108            }
109        }
110    }
111
112    private static boolean isNewPluginsCalled;
113    private static Map<String, String> receivedOptions;
114
115    private static void reset() {
116        isNewPluginsCalled = false;
117        receivedOptions = null;
118    }
119
120    public static void main(String[] args) throws Exception {
121        Helper helper = Helper.newHelper();
122        if (helper == null) {
123            System.err.println("Test not run");
124            return;
125        }
126        helper.generateDefaultModules();
127        test(helper, new Custom());
128    }
129
130    private static void test(Helper helper, Plugin plugin) throws Exception {
131        PluginRepository.registerPlugin(plugin);
132
133        {
134            String[] userOptions = {};
135            Path imageDir = helper.generateDefaultImage(userOptions, "composite2").assertSuccess();
136            helper.checkImage(imageDir, "composite2", null, null);
137            if (!isNewPluginsCalled) {
138                throw new Exception("Should have been called");
139            }
140            reset();
141        }
142
143        {
144            String[] userOptions = {"--disable-toto=false:option1=value1:option2=value2"};
145            Path imageDir = helper.generateDefaultImage(userOptions, "composite2").assertSuccess();
146            helper.checkImage(imageDir, "composite2", null, null);
147            if (!isNewPluginsCalled) {
148                throw new Exception("Should have been called");
149            }
150            if (!receivedOptions.equals(expectedOptions)) {
151                throw new Exception("Optional options " + receivedOptions + " are not expected one "
152                        + expectedOptions);
153            }
154            System.err.println("OPTIONS " + receivedOptions);
155            reset();
156        }
157
158        {
159            String[] userOptions = {"--disable-toto=true:option1=value1"};
160            Path imageDir = helper.generateDefaultImage(userOptions, "composite2").assertSuccess();
161            helper.checkImage(imageDir, "composite2", null, null);
162            if (isNewPluginsCalled) {
163                throw new Exception("Should not have been called");
164            }
165            if (receivedOptions != null) {
166                throw new Exception("Optional options are not expected");
167            }
168            reset();
169        }
170    }
171}
172