1 /* 2 * Copyright (c) 2018, 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 /** 26 * @test 27 * @requires vm.cds 28 * @library /test/jdk/lib/testlibrary /test/lib /test/hotspot/jtreg/runtime/appcds 29 * @modules jdk.compiler 30 * jdk.jartool/sun.tools.jar 31 * jdk.jlink 32 * @run main MainModuleOnly 33 * @summary Test some scenarios with a main modular jar specified in the --module-path and -cp options in the command line. 34 */ 35 36 import java.io.File; 37 import java.nio.file.Files; 38 import java.nio.file.Path; 39 import java.nio.file.Paths; 40 import java.util.Arrays; 41 42 import jdk.test.lib.process.OutputAnalyzer; 43 import jdk.test.lib.Platform; 44 import jdk.testlibrary.ProcessTools; 45 46 public class MainModuleOnly { 47 48 private static final Path USER_DIR = Paths.get(System.getProperty("user.dir")); 49 50 private static final String TEST_SRC = System.getProperty("test.src"); 51 52 private static final Path SRC_DIR = Paths.get(TEST_SRC, "src"); 53 private static final Path MODS_DIR = Paths.get("mods"); 54 55 // the module name of the test module 56 private static final String TEST_MODULE1 = "com.simple"; 57 58 // the module main class 59 private static final String MAIN_CLASS = "com.simple.Main"; 60 61 private static Path moduleDir = null; 62 private static Path moduleDir2 = null; 63 private static Path destJar = null; 64 65 public static void buildTestModule() throws Exception { 66 67 // javac -d mods/$TESTMODULE --module-path MOD_DIR src/$TESTMODULE/** 68 JarBuilder.compileModule(SRC_DIR.resolve(TEST_MODULE1), 69 MODS_DIR.resolve(TEST_MODULE1), 70 MODS_DIR.toString()); 71 72 73 moduleDir = Files.createTempDirectory(USER_DIR, "mlib"); 74 moduleDir2 = Files.createTempDirectory(USER_DIR, "mlib2"); 75 76 Path srcJar = moduleDir.resolve(TEST_MODULE1 + ".jar"); 77 destJar = moduleDir2.resolve(TEST_MODULE1 + ".jar"); 78 String classes = MODS_DIR.resolve(TEST_MODULE1).toString(); 79 JarBuilder.createModularJar(srcJar.toString(), classes, MAIN_CLASS); 80 Files.copy(srcJar, destJar); 81 82 } 83 84 public static void main(String... args) throws Exception { 85 // compile the modules and create the modular jar files 86 buildTestModule(); 87 String appClasses[] = {MAIN_CLASS}; 88 // create an archive with both -cp and --module-path in the command line. 89 // Only the class in the modular jar in the --module-path will be archived; 90 // the class in the modular jar in the -cp won't be archived. 91 OutputAnalyzer output = TestCommon.createArchive( 92 destJar.toString(), appClasses, 93 "-Xlog:class+load=trace", 94 "--module-path", moduleDir.toString(), 95 "-m", TEST_MODULE1); 96 TestCommon.checkDump(output); 97 98 // run with the archive using the same command line as in dump time. 99 // The main class should be loaded from the archive. 100 TestCommon.run("-Xlog:class+load=trace", 101 "-cp", destJar.toString(), 102 "--module-path", moduleDir.toString(), 103 "-m", TEST_MODULE1) 104 .assertNormalExit("[class,load] com.simple.Main source: shared objects file"); 105 106 // run with the archive with the main class name inserted before the -m. 107 // The main class name will be picked up before the module name. So the 108 // main class should be loaded from the jar in the -cp. 109 TestCommon.run("-Xlog:class+load=trace", 110 "-cp", destJar.toString(), 111 "--module-path", moduleDir.toString(), 112 MAIN_CLASS, "-m", TEST_MODULE1) 113 .assertNormalExit(out -> 114 out.shouldMatch(".class.load. com.simple.Main source:.*com.simple.jar")); 115 116 // run with the archive with exploded module. Since during dump time, we 117 // only archive classes from the modular jar in the --module-path, the 118 // main class should be loaded from the exploded module directory. 119 TestCommon.run("-Xlog:class+load=trace", 120 "-cp", destJar.toString(), 121 "--module-path", MODS_DIR.toString(), 122 "-m", TEST_MODULE1 + "/" + MAIN_CLASS) 123 .assertNormalExit(out -> { 124 out.shouldMatch(".class.load. com.simple.Main source:.*com.simple") 125 .shouldContain(MODS_DIR.toString()); 126 }); 127 128 // run with the archive with the --upgrade-module-path option. 129 // CDS will be disabled with this options and the main class will be 130 // loaded from the modular jar. 131 TestCommon.run("-Xlog:class+load=trace", 132 "-cp", destJar.toString(), 133 "--upgrade-module-path", moduleDir.toString(), 134 "--module-path", moduleDir.toString(), 135 "-m", TEST_MODULE1) 136 .assertSilentlyDisabledCDS(out -> { 137 out.shouldHaveExitValue(0) 138 .shouldMatch("CDS is disabled when the.*option is specified") 139 .shouldMatch(".class.load. com.simple.Main source:.*com.simple.jar"); 140 }); 141 // run with the archive with the --limit-modules option. 142 // CDS will be disabled with this options and the main class will be 143 // loaded from the modular jar. 144 TestCommon.run("-Xlog:class+load=trace", 145 "-cp", destJar.toString(), 146 "--limit-modules", "java.base," + TEST_MODULE1, 147 "--module-path", moduleDir.toString(), 148 "-m", TEST_MODULE1) 149 .assertSilentlyDisabledCDS(out -> { 150 out.shouldHaveExitValue(0) 151 .shouldMatch("CDS is disabled when the.*option is specified") 152 .shouldMatch(".class.load. com.simple.Main source:.*com.simple.jar"); 153 }); 154 // run with the archive with the --patch-module option. 155 // CDS will be disabled with this options and the main class will be 156 // loaded from the modular jar. 157 TestCommon.run("-Xlog:class+load=trace", 158 "-cp", destJar.toString(), 159 "--patch-module", TEST_MODULE1 + "=" + MODS_DIR.toString(), 160 "--module-path", moduleDir.toString(), 161 "-m", TEST_MODULE1) 162 .assertSilentlyDisabledCDS(out -> { 163 out.shouldHaveExitValue(0) 164 .shouldMatch("CDS is disabled when the.*option is specified") 165 .shouldMatch(".class.load. com.simple.Main source:.*com.simple.jar"); 166 }); 167 // modify the timestamp of the jar file 168 (new File(destJar.toString())).setLastModified(System.currentTimeMillis() + 2000); 169 // run with the archive and the jar with modified timestamp. 170 // It should fail due to timestamp of the jar doesn't match the one 171 // used during dump time. 172 TestCommon.run("-Xlog:class+load=trace", 173 "-cp", destJar.toString(), 174 "--module-path", moduleDir.toString(), 175 "-m", TEST_MODULE1) 176 .assertAbnormalExit( 177 "A jar file is not the one used while building the shared archive file:"); 178 // create an archive with a non-empty directory in the --module-path. 179 // The dumping process will exit with an error due to non-empty directory 180 // in the --module-path. 181 output = TestCommon.createArchive(destJar.toString(), appClasses, 182 "--module-path", MODS_DIR.toString(), 183 "-m", TEST_MODULE1); 184 output.shouldHaveExitValue(1) 185 .shouldMatch("Error: non-empty directory.*com.simple"); 186 187 // test module path with very long length 188 // 189 // This test can't be run on the windows platform due to an existing 190 // issue in ClassLoader::get_canonical_path() (JDK-8190737). 191 if (Platform.isWindows()) { 192 System.out.println("Long module path test cannot be tested on the Windows platform."); 193 return; 194 } 195 Path longDir = USER_DIR; 196 int pathLen = longDir.toString().length(); 197 int PATH_LEN = 2034; 198 int MAX_DIR_LEN = 250; 199 while (pathLen < PATH_LEN) { 200 int remaining = PATH_LEN - pathLen; 201 int subPathLen = remaining > MAX_DIR_LEN ? MAX_DIR_LEN : remaining; 202 char[] chars = new char[subPathLen]; 203 Arrays.fill(chars, 'x'); 204 String subPath = new String(chars); 205 longDir = Paths.get(longDir.toString(), subPath); 206 pathLen = longDir.toString().length(); 207 } 208 File longDirFile = new File(longDir.toString()); 209 try { 210 longDirFile.mkdirs(); 211 } catch (Exception e) { 212 throw e; 213 } 214 Path longDirJar = longDir.resolve(TEST_MODULE1 + ".jar"); 215 // IOException results from the Files.copy() call on platform 216 // such as MacOS X. Test can't be proceeded further with the 217 // exception. 218 try { 219 Files.copy(destJar, longDirJar); 220 } catch (java.io.IOException ioe) { 221 System.out.println("Caught IOException from Files.copy(). Cannot continue."); 222 return; 223 } 224 output = TestCommon.createArchive(destJar.toString(), appClasses, 225 "-Xlog:exceptions=trace", 226 "--module-path", longDirJar.toString(), 227 "-m", TEST_MODULE1); 228 output.shouldHaveExitValue(1) 229 .shouldMatch("os::stat error.*ENAMETOOLONG.*CDS dump aborted"); 230 } 231 }