blob: 95089e20bad770223f8508edf217341f321d4366 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1999-2004 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/**
25 * @test
26 * @bug 4254808
27 * @summary Naming assumes '/' is present in relative URL; change in URL causes regression
28 * @author Dana Burns
29 * @library ../../testlibrary
30 * @build TestLibrary
31 * @build Legal LegalRegistryNames LegalRegistryNames_Stub
32 * @run main LegalRegistryNames
33 */
34
35import java.net.InetAddress;
36import java.net.UnknownHostException;
37import java.rmi.Naming;
38import java.rmi.RemoteException;
39import java.rmi.Remote;
40import java.rmi.registry.LocateRegistry;
41import java.rmi.registry.Registry;
42import java.rmi.server.UnicastRemoteObject;
43import java.util.Enumeration;
44import java.util.Vector;
45
46/**
47 * Ensure that all legal forms of Naming URLs operate with the
48 * java.rmi.Naming interface
49 */
50public class LegalRegistryNames extends UnicastRemoteObject
51 implements Legal
52{
53
54 public LegalRegistryNames() throws java.rmi.RemoteException {}
55
56 public static void main(String args[]) throws RuntimeException {
57
58 System.err.println("\nRegression test for bug/rfe 4254808\n");
59
60 Registry registry = null;
61 LegalRegistryNames legal = null;
62
63 boolean oneFormFailed = false;
64 String[] names = null;
65 Vector legalForms = getLegalForms();
66 Remote shouldFind = null;
67
68 // create a registry and the test object
69 try {
70 legal = new LegalRegistryNames();
71
72 System.err.println("Starting registry on default port");
73 registry = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
74 } catch (Exception e) {
75 TestLibrary.bomb("registry already running on test port");
76 }
77
78 // enumerate through all legal URLs to verify that a remote
79 // object can be bound and unbound
80 String s = null;
81 Enumeration en = legalForms.elements();
82 while (en.hasMoreElements()) {
83 s = (String) en.nextElement();
84
85 System.err.println("\ntesting form: " + s);
86
87 try {
88 Naming.rebind(s, legal);
89 names = registry.list();
90
91 // ensure that the name in the registry is what is expected
92 if ((names.length > 0) &&
93 (names[0].compareTo("MyName") != 0))
94 {
95 oneFormFailed = true;
96 System.err.println("\tRegistry entry for form: " +
97 s + " is incorrect: " + names[0]);
98 }
99
100 // ensure that the object can be unbound under the URL string
101 shouldFind = Naming.lookup(s);
102 Naming.unbind(s);
103 System.err.println("\tform " + s + " OK");
104
105 } catch (Exception e) {
106
107 e.printStackTrace();
108 oneFormFailed = true;
109 System.err.println("\tunexpected lookup or unbind " +
110 "exception for form: " + s + e.getMessage() );
111 }
112 }
113 if (oneFormFailed) {
114 TestLibrary.bomb("Test failed");
115 }
116
117 // get the test to exit quickly
118 TestLibrary.unexport(legal);
119 }
120
121 /**
122 * return a vector of valid legal RMI naming URLs.
123 */
124 private static Vector getLegalForms() {
125 String localHostAddress = null;
126 String localHostName = null;
127
128 // get the local host name and address
129 try {
130 localHostName = InetAddress.getLocalHost().getHostName();
131 localHostAddress = InetAddress.getLocalHost().getHostAddress();
132 } catch(UnknownHostException e) {
133 TestLibrary.bomb("Test failed: unexpected exception", e);
134 }
135
136 Vector legalForms = new Vector();
137 legalForms.add("///MyName");
138 legalForms.add("//:" + Registry.REGISTRY_PORT + "/MyName");
139 legalForms.add("//" + localHostAddress + "/MyName");
140 legalForms.add("//" + localHostAddress + ":" +
141 Registry.REGISTRY_PORT + "/MyName");
142 legalForms.add("//localhost/MyName");
143 legalForms.add("//localhost:" + Registry.REGISTRY_PORT + "/MyName");
144 legalForms.add("//" + localHostName + "/MyName");
145 legalForms.add("//" + localHostName + ":" + Registry.REGISTRY_PORT +
146 "/MyName");
147 legalForms.add("MyName");
148 legalForms.add("/MyName");
149 legalForms.add("rmi:///MyName");
150 legalForms.add("rmi://:" + Registry.REGISTRY_PORT + "/MyName");
151 legalForms.add("rmi://" + localHostAddress + "/MyName");
152 legalForms.add("rmi://" + localHostAddress + ":" +
153 Registry.REGISTRY_PORT + "/MyName");
154 legalForms.add("rmi://localhost/MyName");
155 legalForms.add("rmi://localhost:" + Registry.REGISTRY_PORT + "/MyName");
156 legalForms.add("rmi://" + localHostName + "/MyName");
157 legalForms.add("rmi://" + localHostName + ":" +
158 Registry.REGISTRY_PORT + "/MyName");
159 return legalForms;
160 }
161}