test/hotspot/jtreg/runtime/appcds/jigsaw/modulepath/AddModules.java
branchJDK-8200758-branch
changeset 57588 dac8f245de8e
parent 57587 16c4975e9e09
parent 57586 f459f98aa30d
child 57591 6805e0ef7453
equal deleted inserted replaced
57587:16c4975e9e09 57588:dac8f245de8e
     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/appcds
       
    29  * @modules jdk.compiler
       
    30  *          jdk.jartool/sun.tools.jar
       
    31  *          jdk.jlink
       
    32  * @compile ../../test-classes/Hello.java
       
    33  * @run driver AddModules
       
    34  * @summary sanity test the --add-modules option
       
    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 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     private static String appJar;
       
    68 
       
    69     public static void buildTestModule() throws Exception {
       
    70 
       
    71         // javac -d mods/$TESTMODULE src/$TESTMODULE/**
       
    72         JarBuilder.compileModule(SRC_DIR.resolve(SUB_MODULE),
       
    73                                  MODS_DIR.resolve(SUB_MODULE),
       
    74                                  null);
       
    75 
       
    76         // javac -d mods/$TESTMODULE --module-path MOD_DIR src/$TESTMODULE/**
       
    77         JarBuilder.compileModule(SRC_DIR.resolve(MAIN_MODULE1),
       
    78                                  MODS_DIR.resolve(MAIN_MODULE1),
       
    79                                  MODS_DIR.toString());
       
    80 
       
    81         JarBuilder.compileModule(SRC_DIR.resolve(MAIN_MODULE2),
       
    82                                  MODS_DIR.resolve(MAIN_MODULE2),
       
    83                                  MODS_DIR.toString());
       
    84 
       
    85         moduleDir = Files.createTempDirectory(USER_DIR, "mlib");
       
    86         subJar = moduleDir.resolve(SUB_MODULE + ".jar");
       
    87         String classes = MODS_DIR.resolve(SUB_MODULE).toString();
       
    88         JarBuilder.createModularJar(subJar.toString(), classes, null);
       
    89 
       
    90         mainJar1 = moduleDir.resolve(MAIN_MODULE1 + ".jar");
       
    91         classes = MODS_DIR.resolve(MAIN_MODULE1).toString();
       
    92         JarBuilder.createModularJar(mainJar1.toString(), classes, MAIN_CLASS1);
       
    93 
       
    94         mainJar2 = moduleDir.resolve(MAIN_MODULE2 + ".jar");
       
    95         classes = MODS_DIR.resolve(MAIN_MODULE2).toString();
       
    96         JarBuilder.createModularJar(mainJar2.toString(), classes, MAIN_CLASS2);
       
    97 
       
    98     }
       
    99 
       
   100     public static void main(String... args) throws Exception {
       
   101         appJar = JarBuilder.getOrCreateHelloJar();
       
   102         // compile the modules and create the modular jar files
       
   103         buildTestModule();
       
   104         String appClasses[] = {MAIN_CLASS1, MAIN_CLASS2, APP_CLASS};
       
   105         // create an archive with the classes in the modules built in the
       
   106         // previous step
       
   107         OutputAnalyzer output = TestCommon.createArchive(
       
   108                                         appJar, appClasses,
       
   109                                         "--module-path", moduleDir.toString(),
       
   110                                         "--add-modules",
       
   111                                         MAIN_MODULE1 + "," + MAIN_MODULE2);
       
   112         TestCommon.checkDump(output);
       
   113         String prefix[] = {"-Djava.class.path=" + appJar, "-Xlog:class+load=trace"};
       
   114 
       
   115         // run the com.greetings module with the archive with the --module-path
       
   116         // the same as the one during dump time.
       
   117         // The classes should be loaded from the archive.
       
   118         TestCommon.runWithModules(prefix,
       
   119                                   null, // --upgrade-module-path
       
   120                                   moduleDir.toString(), // --module-path
       
   121                                   MAIN_MODULE1) // -m
       
   122             .assertNormalExit(out -> {
       
   123                 out.shouldContain("[class,load] com.greetings.Main source: shared objects file")
       
   124                    .shouldContain("[class,load] org.astro.World source: shared objects file");
       
   125             });
       
   126 
       
   127         // run the com.hello module with the archive with the --module-path
       
   128         // the same as the one during dump time.
       
   129         // The classes should be loaded from the archive.
       
   130         TestCommon.runWithModules(prefix,
       
   131                                   null, // --upgrade-module-path
       
   132                                   moduleDir.toString(), // --module-path
       
   133                                   MAIN_MODULE2) // -m
       
   134             .assertNormalExit(out -> {
       
   135                 out.shouldContain("[class,load] com.hello.Main source: shared objects file")
       
   136                    .shouldContain("[class,load] org.astro.World source: shared objects file");
       
   137             });
       
   138     }
       
   139 }