1/*
2 * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @modules java.base/jdk.internal.misc
27 * @library /test/lib ..
28 * @compile p2/c2.java
29 * @build sun.hotspot.WhiteBox
30 * @compile/module=java.base java/lang/ModuleHelper.java
31 * @run main ClassFileInstaller sun.hotspot.WhiteBox
32 *                              sun.hotspot.WhiteBox$WhiteBoxPermission
33 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI JVMGetModuleByPkgName
34 */
35
36import static jdk.test.lib.Asserts.*;
37import java.lang.ClassLoader;
38
39public class JVMGetModuleByPkgName {
40
41    public static void main(String args[]) throws Throwable {
42
43        Module javaBase = ModuleHelper.GetModuleByPackageName(null, "java/lang");
44        if (!javaBase.getName().equals("java.base")) {
45            throw new RuntimeException(
46                "Failed to get module java.base for package java/lang");
47        }
48
49        if (ModuleHelper.GetModuleByPackageName(null, "bad.package.name") != null) {
50            throw new RuntimeException("Failed to get null for bad.package.name");
51        }
52
53        ClassLoader systemLoader = ClassLoader.getSystemClassLoader();
54        if (ModuleHelper.GetModuleByPackageName(systemLoader, "java/lang") != null) {
55            throw new RuntimeException(
56                "Failed to get null for systemClassLoader and java/lang");
57        }
58
59        try {
60            ModuleHelper.GetModuleByPackageName(systemLoader, null);
61            throw new RuntimeException(
62                "Failed to throw NullPointerException for null package name");
63        } catch(NullPointerException e) {
64             // Expected
65        }
66
67        Module unnamedModule = ModuleHelper.GetModuleByPackageName(systemLoader, "");
68        if (unnamedModule.isNamed()) {
69            throw new RuntimeException(
70                "Unexpected named module returned for unnamed package");
71        }
72
73        p2.c2 obj = new p2.c2();
74        unnamedModule = ModuleHelper.GetModuleByPackageName(systemLoader, "p2");
75        if (unnamedModule.isNamed()) {
76            throw new RuntimeException(
77                "Unexpected named module returned for package p2 in unnamed module");
78        }
79
80        MyClassLoader cl1 = new MyClassLoader();
81        Module module_one = (Module)ModuleHelper.ModuleObject("module_one", cl1, new String[] { "mypackage" });
82        assertNotNull(module_one, "Module should not be null");
83        ModuleHelper.DefineModule(module_one, "9.0", "module_one/here", new String[] { "mypackage" });
84        if (ModuleHelper.GetModuleByPackageName(cl1, "mypackage") != module_one) {
85            throw new RuntimeException("Wrong module returned for cl1 mypackage");
86        }
87    }
88
89    static class MyClassLoader extends ClassLoader { }
90}
91