hotspot/test/runtime/modules/JVMIsExportedToModule.java
author jdv
Tue, 09 Aug 2016 13:45:17 +0530
changeset 40432 6a878eae7459
parent 38152 80e5da81fb2c
child 40631 ed82623d7831
permissions -rw-r--r--
8160456: KSS : resource loading issue in TIFFMetadataFormat.java Reviewed-by: prr, bpb
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) 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
 * @library /testlibrary /test/lib /compiler/whitebox ..
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    28
 * @build sun.hotspot.WhiteBox
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    29
 * @compile/module=java.base java/lang/reflect/ModuleHelper.java
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    30
 * @run main ClassFileInstaller sun.hotspot.WhiteBox
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    31
 *                              sun.hotspot.WhiteBox$WhiteBoxPermission
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    32
 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI JVMIsExportedToModule
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    33
 */
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    34
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import static jdk.test.lib.Asserts.*;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    36
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    37
public class JVMIsExportedToModule {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    38
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    39
    public static void main(String args[]) throws Throwable {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    40
        MyClassLoader from_cl = new MyClassLoader();
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    41
        MyClassLoader to_cl = new MyClassLoader();
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    42
        Object from_module, to_module;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    43
        boolean result;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    44
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    45
        from_module = ModuleHelper.ModuleObject("from_module", from_cl, new String[] { "mypackage", "this/package" });
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    46
        assertNotNull(from_module, "Module from_module should not be null");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    47
        ModuleHelper.DefineModule(from_module, "9.0", "from_module/here", new String[] { "mypackage", "this/package" });
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    48
        to_module = ModuleHelper.ModuleObject("to_module", to_cl, new String[] { "yourpackage", "that/package" });
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    49
        assertNotNull(to_module, "Module to_module should not be null");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    50
        ModuleHelper.DefineModule(to_module, "9.0", "to_module/here", new String[] { "yourpackage", "that/package" });
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    51
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    52
        Object unnamed_module = JVMIsExportedToModule.class.getModule();
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    53
        assertNotNull(unnamed_module, "Module unnamed_module should not be null");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    54
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    55
        // Null from_module argument, expect an NPE
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    56
        try {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    57
            result = ModuleHelper.IsExportedToModule(null, "mypackage", to_module);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    58
            throw new RuntimeException("Failed to get the expected NPE for null from_module");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    59
        } catch(NullPointerException e) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    60
            // Expected
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    61
        }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    62
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    63
        // Null to_module argument, expect an NPE
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    64
        try {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    65
          result = ModuleHelper.IsExportedToModule(from_module, "mypackage", null);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    66
          throw new RuntimeException("Failed to get expected NPE for null to_module");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    67
        } catch(NullPointerException e) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    68
            // Expected
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    69
        }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    70
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    71
        // Null package argument, expect an NPE
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    72
        try {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    73
            result = ModuleHelper.IsExportedToModule(from_module, null, to_module);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    74
            throw new RuntimeException("Failed to get the expected NPE for null package");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    75
        } catch(NullPointerException e) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    76
            // Expected
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
        // Bad from_module argument, expect an IAE
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    80
        try {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    81
            result = ModuleHelper.IsExportedToModule(to_cl, "mypackage", to_module);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    82
            throw new RuntimeException("Failed to get the expected IAE for bad from_module");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    83
        } catch(IllegalArgumentException e) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    84
            // Expected
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    85
        }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    86
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    87
        // Bad to_module argument, expect an IAE
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    88
        try {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    89
            result = ModuleHelper.IsExportedToModule(from_module, "mypackage", from_cl);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    90
            throw new RuntimeException("Failed to get the expected IAE");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    91
        } catch(IllegalArgumentException e) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    92
            // Expected
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    93
        }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    94
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    95
        // Check that package is exported to its own module
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    96
        result = ModuleHelper.IsExportedToModule(from_module, "mypackage", from_module);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    97
        assertTrue(result, "Package is always exported to itself");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    98
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    99
        // Package is not in to_module, expect an IAE
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   100
        try {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   101
            result = ModuleHelper.IsExportedToModule(from_module, "yourpackage", from_cl);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   102
            throw new RuntimeException("Failed to get the expected IAE for package not in to_module");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   103
        } catch(IllegalArgumentException e) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   104
            // Expected
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
        // Package is accessible when exported to unnamed module
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   108
        ModuleHelper.AddModuleExportsToAll(from_module, "mypackage");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   109
        result = ModuleHelper.IsExportedToModule(from_module, "mypackage", to_module);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   110
        assertTrue(result, "Package exported to unnamed module is visible to named module");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   111
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   112
        result = ModuleHelper.IsExportedToModule(from_module, "mypackage", unnamed_module);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   113
        assertTrue(result, "Package exported to unnamed module is visible to unnamed module");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   114
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   115
        // Package is accessible only to named module when exported to named module
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   116
        ModuleHelper.AddModuleExports(from_module, "this/package", to_module);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   117
        result = ModuleHelper.IsExportedToModule(from_module, "this/package", to_module);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   118
        assertTrue(result, "Package exported to named module is visible to named module");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   119
        result = ModuleHelper.IsExportedToModule(from_module, "this/package", unnamed_module);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   120
        assertTrue(!result, "Package exported to named module is not visible to unnamed module");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   121
    }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   122
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   123
    static class MyClassLoader extends ClassLoader { }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   124
}