1 /*
   2  * Copyright (c) 2016, 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/reflect/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 
  36 import static jdk.test.lib.Asserts.*;
  37 import java.lang.ClassLoader;
  38 import java.lang.reflect.Module;
  39 
  40 public class JVMGetModuleByPkgName {
  41 
  42     public static void main(String args[]) throws Throwable {
  43 
  44         Module javaBase = ModuleHelper.GetModuleByPackageName(null, "java/lang");
  45         if (!javaBase.getName().equals("java.base")) {
  46             throw new RuntimeException(
  47                 "Failed to get module java.base for package java/lang");
  48         }
  49 
  50         if (ModuleHelper.GetModuleByPackageName(null, "bad.package.name") != null) {
  51             throw new RuntimeException("Failed to get null for bad.package.name");
  52         }
  53 
  54         ClassLoader systemLoader = ClassLoader.getSystemClassLoader();
  55         if (ModuleHelper.GetModuleByPackageName(systemLoader, "java/lang") != null) {
  56             throw new RuntimeException(
  57                 "Failed to get null for systemClassLoader and java/lang");
  58         }
  59 
  60         try {
  61             ModuleHelper.GetModuleByPackageName(systemLoader, null);
  62             throw new RuntimeException(
  63                 "Failed to throw NullPointerException for null package name");
  64         } catch(NullPointerException e) {
  65              // Expected
  66         }
  67 
  68         Module unnamedModule = ModuleHelper.GetModuleByPackageName(systemLoader, "");
  69         if (unnamedModule.isNamed()) {
  70             throw new RuntimeException(
  71                 "Unexpected named module returned for unnamed package");
  72         }
  73 
  74         p2.c2 obj = new p2.c2();
  75         unnamedModule = ModuleHelper.GetModuleByPackageName(systemLoader, "p2");
  76         if (unnamedModule.isNamed()) {
  77             throw new RuntimeException(
  78                 "Unexpected named module returned for package p2 in unnamed module");
  79         }
  80 
  81         MyClassLoader cl1 = new MyClassLoader();
  82         Module module1 = (Module)ModuleHelper.ModuleObject("module1", cl1, new String[] { "mypackage" });
  83         assertNotNull(module1, "Module should not be null");
  84         ModuleHelper.DefineModule(module1, "9.0", "module1/here", new String[] { "mypackage" });
  85         if (ModuleHelper.GetModuleByPackageName(cl1, "mypackage") != module1) {
  86             throw new RuntimeException("Wrong module returned for cl1 mypackage");
  87         }
  88     }
  89 
  90     static class MyClassLoader extends ClassLoader { }
  91 }