blob: fd7aff8ee5919994feb3df056fdb43fd45a4df42 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1999 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any 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 * @build TestLibrary
33 * @build GetClassLoader
34 * @build Foo
35 * @run main/othervm/policy=security.policy GetClassLoader
36 */
37
38import java.net.*;
39import java.rmi.*;
40import java.rmi.server.*;
41
42public class GetClassLoader
43{
44
45 public static void main(String[] args) {
46
47 System.err.println("\nTest for rfe 4240710\n");
48
49 URL codebase1 = null;
50 Class cl = null;
51 ClassLoader loader = null;
52
53 TestLibrary.suggestSecurityManager(null);
54
55 try {
56 codebase1 =
57 TestLibrary.installClassInCodebase("Foo", "codebase1");
58 } catch (MalformedURLException e) {
59 TestLibrary.bomb(e);
60 }
61
62
63 /*
64 * Force SecurityException for attempt to obtain a class loader
65 * for a codebase that the test does not have permission to read from.
66 */
67 try {
68 System.err.println(
69 "getClassLoader for codebase that I can't read");
70 loader = RMIClassLoader.getClassLoader("file:/foo");
71 TestLibrary.bomb(
72 "Failed: no SecurityException obtaining loader");
73 } catch (MalformedURLException e) {
74 System.err.println(
75 "Failed: MalformedURLException getting loader");
76 TestLibrary.bomb(e);
77 } catch (SecurityException e) {
78 System.err.println(
79 "Passed: SecurityException obtaining loader");
80 }
81
82 /*
83 * Verify that a specific class loaded by name via
84 * RMIClassLoader.loadClass is the same as the class obtained by
85 * loading that class by name through the loader obtained using
86 * the RMIClassLoader.getClassLoader method.
87 */
88 System.err.println("load Foo from codebase1");
89 try {
90 cl = RMIClassLoader.loadClass(codebase1.toString(), "Foo");
91 } catch (Exception e) {
92 System.err.println(
93 "Failed: exception loading class from codebase1");
94 TestLibrary.bomb(e);
95 }
96
97 System.err.println(
98 "load Foo using loader obtained using getClassLoader");
99 try {
100 loader = RMIClassLoader.getClassLoader(codebase1.toString());
101 } catch (MalformedURLException e) {
102 System.err.println(
103 "Failed: MalformedURLException getting codebase1 loader");
104 TestLibrary.bomb(e);
105 }
106
107 try {
108 if (cl == loader.loadClass("Foo")) {
109 System.err.println("Passed: Foo classes are equal");
110 } else {
111 TestLibrary.bomb("Failed: Foo classes are not equal");
112 }
113 } catch (Exception e) {
114 System.err.println(
115 "Failed: exception loading class from codebase1");
116 TestLibrary.bomb(e);
117 }
118
119 /*
120 * Force MalformedURLException by passing bogus URL to
121 * getClassLoader.
122 */
123 try {
124 loader = RMIClassLoader.getClassLoader("malformed:///URL");
125 TestLibrary.bomb(
126 "Failed: getClassLoader should throw MalformedURLException");
127 } catch (MalformedURLException e) {
128 System.err.println(
129 "Passed: getClassLoader threw MalformedURLException");
130 }
131 }
132}