1/* 2 * Copyright (c) 1999, 2012, 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/* @test 25 * @bug 4240710 26 * @summary RMIClassLoader.getClassLoader for a codebase should return the 27 * same class loader that is used to load classes from that codebase (using 28 * RMIClassLoader.loadClass). 29 * @author Ann Wollrath 30 * 31 * @library ../../../testlibrary 32 * @modules java.rmi/sun.rmi.registry 33 * java.rmi/sun.rmi.server 34 * java.rmi/sun.rmi.transport 35 * java.rmi/sun.rmi.transport.tcp 36 * @build TestLibrary Foo 37 * @run main/othervm/policy=security.policy GetClassLoader 38 */ 39 40import java.net.*; 41import java.rmi.*; 42import java.rmi.server.*; 43 44public class GetClassLoader 45{ 46 47 public static void main(String[] args) { 48 49 System.err.println("\nTest for rfe 4240710\n"); 50 51 URL codebase1 = null; 52 Class cl = null; 53 ClassLoader loader = null; 54 55 TestLibrary.suggestSecurityManager(null); 56 57 try { 58 codebase1 = 59 TestLibrary.installClassInCodebase("Foo", "codebase1"); 60 } catch (MalformedURLException e) { 61 TestLibrary.bomb(e); 62 } 63 64 65 /* 66 * Force SecurityException for attempt to obtain a class loader 67 * for a codebase that the test does not have permission to read from. 68 */ 69 try { 70 System.err.println( 71 "getClassLoader for codebase that I can't read"); 72 loader = RMIClassLoader.getClassLoader("file:/foo"); 73 TestLibrary.bomb( 74 "Failed: no SecurityException obtaining loader"); 75 } catch (MalformedURLException e) { 76 System.err.println( 77 "Failed: MalformedURLException getting loader"); 78 TestLibrary.bomb(e); 79 } catch (SecurityException e) { 80 System.err.println( 81 "Passed: SecurityException obtaining loader"); 82 } 83 84 /* 85 * Verify that a specific class loaded by name via 86 * RMIClassLoader.loadClass is the same as the class obtained by 87 * loading that class by name through the loader obtained using 88 * the RMIClassLoader.getClassLoader method. 89 */ 90 System.err.println("load Foo from codebase1"); 91 try { 92 cl = RMIClassLoader.loadClass(codebase1.toString(), "Foo"); 93 } catch (Exception e) { 94 System.err.println( 95 "Failed: exception loading class from codebase1"); 96 TestLibrary.bomb(e); 97 } 98 99 System.err.println( 100 "load Foo using loader obtained using getClassLoader"); 101 try { 102 loader = RMIClassLoader.getClassLoader(codebase1.toString()); 103 } catch (MalformedURLException e) { 104 System.err.println( 105 "Failed: MalformedURLException getting codebase1 loader"); 106 TestLibrary.bomb(e); 107 } 108 109 try { 110 if (cl == loader.loadClass("Foo")) { 111 System.err.println("Passed: Foo classes are equal"); 112 } else { 113 TestLibrary.bomb("Failed: Foo classes are not equal"); 114 } 115 } catch (Exception e) { 116 System.err.println( 117 "Failed: exception loading class from codebase1"); 118 TestLibrary.bomb(e); 119 } 120 121 /* 122 * Force MalformedURLException by passing bogus URL to 123 * getClassLoader. 124 */ 125 try { 126 loader = RMIClassLoader.getClassLoader("malformed:///URL"); 127 TestLibrary.bomb( 128 "Failed: getClassLoader should throw MalformedURLException"); 129 } catch (MalformedURLException e) { 130 System.err.println( 131 "Passed: getClassLoader threw MalformedURLException"); 132 } 133 } 134} 135