blob: c8592bfcb3ea0ac22efabbb66cd4f61aeca404b8 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1998-2002 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 4137605
26 * @summary When the RMIClassLoader.getClassAnnotation() is called with a
27 * class loaded from any URLClassLoader instance (not just those created for
28 * internal use by the RMI runtime), then it should return a String containing
29 * a space-separated list of the class loader's path of URLs.
30 * @author Peter Jones
31 *
32 * @library ../../../testlibrary
33 * @build TestLibrary
34 * @build UseGetURLs Dummy
35 * @run main/othervm/policy=security.policy/timeout=120 UseGetURLs
36 */
37
38import java.io.*;
39import java.net.*;
40import java.util.*;
41import java.rmi.*;
42import java.rmi.server.*;
43
44public class UseGetURLs {
45
46 public static void main(String[] args) {
47
48 System.err.println("\nRegression test for bug 4137605\n");
49
50 TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");
51 System.err.println("Security manager: " +
52 System.getSecurityManager().getClass().getName());
53
54 /*
55 * Install dummy class in first codebase to be loaded from an
56 * arbitrary URLClassLoader; the second codebase is used to make
57 * the desired annotation more interesting (more than one element).
58 */
59 URL codebase1 = null;
60 URL codebase2 = null;
61 try {
62 codebase1 = TestLibrary.installClassInCodebase("Dummy",
63 "codebase1");
64
65 File cb2file =
66 new File(TestLibrary.getProperty("user.dir", "."),
67 "codebase2");
68 codebase2 = cb2file.toURL();
69 } catch (MalformedURLException e) {
70 TestLibrary.bomb("failed to install test classes", e);
71 }
72
73 try {
74 /*
75 * Create an arbitary URLClassLoader for the two codebases.
76 */
77 ClassLoader loader = URLClassLoader.newInstance(
78 new URL[] { codebase1, codebase2 });
79 System.err.println(
80 "URLs for class loader: " +
81 Arrays.asList(((URLClassLoader) loader).getURLs()));
82 System.err.println("Expecting annotation: \"" +
83 codebase1 + " " + codebase2 + "\"");
84 System.err.println("First URL:");
85 dumpURL(codebase1);
86 System.err.println("Second URL:");
87 dumpURL(codebase2);
88
89 /*
90 * Load dummy class from the loader, get the annotation string,
91 * and verify that it is correct.
92 */
93 Class cl = loader.loadClass("Dummy");
94 String annotation = RMIClassLoader.getClassAnnotation(cl);
95 System.err.println("Received annotation: \"" +
96 annotation + "\"");
97
98 if (annotation == null) {
99 throw new RuntimeException("annotation was null");
100 }
101 URL[] urls = pathToURLs(annotation);
102 System.err.println(
103 "URLs from annotation: " + Arrays.asList(urls));
104 if (urls.length != 2) {
105 throw new RuntimeException(
106 "wrong number of elements in annotation");
107 }
108 if (!urls[0].equals(codebase1)) {
109 System.err.println("First URL in annotation is incorrect:");
110 dumpURL(urls[0]);
111 throw new RuntimeException(
112 "first URL in annotation is incorrect");
113 }
114 if (!urls[1].equals(codebase2)) {
115 System.err.println("Second URL in annotation is incorrect:");
116 dumpURL(urls[1]);
117 throw new RuntimeException(
118 "second URL in annotation is incorrect");
119 }
120
121 System.err.println("TEST PASSED: annotation matched codebase");
122 } catch (Exception e) {
123 TestLibrary.bomb(e.getMessage(), e);
124 }
125 }
126
127 private static URL[] pathToURLs(String path)
128 throws MalformedURLException
129 {
130 StringTokenizer st = new StringTokenizer(path); // divide by spaces
131 URL[] urls = new URL[st.countTokens()];
132 for (int i = 0; st.hasMoreTokens(); i++) {
133 urls[i] = new URL(st.nextToken());
134 }
135 return urls;
136 }
137
138 private static void dumpURL(URL u) {
139 System.err.println("\tprotocol: " + u.getProtocol());
140 System.err.println("\tauthority: " + u.getAuthority());
141 System.err.println("\tuser info: " + u.getUserInfo());
142 System.err.println("\thost: " + u.getHost());
143 System.err.println("\tport: " + u.getPort());
144 System.err.println("\tpath: " + u.getPath());
145 System.err.println("\tfile: " + u.getFile());
146 System.err.println("\tquery: " + u.getQuery());
147 System.err.println("\tref: " + u.getRef());
148 }
149}