001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: *
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: /**
020: * @author Mikhail A. Markov, Vasily Zakharov
021: * @version $Revision: 1.1.2.2 $
022: */package org.apache.harmony.rmi;
023:
024: import java.net.InetAddress;
025:
026: import java.rmi.Naming;
027: import java.rmi.RMISecurityManager;
028: import java.rmi.Remote;
029:
030: import java.rmi.registry.LocateRegistry;
031: import java.rmi.registry.Registry;
032:
033: import java.rmi.server.UnicastRemoteObject;
034:
035: import org.apache.harmony.rmi.test.MyRemoteObject1;
036:
037: import junit.framework.Test;
038: import junit.framework.TestSuite;
039:
040: /**
041: * Unit test for RMI Registry.
042: *
043: * @author Mikhail A. Markov, Vasily Zakharov
044: * @version $Revision: 1.1.2.2 $
045: */
046: public class RegistryTest extends RMITestBase {
047:
048: /**
049: * No-arg constructor to enable serialization.
050: */
051: public RegistryTest() {
052: super ();
053: }
054:
055: /**
056: * Constructs this test case with the given name.
057: *
058: * @param name
059: * Name for this test case.
060: */
061: public RegistryTest(String name) {
062: super (name);
063: }
064:
065: /**
066: * Tests registry creation and destruction.
067: *
068: * @throws Exception
069: * If some error occurs.
070: */
071: public void testBasic() throws Exception {
072: System.setSecurityManager(new RMISecurityManager());
073:
074: // Create registry.
075: int port = CUSTOM_PORT_1;
076: Registry reg = LocateRegistry.createRegistry(port);
077: System.out.println("Registry on CUSTOM port (" + port
078: + ") created.");
079:
080: // Destroy registry.
081: UnicastRemoteObject.unexportObject(reg, true);
082: System.out.println("Test complete.");
083: }
084:
085: /**
086: * Tests registry operation in detail.
087: *
088: * @throws Exception
089: * If some error occurs.
090: */
091: public void testDetailed() throws Exception {
092: try {
093: System.setSecurityManager(new RMISecurityManager());
094:
095: String localHost = InetAddress.getLocalHost().getHostName();
096:
097: // Create registry.
098: int port = REGISTRY_PORT;
099: Registry reg = LocateRegistry.createRegistry(port);
100: System.out.println("Registry on DEFAULT port (" + port
101: + ") created.");
102:
103: Remote obj1 = new MyRemoteObject1("RemoteObject1");
104: exportedObjects.add(obj1);
105: Remote obj2 = new MyRemoteObject1("RemoteObject2");
106: exportedObjects.add(obj2);
107: Remote obj3 = new MyRemoteObject1("RemoteObject3");
108: exportedObjects.add(obj3);
109: Remote obj4 = new MyRemoteObject1("RemoteObject4");
110: exportedObjects.add(obj4);
111: Remote obj5 = new MyRemoteObject1("RemoteObject5");
112: exportedObjects.add(obj5);
113: Remote obj6 = new MyRemoteObject1("RemoteObject6");
114: exportedObjects.add(obj6);
115: Remote obj7 = new MyRemoteObject1("RemoteObject7");
116: exportedObjects.add(obj7);
117: Remote obj8 = new MyRemoteObject1("RemoteObject8");
118: exportedObjects.add(obj8);
119: System.out.println("Test objects exported.");
120:
121: // Check bind.
122: System.out.println("Testing valid Naming.bind names...");
123: Naming.bind("rmi://" + localHost + ":1099/RemoteObject1",
124: obj1);
125: Naming.bind("rmi://127.0.0.1:1099/RemoteObject2", obj2);
126: Naming.bind("//" + localHost + ":1099/RemoteObject3", obj3);
127: Naming.bind("//localhost:1099/RemoteObject4", obj4);
128: Naming.bind("//:1099/RemoteObject5", obj5);
129: Naming.bind("//:/RemoteObject6", obj6);
130: Naming.bind("//" + localHost + "/RemoteObject7", obj7);
131: Naming.bind("RemoteObject8", obj8);
132: System.out.println("Done:");
133: printArray(Naming.list(""));
134: System.out.println("Checking bind complete.");
135:
136: // Check rebind.
137: System.out.println("Testing valid Naming.rebind names...");
138: Naming.rebind("rmi://" + localHost + ":1099/RemoteObject1",
139: obj1);
140: Naming.rebind("rmi://127.0.0.1:1099/RemoteObject2", obj2);
141: Naming.rebind("//" + localHost + ":1099/RemoteObject3",
142: obj3);
143: Naming.rebind("//localhost:1099/RemoteObject4", obj4);
144: Naming.rebind("//:1099/RemoteObject5", obj5);
145: Naming.rebind("//:/RemoteObject6", obj6);
146: Naming.rebind("//" + localHost + "/RemoteObject7", obj7);
147: Naming.rebind("RemoteObject8", obj8);
148: System.out.println("Done:");
149: printArray(Naming.list("//127.0.0.1:/"));
150: System.out.println("Checking rebind complete.");
151:
152: // Check lookup.
153: System.out.println("Testing valid Naming.lookup names...");
154: System.out.println(Naming.lookup("rmi://" + localHost
155: + ":1099/RemoteObject1"));
156: System.out.println(Naming
157: .lookup("rmi://127.0.0.1:/RemoteObject2"));
158: System.out.println(Naming.lookup("//" + localHost
159: + ":1099/RemoteObject3"));
160: System.out.println(Naming
161: .lookup("//localhost:1099/RemoteObject4"));
162: System.out.println(Naming.lookup("//:1099/RemoteObject5"));
163: System.out.println(Naming.lookup("//:/RemoteObject6"));
164: System.out.println(Naming.lookup("//" + localHost
165: + "/RemoteObject7"));
166: System.out.println(Naming.lookup("RemoteObject8"));
167: System.out.println("Done:");
168: printArray(Naming.list("rmi://" + localHost + ""));
169: System.out.println("Checking lookup complete.");
170:
171: // Check unbind.
172: System.out.println("Testing valid Naming.unbind names...");
173: Naming.unbind("rmi://" + localHost + ":1099/RemoteObject1");
174: Naming.unbind("rmi://127.0.0.1:1099/RemoteObject2");
175: Naming.unbind("//" + localHost + ":1099/RemoteObject3");
176: Naming.unbind("//localhost:1099/RemoteObject4");
177: Naming.unbind("//:1099/RemoteObject5");
178: Naming.unbind("//:/RemoteObject6");
179: Naming.unbind("//" + localHost + "/RemoteObject7");
180: Naming.unbind("RemoteObject8");
181: System.out.println("Done:");
182: printArray(Naming.list("//localhost"));
183: System.out.println("Checking unbind complete.");
184:
185: // Destroy registry.
186: UnicastRemoteObject.unexportObject(reg, true);
187:
188: // Create registry.
189: port = CUSTOM_PORT_2;
190: reg = LocateRegistry.createRegistry(port);
191: System.out.println("Registry on CUSTOM port (" + port
192: + ") created.");
193:
194: // Check bind.
195: System.out.println("Testing valid Naming.bind names...");
196: Naming.bind("rmi://" + localHost + ':' + port
197: + "/RemoteObject1", obj1);
198: Naming.bind("rmi://127.0.0.1:" + port + "/RemoteObject2",
199: obj2);
200: Naming.bind("//" + localHost + ':' + port
201: + "/RemoteObject3", obj3);
202: Naming.bind("//localhost:" + port + "/RemoteObject4", obj4);
203: Naming.bind("//:" + port + "/RemoteObject5", obj5);
204: System.out.println("Done:");
205: printArray(Naming.list("//localhost:" + port));
206: System.out.println("Checking bind complete.");
207:
208: // Check rebind.
209: System.out.println("Testing valid Naming.rebind names...");
210: Naming.rebind("rmi://" + localHost + ':' + port
211: + "/RemoteObject1", obj1);
212: Naming.rebind("rmi://127.0.0.1:" + port + "/RemoteObject2",
213: obj2);
214: Naming.rebind("//" + localHost + ':' + port
215: + "/RemoteObject3", obj3);
216: Naming.rebind("//localhost:" + port + "/RemoteObject4",
217: obj4);
218: Naming.rebind("//:" + port + "/RemoteObject5", obj5);
219: System.out.println("Done:");
220: printArray(Naming.list("//127.0.0.1:" + port + "/"));
221: System.out.println("Checking rebind complete.");
222:
223: // Check lookup.
224: System.out.println("Testing valid Naming.lookup names...");
225: System.out.println(Naming.lookup("rmi://" + localHost + ':'
226: + port + "/RemoteObject1"));
227: System.out.println(Naming.lookup("rmi://127.0.0.1:" + port
228: + "/RemoteObject2"));
229: System.out.println(Naming.lookup("//" + localHost + ':'
230: + port + "/RemoteObject3"));
231: System.out.println(Naming.lookup("//localhost:" + port
232: + "/RemoteObject4"));
233: System.out.println(Naming.lookup("//:" + port
234: + "/RemoteObject5"));
235: System.out.println("Done:");
236: printArray(Naming.list("rmi://:" + port));
237: System.out.println("Checking lookup complete.");
238:
239: // Check unbind.
240: System.out.println("Testing valid Naming.unbind names...");
241: Naming.unbind("rmi://" + localHost + ':' + port
242: + "/RemoteObject1");
243: Naming.unbind("rmi://127.0.0.1:" + port + "/RemoteObject2");
244: Naming.unbind("//" + localHost + ':' + port
245: + "/RemoteObject3");
246: Naming.unbind("//localhost:" + port + "/RemoteObject4");
247: Naming.unbind("//:" + port + "/RemoteObject5");
248: System.out.println("Done:");
249: printArray(Naming.list("//localhost:" + port));
250: System.out.println("Checking unbind complete.");
251:
252: // Destroy registry.
253: UnicastRemoteObject.unexportObject(reg, true);
254: } finally {
255: System.out.println("Unexporting objects");
256: unexportObjects();
257: }
258: System.out.println("Test complete.");
259: }
260:
261: /**
262: * Returns test suite for this class.
263: *
264: * @return Test suite for this class.
265: */
266: public static Test suite() {
267: return new TestSuite(RegistryTest.class);
268: }
269:
270: /**
271: * Starts the testing from the command line.
272: *
273: * @param args
274: * Command line parameters.
275: */
276: public static void main(String args[]) {
277: junit.textui.TestRunner.run(suite());
278: }
279: }
|