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