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 /testlibrary /test/lib /compiler/whitebox ..
  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 
  37 public class JVMAddModulePackage {
  38 
  39     public static void main(String args[]) throws Throwable {
  40         MyClassLoader cl1 = new MyClassLoader();
  41         MyClassLoader cl3 = new MyClassLoader();
  42         Object module1, module2, module3;
  43         boolean result;
  44 
  45         module1 = ModuleHelper.ModuleObject("module1", cl1, new String[] { "mypackage" });
  46         assertNotNull(module1, "Module should not be null");
  47         ModuleHelper.DefineModule(module1, "9.0", "module1/here", new String[] { "mypackage" });
  48         module2 = ModuleHelper.ModuleObject("module2", cl1, new String[] { "yourpackage" });
  49         assertNotNull(module2, "Module should not be null");
  50         ModuleHelper.DefineModule(module2, "9.0", "module2/here", new String[] { "yourpackage" });
  51         module3 = ModuleHelper.ModuleObject("module3", cl3, new String[] { "package/num3" });
  52         assertNotNull(module3, "Module should not be null");
  53         ModuleHelper.DefineModule(module3, "9.0", "module3/here", new String[] { "package/num3" });
  54 
  55         // Simple call
  56         ModuleHelper.AddModulePackage(module1, "new_package");
  57 
  58         // Add a package and export it
  59         ModuleHelper.AddModulePackage(module1, "package/num3");
  60         ModuleHelper.AddModuleExportsToAll(module1, "package/num3");
  61 
  62         // Null module argument, expect an NPE
  63         try {
  64             ModuleHelper.AddModulePackage(null, "new_package");
  65             throw new RuntimeException("Failed to get the expected NPE");
  66         } catch(NullPointerException e) {
  67             // Expected
  68         }
  69 
  70         // Bad module argument, expect an IAE
  71         try {
  72             ModuleHelper.AddModulePackage(cl1, "new_package");
  73             throw new RuntimeException("Failed to get the expected IAE");
  74         } catch(IllegalArgumentException e) {
  75             // Expected
  76         }
  77 
  78         // Null package argument, expect an NPE
  79         try {
  80             ModuleHelper.AddModulePackage(module1, null);
  81             throw new RuntimeException("Failed to get the expected NPE");
  82         } catch(NullPointerException e) {
  83             // Expected
  84         }
  85 
  86         // Existing package, expect an IAE
  87         try {
  88             ModuleHelper.AddModulePackage(module1, "yourpackage");
  89             throw new RuntimeException("Failed to get the expected IAE");
  90         } catch(IllegalArgumentException e) {
  91             // Expected
  92         }
  93 
  94         // Invalid package name, expect an IAE
  95         try {
  96             ModuleHelper.AddModulePackage(module1, "your.package");
  97             throw new RuntimeException("Failed to get the expected IAE");
  98         } catch(IllegalArgumentException e) {
  99             // Expected
 100         }
 101 
 102         // Invalid package name, expect an IAE
 103         try {
 104             ModuleHelper.AddModulePackage(module1, ";your/package");
 105             throw new RuntimeException("Failed to get the expected IAE");
 106         } catch(IllegalArgumentException e) {
 107             // Expected
 108         }
 109 
 110         // Invalid package name, expect an IAE
 111         try {
 112             ModuleHelper.AddModulePackage(module1, "7[743");
 113             throw new RuntimeException("Failed to get the expected IAE");
 114         } catch(IllegalArgumentException e) {
 115             // Expected
 116         }
 117 
 118         // Empty package name, expect an IAE
 119         try {
 120             ModuleHelper.AddModulePackage(module1, "");
 121             throw new RuntimeException("Failed to get the expected IAE");
 122         } catch(IllegalArgumentException e) {
 123             // Expected
 124         }
 125 
 126     }
 127 
 128     static class MyClassLoader extends ClassLoader { }
 129 }
 130