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