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