1 /*
   2  * Copyright (c) 2015, 2018, 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 /**
  26  * Tests loading an archived class that has the same class name as one in the
  27  * jimage. The class should normally fail to load since a classpath class is not
  28  * allowed to have the same package name as a module in the jimage. However,
  29  * if --limit-modules was used then archived class should be loaded.
  30  */
  31 
  32 package jdk.test;
  33 
  34 public class Main {
  35     static final ClassLoader BOOT_LOADER     = null;
  36     static final ClassLoader PLATFORM_LOADER = ClassLoader.getPlatformClassLoader();
  37     static final ClassLoader SYS_LOADER      = ClassLoader.getSystemClassLoader();
  38 
  39     public static void main(String[] args) throws Exception {
  40         boolean shouldLoad = false;
  41         ClassLoader expectedLoader = SYS_LOADER;
  42 
  43         /*
  44          * 3 Arguments are passed to this test:
  45          *   1. testName: Name of the test being run.
  46          *   2. className: Name of the class to load and instantiate.
  47          *   3. shouldLoad: Either "true" or "false" to indicate whether the class should
  48          *      successfully load ("true" indicates --limit-modules was used.)
  49          * The 4th argument is optional. It specifies the classloader.
  50          */
  51 
  52         assertTrue(args.length <= 4);
  53         String testName = args[0];
  54         String className = args[1].replace('/', '.');
  55         String shouldLoadName = args[2];  // "true" or "false"
  56         String loaderName = "SYS";
  57         if (args.length == 4) {
  58             loaderName = args[3];
  59         }
  60 
  61         if (shouldLoadName.equals("true")) {
  62             shouldLoad = true;
  63         } else if (shouldLoadName.equals("false")) {
  64             shouldLoad = false;
  65         } else {
  66             assertTrue(false);
  67         }
  68 
  69         if (loaderName.equals("SYS")) {
  70             expectedLoader = SYS_LOADER;
  71         } else if (loaderName.equals("EXT")) {
  72             expectedLoader = PLATFORM_LOADER;
  73         } else if (loaderName.equals("BOOT")) {
  74             expectedLoader = BOOT_LOADER;
  75         }
  76 
  77         System.out.println(testName + ": class=" + className + " shouldLoad=" +
  78                            shouldLoadName + " by loader:" + expectedLoader);
  79 
  80         // Try to load the specified class with the default ClassLoader.
  81         Class<?> clazz = null;
  82         try {
  83             clazz = Class.forName(className);
  84         } catch (ClassNotFoundException e) {
  85             System.out.println(e);
  86         }
  87 
  88         if (clazz != null) {
  89             // class loaded
  90             if (shouldLoad) {
  91                 // Make sure we got the expected defining ClassLoader
  92                 ClassLoader actualLoader = clazz.getClassLoader();
  93                 if (actualLoader != expectedLoader) {
  94                     throw new RuntimeException(testName + " FAILED: " + clazz + " loaded by " + actualLoader +
  95                                                ", expected " + expectedLoader);
  96                 }
  97                 // Make sure we got the right version of the class. toString() of an instance
  98                 // of the overridden version of the class should return "hi".
  99                 if (actualLoader == SYS_LOADER) {
 100                     String s = clazz.newInstance().toString();
 101                     if (!s.equals("hi")) {
 102                         throw new RuntimeException(testName + " FAILED: toString() returned \"" + s
 103                                                    + "\" instead of \"hi\"" );
 104                     }
 105                 }
 106                 System.out.println(testName + " PASSED: class loaded as expected.");
 107             } else {
 108                 throw new RuntimeException(testName + " FAILED: class loaded, but should have failed to load.");
 109             }
 110         } else {
 111             // class did not load
 112             if (shouldLoad) {
 113                 throw new RuntimeException(testName + " FAILED: class failed to load.");
 114             } else {
 115                 System.out.println(testName + " PASSED: ClassNotFoundException thrown as expected");
 116             }
 117         }
 118     }
 119 
 120     static void assertTrue(boolean expr) {
 121         if (!expr)
 122             throw new RuntimeException("assertion failed");
 123     }
 124 }