23 24 /* 25 * @test 26 * @compile ../common/Foo.java 27 * ../common/PreemptingClassLoader.java 28 * @run main/othervm Test 29 * @run main/othervm -Xint Test 30 * @run main/othervm -Xcomp Test 31 * @run main/othervm -XX:+TieredCompilation -XX:TieredStopAtLevel=1 Test 32 * @run main/othervm -XX:-TieredCompilation Test 33 */ 34 35 public class Test { 36 37 // Check that all names have external formatting ('.' and not '/' in package names). 38 // Check for parent of class loader. 39 static String expectedErrorMessage1 = 40 "loader instance of PreemptingClassLoader (parent: \"app\" " + 41 "jdk.internal.loader.ClassLoaders$AppClassLoader) attempted duplicate class definition for test.Foo."; 42 43 // Test that the error message is correct when a loader constraint error is 44 // detected during vtable creation. 45 // 46 // In this test, during vtable creation for class Task, method "Task.m()LFoo;" 47 // overrides "J.m()LFoo;". But, Task's class Foo and super type J's class Foo 48 // are different. So, a LinkageError exception should be thrown because the 49 // loader constraint check will fail. 50 public static void test1() throws Exception { 51 String[] classNames = {"test.Foo"}; 52 ClassLoader l = new PreemptingClassLoader(null, classNames, false); 53 l.loadClass("test.Foo"); 54 try { 55 l.loadClass("test.Foo").newInstance(); 56 throw new RuntimeException("Expected LinkageError exception not thrown"); 57 } catch (LinkageError e) { 58 String errorMsg = e.getMessage(); 59 if (!errorMsg.equals(expectedErrorMessage1)) { 60 System.out.println("Expected: " + expectedErrorMessage1 + "\n" + 61 "but got: " + errorMsg); 62 throw new RuntimeException("Wrong LinkageError exception thrown: " + errorMsg); 63 } 64 System.out.println("Passed with message: " + errorMsg); 65 } 66 } 67 68 // Check that all names have external formatting ('.' and not '/' in package names). 69 // Check for name and parent of class loader. 70 static String expectedErrorMessage2 = 71 "loader \"DuplicateLE_Test_Loader\" (instance of PreemptingClassLoader, parent: \"app\" " + 72 "jdk.internal.loader.ClassLoaders$AppClassLoader) attempted duplicate class definition for test.Foo."; 73 74 // Same as test1, but ClassLoader has a name. 75 public static void test2() throws Exception { 76 String[] classNames = {"test.Foo"}; 77 ClassLoader l = new PreemptingClassLoader("DuplicateLE_Test_Loader", classNames, false); 78 l.loadClass("test.Foo"); 79 try { 80 l.loadClass("test.Foo"); 81 throw new RuntimeException("Expected LinkageError exception not thrown"); 82 } catch (LinkageError e) { 83 String errorMsg = e.getMessage(); 84 if (!errorMsg.equals(expectedErrorMessage2)) { 85 System.out.println("Expected: " + expectedErrorMessage2 + "\n" + 86 "but got: " + errorMsg); 87 throw new RuntimeException("Wrong LinkageError exception thrown: " + errorMsg); 88 } 89 System.out.println("Passed with message: " + errorMsg); 90 } 91 } 92 93 public static void main(String args[]) throws Exception { 94 test1(); 95 test2(); 96 } 97 } 98 | 23 24 /* 25 * @test 26 * @compile ../common/Foo.java 27 * ../common/PreemptingClassLoader.java 28 * @run main/othervm Test 29 * @run main/othervm -Xint Test 30 * @run main/othervm -Xcomp Test 31 * @run main/othervm -XX:+TieredCompilation -XX:TieredStopAtLevel=1 Test 32 * @run main/othervm -XX:-TieredCompilation Test 33 */ 34 35 public class Test { 36 37 // Check that all names have external formatting ('.' and not '/' in package names). 38 // Check for parent of class loader. 39 static String expectedErrorMessage1 = 40 "loader instance of PreemptingClassLoader (parent: \"app\" " + 41 "jdk.internal.loader.ClassLoaders$AppClassLoader) attempted duplicate class definition for test.Foo."; 42 43 // Check that all names have external formatting ('.' and not '/' in package names). 44 // Check for name and parent of class loader. 45 static String expectedErrorMessage2 = 46 "loader \"DuplicateLE_Test_Loader\" (instance of PreemptingClassLoader, parent: \"app\" " + 47 "jdk.internal.loader.ClassLoaders$AppClassLoader) attempted duplicate class definition for test.Foo."; 48 49 // Test that the error message is correct when a loader constraint error is 50 // detected during vtable creation. 51 // 52 // In this test, during vtable creation for class Task, method "Task.m()LFoo;" 53 // overrides "J.m()LFoo;". But, Task's class Foo and super type J's class Foo 54 // are different. So, a LinkageError exception should be thrown because the 55 // loader constraint check will fail. 56 public static void test(String loaderName, String expectedErrorMessage) throws Exception { 57 String[] classNames = {"test.Foo"}; 58 ClassLoader l = new PreemptingClassLoader(loaderName, classNames, false); 59 l.loadClass("test.Foo"); 60 try { 61 l.loadClass("test.Foo").newInstance(); 62 throw new RuntimeException("Expected LinkageError exception not thrown"); 63 } catch (LinkageError e) { 64 String errorMsg = e.getMessage(); 65 if (!errorMsg.equals(expectedErrorMessage)) { 66 System.out.println("Expected: " + expectedErrorMessage + "\n" + 67 "but got: " + errorMsg); 68 throw new RuntimeException("Wrong LinkageError exception thrown: " + errorMsg); 69 } 70 System.out.println("Passed with message: " + errorMsg); 71 } 72 } 73 74 public static void main(String args[]) throws Exception { 75 test(null, expectedErrorMessage1); 76 test("DuplicateLE_Test_Loader", expectedErrorMessage2); 77 } 78 } 79 |