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