test/hotspot/jtreg/runtime/LoaderConstraints/itableLdrConstraint/Test.java
changeset 50634 c349d409262a
parent 50036 e0dbf14885b8
child 51097 bef02342d179
equal deleted inserted replaced
50633:b8055b38b252 50634:c349d409262a
    33  * @run main/othervm Test
    33  * @run main/othervm Test
    34  */
    34  */
    35 
    35 
    36 public class Test {
    36 public class Test {
    37 
    37 
    38     static String expectedErrorMessage1 =
    38     // Break expected error messages into 2 parts since the loader name includes its identity
       
    39     // hash which is unique and can't be compared against.
       
    40     static String expectedErrorMessage1_part1 =
    39         "loader constraint violation in interface itable initialization for class test.C: " +
    41         "loader constraint violation in interface itable initialization for class test.C: " +
    40         "when selecting method test.I.m()Ltest/Foo; " +
    42         "when selecting method test.I.m()Ltest/Foo; " +
    41         "the class loader \"<unnamed>\" (instance of PreemptingClassLoader, " +
    43         "the class loader PreemptingClassLoader @";
    42         "child of \"app\" jdk.internal.loader.ClassLoaders$AppClassLoader) " +
    44     static String expectedErrorMessage1_part2 =
    43         "for super interface test.I, and the class loader \"app\" " +
    45         " (instance of PreemptingClassLoader, " +
       
    46         "child of 'app' jdk.internal.loader.ClassLoaders$AppClassLoader) " +
       
    47         "for super interface test.I, and the class loader 'app' " +
    44         "(instance of jdk.internal.loader.ClassLoaders$AppClassLoader) " +
    48         "(instance of jdk.internal.loader.ClassLoaders$AppClassLoader) " +
    45         "of the selected method's type, test.J have different Class objects " +
    49         "of the selected method's type, test.J have different Class objects " +
    46         "for the type test.Foo used in the signature";
    50         "for the type test.Foo used in the signature";
    47 
    51 
    48     static String expectedErrorMessage2 =
    52     static String expectedErrorMessage2_part1 =
    49         "loader constraint violation in interface itable initialization for class test.C: " +
    53         "loader constraint violation in interface itable initialization for class test.C: " +
    50         "when selecting method test.I.m()Ltest/Foo; " +
    54         "when selecting method test.I.m()Ltest/Foo; " +
    51         "the class loader \"ItableLdrCnstrnt_Test_Loader\" (instance of PreemptingClassLoader, " +
    55         "the class loader 'ItableLdrCnstrnt_Test_Loader' @";
    52         "child of \"app\" jdk.internal.loader.ClassLoaders$AppClassLoader) " +
    56     static String expectedErrorMessage2_part2 =
    53         "for super interface test.I, and the class loader \"app\" " +
    57         " (instance of PreemptingClassLoader, " +
       
    58         "child of 'app' jdk.internal.loader.ClassLoaders$AppClassLoader) " +
       
    59         "for super interface test.I, and the class loader 'app' " +
    54         "(instance of jdk.internal.loader.ClassLoaders$AppClassLoader) " +
    60         "(instance of jdk.internal.loader.ClassLoaders$AppClassLoader) " +
    55         "of the selected method's type, test.J have different Class objects " +
    61         "of the selected method's type, test.J have different Class objects " +
    56         "for the type test.Foo used in the signature";
    62         "for the type test.Foo used in the signature";
    57 
    63 
    58     // Test that the error message is correct when a loader constraint error is
    64     // Test that the error message is correct when a loader constraint error is
    61     // In this test, during itable creation for class C, method "m()LFoo;" for
    67     // In this test, during itable creation for class C, method "m()LFoo;" for
    62     // C's super interface I has a different class Foo than the selected method's
    68     // C's super interface I has a different class Foo than the selected method's
    63     // type super interface J.  The selected method is not an overpass method nor
    69     // type super interface J.  The selected method is not an overpass method nor
    64     // otherwise excluded from loader constraint checking.  So, a LinkageError
    70     // otherwise excluded from loader constraint checking.  So, a LinkageError
    65     // exception should be thrown because the loader constraint check will fail.
    71     // exception should be thrown because the loader constraint check will fail.
    66     public static void test(String loaderName, String expectedErrorMessage) throws Exception {
    72     public static void test(String loaderName,
       
    73                             String expectedErrorMessage_part1,
       
    74                             String expectedErrorMessage_part2) throws Exception {
    67         Class<?> c = test.Foo.class; // Forces standard class loader to load Foo.
    75         Class<?> c = test.Foo.class; // Forces standard class loader to load Foo.
    68         String[] classNames = {"test.Task", "test.Foo", "test.C", "test.I"};
    76         String[] classNames = {"test.Task", "test.Foo", "test.C", "test.I"};
    69         ClassLoader l = new PreemptingClassLoader(loaderName, classNames);
    77         ClassLoader l = new PreemptingClassLoader(loaderName, classNames);
    70         Runnable r = (Runnable) l.loadClass("test.Task").newInstance();
    78         Runnable r = (Runnable) l.loadClass("test.Task").newInstance();
    71         try {
    79         try {
    72             r.run();
    80             r.run();
    73             throw new RuntimeException("Expected LinkageError exception not thrown");
    81             throw new RuntimeException("Expected LinkageError exception not thrown");
    74         } catch (LinkageError e) {
    82         } catch (LinkageError e) {
    75             String errorMsg = e.getMessage();
    83             String errorMsg = e.getMessage();
    76             if (!errorMsg.equals(expectedErrorMessage)) {
    84             if (!errorMsg.contains(expectedErrorMessage_part1) ||
    77                 System.out.println("Expected: " + expectedErrorMessage + "\n" +
    85                 !errorMsg.contains(expectedErrorMessage_part2)) {
       
    86                 System.out.println("Expected: " + expectedErrorMessage_part1 + "<id>" + expectedErrorMessage_part2 + "\n" +
    78                                    "but got:  " + errorMsg);
    87                                    "but got:  " + errorMsg);
    79                 throw new RuntimeException("Wrong LinkageError exception thrown: " + errorMsg);
    88                 throw new RuntimeException("Wrong LinkageError exception thrown: " + errorMsg);
    80             }
    89             }
    81             System.out.println("Passed with message: " + errorMsg);
    90             System.out.println("Passed with message: " + errorMsg);
    82         }
    91         }
    83     }
    92     }
    84 
    93 
    85     public static void main(String... args) throws Exception {
    94     public static void main(String... args) throws Exception {
    86         test(null, expectedErrorMessage1);
    95         test(null, expectedErrorMessage1_part1, expectedErrorMessage1_part2);
    87         test("ItableLdrCnstrnt_Test_Loader", expectedErrorMessage2);
    96         test("ItableLdrCnstrnt_Test_Loader", expectedErrorMessage2_part1, expectedErrorMessage2_part2);
    88     }
    97     }
    89 }
    98 }