hotspot/test/runtime/modules/JVMAddModulePackage.java
author alanb
Thu, 01 Dec 2016 08:56:41 +0000
changeset 42307 cefc81dc1d52
parent 40631 ed82623d7831
child 43446 4f9ac7ab99d9
permissions -rw-r--r--
8169069: Module system implementation refresh (11/2016) Reviewed-by: lfoltan, acorn, ctornqvi, mchung Contributed-by: lois.foltan@oracle.com, harold.seigel@oracle.com, alan.bateman@oracle.com, mandy.chung@oracle.com, serguei.spitsyn@oracle.com, george.triantafillou@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) 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
40631
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 38152
diff changeset
    27
 * @library /test/lib ..
36508
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 JVMAddModulePackage
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.*;
42307
cefc81dc1d52 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40631
diff changeset
    36
import java.sql.Time;
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    37
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    38
public class JVMAddModulePackage {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    39
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    40
    public static void main(String args[]) throws Throwable {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    41
        MyClassLoader cl1 = new MyClassLoader();
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    42
        MyClassLoader cl3 = new MyClassLoader();
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    43
        Object module1, module2, module3;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    44
        boolean result;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    45
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    46
        module1 = ModuleHelper.ModuleObject("module1", cl1, new String[] { "mypackage" });
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    47
        assertNotNull(module1, "Module should not be null");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    48
        ModuleHelper.DefineModule(module1, "9.0", "module1/here", new String[] { "mypackage" });
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    49
        module2 = ModuleHelper.ModuleObject("module2", cl1, new String[] { "yourpackage" });
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    50
        assertNotNull(module2, "Module should not be null");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    51
        ModuleHelper.DefineModule(module2, "9.0", "module2/here", new String[] { "yourpackage" });
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    52
        module3 = ModuleHelper.ModuleObject("module3", cl3, new String[] { "package/num3" });
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    53
        assertNotNull(module3, "Module should not be null");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    54
        ModuleHelper.DefineModule(module3, "9.0", "module3/here", new String[] { "package/num3" });
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    55
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    56
        // Simple call
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    57
        ModuleHelper.AddModulePackage(module1, "new_package");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    58
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    59
        // Add a package and export it
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    60
        ModuleHelper.AddModulePackage(module1, "package/num3");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    61
        ModuleHelper.AddModuleExportsToAll(module1, "package/num3");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    62
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    63
        // Null 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
            ModuleHelper.AddModulePackage(null, "new_package");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    66
            throw new RuntimeException("Failed to get the expected NPE");
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
        // Bad module argument, expect an IAE
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    72
        try {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    73
            ModuleHelper.AddModulePackage(cl1, "new_package");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    74
            throw new RuntimeException("Failed to get the expected IAE");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    75
        } catch(IllegalArgumentException 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
        // Null package argument, expect an NPE
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    80
        try {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    81
            ModuleHelper.AddModulePackage(module1, null);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    82
            throw new RuntimeException("Failed to get the expected NPE");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    83
        } catch(NullPointerException 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
        // Existing package, expect an IAE
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    88
        try {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    89
            ModuleHelper.AddModulePackage(module1, "yourpackage");
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
        // Invalid package name, expect an IAE
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    96
        try {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    97
            ModuleHelper.AddModulePackage(module1, "your.package");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    98
            throw new RuntimeException("Failed to get the expected IAE");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    99
        } catch(IllegalArgumentException e) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   100
            // Expected
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   101
        }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   102
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   103
        // Invalid package name, expect an IAE
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   104
        try {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   105
            ModuleHelper.AddModulePackage(module1, ";your/package");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   106
            throw new RuntimeException("Failed to get the expected IAE");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   107
        } catch(IllegalArgumentException e) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   108
            // Expected
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   109
        }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   110
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   111
        // Invalid package name, expect an IAE
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   112
        try {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   113
            ModuleHelper.AddModulePackage(module1, "7[743");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   114
            throw new RuntimeException("Failed to get the expected IAE");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   115
        } catch(IllegalArgumentException e) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   116
            // Expected
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   117
        }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   118
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   119
        // Empty package name, expect an IAE
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   120
        try {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   121
            ModuleHelper.AddModulePackage(module1, "");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   122
            throw new RuntimeException("Failed to get the expected IAE");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   123
        } catch(IllegalArgumentException e) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   124
            // Expected
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   125
        }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   126
42307
cefc81dc1d52 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40631
diff changeset
   127
        // Add package named "java" to an module defined to a class loader other than the boot or platform loader.
cefc81dc1d52 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40631
diff changeset
   128
        try {
cefc81dc1d52 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40631
diff changeset
   129
            // module1 is defined to a MyClassLoader class loader.
cefc81dc1d52 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40631
diff changeset
   130
            ModuleHelper.AddModulePackage(module1, "java/foo");
cefc81dc1d52 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40631
diff changeset
   131
            throw new RuntimeException("Failed to get the expected IAE");
cefc81dc1d52 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40631
diff changeset
   132
        } catch(IllegalArgumentException e) {
cefc81dc1d52 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40631
diff changeset
   133
            if (!e.getMessage().contains("prohibited package name")) {
cefc81dc1d52 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40631
diff changeset
   134
              throw new RuntimeException("Failed to get expected IAE message for prohibited package name: " + e.getMessage());
cefc81dc1d52 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40631
diff changeset
   135
            }
cefc81dc1d52 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40631
diff changeset
   136
        }
cefc81dc1d52 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40631
diff changeset
   137
cefc81dc1d52 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40631
diff changeset
   138
        // Package "javabar" should be ok
cefc81dc1d52 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40631
diff changeset
   139
        ModuleHelper.AddModulePackage(module1, "javabar");
cefc81dc1d52 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40631
diff changeset
   140
cefc81dc1d52 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40631
diff changeset
   141
        // Package named "java" defined to the boot class loader, should be ok
cefc81dc1d52 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40631
diff changeset
   142
        Object module_javabase = module1.getClass().getModule();
cefc81dc1d52 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40631
diff changeset
   143
        ModuleHelper.AddModulePackage(module_javabase, "java/foo");
cefc81dc1d52 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40631
diff changeset
   144
cefc81dc1d52 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40631
diff changeset
   145
        // Package named "java" defined to the platform class loader, should be ok
cefc81dc1d52 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40631
diff changeset
   146
        // The module java.sql is defined to the platform class loader.
cefc81dc1d52 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40631
diff changeset
   147
        java.sql.Time jst = new java.sql.Time(45000); // milliseconds
cefc81dc1d52 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40631
diff changeset
   148
        Object module_javasql = jst.getClass().getModule();
cefc81dc1d52 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40631
diff changeset
   149
        ModuleHelper.AddModulePackage(module_javasql, "java/foo");
36508
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
    static class MyClassLoader extends ClassLoader { }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   153
}
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   154