test/hotspot/jtreg/runtime/cds/appcds/jigsaw/modulepath/AddOpens.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 57705 7cf02b2c1455
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
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
57705
7cf02b2c1455 8229267: [TESTBUG] Remove unnecessary @modules dependencies in CDS tests
iklam
parents: 57567
diff changeset
    29
 * @modules jdk.jartool/sun.tools.jar
51990
6003e034cdd8 8209946: [TESTBUG] CDS tests should use "@run driver"
iklam
parents: 51675
diff changeset
    30
 * @run driver AddOpens
49739
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    31
 * @summary sanity test the --add-opens option
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    32
 */
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    33
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    34
import java.io.File;
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    35
import java.nio.file.Files;
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    36
import java.nio.file.Path;
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    37
import java.nio.file.Paths;
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    38
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    39
import jdk.test.lib.process.OutputAnalyzer;
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
public class AddOpens {
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    42
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    43
    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
    44
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    45
    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
    46
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    47
    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
    48
    private static final Path MODS_DIR = Paths.get("mods");
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    49
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    50
    // the module name of the test module
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    51
    private static final String TEST_MODULE1 = "com.simple";
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    52
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    53
    // the module main class
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    54
    private static final String MAIN_CLASS = "com.simple.Main";
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    55
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    56
    private static Path moduleDir = null;
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    57
    private static Path moduleDir2 = null;
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    58
    private static Path destJar = null;
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    59
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    60
    public static void buildTestModule() throws Exception {
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    61
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    62
        // 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
    63
        JarBuilder.compileModule(SRC_DIR.resolve(TEST_MODULE1),
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    64
                                 MODS_DIR.resolve(TEST_MODULE1),
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    65
                                 MODS_DIR.toString());
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    66
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    67
        moduleDir = Files.createTempDirectory(USER_DIR, "mlib");
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    68
        moduleDir2 = Files.createTempDirectory(USER_DIR, "mlib2");
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    69
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    70
        Path srcJar = moduleDir.resolve(TEST_MODULE1 + ".jar");
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    71
        destJar = moduleDir2.resolve(TEST_MODULE1 + ".jar");
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    72
        String classes = MODS_DIR.resolve(TEST_MODULE1).toString();
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    73
        JarBuilder.createModularJar(srcJar.toString(), classes, MAIN_CLASS);
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    74
        Files.copy(srcJar, destJar);
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    75
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    76
    }
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    77
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    78
    public static void main(String... args) throws Exception {
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    79
        // compile the modules and create the modular jar files
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    80
        buildTestModule();
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    81
        String appClasses[] = {MAIN_CLASS};
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    82
        // create an archive with both -cp and --module-path in the command line.
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    83
        // Only the class in the modular jar in the --module-path will be archived;
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    84
        // the class in the modular jar in the -cp won't be archived.
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    85
        OutputAnalyzer output = TestCommon.createArchive(
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    86
                                        destJar.toString(), appClasses,
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    87
                                        "--module-path", moduleDir.toString(),
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    88
                                        "-m", TEST_MODULE1);
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    89
        TestCommon.checkDump(output);
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    90
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    91
        // run with the archive using the same command line as in dump time
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    92
        // plus the "--add-opens java.base/java.lang=com.simple" option.
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    93
        // The main class should be loaded from the archive.
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    94
        // The setaccessible(true) on the ClassLoader.defineClass method should
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    95
        // be successful.
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    96
        TestCommon.run( "-Xlog:class+load=trace",
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    97
                        "-cp", destJar.toString(),
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    98
                        "--add-opens", "java.base/java.lang=" + TEST_MODULE1,
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
    99
                        "--module-path", moduleDir.toString(),
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   100
                        "-m", TEST_MODULE1, "with_add_opens")
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   101
            .assertNormalExit(
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   102
                "[class,load] com.simple.Main source: shared objects file",
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   103
                "method.setAccessible succeeded!");
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
    }
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents:
diff changeset
   106
}