Path: blob/master/test/hotspot/jtreg/runtime/LoaderConstraints/duplicateParentLE/Test.java
41153 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 819985226* @summary Test exception messages of LinkageError with a parent class loader27* that is not known to the VM. A class loader loads28* twice the same class. Should trigger exception in29* SystemDictionary::check_constraints().30* @compile ../common/Foo.java31* @compile ../common/J.java32* @compile ParentClassLoader.java33* PreemptingChildClassLoader.java34* @run main/othervm Test35*/3637public class Test {3839// Check that all names have external formatting ('.' and not '/' in package names).40// Check for parent of class loader.41// Break expectedErrorMessage into 2 parts due to the class loader name containing42// the unique @<id> identity hash which cannot be compared against.4344// Check that all names have external formatting ('.' and not '/' in package names).45// Check for name and parent of class loader. Type should be mentioned as 'class'.46static String expectedErrorMessage_part1 = "loader 'DuplicateParentLE_Test_Loader' @";47static String expectedErrorMessage_part2 = " attempted duplicate class definition for test.Foo. (test.Foo is in unnamed module of loader 'DuplicateParentLE_Test_Loader' @";48static String expectedErrorMessage_part3 = ", parent loader ParentClassLoader @";495051// Test that the error message is correct when a loader constraint error is52// detected during vtable creation.53//54// In this test, during vtable creation for class Task, method "Task.m()LFoo;"55// overrides "J.m()LFoo;". But, Task's class Foo and super type J's class Foo56// are different. So, a LinkageError exception should be thrown because the57// loader constraint check will fail.58public static void test(String loaderName,59String testType) throws Exception {60String[] classNames = {testType};61ClassLoader l = new PreemptingChildClassLoader(loaderName, classNames, false);62l.loadClass(testType);63try {64l.loadClass(testType).newInstance();65throw new RuntimeException("Expected LinkageError exception not thrown");66} catch (LinkageError e) {67String errorMsg = e.getMessage();68if (!errorMsg.contains(expectedErrorMessage_part1) ||69!errorMsg.contains(expectedErrorMessage_part2) ||70!errorMsg.contains(expectedErrorMessage_part3)) {71System.out.println("Expected: " + expectedErrorMessage_part1 + "<id>" + expectedErrorMessage_part2 + "\n" +72"but got: " + errorMsg);73throw new RuntimeException("Wrong LinkageError exception thrown: " + errorMsg);74}75System.out.println("Passed with message: " + errorMsg);76}77}7879public static void main(String args[]) throws Exception {80test("DuplicateParentLE_Test_Loader", "test.Foo");81}82}838485