hotspot/test/runtime/modules/JVMAddModulePackage.java
changeset 43665 4bb003cad9b9
parent 43446 4f9ac7ab99d9
child 44520 0553e129e0ec
equal deleted inserted replaced
43606:a5aa7536131c 43665:4bb003cad9b9
     1 /*
     1 /*
     2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    38 public class JVMAddModulePackage {
    38 public class JVMAddModulePackage {
    39 
    39 
    40     public static void main(String args[]) throws Throwable {
    40     public static void main(String args[]) throws Throwable {
    41         MyClassLoader cl1 = new MyClassLoader();
    41         MyClassLoader cl1 = new MyClassLoader();
    42         MyClassLoader cl3 = new MyClassLoader();
    42         MyClassLoader cl3 = new MyClassLoader();
    43         Object module1, module2, module3;
    43         Object module_one, module_two, module_three;
    44         boolean result;
    44         boolean result;
    45 
    45 
    46         module1 = ModuleHelper.ModuleObject("module1", cl1, new String[] { "mypackage" });
    46         module_one = ModuleHelper.ModuleObject("module_one", cl1, new String[] { "mypackage" });
    47         assertNotNull(module1, "Module should not be null");
    47         assertNotNull(module_one, "Module should not be null");
    48         ModuleHelper.DefineModule(module1, "9.0", "module1/here", new String[] { "mypackage" });
    48         ModuleHelper.DefineModule(module_one, "9.0", "module_one/here", new String[] { "mypackage" });
    49         module2 = ModuleHelper.ModuleObject("module2", cl1, new String[] { "yourpackage" });
    49         module_two = ModuleHelper.ModuleObject("module_two", cl1, new String[] { "yourpackage" });
    50         assertNotNull(module2, "Module should not be null");
    50         assertNotNull(module_two, "Module should not be null");
    51         ModuleHelper.DefineModule(module2, "9.0", "module2/here", new String[] { "yourpackage" });
    51         ModuleHelper.DefineModule(module_two, "9.0", "module_two/here", new String[] { "yourpackage" });
    52         module3 = ModuleHelper.ModuleObject("module3", cl3, new String[] { "package/num3" });
    52         module_three = ModuleHelper.ModuleObject("module_three", cl3, new String[] { "package/num3" });
    53         assertNotNull(module3, "Module should not be null");
    53         assertNotNull(module_three, "Module should not be null");
    54         ModuleHelper.DefineModule(module3, "9.0", "module3/here", new String[] { "package/num3" });
    54         ModuleHelper.DefineModule(module_three, "9.0", "module_three/here", new String[] { "package/num3" });
    55 
    55 
    56         // Simple call
    56         // Simple call
    57         ModuleHelper.AddModulePackage(module1, "new_package");
    57         ModuleHelper.AddModulePackage(module_one, "new_package");
    58 
    58 
    59         // Add a package and export it
    59         // Add a package and export it
    60         ModuleHelper.AddModulePackage(module1, "package/num3");
    60         ModuleHelper.AddModulePackage(module_one, "package/num3");
    61         ModuleHelper.AddModuleExportsToAll(module1, "package/num3");
    61         ModuleHelper.AddModuleExportsToAll(module_one, "package/num3");
    62 
    62 
    63         // Null module argument, expect an NPE
    63         // Null module argument, expect an NPE
    64         try {
    64         try {
    65             ModuleHelper.AddModulePackage(null, "new_package");
    65             ModuleHelper.AddModulePackage(null, "new_package");
    66             throw new RuntimeException("Failed to get the expected NPE");
    66             throw new RuntimeException("Failed to get the expected NPE");
    76             // Expected
    76             // Expected
    77         }
    77         }
    78 
    78 
    79         // Null package argument, expect an NPE
    79         // Null package argument, expect an NPE
    80         try {
    80         try {
    81             ModuleHelper.AddModulePackage(module1, null);
    81             ModuleHelper.AddModulePackage(module_one, null);
    82             throw new RuntimeException("Failed to get the expected NPE");
    82             throw new RuntimeException("Failed to get the expected NPE");
    83         } catch(NullPointerException e) {
    83         } catch(NullPointerException e) {
    84             // Expected
    84             // Expected
    85         }
    85         }
    86 
    86 
    87         // Existing package, expect an ISE
    87         // Existing package, expect an ISE
    88         try {
    88         try {
    89             ModuleHelper.AddModulePackage(module1, "yourpackage");
    89             ModuleHelper.AddModulePackage(module_one, "yourpackage");
    90             throw new RuntimeException("Failed to get the expected ISE");
    90             throw new RuntimeException("Failed to get the expected ISE");
    91         } catch(IllegalStateException e) {
    91         } catch(IllegalStateException e) {
    92             // Expected
    92             // Expected
    93         }
    93         }
    94 
    94 
    95         // Invalid package name, expect an IAE
    95         // Invalid package name, expect an IAE
    96         try {
    96         try {
    97             ModuleHelper.AddModulePackage(module1, "your.package");
    97             ModuleHelper.AddModulePackage(module_one, "your.package");
    98             throw new RuntimeException("Failed to get the expected IAE");
    98             throw new RuntimeException("Failed to get the expected IAE");
    99         } catch(IllegalArgumentException e) {
    99         } catch(IllegalArgumentException e) {
   100             // Expected
   100             // Expected
   101         }
   101         }
   102 
   102 
   103         // Invalid package name, expect an IAE
   103         // Invalid package name, expect an IAE
   104         try {
   104         try {
   105             ModuleHelper.AddModulePackage(module1, ";your/package");
   105             ModuleHelper.AddModulePackage(module_one, ";your/package");
   106             throw new RuntimeException("Failed to get the expected IAE");
   106             throw new RuntimeException("Failed to get the expected IAE");
   107         } catch(IllegalArgumentException e) {
   107         } catch(IllegalArgumentException e) {
   108             // Expected
   108             // Expected
   109         }
   109         }
   110 
   110 
   111         // Invalid package name, expect an IAE
   111         // Invalid package name, expect an IAE
   112         try {
   112         try {
   113             ModuleHelper.AddModulePackage(module1, "7[743");
   113             ModuleHelper.AddModulePackage(module_one, "7[743");
   114             throw new RuntimeException("Failed to get the expected IAE");
   114             throw new RuntimeException("Failed to get the expected IAE");
   115         } catch(IllegalArgumentException e) {
   115         } catch(IllegalArgumentException e) {
   116             // Expected
   116             // Expected
   117         }
   117         }
   118 
   118 
   119         // Empty package name, expect an IAE
   119         // Empty package name, expect an IAE
   120         try {
   120         try {
   121             ModuleHelper.AddModulePackage(module1, "");
   121             ModuleHelper.AddModulePackage(module_one, "");
   122             throw new RuntimeException("Failed to get the expected IAE");
   122             throw new RuntimeException("Failed to get the expected IAE");
   123         } catch(IllegalArgumentException e) {
   123         } catch(IllegalArgumentException e) {
   124             // Expected
   124             // Expected
   125         }
   125         }
   126 
   126 
   127         // Add package named "java" to an module defined to a class loader other than the boot or platform loader.
   127         // Add package named "java" to an module defined to a class loader other than the boot or platform loader.
   128         try {
   128         try {
   129             // module1 is defined to a MyClassLoader class loader.
   129             // module_one is defined to a MyClassLoader class loader.
   130             ModuleHelper.AddModulePackage(module1, "java/foo");
   130             ModuleHelper.AddModulePackage(module_one, "java/foo");
   131             throw new RuntimeException("Failed to get the expected IAE");
   131             throw new RuntimeException("Failed to get the expected IAE");
   132         } catch(IllegalArgumentException e) {
   132         } catch(IllegalArgumentException e) {
   133             if (!e.getMessage().contains("prohibited package name")) {
   133             if (!e.getMessage().contains("prohibited package name")) {
   134               throw new RuntimeException("Failed to get expected IAE message for prohibited package name: " + e.getMessage());
   134               throw new RuntimeException("Failed to get expected IAE message for prohibited package name: " + e.getMessage());
   135             }
   135             }
   136         }
   136         }
   137 
   137 
   138         // Package "javabar" should be ok
   138         // Package "javabar" should be ok
   139         ModuleHelper.AddModulePackage(module1, "javabar");
   139         ModuleHelper.AddModulePackage(module_one, "javabar");
   140 
   140 
   141         // Package named "java" defined to the boot class loader, should be ok
   141         // Package named "java" defined to the boot class loader, should be ok
   142         Object module_javabase = module1.getClass().getModule();
   142         Object module_javabase = module_one.getClass().getModule();
   143         ModuleHelper.AddModulePackage(module_javabase, "java/foo");
   143         ModuleHelper.AddModulePackage(module_javabase, "java/foo");
   144 
   144 
   145         // Package named "java" defined to the platform class loader, should be ok
   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.
   146         // The module java.sql is defined to the platform class loader.
   147         java.sql.Time jst = new java.sql.Time(45000); // milliseconds
   147         java.sql.Time jst = new java.sql.Time(45000); // milliseconds