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