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     // 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