test/hotspot/jtreg/runtime/appcds/jigsaw/modulepath/AddReads.java
changeset 57567 b000362a89a0
parent 57566 ad84ae073248
child 57568 460ac76019f4
equal deleted inserted replaced
57566:ad84ae073248 57567:b000362a89a0
     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  * @run driver AddReads
       
    33  * @summary sanity test the --add-reads 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.test.lib.Asserts;
       
    43 
       
    44 public class AddReads {
       
    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.norequires";
       
    55     private static final String SUB_MODULE = "org.astro";
       
    56 
       
    57     // the module main class
       
    58     private static final String MAIN_CLASS = "com.norequires.Main";
       
    59     private static final String APP_CLASS = "org.astro.World";
       
    60 
       
    61     private static Path moduleDir = null;
       
    62     private static Path subJar = null;
       
    63     private static Path mainJar = null;
       
    64 
       
    65     public static void buildTestModule() throws Exception {
       
    66 
       
    67         // javac -d mods/$TESTMODULE src/$TESTMODULE/**
       
    68         JarBuilder.compileModule(SRC_DIR.resolve(SUB_MODULE),
       
    69                                        MODS_DIR.resolve(SUB_MODULE),
       
    70                                        null);
       
    71 
       
    72         Asserts.assertTrue(CompilerUtils
       
    73             .compile(SRC_DIR.resolve(MAIN_MODULE),
       
    74                      MODS_DIR.resolve(MAIN_MODULE),
       
    75                      "-cp", MODS_DIR.resolve(SUB_MODULE).toString(),
       
    76                      "--add-reads", "com.norequires=ALL-UNNAMED"));
       
    77 
       
    78         moduleDir = Files.createTempDirectory(USER_DIR, "mlib");
       
    79         subJar = moduleDir.resolve(SUB_MODULE + ".jar");
       
    80         String classes = MODS_DIR.resolve(SUB_MODULE).toString();
       
    81         JarBuilder.createModularJar(subJar.toString(), classes, null);
       
    82 
       
    83         mainJar = moduleDir.resolve(MAIN_MODULE + ".jar");
       
    84         classes = MODS_DIR.resolve(MAIN_MODULE).toString();
       
    85         JarBuilder.createModularJar(mainJar.toString(), classes, MAIN_CLASS);
       
    86     }
       
    87 
       
    88     public static void main(String... args) throws Exception {
       
    89         // compile the modules and create the modular jar files
       
    90         buildTestModule();
       
    91         String appClasses[] = {MAIN_CLASS, APP_CLASS};
       
    92         // create an archive with the classes in the modules built in the
       
    93         // previous step
       
    94         OutputAnalyzer output = TestCommon.createArchive(
       
    95                                         null, appClasses,
       
    96                                         "--module-path", moduleDir.toString(),
       
    97                                         "--add-modules", SUB_MODULE,
       
    98                                         "--add-reads", "com.norequires=org.astro",
       
    99                                         "-m", MAIN_MODULE);
       
   100         TestCommon.checkDump(output);
       
   101         String prefix[] = {"-Djava.class.path=", "-Xlog:class+load=trace",
       
   102                            "--add-modules", SUB_MODULE,
       
   103                            "--add-reads", "com.norequires=org.astro"};
       
   104 
       
   105         // run the com.norequires module with the archive with the same args
       
   106         // used during dump time.
       
   107         // The classes should be loaded from the archive.
       
   108         TestCommon.runWithModules(prefix,
       
   109                                   null, // --upgrade-module-path
       
   110                                   moduleDir.toString(), // --module-path
       
   111                                   MAIN_MODULE) // -m
       
   112             .assertNormalExit(out -> {
       
   113                 out.shouldContain("[class,load] com.norequires.Main source: shared objects file")
       
   114                    .shouldContain("[class,load] org.astro.World source: shared objects file");
       
   115             });
       
   116 
       
   117         // create an archive with -cp pointing to the jar file containing the
       
   118         // org.astro module and --module-path pointing to the main module
       
   119         output = TestCommon.createArchive(
       
   120                                         subJar.toString(), appClasses,
       
   121                                         "--module-path", moduleDir.toString(),
       
   122                                         "--add-modules", SUB_MODULE,
       
   123                                         "--add-reads", "com.norequires=org.astro",
       
   124                                         "-m", MAIN_MODULE);
       
   125         TestCommon.checkDump(output);
       
   126         // run the com.norequires module with the archive with the sub-module
       
   127         // in the -cp and with -add-reads=com.norequires=ALL-UNNAMED
       
   128         // The main class should be loaded from the archive.
       
   129         // The org.astro.World should be loaded from the jar.
       
   130         String prefix2[] = {"-cp", subJar.toString(), "-Xlog:class+load=trace",
       
   131                            "--add-reads", "com.norequires=ALL-UNNAMED"};
       
   132         TestCommon.runWithModules(prefix2,
       
   133                                   null, // --upgrade-module-path
       
   134                                   moduleDir.toString(), // --module-path
       
   135                                   MAIN_MODULE) // -m
       
   136             .assertNormalExit(out -> {
       
   137                 out.shouldContain("[class,load] com.norequires.Main source: shared objects file")
       
   138                    .shouldMatch(".class.load. org.astro.World source:.*org.astro.jar");
       
   139             });
       
   140 
       
   141     }
       
   142 }