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 AddModules
  33  * @summary sanity test the --add-modules option
  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 
  41 import jdk.test.lib.process.OutputAnalyzer;
  42 
  43 public class AddModules {
  44 
  45     private static final Path USER_DIR = Paths.get(System.getProperty("user.dir"));
  46 
  47     private static final String TEST_SRC = System.getProperty("test.src");
  48 
  49     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
  50     private static final Path MODS_DIR = Paths.get("mods");
  51 
  52     // the module name of the test module
  53     private static final String MAIN_MODULE1 = "com.greetings";
  54     private static final String MAIN_MODULE2 = "com.hello";
  55     private static final String SUB_MODULE = "org.astro";
  56 
  57     // the module main class
  58     private static final String MAIN_CLASS1 = "com.greetings.Main";
  59     private static final String MAIN_CLASS2 = "com.hello.Main";
  60     private static final String APP_CLASS = "org.astro.World";
  61 
  62     private static Path moduleDir = null;
  63     private static Path subJar = null;
  64     private static Path mainJar1 = null;
  65     private static Path mainJar2 = null;
  66 
  67     public static void buildTestModule() throws Exception {
  68 
  69         // javac -d mods/$TESTMODULE src/$TESTMODULE/**
  70         JarBuilder.compileModule(SRC_DIR.resolve(SUB_MODULE),
  71                                  MODS_DIR.resolve(SUB_MODULE),
  72                                  null);
  73 
  74         // javac -d mods/$TESTMODULE --module-path MOD_DIR src/$TESTMODULE/**
  75         JarBuilder.compileModule(SRC_DIR.resolve(MAIN_MODULE1),
  76                                  MODS_DIR.resolve(MAIN_MODULE1),
  77                                  MODS_DIR.toString());
  78 
  79         JarBuilder.compileModule(SRC_DIR.resolve(MAIN_MODULE2),
  80                                  MODS_DIR.resolve(MAIN_MODULE2),
  81                                  MODS_DIR.toString());
  82 
  83         moduleDir = Files.createTempDirectory(USER_DIR, "mlib");
  84         subJar = moduleDir.resolve(SUB_MODULE + ".jar");
  85         String classes = MODS_DIR.resolve(SUB_MODULE).toString();
  86         JarBuilder.createModularJar(subJar.toString(), classes, null);
  87 
  88         mainJar1 = moduleDir.resolve(MAIN_MODULE1 + ".jar");
  89         classes = MODS_DIR.resolve(MAIN_MODULE1).toString();
  90         JarBuilder.createModularJar(mainJar1.toString(), classes, MAIN_CLASS1);
  91 
  92         mainJar2 = moduleDir.resolve(MAIN_MODULE2 + ".jar");
  93         classes = MODS_DIR.resolve(MAIN_MODULE2).toString();
  94         JarBuilder.createModularJar(mainJar2.toString(), classes, MAIN_CLASS2);
  95 
  96     }
  97 
  98     public static void main(String... args) throws Exception {
  99         // compile the modules and create the modular jar files
 100         buildTestModule();
 101         String appClasses[] = {MAIN_CLASS1, MAIN_CLASS2, APP_CLASS};
 102         // create an archive with the classes in the modules built in the
 103         // previous step
 104         OutputAnalyzer output = TestCommon.createArchive(
 105                                         null, appClasses,
 106                                         "--module-path", moduleDir.toString(),
 107                                         "--add-modules",
 108                                         MAIN_MODULE1 + "," + MAIN_MODULE2);
 109         TestCommon.checkDump(output);
 110         String prefix[] = {"-cp", "\"\"", "-Xlog:class+load=trace"};
 111 
 112         // run the com.greetings module with the archive with the --module-path
 113         // the same as the one during dump time.
 114         // The classes should be loaded from the archive.
 115         TestCommon.runWithModules(prefix,
 116                                   null, // --upgrade-module-path
 117                                   moduleDir.toString(), // --module-path
 118                                   MAIN_MODULE1) // -m
 119             .assertNormalExit(out -> {
 120                 out.shouldContain("[class,load] com.greetings.Main source: shared objects file")
 121                    .shouldContain("[class,load] org.astro.World source: shared objects file");
 122             });
 123 
 124         // run the com.hello module with the archive with the --module-path
 125         // the same as the one during dump time.
 126         // The classes should be loaded from the archive.
 127         TestCommon.runWithModules(prefix,
 128                                   null, // --upgrade-module-path
 129                                   moduleDir.toString(), // --module-path
 130                                   MAIN_MODULE2) // -m
 131             .assertNormalExit(out -> {
 132                 out.shouldContain("[class,load] com.hello.Main source: shared objects file")
 133                    .shouldContain("[class,load] org.astro.World source: shared objects file");
 134             });
 135     }
 136 }