hotspot/test/runtime/getSysPackage/GetSysPkgTest.java
author hseigel
Wed, 10 Aug 2016 15:48:04 -0700
changeset 40244 b3055c216762
parent 38207 2ed792c9481d
child 40631 ed82623d7831
permissions -rw-r--r--
8136930: Simplify use of module-system options by custom launchers Reviewed-by: coleenp, lfoltan, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 *
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 *
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 *
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * questions.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 */
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    23
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    24
/*
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    25
 * @test
38152
80e5da81fb2c 8154258: [TESTBUG] Various serviceability tests fail compilation
dsamersoff
parents: 36508
diff changeset
    26
 * @modules java.base/jdk.internal.misc
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    27
 * @modules java.base/jdk.internal.loader
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    28
 *          java.desktop
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    29
 * @library /testlibrary
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    30
 * @run main/othervm GetSysPkgTest
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    31
 */
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    32
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.io.File;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.lang.reflect.InvocationTargetException;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.lang.reflect.Method;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import jdk.test.lib.*;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    37
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    38
// Test that JVM get_system_package() returns the module location for defined packages.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    39
public class GetSysPkgTest {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    40
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    41
    private static Object invoke(Method m, Object obj, Object... args) throws Throwable {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    42
        try {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    43
            return m.invoke(obj, args);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    44
        } catch (InvocationTargetException e) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    45
            throw e.getCause();
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    46
        }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    47
    }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    48
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    49
    private static Method findMethod(String name) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    50
        for (Method m : jdk.internal.loader.BootLoader.class.getDeclaredMethods()) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    51
            if (m.getName().equals(name)) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    52
                m.setAccessible(true);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    53
                return m;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    54
            }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    55
        }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    56
        throw new RuntimeException("Failed to find method " + name + " in java.lang.reflect.Module");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    57
    }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    58
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    59
    // Throw RuntimeException if getSystemPackageLocation() does not return
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    60
    // the expected location.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    61
    static void getPkg(String name, String expected_loc) throws Throwable {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    62
        String loc = (String)invoke(findMethod("getSystemPackageLocation"), null, name);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    63
        if (loc == null) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    64
            if (expected_loc == null) return;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    65
            System.out.println("Expected location: " + expected_loc +
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    66
                ", for package: " + name + ", got: null");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    67
        } else if (expected_loc == null) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    68
            System.out.println("Expected location: null, for package: " +
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    69
                name + ", got: " + loc);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    70
        } else if (!loc.equals(expected_loc)) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    71
            System.out.println("Expected location: " +
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    72
                expected_loc + ", for package: " + name + ", got: " + loc);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    73
        } else {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    74
            return;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    75
        }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    76
        throw new RuntimeException();
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    77
    }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    78
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    79
    public static void main(String args[]) throws Throwable {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    80
        if (args.length == 0 || !args[0].equals("do_tests")) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    81
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    82
            // Create a package found via -Xbootclasspath/a
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    83
            String source = "package BootLdr_package; " +
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    84
                            "public class BootLdrPkg { " +
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    85
                            "    public int mth() { return 4; } " +
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    86
                            "}";
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    87
            byte[] klassbuf =
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    88
                InMemoryJavaCompiler.compile("BootLdr_package.BootLdrPkg", source);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    89
            ClassFileInstaller.writeClassToDisk("BootLdr_package/BootLdrPkg", klassbuf, "bl_dir");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    90
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    91
            // Create a package found via -cp.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    92
            source = "package GetSysPkg_package; " +
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    93
                     "public class GetSysClass { " +
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    94
                     "    public int mth() { return 4; } " +
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    95
                     "}";
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    96
            klassbuf =
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    97
                InMemoryJavaCompiler.compile("GetSysPkg_package.GetSysClass", source);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    98
            ClassFileInstaller.writeClassToDisk("GetSysPkg_package/GetSysClass", klassbuf);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    99
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   100
            ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-Xbootclasspath/a:bl_dir",
40244
b3055c216762 8136930: Simplify use of module-system options by custom launchers
hseigel
parents: 38207
diff changeset
   101
                "--add-exports=java.base/jdk.internal.loader=ALL-UNNAMED", "-cp", "." + File.pathSeparator +
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   102
                System.getProperty("test.classes"), "GetSysPkgTest", "do_tests");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   103
            OutputAnalyzer output = new OutputAnalyzer(pb.start());
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   104
            output.shouldHaveExitValue(0);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   105
            return;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   106
        }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   107
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   108
        getPkg("java/lang", "jrt:/java.base");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   109
        getPkg("javax/script", null);          // Package not defined
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   110
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   111
        // Test a package that does not yet have any referenced classes.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   112
        // Note: if another class in com/sun/crypto/provider/ happens to get
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   113
        //       loaded or if class PrivateKeyInfo disappears from this package
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   114
        //       then this test will fail.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   115
        getPkg("com/sun/crypto/provider", null);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   116
        // Now make sure a class in the package is referenced.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   117
        Class newClass = Class.forName("com.sun.crypto.provider.PrivateKeyInfo");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   118
        getPkg("com/sun/crypto/provider", "jrt:/java.base");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   119
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   120
        getPkg("java/nio/charset", "jrt:/java.base");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   121
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   122
        // Test a package in a module not owned by boot loader.
37773
e5b3e9732c3c 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36508
diff changeset
   123
        Class clss = Class.forName("jdk.nio.zipfs.ZipPath");
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   124
        if (clss == null)
37773
e5b3e9732c3c 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36508
diff changeset
   125
            throw new RuntimeException("Could not find class jdk.nio.zipfs.ZipPath");
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   126
        getPkg("javax/activation", null);       // Not owned by boot loader
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   127
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   128
        // Test a package not in jimage file.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   129
        clss = Class.forName("GetSysPkg_package.GetSysClass");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   130
        if (clss == null)
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   131
            throw new RuntimeException("Could not find class GetSysPkg_package.GetSysClass");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   132
        getPkg("GetSysPkg_package", null);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   133
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   134
        // Access a class with a package in a boot loader module other than java.base
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   135
        clss = Class.forName("java.awt.Button");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   136
        if (clss == null)
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   137
            throw new RuntimeException("Could not find class java.awt.Button");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   138
        getPkg("java/awt", "jrt:/java.desktop");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   139
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   140
        // Test getting the package location from a class found via -Xbootclasspath/a
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   141
        clss = Class.forName("BootLdr_package.BootLdrPkg");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   142
        if (clss == null)
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   143
            throw new RuntimeException("Could not find class BootLdr_package.BootLdrPkg");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   144
        String bootldrPkg = (String)invoke(findMethod("getSystemPackageLocation"), null, "BootLdr_package");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   145
        if (bootldrPkg == null) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   146
            throw new RuntimeException("Expected BootLdr_package to return non-null value");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   147
        }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   148
        if (!bootldrPkg.equals("bl_dir")) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   149
            throw new RuntimeException("Expected BootLdr_package to return bl_dir, got: " + bootldrPkg);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   150
        }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   151
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   152
        // Test when package's class reference is an array.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   153
        // Note: if another class in javax/crypto happens to get loaded
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   154
        //       or if class AEADBadTagException disappears from this package
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   155
        //       then this test will fail.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   156
        getPkg("javax/crypto", null);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   157
        javax.crypto.AEADBadTagException[] blah = new javax.crypto.AEADBadTagException[3];
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   158
        getPkg("javax/crypto", "jrt:/java.base");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   159
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   160
    }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   161
}