Path: blob/master/test/hotspot/jtreg/runtime/LoaderConstraints/duplicateLE/Test.java
40948 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. A class loader loads27* twice the same class. Should trigger exception in28* SystemDictionary::check_constraints().29* @compile ../common/Foo.java30* @compile ../common/J.java31* ../common/PreemptingClassLoader.java32* @run main/othervm Test33*/3435public class Test {3637// Check that all names have external formatting ('.' and not '/' in package names).38// Check for parent of class loader.39// Break each expectedErrorMessage into 2 parts due to the class loader name containing40// the unique @<id> identity hash which cannot be compared against.41static String expectedErrorMessage1_part1 = "loader PreemptingClassLoader @";42static String expectedErrorMessage1_part2 = " attempted duplicate class definition for test.Foo. (test.Foo is in unnamed module of loader PreemptingClassLoader @";43static String expectedErrorMessage1_part3 = ", parent loader 'app')";4445// Check that all names have external formatting ('.' and not '/' in package names).46// Check for name and parent of class loader.47static String expectedErrorMessage2_part1 = "loader 'DuplicateLE_Test_Loader' @";48static String expectedErrorMessage2_part2 = " attempted duplicate class definition for test.Foo. (test.Foo is in unnamed module of loader 'DuplicateLE_Test_Loader' @";49static String expectedErrorMessage2_part3 = ", parent loader 'app')";5051// Check that all names have external formatting ('.' and not '/' in package names).52// Check for name and parent of class loader. Type should be mentioned as 'interface'.53static String expectedErrorMessage3_part1 = "loader 'DuplicateLE_Test_Loader_IF' @";54static String expectedErrorMessage3_part2 = " attempted duplicate interface definition for test.J. (test.J is in unnamed module of loader 'DuplicateLE_Test_Loader_IF' @";55static String expectedErrorMessage3_part3 = ", parent loader 'app')";565758// Test that the error message is correct when a loader constraint error is59// detected during vtable creation.60//61// In this test, during vtable creation for class Task, method "Task.m()LFoo;"62// overrides "J.m()LFoo;". But, Task's class Foo and super type J's class Foo63// are different. So, a LinkageError exception should be thrown because the64// loader constraint check will fail.65public static void test(String loaderName,66String expectedErrorMessage_part1,67String expectedErrorMessage_part2,68String expectedErrorMessage_part3,69String testType) throws Exception {70String[] classNames = {testType};71ClassLoader l = new PreemptingClassLoader(loaderName, classNames, false);72l.loadClass(testType);73try {74l.loadClass(testType).newInstance();75throw new RuntimeException("Expected LinkageError exception not thrown");76} catch (LinkageError e) {77String errorMsg = e.getMessage();78if (!errorMsg.contains(expectedErrorMessage_part1) ||79!errorMsg.contains(expectedErrorMessage_part2) ||80!errorMsg.contains(expectedErrorMessage_part3)) {81System.out.println("Expected: " + expectedErrorMessage_part1 + "<id>" + expectedErrorMessage_part2 + "\n" +82"but got: " + errorMsg);83throw new RuntimeException("Wrong LinkageError exception thrown: " + errorMsg);84}85System.out.println("Passed with message: " + errorMsg);86}87}8889public static void main(String args[]) throws Exception {90test(null, expectedErrorMessage1_part1, expectedErrorMessage1_part2,91expectedErrorMessage1_part3, "test.Foo");92test("DuplicateLE_Test_Loader", expectedErrorMessage2_part1, expectedErrorMessage2_part2,93expectedErrorMessage2_part3, "test.Foo");94test("DuplicateLE_Test_Loader_IF", expectedErrorMessage3_part1, expectedErrorMessage3_part2,95expectedErrorMessage3_part3, "test.J");96}97}9899100