test/hotspot/jtreg/runtime/cds/appcds/jigsaw/modulepath/ExportModule.java
author coleenp
Mon, 29 Jul 2019 10:34:20 -0400
changeset 57567 b000362a89a0
parent 51990 test/hotspot/jtreg/runtime/appcds/jigsaw/modulepath/ExportModule.java@6003e034cdd8
child 57705 7cf02b2c1455
permissions -rw-r--r--
8202339: [TESTBUG] Consolidate the tests in runtime/SharedArchiveFile and runtime/appcds Summary: add top level cds directory and adjust test groups Reviewed-by: mseledtsov, ccheung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49739
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
     1
/*
57567
b000362a89a0 8202339: [TESTBUG] Consolidate the tests in runtime/SharedArchiveFile and runtime/appcds
coleenp
parents: 51990
diff changeset
     2
 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
49739
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
     4
 *
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
     7
 * published by the Free Software Foundation.
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
     8
 *
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    13
 * accompanied this code).
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    14
 *
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    18
 *
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    21
 * questions.
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    22
 *
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    23
 */
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    24
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    25
/**
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    26
 * @test
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    27
 * @requires vm.cds
57567
b000362a89a0 8202339: [TESTBUG] Consolidate the tests in runtime/SharedArchiveFile and runtime/appcds
coleenp
parents: 51990
diff changeset
    28
 * @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds
49739
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    29
 * @modules jdk.compiler
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    30
 *          jdk.jartool/sun.tools.jar
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    31
 *          jdk.jlink
51990
6003e034cdd8 8209946: [TESTBUG] CDS tests should use "@run driver"
iklam
parents: 51840
diff changeset
    32
 * @run driver ExportModule
49739
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    33
 * @summary Tests involve exporting a module from the module path to a jar in the -cp.
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    34
 */
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    35
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    36
import java.io.File;
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    37
import java.nio.file.Files;
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    38
import java.nio.file.Path;
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    39
import java.nio.file.Paths;
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    40
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    41
import jdk.test.lib.compiler.CompilerUtils;
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    42
import jdk.test.lib.process.OutputAnalyzer;
51840
dc15e45122b2 8210894: remove jdk/testlibrary/Asserts
iignatyev
parents: 51675
diff changeset
    43
import jdk.test.lib.Asserts;
49739
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    44
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    45
public class ExportModule {
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    46
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    47
    private static final Path USER_DIR = Paths.get(System.getProperty("user.dir"));
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    48
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    49
    private static final String TEST_SRC = System.getProperty("test.src");
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    50
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    51
    private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    52
    private static final Path MODS_DIR = Paths.get("mods");
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    53
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    54
    // the module name of the test module
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    55
    private static final String TEST_MODULE1 = "com.greetings";
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    56
    private static final String TEST_MODULE2 = "org.astro";
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    57
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    58
    // unnamed module package name
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    59
    private static final String PKG_NAME = "com.nomodule";
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    60
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    61
    // the module main class
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    62
    private static final String MAIN_CLASS = "com.greetings.Main";
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    63
    private static final String APP_CLASS = "org.astro.World";
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    64
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    65
    // unnamed module main class
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    66
    private static final String UNNAMED_MAIN = "com.nomodule.Main";
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    67
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    68
    private static Path moduleDir = null;
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    69
    private static Path moduleDir2 = null;
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    70
    private static Path appJar = null;
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    71
    private static Path appJar2 = null;
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    72
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    73
    public static void buildTestModule() throws Exception {
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    74
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    75
        // javac -d mods/$TESTMODULE src/$TESTMODULE/**
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    76
        JarBuilder.compileModule(SRC_DIR.resolve(TEST_MODULE2),
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    77
                                 MODS_DIR.resolve(TEST_MODULE2),
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    78
                                 null);
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    79
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    80
        // javac -d mods/$TESTMODULE --module-path MOD_DIR src/$TESTMODULE/**
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    81
        JarBuilder.compileModule(SRC_DIR.resolve(TEST_MODULE1),
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    82
                                 MODS_DIR.resolve(TEST_MODULE1),
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    83
                                 MODS_DIR.toString());
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    84
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    85
        moduleDir = Files.createTempDirectory(USER_DIR, "mlib");
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    86
        Path jar = moduleDir.resolve(TEST_MODULE2 + ".jar");
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    87
        String classes = MODS_DIR.resolve(TEST_MODULE2).toString();
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    88
        JarBuilder.createModularJar(jar.toString(), classes, null);
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    89
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    90
        moduleDir2 = Files.createTempDirectory(USER_DIR, "mlib2");
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    91
        appJar = moduleDir2.resolve(TEST_MODULE1 + ".jar");
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    92
        classes = MODS_DIR.resolve(TEST_MODULE1).toString();
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    93
        JarBuilder.createModularJar(appJar.toString(), classes, MAIN_CLASS);
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    94
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    95
        // build a non-modular jar containing the main class which
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    96
        // requires the org.astro package
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    97
        boolean compiled
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    98
            = CompilerUtils.compile(SRC_DIR.resolve(PKG_NAME),
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    99
                                    MODS_DIR.resolve(PKG_NAME),
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   100
                                    "--module-path", MODS_DIR.toString(),
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   101
                                    "--add-modules", TEST_MODULE2,
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   102
                                    "--add-exports", "org.astro/org.astro=ALL-UNNAMED");
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   103
        Asserts.assertTrue(compiled, "test package did not compile");
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   104
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   105
        appJar2 = moduleDir2.resolve(PKG_NAME + ".jar");
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   106
        classes = MODS_DIR.resolve(PKG_NAME).toString();
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   107
        JarBuilder.createModularJar(appJar2.toString(), classes, null);
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   108
    }
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   109
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   110
    public static void main(String... args) throws Exception {
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   111
        // compile the modules and create the modular jar files
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   112
        buildTestModule();
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   113
        String appClasses[] = {MAIN_CLASS, APP_CLASS};
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   114
        // create an archive with the class in the org.astro module built in the
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   115
        // previous step and the main class from the modular jar in the -cp
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   116
        // note: the main class is in the modular jar in the -cp which requires
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   117
        // the module in the --module-path
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   118
        OutputAnalyzer output = TestCommon.createArchive(
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   119
                                        appJar.toString(), appClasses,
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   120
                                        "--module-path", moduleDir.toString(),
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   121
                                        "--add-modules", TEST_MODULE2, MAIN_CLASS);
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   122
        TestCommon.checkDump(output);
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   123
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   124
        // run it using the archive
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   125
        // both the main class and the class from the org.astro module should
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   126
        // be loaded from the archive
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   127
        TestCommon.run("-Xlog:class+load=trace",
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   128
                              "-cp", appJar.toString(),
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   129
                              "--module-path", moduleDir.toString(),
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   130
                              "--add-modules", TEST_MODULE2, MAIN_CLASS)
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   131
            .assertNormalExit(
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   132
                "[class,load] org.astro.World source: shared objects file",
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   133
                "[class,load] com.greetings.Main source: shared objects file");
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   134
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   135
        String appClasses2[] = {UNNAMED_MAIN, APP_CLASS};
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   136
        // create an archive with the main class from a non-modular jar in the
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   137
        // -cp and the class from the org.astro module
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   138
        // note: the org.astro package needs to be exported to "ALL-UNNAMED"
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   139
        // module since the jar in the -cp is a non-modular jar and thus it is
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   140
        // unnmaed.
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   141
        output = TestCommon.createArchive(
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   142
                                        appJar2.toString(), appClasses2,
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   143
                                        "--module-path", moduleDir.toString(),
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   144
                                        "--add-modules", TEST_MODULE2,
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   145
                                        "--add-exports", "org.astro/org.astro=ALL-UNNAMED",
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   146
                                        UNNAMED_MAIN);
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   147
        TestCommon.checkDump(output);
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   148
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   149
        // both the main class and the class from the org.astro module should
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   150
        // be loaded from the archive
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   151
        TestCommon.run("-Xlog:class+load=trace",
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   152
                       "-cp", appJar2.toString(),
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   153
                       "--module-path", moduleDir.toString(),
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   154
                       "--add-modules", TEST_MODULE2,
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   155
                       "--add-exports", "org.astro/org.astro=ALL-UNNAMED",
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   156
                       UNNAMED_MAIN)
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   157
            .assertNormalExit(
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   158
                "[class,load] org.astro.World source: shared objects file",
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   159
                "[class,load] com.nomodule.Main source: shared objects file");
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   160
    }
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   161
}