hotspot/test/runtime/getSysPackage/GetSysPkgTest.java
author alanb
Tue, 03 May 2016 09:09:20 +0100
changeset 37773 e5b3e9732c3c
parent 36508 5f9eee6b383b
child 38207 2ed792c9481d
permissions -rw-r--r--
8154956: Module system implementation refresh (4/2016) Reviewed-by: acorn, jiangli, ccheung, hseigel Contributed-by: alan.bateman@oracle.com, lois.foltan@oracle.com, harold.seigel@oracle.com
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
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    26
 * @modules java.base/jdk.internal.loader
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    27
 *          java.desktop
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    28
 * @library /testlibrary
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    29
 * @run main/othervm GetSysPkgTest
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    30
 */
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    31
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.io.File;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.lang.reflect.InvocationTargetException;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.lang.reflect.Method;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import jdk.test.lib.*;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    36
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    37
// Test that JVM get_system_package() returns the module location for defined packages.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    38
public class GetSysPkgTest {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    39
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    40
    private static Object invoke(Method m, Object obj, Object... args) throws Throwable {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    41
        try {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    42
            return m.invoke(obj, args);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    43
        } catch (InvocationTargetException e) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    44
            throw e.getCause();
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    45
        }
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
    private static Method findMethod(String name) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    49
        for (Method m : jdk.internal.loader.BootLoader.class.getDeclaredMethods()) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    50
            if (m.getName().equals(name)) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    51
                m.setAccessible(true);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    52
                return m;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    53
            }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    54
        }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    55
        throw new RuntimeException("Failed to find method " + name + " in java.lang.reflect.Module");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    56
    }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    57
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    58
    // Throw RuntimeException if getSystemPackageLocation() does not return
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    59
    // the expected location.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    60
    static void getPkg(String name, String expected_loc) throws Throwable {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    61
        String loc = (String)invoke(findMethod("getSystemPackageLocation"), null, name);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    62
        if (loc == null) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    63
            if (expected_loc == null) return;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    64
            System.out.println("Expected location: " + expected_loc +
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    65
                ", for package: " + name + ", got: null");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    66
        } else if (expected_loc == null) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    67
            System.out.println("Expected location: null, for package: " +
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    68
                name + ", got: " + loc);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    69
        } else if (!loc.equals(expected_loc)) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    70
            System.out.println("Expected location: " +
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    71
                expected_loc + ", for package: " + name + ", got: " + loc);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    72
        } else {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    73
            return;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    74
        }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    75
        throw new RuntimeException();
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    76
    }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    77
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    78
    public static void main(String args[]) throws Throwable {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    79
        if (args.length == 0 || !args[0].equals("do_tests")) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    80
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    81
            // Create a package found via -Xbootclasspath/a
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    82
            String source = "package BootLdr_package; " +
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    83
                            "public class BootLdrPkg { " +
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    84
                            "    public int mth() { return 4; } " +
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    85
                            "}";
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    86
            byte[] klassbuf =
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    87
                InMemoryJavaCompiler.compile("BootLdr_package.BootLdrPkg", source);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    88
            ClassFileInstaller.writeClassToDisk("BootLdr_package/BootLdrPkg", klassbuf, "bl_dir");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    89
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    90
            // Create a package found via -cp.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    91
            source = "package GetSysPkg_package; " +
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    92
                     "public class GetSysClass { " +
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    93
                     "    public int mth() { return 4; } " +
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    94
                     "}";
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    95
            klassbuf =
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    96
                InMemoryJavaCompiler.compile("GetSysPkg_package.GetSysClass", source);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    97
            ClassFileInstaller.writeClassToDisk("GetSysPkg_package/GetSysClass", klassbuf);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    98
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    99
            ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-Xbootclasspath/a:bl_dir",
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   100
                "-XaddExports:java.base/jdk.internal.loader=ALL-UNNAMED", "-cp", "." + File.pathSeparator +
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   101
                System.getProperty("test.classes"), "GetSysPkgTest", "do_tests");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   102
            OutputAnalyzer output = new OutputAnalyzer(pb.start());
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   103
            output.shouldHaveExitValue(0);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   104
            return;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   105
        }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   106
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   107
        getPkg("java/lang", "jrt:/java.base");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   108
        getPkg("javax/script", null);          // Package not defined
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   109
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   110
        // Test a package that does not yet have any referenced classes.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   111
        // Note: if another class in com/sun/crypto/provider/ happens to get
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   112
        //       loaded or if class PrivateKeyInfo disappears from this package
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   113
        //       then this test will fail.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   114
        getPkg("com/sun/crypto/provider", null);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   115
        // Now make sure a class in the package is referenced.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   116
        Class newClass = Class.forName("com.sun.crypto.provider.PrivateKeyInfo");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   117
        getPkg("com/sun/crypto/provider", "jrt:/java.base");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   118
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   119
        getPkg("java/nio/charset", "jrt:/java.base");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   120
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   121
        // 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
   122
        Class clss = Class.forName("jdk.nio.zipfs.ZipPath");
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   123
        if (clss == null)
37773
e5b3e9732c3c 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36508
diff changeset
   124
            throw new RuntimeException("Could not find class jdk.nio.zipfs.ZipPath");
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   125
        getPkg("javax/activation", null);       // Not owned by boot loader
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   126
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   127
        // Test a package not in jimage file.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   128
        clss = Class.forName("GetSysPkg_package.GetSysClass");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   129
        if (clss == null)
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   130
            throw new RuntimeException("Could not find class GetSysPkg_package.GetSysClass");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   131
        getPkg("GetSysPkg_package", null);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   132
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   133
        // Access a class with a package in a boot loader module other than java.base
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   134
        clss = Class.forName("java.awt.Button");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   135
        if (clss == null)
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   136
            throw new RuntimeException("Could not find class java.awt.Button");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   137
        getPkg("java/awt", "jrt:/java.desktop");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   138
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   139
        // Test getting the package location from a class found via -Xbootclasspath/a
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   140
        clss = Class.forName("BootLdr_package.BootLdrPkg");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   141
        if (clss == null)
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   142
            throw new RuntimeException("Could not find class BootLdr_package.BootLdrPkg");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   143
        String bootldrPkg = (String)invoke(findMethod("getSystemPackageLocation"), null, "BootLdr_package");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   144
        if (bootldrPkg == null) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   145
            throw new RuntimeException("Expected BootLdr_package to return non-null value");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   146
        }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   147
        if (!bootldrPkg.equals("bl_dir")) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   148
            throw new RuntimeException("Expected BootLdr_package to return bl_dir, got: " + bootldrPkg);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   149
        }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   150
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   151
        // Test when package's class reference is an array.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   152
        // Note: if another class in javax/crypto happens to get loaded
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   153
        //       or if class AEADBadTagException disappears from this package
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   154
        //       then this test will fail.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   155
        getPkg("javax/crypto", null);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   156
        javax.crypto.AEADBadTagException[] blah = new javax.crypto.AEADBadTagException[3];
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   157
        getPkg("javax/crypto", "jrt:/java.base");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   158
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   159
    }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   160
}