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.jmod
  33  *          jdk.jlink/jdk.tools.jimage
  34  *          jdk.compiler
  35  * @build tests.*
  36  * @run main/othervm -verbose:gc -Xmx1g JLink2Test
  37  */
  38 import java.io.File;
  39 import java.io.FileOutputStream;
  40 import java.io.IOException;
  41 import java.lang.reflect.Layer;
  42 import java.nio.file.Files;
  43 import java.nio.file.Path;
  44 import java.util.ArrayList;
  45 import java.util.Arrays;
  46 import java.util.Collections;
  47 import java.util.List;
  48 import java.util.jar.JarEntry;
  49 import java.util.jar.JarOutputStream;
  50 import jdk.tools.jlink.internal.PluginRepository;
  51 import jdk.tools.jlink.plugin.Plugin;
  52 
  53 import tests.Helper;
  54 import tests.JImageGenerator;
  55 import tests.JImageValidator;
  56 
  57 public class JLink2Test {
  58 
  59     public static void main(String[] args) throws Exception {
  60         Helper helper = Helper.newHelper();
  61         if (helper == null) {
  62             System.err.println("Test not run");
  63             return;
  64         }
  65         helper.generateDefaultModules();
  66 
  67         // This test case must be first one, the JlinkTask is clean
  68         // and reveals possible bug related to plugin options in defaults
  69         testSameNames(helper);
  70         testModulePath(helper);
  71         testOptions();
  72     }
  73 
  74     private static void testModulePath(Helper helper) throws IOException {
  75         Path doesNotExist = helper.createNewImageDir("doesnotexist");
  76         Path jar = helper.getJarDir().resolve("bad.jar");
  77         JImageGenerator.getJLinkTask()
  78                 .pluginModulePath(doesNotExist)
  79                 .option("--help")
  80                 .call().assertSuccess();
  81         Files.createFile(jar);
  82         JImageGenerator.getJLinkTask()
  83                 .pluginModulePath(jar)
  84                 .option("--help")
  85                 .call().assertFailure("(\n|\r|.)*Error: Invalid modules in the plugins path: (\n|\r|.)*");
  86         JImageGenerator.getJLinkTask()
  87                 .pluginModulePath(jar.getParent())
  88                 .option("--help")
  89                 .call().assertFailure("Error: Invalid modules in the plugins path: .*zip file is empty(\n|\r|.)*");
  90         try (JarOutputStream out = new JarOutputStream(new FileOutputStream(jar.toFile()))) {
  91             JarEntry entry = new JarEntry("class");
  92             out.putNextEntry(entry);
  93             out.write("AAAA".getBytes());
  94             out.closeEntry();
  95         }
  96         JImageGenerator.getJLinkTask()
  97                 .pluginModulePath(jar.getParent())
  98                 .output(helper.createNewImageDir("crash"))
  99                 .addJmods(helper.getStdJmodsDir())
 100                 .addJmods(jar.getParent())
 101                 .addMods("bad")
 102                 .call().assertFailure("(\n|\r|.)*Error: jdk.tools.jlink.plugin.PluginException: module-info.class not found for bad module(\n|\r|.)*");
 103         try (JarOutputStream out = new JarOutputStream(new FileOutputStream(jar.toFile()))) {
 104             JarEntry entry = new JarEntry("classes");
 105             out.putNextEntry(entry);
 106             out.closeEntry();
 107 
 108             entry = new JarEntry("classes/class");
 109             out.putNextEntry(entry);
 110             out.write("AAAA".getBytes());
 111             out.closeEntry();
 112         }
 113         JImageGenerator.getJLinkTask()
 114                 .pluginModulePath(jar.getParent())
 115                 .output(helper.createNewImageDir("bad"))
 116                 .addJmods(jar.getParent())
 117                 .addJars(helper.getStdJmodsDir())
 118                 .addMods("bad")
 119                 .call().assertFailure("(\n|\r|.)*Error: jdk.tools.jlink.plugin.PluginException: module-info.class not found for bad module(\n|\r|.)*");
 120     }
 121 
 122     private static void testSameNames(Helper helper) throws Exception {
 123         // Multiple modules with the same name in modulepath, take the first one in the path.
 124         // First jmods then jars. So jmods are found, jars are hidden.
 125         String[] jarClasses = {"amodule.jar.Main"};
 126         String[] jmodsClasses = {"amodule.jmods.Main"};
 127         helper.generateDefaultJarModule("amodule", Arrays.asList(jarClasses));
 128         helper.generateDefaultJModule("amodule", Arrays.asList(jmodsClasses));
 129         List<String> okLocations = new ArrayList<>();
 130         okLocations.addAll(Helper.toLocation("amodule", Arrays.asList(jmodsClasses)));
 131         Path image = helper.generateDefaultImage(new String[0], "amodule").assertSuccess();
 132         JImageValidator validator = new JImageValidator("amodule", okLocations,
 133                 image.toFile(), Collections.emptyList(), Collections.emptyList());
 134         validator.validate();
 135     }
 136 
 137     private static void testOptions() throws Exception {
 138         List<Plugin> builtInPlugins = new ArrayList<>();
 139         builtInPlugins.addAll(PluginRepository.getPlugins(Layer.boot()));
 140         if(builtInPlugins.isEmpty()) {
 141             throw new Exception("No builtin plugins");
 142         }
 143         List<String> options = new ArrayList<>();
 144         for (Plugin p : builtInPlugins) {
 145             if (p.getOption() == null) {
 146                 throw new Exception("Null option for " + p.getName());
 147             }
 148             if (options.contains(p.getName())) {
 149                 throw new Exception("Option " + p.getOption() + " used more than once");
 150             }
 151             options.add(p.getName());
 152         }
 153     }
 154 }