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 JVMIsExportedToModule
  33  */
  34 
  35 import static jdk.test.lib.Asserts.*;
  36 
  37 public class JVMIsExportedToModule {
  38 
  39     public static void main(String args[]) throws Throwable {
  40         MyClassLoader from_cl = new MyClassLoader();
  41         MyClassLoader to_cl = new MyClassLoader();
  42         Object from_module, to_module;
  43         boolean result;
  44 
  45         from_module = ModuleHelper.ModuleObject("from_module", from_cl, new String[] { "mypackage", "this/package" });
  46         assertNotNull(from_module, "Module from_module should not be null");
  47         ModuleHelper.DefineModule(from_module, "9.0", "from_module/here", new String[] { "mypackage", "this/package" });
  48         to_module = ModuleHelper.ModuleObject("to_module", to_cl, new String[] { "yourpackage", "that/package" });
  49         assertNotNull(to_module, "Module to_module should not be null");
  50         ModuleHelper.DefineModule(to_module, "9.0", "to_module/here", new String[] { "yourpackage", "that/package" });
  51 
  52         Object unnamed_module = JVMIsExportedToModule.class.getModule();
  53         assertNotNull(unnamed_module, "Module unnamed_module should not be null");
  54 
  55         // Null from_module argument, expect an NPE
  56         try {
  57             result = ModuleHelper.IsExportedToModule(null, "mypackage", to_module);
  58             throw new RuntimeException("Failed to get the expected NPE for null from_module");
  59         } catch(NullPointerException e) {
  60             // Expected
  61         }
  62 
  63         // Null to_module argument, expect an NPE
  64         try {
  65           result = ModuleHelper.IsExportedToModule(from_module, "mypackage", null);
  66           throw new RuntimeException("Failed to get expected NPE for null to_module");
  67         } catch(NullPointerException e) {
  68             // Expected
  69         }
  70 
  71         // Null package argument, expect an NPE
  72         try {
  73             result = ModuleHelper.IsExportedToModule(from_module, null, to_module);
  74             throw new RuntimeException("Failed to get the expected NPE for null package");
  75         } catch(NullPointerException e) {
  76             // Expected
  77         }
  78 
  79         // Bad from_module argument, expect an IAE
  80         try {
  81             result = ModuleHelper.IsExportedToModule(to_cl, "mypackage", to_module);
  82             throw new RuntimeException("Failed to get the expected IAE for bad from_module");
  83         } catch(IllegalArgumentException e) {
  84             // Expected
  85         }
  86 
  87         // Bad to_module argument, expect an IAE
  88         try {
  89             result = ModuleHelper.IsExportedToModule(from_module, "mypackage", from_cl);
  90             throw new RuntimeException("Failed to get the expected IAE");
  91         } catch(IllegalArgumentException e) {
  92             // Expected
  93         }
  94 
  95         // Check that package is exported to its own module
  96         result = ModuleHelper.IsExportedToModule(from_module, "mypackage", from_module);
  97         assertTrue(result, "Package is always exported to itself");
  98 
  99         // Package is not in to_module, expect an IAE
 100         try {
 101             result = ModuleHelper.IsExportedToModule(from_module, "yourpackage", from_cl);
 102             throw new RuntimeException("Failed to get the expected IAE for package not in to_module");
 103         } catch(IllegalArgumentException e) {
 104             // Expected
 105         }
 106 
 107         // Package is accessible when exported to unnamed module
 108         ModuleHelper.AddModuleExportsToAll(from_module, "mypackage");
 109         result = ModuleHelper.IsExportedToModule(from_module, "mypackage", to_module);
 110         assertTrue(result, "Package exported to unnamed module is visible to named module");
 111 
 112         result = ModuleHelper.IsExportedToModule(from_module, "mypackage", unnamed_module);
 113         assertTrue(result, "Package exported to unnamed module is visible to unnamed module");
 114 
 115         // Package is accessible only to named module when exported to named module
 116         ModuleHelper.AddModuleExports(from_module, "this/package", to_module);
 117         result = ModuleHelper.IsExportedToModule(from_module, "this/package", to_module);
 118         assertTrue(result, "Package exported to named module is visible to named module");
 119         result = ModuleHelper.IsExportedToModule(from_module, "this/package", unnamed_module);
 120         assertTrue(!result, "Package exported to named module is not visible to unnamed module");
 121     }
 122 
 123     static class MyClassLoader extends ClassLoader { }
 124 }