1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * Copyright (c) 2018 SAP SE. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 */ 24 25 /** 26 * @test 27 * @bug 8204955 28 * @summary Test extended ClassCastException message. 29 * @library /test/lib 30 * @compile fixture/EmptyInterface.java 31 * @compile fixture/ReallyEmptyInterface.java 32 * @compile fixture/ClassWithEmptyInterface.java 33 * @compile fixture/ClassWithReallyEmptyInterface.java 34 * @compile fixture/ClassExtendingClassWithEmptyInterface.java 35 * @compile CloneClassLoader.java 36 * @compile ClassCastExceptionTest.java 37 * @run main ClassCastExceptionTest 38 */ 39 40 import jdk.test.lib.Asserts; 41 42 import fixture.*; 43 44 public class ClassCastExceptionTest { 45 46 private static void fail () throws Exception { 47 throw new RuntimeException("Class cast expected to fail!"); 48 } 49 50 public static void main(String[] args) throws Exception { 51 52 // Test case for casting "non-related" types. 53 54 try { 55 String s = (String) new Object(); 56 fail(); 57 } catch (ClassCastException e) { 58 Asserts.assertEQ("java.base/java.lang.Object cannot be cast to java.base/java.lang.String", e.getMessage()); 59 } 60 61 CloneClassLoader cl1 = new CloneClassLoader(ClassCastExceptionTest.class.getClassLoader()); 62 String own_class_loader_class_name = ClassCastExceptionTest.class.getClassLoader().getClass().getName(); 63 64 65 // Test case for casting the caster class to self, which was loaded 66 // by a different class loader. 67 68 try { 69 fixture.ClassWithEmptyInterface i = (fixture.ClassWithEmptyInterface) 70 cl1.loadClass(fixture.ClassWithEmptyInterface.class.getName()).newInstance(); 71 fail(); 72 } catch (ClassCastException e) { 73 Asserts.assertEQ( 74 "Clone//fixture.ClassWithEmptyInterface cannot be cast to fixture.ClassWithEmptyInterface. " + 75 "Loaded by CloneClassLoader, but needed class loader " + own_class_loader_class_name + ".", 76 e.getMessage()); 77 } 78 79 80 // Test case for casting the caster class to the implemented interface (EmptyInterface), which was loaded 81 // by a different class loader. 82 83 try { 84 fixture.EmptyInterface i = (fixture.EmptyInterface) 85 cl1.loadClass(fixture.ClassWithEmptyInterface.class.getName()).newInstance(); 86 fail(); 87 } catch (ClassCastException e) { 88 Asserts.assertEQ( 89 "Clone//fixture.ClassWithEmptyInterface cannot be cast to fixture.EmptyInterface. " + 90 "Found matching interface Clone//fixture.EmptyInterface loaded by CloneClassLoader, " + 91 "but needed class loader " + own_class_loader_class_name + ".", 92 e.getMessage() 93 ); 94 } 95 96 97 // Test case for casting the caster class to the implemented interface (ReallyEmptyInterface -> EmptyInterface), 98 // which was loaded by a different class loader. 99 100 try { 101 fixture.EmptyInterface i = (fixture.EmptyInterface) 102 cl1.loadClass(fixture.ClassWithReallyEmptyInterface.class.getName()).newInstance(); 103 fail(); 104 } catch (ClassCastException e) { 105 Asserts.assertEQ( 106 "Clone//fixture.ClassWithReallyEmptyInterface cannot be cast to fixture.EmptyInterface. " + 107 "Found matching interface Clone//fixture.EmptyInterface loaded by CloneClassLoader, " + 108 "but needed class loader " + own_class_loader_class_name + ".", 109 e.getMessage() 110 ); 111 } 112 113 114 // Test case for casting the caster class to the implemented interface of the super class (ClassWithEmptyInterface), 115 // which was loaded by a different class loader. 116 117 try { 118 fixture.ClassWithEmptyInterface c = (fixture.ClassWithEmptyInterface) 119 cl1.loadClass(fixture.ClassExtendingClassWithEmptyInterface.class.getName()).newInstance(); 120 fail(); 121 } catch (ClassCastException e) { 122 Asserts.assertEQ( 123 "Clone//fixture.ClassExtendingClassWithEmptyInterface cannot be cast to fixture.ClassWithEmptyInterface. " + 124 "Found matching super class Clone//fixture.ClassWithEmptyInterface loaded by CloneClassLoader, " + 125 "but needed class loader " + own_class_loader_class_name + ".", 126 e.getMessage() 127 ); 128 } 129 } 130 }