test/jdk/java/lang/Class/forName/NonLinking/NonLinking.java
changeset 59137 faac483dfb30
equal deleted inserted replaced
59136:fc998c509521 59137:faac483dfb30
       
     1 /*
       
     2  * Copyright (c) 2019, 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 import java.net.URL;
       
    25 import java.net.URLClassLoader;
       
    26 import java.nio.file.Path;
       
    27 import java.nio.file.Paths;
       
    28 
       
    29 /*
       
    30  * @test
       
    31  * @bug 8231924 8233091 8233272
       
    32  * @summary Confirm load (but not link) behavior of Class.forName()
       
    33  * @library /test/lib
       
    34  *
       
    35  * @compile MissingClass.java Container.java
       
    36  *
       
    37  * @run driver ClassFileInstaller -jar classes.jar Container Container$1
       
    38  *
       
    39  * @run main/othervm NonLinking init
       
    40  * @run main/othervm NonLinking load
       
    41  */
       
    42 /*
       
    43  * The @compile and '@main ClassFileInstaller' tasks above create a classes.jar
       
    44  * file containing the .class file for Container, but not MissingClass.
       
    45  */
       
    46 
       
    47 public class NonLinking {
       
    48     public static void main(String[] args) throws Throwable {
       
    49         Path jarPath = Paths.get("classes.jar");
       
    50         URL url = jarPath.toUri().toURL();
       
    51         URLClassLoader ucl1 = new URLClassLoader("UCL1",
       
    52                                                  new URL[] { url },
       
    53                                                  null); // Don't delegate
       
    54         switch(args[0]) {
       
    55             case "init":
       
    56                 try {
       
    57                     // Trying to initialize Container without MissingClass -> NCDFE
       
    58                     Class.forName("Container", true, ucl1);
       
    59                     throw new RuntimeException("Missed expected NoClassDefFoundError");
       
    60                 } catch (NoClassDefFoundError expected) {
       
    61                     final String CLASSNAME = "MissingClass";
       
    62                     Throwable cause = expected.getCause();
       
    63                     if (!cause.getMessage().contains(CLASSNAME)) {
       
    64                         throw new RuntimeException("Cause of NoClassDefFoundError does not contain \"" + CLASSNAME + "\"", cause);
       
    65                     }
       
    66                 }
       
    67                 break;
       
    68             case "load":
       
    69                 // Loading (but not linking) Container will succeed.
       
    70                 // Before 8233091, this fails with NCDFE due to linking.
       
    71                 Class.forName("Container", false, ucl1);
       
    72                 break;
       
    73             default:
       
    74                 throw new RuntimeException("Unknown command: " + args[0]);
       
    75         }
       
    76     }
       
    77 }