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 -Xmx1g 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 testModulePath(helper); 72 testOptions(); 73 } 74 75 private static void testModulePath(Helper helper) throws IOException { 76 Path doesNotExist = helper.createNewImageDir("doesnotexist"); 77 Path jar = helper.getJarDir().resolve("bad.jar"); 78 JImageGenerator.getJLinkTask() 79 .pluginModulePath(doesNotExist) 80 .option("--help") 81 .call().assertSuccess(); 82 Files.createFile(jar); 83 JImageGenerator.getJLinkTask() 84 .pluginModulePath(jar) 85 .option("--help") 86 .call().assertFailure("(\n|\r|.)*Error: Invalid modules in the plugins path: (\n|\r|.)*"); 87 JImageGenerator.getJLinkTask() 88 .pluginModulePath(jar.getParent()) 89 .option("--help") 90 .call().assertFailure("Error: Invalid modules in the plugins path: .*zip file is empty(\n|\r|.)*"); 91 try (JarOutputStream out = new JarOutputStream(new FileOutputStream(jar.toFile()))) { 92 JarEntry entry = new JarEntry("class"); 93 out.putNextEntry(entry); 94 out.write("AAAA".getBytes()); 95 out.closeEntry(); 96 } 97 JImageGenerator.getJLinkTask() 98 .pluginModulePath(jar.getParent()) 99 .output(helper.createNewImageDir("crash")) 100 .addJmods(helper.getStdJmodsDir()) 101 .addJmods(jar.getParent()) 102 .addMods("bad") 103 .call().assertFailure("(\n|\r|.)*Error: module-info.class not found for bad module(\n|\r|.)*"); 104 try (JarOutputStream out = new JarOutputStream(new FileOutputStream(jar.toFile()))) { 105 JarEntry entry = new JarEntry("classes"); 106 out.putNextEntry(entry); 107 out.closeEntry(); 108 109 entry = new JarEntry("classes/class"); 110 out.putNextEntry(entry); 111 out.write("AAAA".getBytes()); 112 out.closeEntry(); 113 } 114 JImageGenerator.getJLinkTask() 115 .pluginModulePath(jar.getParent()) 116 .output(helper.createNewImageDir("bad")) 117 .addJmods(jar.getParent()) 118 .addJars(helper.getStdJmodsDir()) 119 .addMods("bad") 120 .call().assertFailure("(\n|\r|.)*Error: module-info.class not found for bad module(\n|\r|.)*"); 121 } 122 123 private static void testSameNames(Helper helper) throws Exception { 124 // Multiple modules with the same name in modulepath, take the first one in the path. 125 // First jmods then jars. So jmods are found, jars are hidden. 126 String[] jarClasses = {"amodule.jar.Main"}; 127 String[] jmodsClasses = {"amodule.jmods.Main"}; 128 helper.generateDefaultJarModule("amodule", Arrays.asList(jarClasses)); 129 helper.generateDefaultJModule("amodule", Arrays.asList(jmodsClasses)); 130 List<String> okLocations = new ArrayList<>(); 131 okLocations.addAll(Helper.toLocation("amodule", Arrays.asList(jmodsClasses))); 132 Path image = helper.generateDefaultImage(new String[0], "amodule").assertSuccess(); 133 JImageValidator validator = new JImageValidator("amodule", okLocations, 134 image.toFile(), Collections.emptyList(), Collections.emptyList()); 135 validator.validate(); 136 } 137 138 private static void testOptions() throws Exception { 139 List<Plugin> builtInPlugins = new ArrayList<>(); 140 builtInPlugins.addAll(PluginRepository.getPlugins(Layer.boot())); 141 if(builtInPlugins.isEmpty()) { 142 throw new Exception("No builtin plugins"); 143 } 144 List<String> options = new ArrayList<>(); 145 for (Plugin p : builtInPlugins) { 146 if (p.getOption() == null) { 147 throw new Exception("Null option for " + p.getName()); 148 } 149 if (options.contains(p.getName())) { 150 throw new Exception("Option " + p.getOption() + " used more than once"); 151 } 152 options.add(p.getName()); 153 } 154 } 155 }