1 /* 2 * Copyright (c) 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 * @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