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  * @build sun.hotspot.WhiteBox
  29  * @compile/module=java.base java/lang/reflect/ModuleHelper.java
  30  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  31  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  32  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI JVMAddModulePackage
  33  */
  34 
  35 import static jdk.test.lib.Asserts.*;
  36 import java.sql.Time;
  37 
  38 public class JVMAddModulePackage {
  39 
  40     public static void main(String args[]) throws Throwable {
  41         MyClassLoader cl1 = new MyClassLoader();
  42         MyClassLoader cl3 = new MyClassLoader();
  43         Object module_one, module_two, module_three;
  44         boolean result;
  45 
  46         module_one = ModuleHelper.ModuleObject("module_one", cl1, new String[] { "mypackage" });
  47         assertNotNull(module_one, "Module should not be null");
  48         ModuleHelper.DefineModule(module_one, false, "9.0", "module_one/here", new String[] { "mypackage" });
  49         module_two = ModuleHelper.ModuleObject("module_two", cl1, new String[] { "yourpackage" });
  50         assertNotNull(module_two, "Module should not be null");
  51         ModuleHelper.DefineModule(module_two, false, "9.0", "module_two/here", new String[] { "yourpackage" });
  52         module_three = ModuleHelper.ModuleObject("module_three", cl3, new String[] { "package/num3" });
  53         assertNotNull(module_three, "Module should not be null");
  54         ModuleHelper.DefineModule(module_three, false, "9.0", "module_three/here", new String[] { "package/num3" });
  55 
  56         // Simple call
  57         ModuleHelper.AddModulePackage(module_one, "new_package");
  58 
  59         // Add a package and export it
  60         ModuleHelper.AddModulePackage(module_one, "package/num3");
  61         ModuleHelper.AddModuleExportsToAll(module_one, "package/num3");
  62 
  63         // Null module argument, expect an NPE
  64         try {
  65             ModuleHelper.AddModulePackage(null, "new_package");
  66             throw new RuntimeException("Failed to get the expected NPE");
  67         } catch(NullPointerException e) {
  68             // Expected
  69         }
  70 
  71         // Bad module argument, expect an IAE
  72         try {
  73             ModuleHelper.AddModulePackage(cl1, "new_package");
  74             throw new RuntimeException("Failed to get the expected IAE");
  75         } catch(IllegalArgumentException e) {
  76             // Expected
  77         }
  78 
  79         // Null package argument, expect an NPE
  80         try {
  81             ModuleHelper.AddModulePackage(module_one, null);
  82             throw new RuntimeException("Failed to get the expected NPE");
  83         } catch(NullPointerException e) {
  84             // Expected
  85         }
  86 
  87         // Existing package, expect an ISE
  88         try {
  89             ModuleHelper.AddModulePackage(module_one, "yourpackage");
  90             throw new RuntimeException("Failed to get the expected ISE");
  91         } catch(IllegalStateException e) {
  92             // Expected
  93         }
  94 
  95         // Invalid package name, expect an IAE
  96         try {
  97             ModuleHelper.AddModulePackage(module_one, "your.package");
  98             throw new RuntimeException("Failed to get the expected IAE");
  99         } catch(IllegalArgumentException e) {
 100             // Expected
 101         }
 102 
 103         // Invalid package name, expect an IAE
 104         try {
 105             ModuleHelper.AddModulePackage(module_one, ";your/package");
 106             throw new RuntimeException("Failed to get the expected IAE");
 107         } catch(IllegalArgumentException e) {
 108             // Expected
 109         }
 110 
 111         // Invalid package name, expect an IAE
 112         try {
 113             ModuleHelper.AddModulePackage(module_one, "7[743");
 114             throw new RuntimeException("Failed to get the expected IAE");
 115         } catch(IllegalArgumentException e) {
 116             // Expected
 117         }
 118 
 119         // Empty package name, expect an IAE
 120         try {
 121             ModuleHelper.AddModulePackage(module_one, "");
 122             throw new RuntimeException("Failed to get the expected IAE");
 123         } catch(IllegalArgumentException e) {
 124             // Expected
 125         }
 126 
 127         // Add package named "java" to an module defined to a class loader other than the boot or platform loader.
 128         try {
 129             // module_one is defined to a MyClassLoader class loader.
 130             ModuleHelper.AddModulePackage(module_one, "java/foo");
 131             throw new RuntimeException("Failed to get the expected IAE");
 132         } catch(IllegalArgumentException e) {
 133             if (!e.getMessage().contains("prohibited package name")) {
 134               throw new RuntimeException("Failed to get expected IAE message for prohibited package name: " + e.getMessage());
 135             }
 136         }
 137 
 138         // Package "javabar" should be ok
 139         ModuleHelper.AddModulePackage(module_one, "javabar");
 140 
 141         // Package named "java" defined to the boot class loader, should be ok
 142         Object module_javabase = module_one.getClass().getModule();
 143         ModuleHelper.AddModulePackage(module_javabase, "java/foo");
 144 
 145         // Package named "java" defined to the platform class loader, should be ok
 146         // The module java.sql is defined to the platform class loader.
 147         java.sql.Time jst = new java.sql.Time(45000); // milliseconds
 148         Object module_javasql = jst.getClass().getModule();
 149         ModuleHelper.AddModulePackage(module_javasql, "java/foo");
 150     }
 151 
 152     static class MyClassLoader extends ClassLoader { }
 153 }
 154