0N/A/*
2362N/A * Copyright (c) 1998, 2002, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @bug 4137605
0N/A * @summary When the RMIClassLoader.getClassAnnotation() is called with a
0N/A * class loaded from any URLClassLoader instance (not just those created for
0N/A * internal use by the RMI runtime), then it should return a String containing
0N/A * a space-separated list of the class loader's path of URLs.
0N/A * @author Peter Jones
0N/A *
0N/A * @library ../../../testlibrary
5551N/A * @build TestLibrary Dummy
0N/A * @run main/othervm/policy=security.policy/timeout=120 UseGetURLs
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.net.*;
0N/Aimport java.util.*;
0N/Aimport java.rmi.*;
0N/Aimport java.rmi.server.*;
0N/A
0N/Apublic class UseGetURLs {
0N/A
0N/A public static void main(String[] args) {
0N/A
0N/A System.err.println("\nRegression test for bug 4137605\n");
0N/A
0N/A TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");
0N/A System.err.println("Security manager: " +
0N/A System.getSecurityManager().getClass().getName());
0N/A
0N/A /*
0N/A * Install dummy class in first codebase to be loaded from an
0N/A * arbitrary URLClassLoader; the second codebase is used to make
0N/A * the desired annotation more interesting (more than one element).
0N/A */
0N/A URL codebase1 = null;
0N/A URL codebase2 = null;
0N/A try {
0N/A codebase1 = TestLibrary.installClassInCodebase("Dummy",
0N/A "codebase1");
0N/A
0N/A File cb2file =
0N/A new File(TestLibrary.getProperty("user.dir", "."),
0N/A "codebase2");
0N/A codebase2 = cb2file.toURL();
0N/A } catch (MalformedURLException e) {
0N/A TestLibrary.bomb("failed to install test classes", e);
0N/A }
0N/A
0N/A try {
0N/A /*
0N/A * Create an arbitary URLClassLoader for the two codebases.
0N/A */
0N/A ClassLoader loader = URLClassLoader.newInstance(
0N/A new URL[] { codebase1, codebase2 });
0N/A System.err.println(
0N/A "URLs for class loader: " +
0N/A Arrays.asList(((URLClassLoader) loader).getURLs()));
0N/A System.err.println("Expecting annotation: \"" +
0N/A codebase1 + " " + codebase2 + "\"");
0N/A System.err.println("First URL:");
0N/A dumpURL(codebase1);
0N/A System.err.println("Second URL:");
0N/A dumpURL(codebase2);
0N/A
0N/A /*
0N/A * Load dummy class from the loader, get the annotation string,
0N/A * and verify that it is correct.
0N/A */
0N/A Class cl = loader.loadClass("Dummy");
0N/A String annotation = RMIClassLoader.getClassAnnotation(cl);
0N/A System.err.println("Received annotation: \"" +
0N/A annotation + "\"");
0N/A
0N/A if (annotation == null) {
0N/A throw new RuntimeException("annotation was null");
0N/A }
0N/A URL[] urls = pathToURLs(annotation);
0N/A System.err.println(
0N/A "URLs from annotation: " + Arrays.asList(urls));
0N/A if (urls.length != 2) {
0N/A throw new RuntimeException(
0N/A "wrong number of elements in annotation");
0N/A }
0N/A if (!urls[0].equals(codebase1)) {
0N/A System.err.println("First URL in annotation is incorrect:");
0N/A dumpURL(urls[0]);
0N/A throw new RuntimeException(
0N/A "first URL in annotation is incorrect");
0N/A }
0N/A if (!urls[1].equals(codebase2)) {
0N/A System.err.println("Second URL in annotation is incorrect:");
0N/A dumpURL(urls[1]);
0N/A throw new RuntimeException(
0N/A "second URL in annotation is incorrect");
0N/A }
0N/A
0N/A System.err.println("TEST PASSED: annotation matched codebase");
0N/A } catch (Exception e) {
0N/A TestLibrary.bomb(e.getMessage(), e);
0N/A }
0N/A }
0N/A
0N/A private static URL[] pathToURLs(String path)
0N/A throws MalformedURLException
0N/A {
0N/A StringTokenizer st = new StringTokenizer(path); // divide by spaces
0N/A URL[] urls = new URL[st.countTokens()];
0N/A for (int i = 0; st.hasMoreTokens(); i++) {
0N/A urls[i] = new URL(st.nextToken());
0N/A }
0N/A return urls;
0N/A }
0N/A
0N/A private static void dumpURL(URL u) {
0N/A System.err.println("\tprotocol: " + u.getProtocol());
0N/A System.err.println("\tauthority: " + u.getAuthority());
0N/A System.err.println("\tuser info: " + u.getUserInfo());
0N/A System.err.println("\thost: " + u.getHost());
0N/A System.err.println("\tport: " + u.getPort());
0N/A System.err.println("\tpath: " + u.getPath());
0N/A System.err.println("\tfile: " + u.getFile());
0N/A System.err.println("\tquery: " + u.getQuery());
0N/A System.err.println("\tref: " + u.getRef());
0N/A }
0N/A}