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 ModulePathAndCP
  33  * @summary 2 sets of tests: one with only --module-path in the command line;
  34  *          another with both -cp and --module-path in the command line.
  35  */
  36 
  37 import java.io.File;
  38 import java.nio.file.Files;
  39 import java.nio.file.Path;
  40 import java.nio.file.Paths;
  41 
  42 import jdk.test.lib.process.OutputAnalyzer;
  43 import jdk.testlibrary.ProcessTools;
  44 
  45 public class ModulePathAndCP {
  46 
  47     private static final Path USER_DIR = Paths.get(System.getProperty("user.dir"));
  48 
  49     private static final String TEST_SRC = System.getProperty("test.src");
  50 
  51     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
  52     private static final Path MODS_DIR = Paths.get("mods");
  53 
  54     // the module name of the test module
  55     private static final String MAIN_MODULE = "com.greetings";
  56     private static final String APP_MODULE = "org.astro";
  57 
  58     // the module main class
  59     private static final String MAIN_CLASS = "com.greetings.Main";
  60     private static final String APP_CLASS = "org.astro.World";
  61 
  62     private static Path moduleDir = null;
  63     private static Path moduleDir2 = null;
  64     private static Path subJar = null;
  65     private static Path mainJar = null;
  66     private static Path destJar = null;
  67 
  68     public static void buildTestModule() throws Exception {
  69 
  70         // javac -d mods/$TESTMODULE src/$TESTMODULE/**
  71         JarBuilder.compileModule(SRC_DIR.resolve(APP_MODULE),
  72                                  MODS_DIR.resolve(APP_MODULE),
  73                                  null);
  74 
  75         // javac -d mods/$TESTMODULE --module-path MOD_DIR src/$TESTMODULE/**
  76         JarBuilder.compileModule(SRC_DIR.resolve(MAIN_MODULE),
  77                                  MODS_DIR.resolve(MAIN_MODULE),
  78                                  MODS_DIR.toString());
  79 
  80         moduleDir = Files.createTempDirectory(USER_DIR, "mlib");
  81         moduleDir2 = Files.createTempDirectory(USER_DIR, "mlib2");
  82         subJar = moduleDir.resolve(APP_MODULE + ".jar");
  83         destJar = moduleDir2.resolve(APP_MODULE + ".jar");
  84         String classes = MODS_DIR.resolve(APP_MODULE).toString();
  85         JarBuilder.createModularJar(subJar.toString(), classes, null);
  86         Files.copy(subJar, destJar);
  87 
  88         mainJar = moduleDir.resolve(MAIN_MODULE + ".jar");
  89         Path mainJar2 = moduleDir2.resolve(MAIN_MODULE + ".jar");
  90         classes = MODS_DIR.resolve(MAIN_MODULE).toString();
  91         JarBuilder.createModularJar(mainJar.toString(), classes, MAIN_CLASS);
  92         Files.copy(mainJar, mainJar2);
  93 
  94     }
  95 
  96     public static void main(String... args) throws Exception {
  97         // compile the modules and create the modular jar files
  98         buildTestModule();
  99         String appClasses[] = {MAIN_CLASS, APP_CLASS};
 100         // create an archive with the classes in the modules built in the
 101         // previous step
 102         OutputAnalyzer output = TestCommon.createArchive(
 103                                         null, appClasses,
 104                                         "--module-path", moduleDir.toString(),
 105                                         "-m", MAIN_MODULE);
 106         TestCommon.checkDump(output);
 107         String prefix[] = {"-cp", "\"\"", "-Xlog:class+load=trace"};
 108 
 109         // run with the archive with the --module-path the same as the one during
 110         // dump time. The classes should be loaded from the archive.
 111         TestCommon.runWithModules(prefix,
 112                                   null, // --upgrade-module-path
 113                                   moduleDir.toString(), // --module-path
 114                                   MAIN_MODULE) // -m
 115             .assertNormalExit(out -> {
 116                 out.shouldContain("[class,load] com.greetings.Main source: shared objects file")
 117                    .shouldContain("[class,load] org.astro.World source: shared objects file");
 118             });
 119 
 120         // run with the archive with the --module-path different from the one during
 121         // dump time. The classes should be loaded from the jar files.
 122         TestCommon.runWithModules(prefix,
 123                                   null, // --upgrade-module-path
 124                                   moduleDir2.toString(), // --module-path
 125                                   MAIN_MODULE) // -m
 126             .assertNormalExit(out -> {
 127                 out.shouldMatch(".class.load. com.greetings.Main source:.*com.greetings.jar")
 128                    .shouldMatch(".class.load. org.astro.World source:.*org.astro.jar");
 129             });
 130 
 131         // create an archive with modular jar files in both -cp and --module-path
 132         String jars = subJar.toString() + System.getProperty("path.separator") +
 133                       mainJar.toString();
 134         output = TestCommon.createArchive( jars, appClasses,
 135                                            "-Xlog:class+load=trace",
 136                                            "--module-path", moduleDir.toString(),
 137                                            "-m", MAIN_MODULE);
 138         TestCommon.checkDump(output);
 139 
 140         // run with archive with the main class name specified before
 141         // the module name with the -m option. Since the -m option was specified
 142         // during dump time, the classes in the jar files after the -cp won't be
 143         // archived. Therefore, the classes won't be loaded from the archive but
 144         // will be loaded from the jar files.
 145         TestCommon.run("-Xlog:class+load=trace",
 146                        "-cp", jars,
 147                        "--module-path", moduleDir.toString(),
 148                        MAIN_CLASS, "-m", MAIN_MODULE)
 149             .assertNormalExit(out -> {
 150                 out.shouldMatch(".class.load. com.greetings.Main source:.*com.greetings.jar")
 151                    .shouldMatch(".class.load. org.astro.World source:.*org.astro.jar");
 152             });
 153 
 154         // similar to the above case but without the main class name. The classes
 155         // should be loaded from the archive.
 156         TestCommon.run("-Xlog:class+load=trace",
 157                        "-cp", jars,
 158                        "--module-path", moduleDir.toString(),
 159                        "-m", MAIN_MODULE)
 160             .assertNormalExit(
 161               "[class,load] com.greetings.Main source: shared objects file",
 162               "[class,load] org.astro.World source: shared objects file");
 163 
 164         // create an archive with two modular jars in the --module-path
 165         output = TestCommon.createArchive(
 166                                         null, appClasses,
 167                                         "--module-path", jars,
 168                                         "-m", MAIN_MODULE);
 169         TestCommon.checkDump(output);
 170 
 171         // run with the above archive but with the modular jar containing the
 172         // org.astro module in a different location.
 173         // The org.astro.World class should be loaded from the jar.
 174         // The Main class should still be loaded from the archive.
 175         jars = destJar.toString() + System.getProperty("path.separator") +
 176                       mainJar.toString();
 177         TestCommon.runWithModules(prefix,
 178                                   null, // --upgrade-module-path
 179                                   jars, // --module-path
 180                                   MAIN_MODULE) // -m
 181             .assertNormalExit(out -> {
 182                 out.shouldContain("[class,load] com.greetings.Main source: shared objects file")
 183                    .shouldMatch(".class.load. org.astro.World source:.*org.astro.jar");
 184             });
 185     }
 186 }