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