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