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         // e. g.: --genbom
  70         testBomFile(helper);
  71         testSameNames(helper);
  72         testModulePath(helper);
  73         testOptions();
  74     }
  75 
  76     private static void testModulePath(Helper helper) throws IOException {
  77         Path doesNotExist = helper.createNewImageDir("doesnotexist");
  78         Path jar = helper.getJarDir().resolve("bad.jar");
  79         JImageGenerator.getJLinkTask()
  80                 .pluginModulePath(doesNotExist)
  81                 .option("--help")
  82                 .call().assertSuccess();
  83         Files.createFile(jar);
  84         JImageGenerator.getJLinkTask()
  85                 .pluginModulePath(jar)
  86                 .option("--help")
  87                 .call().assertFailure("(\n|\r|.)*Error: Invalid modules in the plugins path: (\n|\r|.)*");
  88         JImageGenerator.getJLinkTask()
  89                 .pluginModulePath(jar.getParent())
  90                 .option("--help")
  91                 .call().assertFailure("Error: Invalid modules in the plugins path: .*zip file is empty(\n|\r|.)*");
  92         try (JarOutputStream out = new JarOutputStream(new FileOutputStream(jar.toFile()))) {
  93             JarEntry entry = new JarEntry("class");
  94             out.putNextEntry(entry);
  95             out.write("AAAA".getBytes());
  96             out.closeEntry();
  97         }
  98         JImageGenerator.getJLinkTask()
  99                 .pluginModulePath(jar.getParent())
 100                 .output(helper.createNewImageDir("crash"))
 101                 .addJmods(helper.getStdJmodsDir())
 102                 .addJmods(jar.getParent())
 103                 .addMods("bad")
 104                 .call().assertFailure("(\n|\r|.)*Error: jdk.tools.jlink.plugin.PluginException: module-info.class not found for bad module(\n|\r|.)*");
 105         try (JarOutputStream out = new JarOutputStream(new FileOutputStream(jar.toFile()))) {
 106             JarEntry entry = new JarEntry("classes");
 107             out.putNextEntry(entry);
 108             out.closeEntry();
 109 
 110             entry = new JarEntry("classes/class");
 111             out.putNextEntry(entry);
 112             out.write("AAAA".getBytes());
 113             out.closeEntry();
 114         }
 115         JImageGenerator.getJLinkTask()
 116                 .pluginModulePath(jar.getParent())
 117                 .output(helper.createNewImageDir("bad"))
 118                 .addJmods(jar.getParent())
 119                 .addJars(helper.getStdJmodsDir())
 120                 .addMods("bad")
 121                 .call().assertFailure("(\n|\r|.)*Error: jdk.tools.jlink.plugin.PluginException: module-info.class not found for bad module(\n|\r|.)*");
 122     }
 123 
 124     private static void testSameNames(Helper helper) throws Exception {
 125         // Multiple modules with the same name in modulepath, take the first one in the path.
 126         // First jmods then jars. So jmods are found, jars are hidden.
 127         String[] jarClasses = {"amodule.jar.Main"};
 128         String[] jmodsClasses = {"amodule.jmods.Main"};
 129         helper.generateDefaultJarModule("amodule", Arrays.asList(jarClasses));
 130         helper.generateDefaultJModule("amodule", Arrays.asList(jmodsClasses));
 131         List<String> okLocations = new ArrayList<>();
 132         okLocations.addAll(Helper.toLocation("amodule", Arrays.asList(jmodsClasses)));
 133         Path image = helper.generateDefaultImage(new String[0], "amodule").assertSuccess();
 134         JImageValidator validator = new JImageValidator("amodule", okLocations,
 135                 image.toFile(), Collections.emptyList(), Collections.emptyList());
 136         validator.validate();
 137     }
 138 
 139     private static void testBomFile(Helper helper) throws Exception {
 140         String[] userOptions = {
 141             "--compress",
 142             "2",
 143             "--addmods",
 144             "bomzip",
 145             "--strip-debug",
 146             "--genbom",
 147             "--exclude-resources",
 148             "*.jcov,*/META-INF/*"};
 149         String moduleName = "bomzip";
 150         helper.generateDefaultJModule(moduleName, "composite2");
 151         Path imgDir = helper.generateDefaultImage(userOptions, moduleName).assertSuccess();
 152         helper.checkImage(imgDir, moduleName, userOptions, null, null);
 153         File bom = new File(imgDir.toFile(), "bom");
 154         if (!bom.exists()) {
 155             throw new RuntimeException(bom.getAbsolutePath() + " not generated");
 156         }
 157         String bomcontent = new String(Files.readAllBytes(bom.toPath()));
 158         if (!bomcontent.contains("--strip-debug")
 159                 || !bomcontent.contains("--compress")
 160                 || !bomcontent.contains("--genbom")
 161                 || !bomcontent.contains("--exclude-resources *.jcov,"
 162                         + "*/META-INF/*")
 163                 || !bomcontent.contains("--addmods bomzip")) {
 164             throw new Exception("Not expected content in " + bom);
 165         }
 166     }
 167 
 168     private static void testOptions() throws Exception {
 169         List<Plugin> builtInPlugins = new ArrayList<>();
 170         builtInPlugins.addAll(PluginRepository.getPlugins(Layer.boot()));
 171         if(builtInPlugins.isEmpty()) {
 172             throw new Exception("No builtin plugins");
 173         }
 174         List<String> options = new ArrayList<>();
 175         for (Plugin p : builtInPlugins) {
 176             if (p.getOption() == null) {
 177                 throw new Exception("Null option for " + p.getName());
 178             }
 179             if (options.contains(p.getName())) {
 180                 throw new Exception("Option " + p.getOption() + " used more than once");
 181             }
 182             options.add(p.getName());
 183         }
 184     }
 185 }