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  /*
  25  * @test
  26  * @summary Test image creation
  27  * @author Jean-Francois Denise
  28  * @library ../lib
  29  * @modules java.base/jdk.internal.jimage
  30  *          jdk.jdeps/com.sun.tools.classfile
  31  *          jdk.jlink/jdk.tools.jlink.internal
  32  *          jdk.jlink/jdk.tools.jlink.plugin
  33  *          jdk.jlink/jdk.tools.jmod
  34  *          jdk.jlink/jdk.tools.jimage
  35  *          jdk.compiler
  36  * @build tests.*
  37  * @run main/othervm -verbose:gc JLink2Test
  38  */
  39 import java.io.File;
  40 import java.io.FileOutputStream;
  41 import java.io.IOException;
  42 import java.lang.reflect.Layer;
  43 import java.nio.file.Files;
  44 import java.nio.file.Path;
  45 import java.util.ArrayList;
  46 import java.util.Arrays;
  47 import java.util.Collections;
  48 import java.util.List;
  49 import java.util.jar.JarEntry;
  50 import java.util.jar.JarOutputStream;
  51 import jdk.tools.jlink.internal.PluginRepository;
  52 import jdk.tools.jlink.plugin.Plugin;
  53 
  54 import tests.Helper;
  55 import tests.JImageGenerator;
  56 import tests.JImageValidator;
  57 
  58 public class JLink2Test {
  59 
  60     public static void main(String[] args) throws Exception {
  61         Helper helper = Helper.newHelper();
  62         if (helper == null) {
  63             System.err.println("Test not run");
  64             return;
  65         }
  66         helper.generateDefaultModules();
  67 
  68         // This test case must be first one, the JlinkTask is clean
  69         // and reveals possible bug related to plugin options in defaults
  70         testSameNames(helper);
  71         testOptions();
  72     }
  73 
  74     private static void testSameNames(Helper helper) throws Exception {
  75         // Multiple modules with the same name in modulepath, take the first one in the path.
  76         // First jmods then jars. So jmods are found, jars are hidden.
  77         String[] jarClasses = {"amodule.jar.Main"};
  78         String[] jmodsClasses = {"amodule.jmods.Main"};
  79         helper.generateDefaultJarModule("amodule", Arrays.asList(jarClasses));
  80         helper.generateDefaultJModule("amodule", Arrays.asList(jmodsClasses));
  81         List<String> okLocations = new ArrayList<>();
  82         okLocations.addAll(Helper.toLocation("amodule", Arrays.asList(jmodsClasses)));
  83         Path image = helper.generateDefaultImage(new String[0], "amodule").assertSuccess();
  84         JImageValidator validator = new JImageValidator("amodule", okLocations,
  85                 image.toFile(), Collections.emptyList(), Collections.emptyList());
  86         validator.validate();
  87     }
  88 
  89     private static void testOptions() throws Exception {
  90         List<Plugin> builtInPlugins = new ArrayList<>();
  91         builtInPlugins.addAll(PluginRepository.getPlugins(Layer.boot()));
  92         if(builtInPlugins.isEmpty()) {
  93             throw new Exception("No builtin plugins");
  94         }
  95         List<String> options = new ArrayList<>();
  96         for (Plugin p : builtInPlugins) {
  97             if (p.getOption() == null) {
  98                 throw new Exception("Null option for " + p.getName());
  99             }
 100             if (options.contains(p.getName())) {
 101                 throw new Exception("Option " + p.getOption() + " used more than once");
 102             }
 103             options.add(p.getName());
 104         }
 105     }
 106 }