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 "--module-path", moduleDir.toString(), 94 "-m", TEST_MODULE1); 95 TestCommon.checkDump(output); 96 97 // run with the archive using the same command line as in dump time. 98 // The main class should be loaded from the archive. 99 TestCommon.run("-Xlog:class+load=trace", 100 "-cp", destJar.toString(), 101 "--module-path", moduleDir.toString(), 102 "-m", TEST_MODULE1) 103 .assertNormalExit("[class,load] com.simple.Main source: shared objects file"); 104 105 // run with the archive with the main class name inserted before the -m. 106 // The main class name will be picked up before the module name. So the 107 // main class should be loaded from the jar in the -cp. 108 TestCommon.run("-Xlog:class+load=trace", 109 "-cp", destJar.toString(), 110 "--module-path", moduleDir.toString(), 111 MAIN_CLASS, "-m", TEST_MODULE1) 112 .assertNormalExit(out -> 113 out.shouldMatch(".class.load. com.simple.Main source:.*com.simple.jar")); 114 115 // run with the archive with exploded module. Since during dump time, we 116 // only archive classes from the modular jar in the --module-path, the 117 // main class should be loaded from the exploded module directory. 118 TestCommon.run("-Xlog:class+load=trace", 119 "-cp", destJar.toString(), 120 "--module-path", MODS_DIR.toString(), 121 "-m", TEST_MODULE1 + "/" + MAIN_CLASS) 122 .assertNormalExit(out -> { 123 out.shouldMatch(".class.load. com.simple.Main source:.*com.simple") 124 .shouldContain(MODS_DIR.toString()); 125 }); 126 127 // run with the archive with the --upgrade-module-path option. 128 // CDS will be disabled with this options and the main class will be 129 // loaded from the modular jar. 130 TestCommon.run("-Xlog:class+load=trace", 131 "-cp", destJar.toString(), 132 "--upgrade-module-path", moduleDir.toString(), 133 "--module-path", moduleDir.toString(), 134 "-m", TEST_MODULE1) 135 .assertSilentlyDisabledCDS(out -> { 136 out.shouldHaveExitValue(0) 137 .shouldMatch("CDS is disabled when the.*option is specified") 138 .shouldMatch(".class.load. com.simple.Main source:.*com.simple.jar"); 139 }); 140 // run with the archive with the --limit-modules option. 141 // CDS will be disabled with this options and the main class will be 142 // loaded from the modular jar. 143 TestCommon.run("-Xlog:class+load=trace", 144 "-cp", destJar.toString(), 145 "--limit-modules", "java.base," + TEST_MODULE1, 146 "--module-path", moduleDir.toString(), 147 "-m", TEST_MODULE1) 148 .assertSilentlyDisabledCDS(out -> { 149 out.shouldHaveExitValue(0) 150 .shouldMatch("CDS is disabled when the.*option is specified") 151 .shouldMatch(".class.load. com.simple.Main source:.*com.simple.jar"); 152 }); 153 // run with the archive with the --patch-module option. 154 // CDS will be disabled with this options and the main class will be 155 // loaded from the modular jar. 156 TestCommon.run("-Xlog:class+load=trace", 157 "-cp", destJar.toString(), 158 "--patch-module", TEST_MODULE1 + "=" + MODS_DIR.toString(), 159 "--module-path", moduleDir.toString(), 160 "-m", TEST_MODULE1) 161 .assertSilentlyDisabledCDS(out -> { 162 out.shouldHaveExitValue(0) 163 .shouldMatch("CDS is disabled when the.*option is specified") 164 .shouldMatch(".class.load. com.simple.Main source:.*com.simple.jar"); 165 }); 166 // modify the timestamp of the jar file 167 (new File(destJar.toString())).setLastModified(System.currentTimeMillis() + 2000); 168 // run with the archive and the jar with modified timestamp. 169 // It should fail due to timestamp of the jar doesn't match the one 170 // used during dump time. 171 TestCommon.run("-cp", destJar.toString(), 172 "--module-path", moduleDir.toString(), 173 "-m", TEST_MODULE1) 174 .assertAbnormalExit( 175 "A jar file is not the one used while building the shared archive file:"); 176 // create an archive with a non-empty directory in the --module-path. 177 // The dumping process will exit with an error due to non-empty directory 178 // in the --module-path. 179 output = TestCommon.createArchive(destJar.toString(), appClasses, 180 "--module-path", MODS_DIR.toString(), 181 "-m", TEST_MODULE1); 182 output.shouldHaveExitValue(1) 183 .shouldMatch("Error: non-empty directory.*com.simple"); 184 185 // test module path with very long length 186 // 187 // This test can't be run on the windows platform due to an existing 188 // issue in ClassLoader::get_canonical_path() (JDK-8190737). 189 if (Platform.isWindows()) { 190 System.out.println("Long module path test cannot be tested on the Windows platform."); 191 return; 192 } 193 Path longDir = USER_DIR; 194 int pathLen = longDir.toString().length(); 195 int PATH_LEN = 2034; 196 int MAX_DIR_LEN = 250; 197 while (pathLen < PATH_LEN) { 198 int remaining = PATH_LEN - pathLen; 199 int subPathLen = remaining > MAX_DIR_LEN ? MAX_DIR_LEN : remaining; 200 char[] chars = new char[subPathLen]; 201 Arrays.fill(chars, 'x'); 202 String subPath = new String(chars); 203 longDir = Paths.get(longDir.toString(), subPath); 204 pathLen = longDir.toString().length(); 205 } 206 File longDirFile = new File(longDir.toString()); 207 try { 208 longDirFile.mkdirs(); 209 } catch (Exception e) { 210 throw e; 211 } 212 Path longDirJar = longDir.resolve(TEST_MODULE1 + ".jar"); 213 // IOException results from the Files.copy() call on platform 214 // such as MacOS X. Test can't be proceeded further with the 215 // exception. 216 try { 217 Files.copy(destJar, longDirJar); 218 } catch (java.io.IOException ioe) { 219 System.out.println("Caught IOException from Files.copy(). Cannot continue."); 220 return; 221 } 222 output = TestCommon.createArchive(destJar.toString(), appClasses, 223 "-Xlog:exceptions=trace", 224 "--module-path", longDirJar.toString(), 225 "-m", TEST_MODULE1); 226 if (output.getExitValue() != 0) { 227 output.shouldMatch("os::stat error.*CDS dump aborted"); 228 } 229 } 230 }