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.io.IOException;
  25 import java.nio.file.Path;
  26 import java.util.HashMap;
  27 import java.util.Map;
  28 import jdk.tools.jlink.internal.ImagePluginProviderRepository;
  29 import jdk.tools.jlink.plugins.ImageFilePlugin;
  30 import jdk.tools.jlink.plugins.OnOffImageFilePluginProvider;
  31 import jdk.tools.jlink.plugins.OnOffResourcePluginProvider;
  32 import jdk.tools.jlink.plugins.PluginProvider;
  33 import jdk.tools.jlink.plugins.ResourcePlugin;
  34 import tests.Helper;
  35 
  36 /*
  37  * @test
  38  * @summary Test plugins enabled by default
  39  * @author Jean-Francois Denise
  40  * @library ../lib
  41  * @modules java.base/jdk.internal.jimage
  42  *          jdk.jdeps/com.sun.tools.classfile
  43  *          jdk.jlink/jdk.tools.jlink
  44  *          jdk.jlink/jdk.tools.jlink.internal
  45  *          jdk.jlink/jdk.tools.jmod
  46  *          jdk.jlink/jdk.tools.jimage
  47  * @build tests.*
  48  * @run main/othervm DefaultProviderTest
  49  */
  50 
  51 public class DefaultProviderTest {
  52 
  53     private final static Map<String, String> additionalOptions;
  54 
  55     static {
  56         additionalOptions = new HashMap<>();
  57         additionalOptions.put("option1", "value1");
  58         additionalOptions.put("option2", "value2");
  59     }
  60 
  61     private static class CustomProvider extends OnOffResourcePluginProvider {
  62 
  63         public CustomProvider() {
  64             super(NAME, NAME);
  65         }
  66 
  67         @Override
  68         public ResourcePlugin[] createPlugins(Map<String, String> otherOptions) throws IOException {
  69             DefaultProviderTest.isNewPluginsCalled = true;
  70             DefaultProviderTest.otherOptions = otherOptions;
  71             return null;
  72         }
  73 
  74         @Override
  75         public boolean isEnabledByDefault() {
  76             return true;
  77         }
  78 
  79         @Override
  80         public String getCategory() {
  81             return PluginProvider.TRANSFORMER;
  82         }
  83 
  84         @Override
  85         public String getToolOption() {
  86             return NAME;
  87         }
  88 
  89         @Override
  90         public Map<String, String> getAdditionalOptions() {
  91             return additionalOptions;
  92         }
  93     }
  94 
  95     private static class CustomProvider2 extends OnOffImageFilePluginProvider {
  96         public CustomProvider2() {
  97             super(NAME, NAME);
  98         }
  99 
 100         @Override
 101         public ImageFilePlugin[] createPlugins(Map<String, String> otherOptions) throws IOException {
 102             DefaultProviderTest.isNewPluginsCalled = true;
 103             DefaultProviderTest.otherOptions = otherOptions;
 104             return null;
 105         }
 106 
 107         @Override
 108         public boolean isEnabledByDefault() {
 109             return true;
 110         }
 111 
 112         @Override
 113         public String getCategory() {
 114             return PluginProvider.TRANSFORMER;
 115         }
 116 
 117         @Override
 118         public String getToolOption() {
 119             return NAME;
 120         }
 121 
 122         @Override
 123         public Map<String, String> getAdditionalOptions() {
 124             return additionalOptions;
 125         }
 126     }
 127     private static final String NAME = "toto";
 128 
 129     private static boolean isNewPluginsCalled;
 130     private static Map<String, String> otherOptions;
 131 
 132     private static void reset() {
 133         isNewPluginsCalled = false;
 134         otherOptions = null;
 135     }
 136 
 137     public static void main(String[] args) throws Exception {
 138         Helper helper = Helper.newHelper();
 139         if (helper == null) {
 140             System.err.println("Test not run");
 141             return;
 142         }
 143         helper.generateDefaultModules();
 144         test(helper, new CustomProvider());
 145         test(helper, new CustomProvider2());
 146     }
 147 
 148     private static void test(Helper helper, PluginProvider provider) throws Exception {
 149         ImagePluginProviderRepository.registerPluginProvider(provider);
 150 
 151         {
 152             String[] userOptions = {};
 153             Path imageDir = helper.generateDefaultImage(userOptions, "composite2").assertSuccess();
 154             helper.checkImage(imageDir, "composite2", null, null);
 155             if (!isNewPluginsCalled) {
 156                 throw new Exception("Should have been called");
 157             }
 158             reset();
 159         }
 160 
 161         {
 162             String[] userOptions = {"--option1", "value1", "--option2", "value2"};
 163             Path imageDir = helper.generateDefaultImage(userOptions, "composite2").assertSuccess();
 164             helper.checkImage(imageDir, "composite2", null, null);
 165             if (!isNewPluginsCalled) {
 166                 throw new Exception("Should have been called");
 167             }
 168             if (!otherOptions.equals(additionalOptions)) {
 169                 throw new Exception("Optional options are not expected one "
 170                         + otherOptions);
 171             }
 172             System.err.println("OPTIONS " + otherOptions);
 173             reset();
 174         }
 175 
 176         {
 177             String[] userOptions = {"--toto", "off", "--option1", "value1"};
 178             Path imageDir = helper.generateDefaultImage(userOptions, "composite2").assertSuccess();
 179             helper.checkImage(imageDir, "composite2", null, null);
 180             if (isNewPluginsCalled) {
 181                 throw new Exception("Should not have been called");
 182             }
 183             if (otherOptions != null) {
 184                 throw new Exception("Optional options are not expected");
 185             }
 186             reset();
 187         }
 188 
 189     }
 190 }