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 import java.io.IOException;
  24 import java.util.HashMap;
  25 import java.util.Map;
  26 import jdk.tools.jlink.internal.ImagePluginProviderRepository;
  27 import jdk.tools.jlink.plugins.CmdResourcePluginProvider;
  28 import jdk.tools.jlink.plugins.ResourcePlugin;
  29 import tests.Helper;
  30 
  31 /*
  32  * @test
  33  * @summary Test jlink options
  34  * @author Jean-Francois Denise
  35  * @library ../lib
  36  * @modules java.base/jdk.internal.jimage
  37  *          jdk.jdeps/com.sun.tools.classfile
  38  *          jdk.jlink/jdk.tools.jlink
  39  *          jdk.jlink/jdk.tools.jlink.internal
  40  *          jdk.jlink/jdk.tools.jmod
  41  *          jdk.jlink/jdk.tools.jimage
  42  *          jdk.compiler
  43  * @build tests.*
  44  * @run main JLinkOptionsTest
  45  */
  46 public class JLinkOptionsTest {
  47 
  48     private static class TestProvider extends CmdResourcePluginProvider {
  49 
  50         private final String option;
  51         private final Map<String, String> options;
  52 
  53         private TestProvider(String name, String option,
  54                 Map<String, String> options) {
  55             super(name, "");
  56             this.option = option;
  57             this.options = options;
  58         }
  59 
  60         @Override
  61         public ResourcePlugin[] newPlugins(String[] arguments,
  62                 Map<String, String> otherOptions) throws IOException {
  63             return null;
  64         }
  65 
  66         @Override
  67         public String getCategory() {
  68             return null;
  69         }
  70 
  71         @Override
  72         public String getToolArgument() {
  73             return null;
  74         }
  75 
  76         @Override
  77         public String getToolOption() {
  78             return option;
  79 
  80         }
  81 
  82         @Override
  83         public Map<String, String> getAdditionalOptions() {
  84             return options;
  85         }
  86     }
  87 
  88     public static void main(String[] args) throws Exception {
  89         Helper helper = Helper.newHelper();
  90         if (helper == null) {
  91             System.err.println("Test not run");
  92             return;
  93         }
  94         helper.generateDefaultModules();
  95         {
  96             // multiple plugins with same option
  97             String option = "test1";
  98             ImagePluginProviderRepository.
  99                     registerPluginProvider(new TestProvider("test1", option, null));
 100             ImagePluginProviderRepository.
 101                     registerPluginProvider(new TestProvider("test2", option, null));
 102             helper.generateDefaultImage("composite2").assertFailure("Error: More than one plugin enabled by test1 option");
 103             ImagePluginProviderRepository.unregisterPluginProvider("test1");
 104             ImagePluginProviderRepository.unregisterPluginProvider("test2");
 105         }
 106 
 107         {
 108             // option and optional options collision
 109             Map<String, String> options = new HashMap<>();
 110             options.put("test1", "");
 111             ImagePluginProviderRepository.
 112                     registerPluginProvider(new TestProvider("test1", "test1", null));
 113             ImagePluginProviderRepository.
 114                     registerPluginProvider(new TestProvider("test2", "test2", options));
 115 
 116             helper.generateDefaultImage("composite2").assertFailure("Error: More than one plugin enabled by test1 option");
 117             ImagePluginProviderRepository.unregisterPluginProvider("test1");
 118             ImagePluginProviderRepository.unregisterPluginProvider("test2");
 119         }
 120     }
 121 }