hotspot/test/runtime/getSysPackage/GetSysPkgTest.java
author rriggs
Wed, 12 Apr 2017 11:43:49 -0400
changeset 44633 d2f5e8921045
parent 44520 0553e129e0ec
child 45112 fbec2bfa3839
permissions -rw-r--r--
8178347: Process and ProcessHandle getPid method name inconsistency Reviewed-by: hseigel
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
44520
0553e129e0ec 8177530: Module system implementation refresh (4/2017)
alanb
parents: 42307
diff changeset
     2
 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
36508
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
40631
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 40244
diff changeset
    29
 * @library /test/lib
36508
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;
40631
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 40244
diff changeset
    36
import jdk.test.lib.InMemoryJavaCompiler;
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 40244
diff changeset
    37
import jdk.test.lib.process.ProcessTools;
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 40244
diff changeset
    38
import jdk.test.lib.process.OutputAnalyzer;
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    39
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    40
// Test that JVM get_system_package() returns the module location for defined packages.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    41
public class GetSysPkgTest {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    42
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    43
    private static Object invoke(Method m, Object obj, Object... args) throws Throwable {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    44
        try {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    45
            return m.invoke(obj, args);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    46
        } catch (InvocationTargetException e) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    47
            throw e.getCause();
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    48
        }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    49
    }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    50
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    51
    private static Method findMethod(String name) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    52
        for (Method m : jdk.internal.loader.BootLoader.class.getDeclaredMethods()) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    53
            if (m.getName().equals(name)) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    54
                m.setAccessible(true);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    55
                return m;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    56
            }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    57
        }
44520
0553e129e0ec 8177530: Module system implementation refresh (4/2017)
alanb
parents: 42307
diff changeset
    58
        throw new RuntimeException("Failed to find method " + name + " in java.lang.Module");
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    59
    }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    60
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    61
    // Throw RuntimeException if getSystemPackageLocation() does not return
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    62
    // the expected location.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    63
    static void getPkg(String name, String expected_loc) throws Throwable {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    64
        String loc = (String)invoke(findMethod("getSystemPackageLocation"), null, name);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    65
        if (loc == null) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    66
            if (expected_loc == null) return;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    67
            System.out.println("Expected location: " + expected_loc +
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    68
                ", for package: " + name + ", got: null");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    69
        } else if (expected_loc == null) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    70
            System.out.println("Expected location: null, for package: " +
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    71
                name + ", got: " + loc);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    72
        } else if (!loc.equals(expected_loc)) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    73
            System.out.println("Expected location: " +
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    74
                expected_loc + ", for package: " + name + ", got: " + loc);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    75
        } else {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    76
            return;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    77
        }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    78
        throw new RuntimeException();
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    79
    }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    80
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    81
    public static void main(String args[]) throws Throwable {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    82
        if (args.length == 0 || !args[0].equals("do_tests")) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    83
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    84
            // Create a package found via -Xbootclasspath/a
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    85
            String source = "package BootLdr_package; " +
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    86
                            "public class BootLdrPkg { " +
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    87
                            "    public int mth() { return 4; } " +
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    88
                            "}";
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    89
            byte[] klassbuf =
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    90
                InMemoryJavaCompiler.compile("BootLdr_package.BootLdrPkg", source);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    91
            ClassFileInstaller.writeClassToDisk("BootLdr_package/BootLdrPkg", klassbuf, "bl_dir");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    92
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    93
            // Create a package found via -cp.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    94
            source = "package GetSysPkg_package; " +
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    95
                     "public class GetSysClass { " +
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    96
                     "    public int mth() { return 4; } " +
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    97
                     "}";
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    98
            klassbuf =
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    99
                InMemoryJavaCompiler.compile("GetSysPkg_package.GetSysClass", source);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   100
            ClassFileInstaller.writeClassToDisk("GetSysPkg_package/GetSysClass", klassbuf);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   101
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   102
            ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-Xbootclasspath/a:bl_dir",
42307
cefc81dc1d52 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40631
diff changeset
   103
                "--add-opens=java.base/jdk.internal.loader=ALL-UNNAMED", "-cp", "." + File.pathSeparator +
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   104
                System.getProperty("test.classes"), "GetSysPkgTest", "do_tests");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   105
            OutputAnalyzer output = new OutputAnalyzer(pb.start());
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   106
            output.shouldHaveExitValue(0);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   107
            return;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   108
        }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   109
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   110
        getPkg("java/lang", "jrt:/java.base");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   111
        getPkg("javax/script", null);          // Package not defined
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   112
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   113
        // Test a package that does not yet have any referenced classes.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   114
        // Note: if another class in com/sun/crypto/provider/ happens to get
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   115
        //       loaded or if class PrivateKeyInfo disappears from this package
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   116
        //       then this test will fail.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   117
        getPkg("com/sun/crypto/provider", null);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   118
        // Now make sure a class in the package is referenced.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   119
        Class newClass = Class.forName("com.sun.crypto.provider.PrivateKeyInfo");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   120
        getPkg("com/sun/crypto/provider", "jrt:/java.base");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   121
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   122
        getPkg("java/nio/charset", "jrt:/java.base");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   123
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   124
        // 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
   125
        Class clss = Class.forName("jdk.nio.zipfs.ZipPath");
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   126
        if (clss == null)
37773
e5b3e9732c3c 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36508
diff changeset
   127
            throw new RuntimeException("Could not find class jdk.nio.zipfs.ZipPath");
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   128
        getPkg("javax/activation", null);       // Not owned by boot loader
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   129
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   130
        // Test a package not in jimage file.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   131
        clss = Class.forName("GetSysPkg_package.GetSysClass");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   132
        if (clss == null)
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   133
            throw new RuntimeException("Could not find class GetSysPkg_package.GetSysClass");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   134
        getPkg("GetSysPkg_package", null);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   135
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   136
        // Access a class with a package in a boot loader module other than java.base
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   137
        clss = Class.forName("java.awt.Button");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   138
        if (clss == null)
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   139
            throw new RuntimeException("Could not find class java.awt.Button");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   140
        getPkg("java/awt", "jrt:/java.desktop");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   141
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   142
        // Test getting the package location from a class found via -Xbootclasspath/a
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   143
        clss = Class.forName("BootLdr_package.BootLdrPkg");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   144
        if (clss == null)
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   145
            throw new RuntimeException("Could not find class BootLdr_package.BootLdrPkg");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   146
        String bootldrPkg = (String)invoke(findMethod("getSystemPackageLocation"), null, "BootLdr_package");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   147
        if (bootldrPkg == null) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   148
            throw new RuntimeException("Expected BootLdr_package to return non-null value");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   149
        }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   150
        if (!bootldrPkg.equals("bl_dir")) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   151
            throw new RuntimeException("Expected BootLdr_package to return bl_dir, got: " + bootldrPkg);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   152
        }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   153
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   154
        // Test when package's class reference is an array.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   155
        // Note: if another class in javax/crypto happens to get loaded
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   156
        //       or if class AEADBadTagException disappears from this package
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   157
        //       then this test will fail.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   158
        getPkg("javax/crypto", null);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   159
        javax.crypto.AEADBadTagException[] blah = new javax.crypto.AEADBadTagException[3];
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   160
        getPkg("javax/crypto", "jrt:/java.base");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   161
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   162
    }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   163
}