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.4 $
022: */package org.apache.harmony.rmi;
023:
024: import java.lang.reflect.Proxy;
025:
026: import java.rmi.RMISecurityManager;
027: import java.rmi.Remote;
028: import java.rmi.RemoteException;
029: import java.rmi.ServerError;
030:
031: import java.rmi.registry.LocateRegistry;
032: import java.rmi.registry.Registry;
033:
034: import java.rmi.server.RemoteObject;
035:
036: import org.apache.harmony.rmi.common.SubProcess;
037: import org.apache.harmony.rmi.test.MyException;
038: import org.apache.harmony.rmi.test.MyInvocationHandler;
039: import org.apache.harmony.rmi.test.MyRemoteInterface;
040: import org.apache.harmony.rmi.test.MyRemoteInterface1;
041: import org.apache.harmony.rmi.test.MyRemoteObject;
042: import org.apache.harmony.rmi.test.MyRemoteObject1;
043: import org.apache.harmony.rmi.test.MyRemoteObject3;
044: import org.apache.harmony.rmi.test.MyRemoteObject4;
045:
046: import junit.framework.Test;
047: import junit.framework.TestSuite;
048:
049: /**
050: * Unit test for RMI.
051: *
052: * @author Mikhail A. Markov, Vasily Zakharov
053: * @version $Revision: 1.1.2.4 $
054: */
055: public class ConnectionTest extends RMITestBase {
056:
057: /**
058: * String to identify that a child test process must be started.
059: */
060: private static final String CHILD_ID = "child";
061:
062: /**
063: * No-arg constructor to enable serialization.
064: */
065: public ConnectionTest() {
066: super ();
067: }
068:
069: /**
070: * Constructs this test case with the given name.
071: *
072: * @param name
073: * Name for this test case.
074: */
075: public ConnectionTest(String name) {
076: super (name);
077: }
078:
079: /**
080: * Tests direct socket RMI data exchange.
081: * with both server and client run on a single VM.
082: *
083: * @throws Exception
084: * If some error occurs.
085: */
086: public void testDirectSocket_SingleVM() throws Exception {
087: System.err.println("testDirectSocket_SingleVM starting");
088: testSingleVM(CONFIG_DIRECT_SOCKET, true);
089: }
090:
091: /**
092: * Tests direct HTTP RMI data exchange
093: * with both server and client run on a single VM.
094: *
095: * @throws Exception
096: * If some error occurs.
097: */
098: public void testDirectHTTP_SingleVM() throws Exception {
099: System.err.println("testDirectHTTP_SingleVM starting");
100: testSingleVM(CONFIG_DIRECT_HTTP, true);
101: }
102:
103: /**
104: * Tests proxy HTTP RMI data exchange
105: * with both server and client run on a single VM.
106: *
107: * @throws Exception
108: * If some error occurs.
109: */
110: public void testProxyHTTP_SingleVM() throws Exception {
111: if (checkProxy("testProxyHTTP_SingleVM")) {
112: System.err.println("testProxyHTTP_SingleVM starting");
113: testSingleVM(CONFIG_PROXY_HTTP, true);
114: }
115: }
116:
117: /**
118: * Tests proxy HTTP-CGI RMI data exchange
119: * with both server and client run on a single VM.
120: *
121: * @throws Exception
122: * If some error occurs.
123: */
124: public void testProxyCGI_SingleVM() throws Exception {
125: if (checkProxy("testProxyCGI_SingleVM")) {
126: System.err.println("testProxyCGI_SingleVM starting");
127: testSingleVM(CONFIG_PROXY_CGI, true);
128: }
129: }
130:
131: /**
132: * Tests RMI data exchange in one separate VM.
133: *
134: * @param config
135: * Configuration to set environment for.
136: *
137: * @param endorsed
138: * If endorsedDirs and bootClassPath
139: * should be propagated to test VM.
140: *
141: * @throws Exception
142: * If some error occurs.
143: */
144: public void testSingleVM(int config, boolean endorsed)
145: throws Exception {
146: SubProcess server = null;
147:
148: try {
149: System.out.println("Starting test server");
150: server = startProcess(
151: "org.apache.harmony.rmi.ConnectionTest", CHILD_ID,
152: config, endorsed);
153: server.pipeError();
154: server.closeOutput();
155: System.out.println("Expecting READY from server");
156: server.expect();
157: server.pipeInput();
158: } finally {
159: if (server != null) {
160: System.out.println("Destroying server");
161: server.destroy();
162: }
163: }
164: }
165:
166: /**
167: * Creates RMI server part.
168: *
169: * @throws Exception
170: * If some error occurs.
171: */
172: private void initServer() throws Exception {
173: MyRemoteObject obj = new MyRemoteObject();
174: System.err.println("Object: " + obj + " created and exported.");
175: Registry reg = LocateRegistry.createRegistry(REGISTRY_PORT);
176: exportedObjects.add(reg);
177: System.err.println("Registry created.");
178: reg.bind("MyRemoteObject", RemoteObject.toStub(obj));
179: exportedObjects.add(obj);
180: System.err.println("Object bound in the registry.");
181: }
182:
183: /**
184: * Performs various data exchange tests.
185: *
186: * @throws Exception
187: * If some error occurs.
188: */
189: private void mainTestBody() throws Exception {
190: Registry reg = LocateRegistry.getRegistry(REGISTRY_HOST);
191: System.err.println("Registry located.");
192: MyRemoteInterface mri = (MyRemoteInterface) reg
193: .lookup("MyRemoteObject");
194: System.err.println("Lookup object is: " + mri);
195: Remote obj;
196:
197: // test_String_Void
198: System.err.println("Testing test_String_Void...");
199: mri.test_String_Void("Main for test_String_Void.");
200: System.err.println("Done.\n");
201:
202: // test_Void_String
203: System.err.println("Testing test_Void_String...");
204: System.err.println("Returned: " + mri.test_Void_String());
205: System.err.println("Done.\n");
206:
207: // test_Int_Void
208: System.err.println("Testing test_Int_Void...");
209: mri.test_Int_Void(1234567890);
210: System.err.println("Done.\n");
211:
212: // test_Void_Int
213: System.err.println("Testing test_Void_Int...");
214: System.err.println("Returned: " + mri.test_Void_Int());
215: System.err.println("Done.\n");
216:
217: //test_Remote_Void
218: System.err.println("Testing test_Remote_Void...");
219: obj = new MyRemoteObject1("Main for test_Remote_Void.");
220: exportedObjects.add(obj);
221: mri.test_Remote_Void(obj);
222: System.err.println("Done.\n");
223:
224: //tests remote Object extending another remote object
225: System.err
226: .println("Testing test_Remote_Void with remote object "
227: + "extending another remote object...");
228: obj = new MyRemoteObject4("Main for test_Remote_Void 1.");
229: exportedObjects.add(obj);
230: mri.test_Remote_Void(obj);
231: System.err.println("Done.\n");
232:
233: // test_Void_Remote
234: System.err.println("Testing test_Void_Remote...");
235: System.err.println("Returned: " + mri.test_Void_Remote());
236: System.err.println("Done.\n");
237:
238: // test_Long_Long
239: System.err.println("Testing test_Long_Long...");
240: System.err.println("Returned: "
241: + mri.test_Long_Long(112233445566778899L));
242: System.err.println("Done.\n");
243:
244: // test_String_String
245: System.err.println("Testing test_String_String...");
246: System.err
247: .println("Returned: "
248: + mri
249: .test_String_String("Main for test_String_String."));
250: System.err.println("Done.\n");
251:
252: // test_Remote_Remote
253: System.err.println("Testing test_Remote_Remote...");
254: obj = new MyRemoteObject1("Main for test_Remote_Remote.");
255: exportedObjects.add(obj);
256: System.err.println("Returned: " + mri.test_Remote_Remote(obj));
257: System.err.println("Done.\n");
258:
259: // test_RemoteString_Void
260: System.err.println("Testing test_RemoteString_Void...");
261: obj = new MyRemoteObject1("Main for test_RemoteString_Void.");
262: exportedObjects.add(obj);
263: mri.test_RemoteString_Void(obj,
264: "Main for test_RemoteString_Void (2).");
265: System.err.println("Done.\n");
266:
267: // test_RemoteRemote_Remote
268: System.err.println("Testing test_RemoteRemote_Remote...");
269: obj = new MyRemoteObject1("Main for test_RemoteRemote_Remote.");
270: Remote obj1 = new MyRemoteObject3(
271: "Main for test_RemoteRemote_Remote (2).");
272: exportedObjects.add(obj);
273: exportedObjects.add(obj1);
274: System.err.println("Returned: "
275: + mri.test_RemoteRemote_Remote(obj, obj1));
276: System.err.println("Done.\n");
277:
278: // test_BooleanStringRemote_Void
279: System.err.println("Testing test_BooleanStringRemote_Void...");
280: obj = new MyRemoteObject1(
281: "Main for test_BooleanStringRemote_Void (2).");
282: exportedObjects.add(obj);
283: mri.test_BooleanStringRemote_Void(false,
284: "Main for test_BooleanStringRemote_Void.", obj);
285: System.err.println("Done.\n");
286:
287: // test_Proxy_Void
288: System.err.println("Testing test_Proxy_Void...");
289: Object proxy = Proxy.newProxyInstance(Thread.currentThread()
290: .getContextClassLoader(),
291: new Class[] { MyRemoteInterface1.class },
292: new MyInvocationHandler());
293: mri.test_Proxy_Void(proxy);
294: System.err.println("Done.\n");
295:
296: // test_Array_Void
297: System.err.println("Testing test_Array_Void...");
298: mri.test_Array_Void(new String[] {
299: "Main for test_Array_Void 1.",
300: "Main for test_Array_Void 2." });
301: System.err.println("Done.\n");
302:
303: // test_Void_Array
304: System.err.println("Testing test_Void_Array...");
305: printArray(mri.test_Void_Array());
306: System.err.println("Done.\n");
307:
308: // test_RemoteArray_Void
309: System.err.println("Testing test_RemoteArray_Void...");
310: obj = new MyRemoteObject1("Main for test_RemoteArray_Void 1.");
311: obj1 = new MyRemoteObject1("Main for test_RemoteArray_Void 2.");
312: exportedObjects.add(obj);
313: exportedObjects.add(obj1);
314: mri.test_RemoteArray_Void(new Remote[] { obj, obj1 });
315: System.err.println("Done.\n");
316:
317: // test_Void_RemoteArray
318: System.err.println("Testing test_Void_RemoteArray...");
319: printArray(mri.test_Void_RemoteArray());
320: System.err.println("Done.\n");
321:
322: // test_Exception
323: System.err.println("Testing test_Exception...");
324: try {
325: mri.test_Exception();
326: fail("test_Exception() should have thrown MyException");
327: } catch (MyException e) {
328: System.err.println(e.toString());
329: }
330: System.err.println("Done.\n");
331:
332: // test_Error
333: System.err.println("Testing test_Error...");
334: try {
335: mri.test_Error();
336: fail("test_Error() should have thrown ServerError");
337: } catch (ServerError e) {
338: System.err.println(e.toString());
339: }
340: System.err.println("Done.\n");
341:
342: // test_RuntimeException
343: System.err.println("Testing test_RuntimeException...");
344: try {
345: mri.test_RuntimeException();
346: fail("test_RuntimeException() should have thrown RuntimeException");
347: } catch (RuntimeException e) {
348: System.err.println(e.toString());
349: }
350: System.err.println("Done.\n");
351:
352: // test_RemoteException
353: System.err.println("Testing test_RemoteException...");
354: try {
355: mri.test_RemoteException();
356: fail("test_RemoteException() should have thrown RemoteException");
357: } catch (RemoteException e) {
358: System.err.println(e.toString());
359: }
360: System.err.println("Done.\n");
361: }
362:
363: /**
364: * Runs test server process.
365: *
366: * @param config
367: * Number of the configuration to run.
368: *
369: * @throws Exception
370: * If some error occurs.
371: */
372: private void runTestSingle(int config) throws Exception {
373: try {
374: System.err.println("Test server started.");
375: System.setSecurityManager(new RMISecurityManager());
376: setEnvironmentForConfig(config);
377: initServer();
378: mainTestBody();
379: System.err.println("Test server complete.");
380: SubProcess.tellOut();
381: } finally {
382: System.err.println("Test server closing.");
383: unexportObjects();
384: }
385: System.err.println("Test server exiting.");
386: }
387:
388: /**
389: * Returns test suite for this class.
390: *
391: * @return Test suite for this class.
392: */
393: public static Test suite() {
394: return new TestSuite(ConnectionTest.class);
395: }
396:
397: /**
398: * Starts the testing from the command line.
399: *
400: * If first command line parameter exists, the test server is started
401: * instead. The parameter value is used as a suffix of the respective
402: * setEnvironmentXXX method that is called to setup the environment
403: * configuration.
404: *
405: * @param args
406: * Command line parameters.
407: */
408: public static void main(String args[]) {
409: switch (args.length) {
410: case 0:
411: // Run tests normally.
412: junit.textui.TestRunner.run(suite());
413: break;
414: case 2:
415: // Run child test server process.
416: String param = args[0];
417: int config = new Integer(args[1]).intValue();
418: ConnectionTest connectionTest = new ConnectionTest();
419:
420: try {
421: if (param.equals(CHILD_ID)) {
422: connectionTest.runTestSingle(config);
423: } else {
424: System.err.println("Bad parameter: " + param);
425: abort();
426: }
427: } catch (Exception e) {
428: System.err.print("Child process (" + param + ", "
429: + config + ") failed: ");
430: e.printStackTrace();
431: abort();
432: }
433: break;
434: default:
435: System.err.println("Bad number of parameters: "
436: + args.length + ", expected: 2.");
437: abort();
438: }
439: }
440: }
|