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