1 /*
   2  * Copyright (c) 2015, 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 public class JimageClassPackage {
  26     public static void main(String args[]) throws Throwable {
  27         // Test Package for boot/app/ext module classes from the "modules" jimage.
  28         // The following classes are archived. See runtime/AppCDS/Package.java.
  29         //     java.util.Dictionary (testcase 0),
  30         //     sun.tools.javac.Main (testcase 1),
  31         //     jdk.nio.zipfs.ZipInfo (testcase 2),
  32         //     java.net.URL (testcase 3),
  33         //     sun.rmi.rmic.Main (testcase 4),
  34         //     com.sun.jndi.dns.DnsName (testcase 5)
  35         String testcases[][] =
  36             {{"Loading shared boot module class first", "java.util",
  37               "java.util.Dictionary", "java.util.ServiceConfigurationError"},
  38 
  39              {"Loading shared app module class first", "sun.tools.javac",
  40               "sun.tools.javac.Main", "sun.tools.javac.BatchParser"},
  41 
  42              {"Loading shared ext module class first", "jdk.nio.zipfs",
  43               "jdk.nio.zipfs.ZipInfo", "jdk.nio.zipfs.ZipPath"},
  44 
  45              {"Loading non-shared boot module class first", "java.net",
  46               "java.net.HttpCookie", "java.net.URL"},
  47 
  48              {"Loading non-shared app module class first", "sun.rmi.rmic",
  49               "sun.rmi.rmic.RMIGenerator", "sun.rmi.rmic.Main"},
  50 
  51              {"Loading non-shared ext module class first", "com.sun.jndi.dns",
  52               "com.sun.jndi.dns.Resolver", "com.sun.jndi.dns.DnsName"}};
  53 
  54         JimageClassPackage test = new JimageClassPackage();
  55         for (int i = 0; i < testcases.length; i++) {
  56             System.out.println("Testcase " + i + ": " + testcases[i][0]);
  57             test.testPackage(testcases[i][1], testcases[i][2], testcases[i][3]);
  58         }
  59     }
  60 
  61     private void testPackage (String pkg,
  62                               String shared,
  63                               String nonShared) throws Throwable {
  64         Class c1 = Class.forName(shared);
  65         ClassLoader cl = c1.getClassLoader();
  66         Package pkg_from_loader;
  67         if (cl != null) {
  68             pkg_from_loader = cl.getDefinedPackage(pkg);
  69         } else {
  70             pkg_from_loader = Package.getPackage(pkg);
  71         }
  72 
  73         Package pkg_from_shared_class = c1.getPackage();
  74 
  75         Class c2 = Class.forName(nonShared);
  76         Package pkg_from_nonshared_class = c2.getPackage();
  77 
  78         if (pkg_from_loader != null &&
  79             pkg_from_shared_class != null &&
  80             pkg_from_loader == pkg_from_shared_class &&
  81             pkg_from_shared_class == pkg_from_nonshared_class &&
  82             pkg_from_shared_class.getName().equals(pkg)) {
  83             System.out.println("Expected package: " + pkg_from_shared_class.toString());
  84         } else {
  85             System.out.println("Unexpected package" + pkg_from_shared_class);
  86             System.exit(1);
  87         }
  88         if (pkg_from_shared_class.isSealed()) {
  89             System.out.println("Package is sealed");
  90         } else {
  91             System.out.println("Package is not sealed");
  92             System.exit(1);
  93         }
  94     }
  95 }