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