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 \"<unnamed>\" (instance of PreemptingClassLoader, " +
  41         "child of \"app\" jdk.internal.loader.ClassLoaders$AppClassLoader) " +
  42         "attempted duplicate class definition for test.Foo.";
  43 
  44     // Check that all names have external formatting ('.' and not '/' in package names).
  45     // Check for name and parent of class loader.
  46     static String expectedErrorMessage2 =
  47         "loader \"DuplicateLE_Test_Loader\" (instance of PreemptingClassLoader, " +
  48         "child of \"app\" jdk.internal.loader.ClassLoaders$AppClassLoader) " +
  49         "attempted duplicate class definition for test.Foo.";
  50 
  51     // Test that the error message is correct when a loader constraint error is
  52     // 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 Foo
  56     // are different.  So, a LinkageError exception should be thrown because the
  57     // loader constraint check will fail.
  58     public static void test(String loaderName, String expectedErrorMessage) throws Exception {
  59         String[] classNames = {"test.Foo"};
  60         ClassLoader l = new PreemptingClassLoader(loaderName, classNames, false);
  61         l.loadClass("test.Foo");
  62         try {
  63             l.loadClass("test.Foo").newInstance();
  64             throw new RuntimeException("Expected LinkageError exception not thrown");
  65         } catch (LinkageError e) {
  66             String errorMsg = e.getMessage();
  67             if (!errorMsg.equals(expectedErrorMessage)) {
  68                 System.out.println("Expected: " + expectedErrorMessage + "\n" +
  69                                    "but got:  " + errorMsg);
  70                 throw new RuntimeException("Wrong LinkageError exception thrown: " + errorMsg);
  71             }
  72             System.out.println("Passed with message: " + errorMsg);
  73         }
  74     }
  75 
  76     public static void main(String args[]) throws Exception {
  77         test(null, expectedErrorMessage1);
  78         test("DuplicateLE_Test_Loader", expectedErrorMessage2);
  79     }
  80 }
  81