KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > test > orb > rmi > RMITest


1 package org.jacorb.test.orb.rmi;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2003 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 import java.rmi.Remote JavaDoc;
24 import java.util.Properties JavaDoc;
25 import javax.rmi.PortableRemoteObject JavaDoc;
26 import junit.framework.Test;
27 import junit.framework.TestSuite;
28 import junit.framework.Assert;
29 import org.jacorb.test.common.ClientServerSetup;
30 import org.jacorb.test.common.ClientServerTestCase;
31 import org.jacorb.test.orb.rmi.Boo;
32 import org.jacorb.test.orb.rmi.Foo;
33 import org.jacorb.test.orb.rmi.NegativeArgumentException;
34
35 public class RMITest extends ClientServerTestCase
36 {
37     private RMITestInterface server;
38
39     public RMITest(String JavaDoc name, ClientServerSetup setup)
40     {
41         super(name, setup);
42     }
43
44     public void setUp() throws Exception JavaDoc
45     {
46         server = (RMITestInterface)javax.rmi.PortableRemoteObject.narrow(
47                                                     setup.getServerObject(),
48                                                     RMITestInterface.class);
49     }
50
51     public static Test suite()
52     {
53         TestSuite suite = new TestSuite( "RMI/IIOP tests" );
54
55         Properties JavaDoc client_props = new Properties JavaDoc();
56         client_props.setProperty
57             ("jacorb.interop.strict_check_on_tc_creation", "off");
58
59         Properties JavaDoc server_props = new Properties JavaDoc();
60         server_props.setProperty
61             ("jacorb.interop.strict_check_on_tc_creation", "off");
62
63         ClientServerSetup setup =
64             new ClientServerSetup( suite,
65                                    "org.jacorb.test.orb.rmi.RMITestServant",
66                                    client_props,
67                                    server_props);
68
69         suite.addTest( new RMITest( "test_getString", setup ));
70         suite.addTest( new RMITest( "test_primitiveTypes", setup ));
71         suite.addTest( new RMITest( "test_String", setup ));
72         suite.addTest( new RMITest( "test_RMITestInterface", setup ));
73         suite.addTest( new RMITest( "test_Remote", setup ));
74         suite.addTest( new RMITest( "test_Serializable", setup ));
75         suite.addTest( new RMITest( "test_intArray", setup ));
76         suite.addTest( new RMITest( "test_valueArray", setup ));
77         suite.addTest( new RMITest( "test_exception", setup ));
78         suite.addTest( new RMITest( "test_FooValueToObject", setup ));
79         suite.addTest( new RMITest( "test_BooValueToObject", setup ));
80         suite.addTest( new RMITest( "test_valueArrayToVector", setup ));
81         suite.addTest( new RMITest( "test_vectorToValueArray", setup ));
82         suite.addTest( new RMITest( "test_getException", setup ));
83         suite.addTest( new RMITest( "test_getZooValue", setup ));
84         suite.addTest( new RMITest( "test_referenceSharingWithinArray",
85                                      setup ));
86         suite.addTest( new RMITest( "test_referenceSharingWithinCollection",
87                                      setup ));
88         suite.addTest( new RMITest( "test_getVectorWithObjectArrayAsElement",
89                                      setup ));
90         suite.addTest( new RMITest( "test_getVectorWithVectorAsElement",
91                                      setup ));
92         suite.addTest( new RMITest( "test_getVectorWithHashtableAsElement",
93                                      setup ));
94
95         return setup;
96     }
97
98     public void test_getString()
99     {
100         try
101         {
102             //System.out.println("getString" + " ---------------------------");
103
String JavaDoc s = server.getString();
104             //System.out.println(s);
105
assertEquals(RMITestUtil.STRING, s);
106         }
107         catch (java.rmi.RemoteException JavaDoc re)
108         {
109             throw new RuntimeException JavaDoc(re.toString());
110         }
111     }
112
113     public void test_primitiveTypes()
114     {
115         try
116         {
117             String JavaDoc s;
118             //System.out.println("testPrimitiveTypes" + " ------------------");
119
s = server.testPrimitiveTypes(false,
120                                           'A',
121                                           Byte.MIN_VALUE,
122                                           Short.MIN_VALUE,
123                                           Integer.MIN_VALUE,
124                                           Long.MIN_VALUE,
125                                           Float.MIN_VALUE,
126                                           Double.MIN_VALUE);
127             //System.out.println(s);
128
assertEquals(RMITestUtil.primitiveTypesToString(false,
129                                                             'A',
130                                                             Byte.MIN_VALUE,
131                                                             Short.MIN_VALUE,
132                                                             Integer.MIN_VALUE,
133                                                             Long.MIN_VALUE,
134                                                             Float.MIN_VALUE,
135                                                             Double.MIN_VALUE),
136                          s);
137             s = server.testPrimitiveTypes(true,
138                                           'Z',
139                                           Byte.MAX_VALUE,
140                                           Short.MAX_VALUE,
141                                           Integer.MAX_VALUE,
142                                           Long.MAX_VALUE,
143                                           Float.MAX_VALUE,
144                                           Double.MAX_VALUE);
145             //System.out.println(s);
146
assertEquals(RMITestUtil.primitiveTypesToString(true,
147                                                             'Z',
148                                                             Byte.MAX_VALUE,
149                                                             Short.MAX_VALUE,
150                                                             Integer.MAX_VALUE,
151                                                             Long.MAX_VALUE,
152                                                             Float.MAX_VALUE,
153                                                             Double.MAX_VALUE),
154                          s);
155         }
156         catch (java.rmi.RemoteException JavaDoc re)
157         {
158             throw new RuntimeException JavaDoc(re.toString());
159         }
160     }
161
162     public void test_String()
163     {
164         try
165         {
166             //System.out.println("testString" + " --------------------------");
167
String JavaDoc original = "0123456789";
168             String JavaDoc echoedBack = server.testString("0123456789");
169             //System.out.println(echoedBack);
170
assertEquals(RMITestUtil.echo(original), echoedBack);
171         }
172         catch (java.rmi.RemoteException JavaDoc re)
173         {
174             throw new RuntimeException JavaDoc(re.toString());
175         }
176     }
177
178     public void test_RMITestInterface()
179     {
180         try
181         {
182             //System.out.println("testRMITestInterface" + " ----------------");
183
RMITestInterface t =
184                 server.testRMITestInterface("the quick brown fox", server);
185             String JavaDoc s = t.getString();
186             //System.out.println(s);
187
assertEquals(RMITestUtil.STRING, s);
188         }
189         catch (java.rmi.RemoteException JavaDoc re)
190         {
191             throw new RuntimeException JavaDoc(re.toString());
192         }
193     }
194
195     public void test_Remote()
196     {
197         try
198         {
199             //System.out.println("testRemote" + " --------------------------");
200
Remote JavaDoc r = server.testRemote("jumps over the lazy dog", server);
201             RMITestInterface t =
202                 (RMITestInterface)PortableRemoteObject.narrow(r,
203                                                        RMITestInterface.class);
204             String JavaDoc s = t.getString();
205             //System.out.println(s);
206
assertEquals(RMITestUtil.STRING, s);
207         }
208         catch (java.rmi.RemoteException JavaDoc re)
209         {
210             throw new RuntimeException JavaDoc(re.toString());
211         }
212     }
213
214     public void test_Serializable()
215     {
216         try
217         {
218             //System.out.println("testSerializable" + " --------------------");
219
Foo original = new Foo(7, "foo test");
220             Foo echoedBack = server.testSerializable(original);
221             //System.out.println(echoedBack.toString());
222
assertEquals(RMITestUtil.echoFoo(original), echoedBack);
223         }
224         catch (java.rmi.RemoteException JavaDoc re)
225         {
226             throw new RuntimeException JavaDoc(re.toString());
227         }
228     }
229
230     public void test_intArray()
231     {
232         try
233         {
234             //System.out.println("testIntArray" + " ------------------------");
235
int[] original= new int[10];
236             for (int i = 0; i < original.length; i++)
237             {
238                 original[i] = 100 + i;
239             }
240             int[] echoedBack = server.testIntArray(original);
241             assertEquals(original.length, echoedBack.length);
242             for (int i = 0; i < echoedBack.length; i++)
243             {
244                 //System.out.print(""+ a[i] + " ");
245
assertEquals(original[i] + 1, echoedBack[i]);
246             }
247             //System.out.println();
248
}
249         catch (java.rmi.RemoteException JavaDoc re)
250         {
251             throw new RuntimeException JavaDoc(re.toString());
252         }
253     }
254
255     public void test_valueArray()
256     {
257         try
258         {
259             //System.out.println("testValueArray" + " ----------------------");
260
Foo[] original = new Foo[4];
261             for (int i = 0; i < original.length; i++)
262             {
263                 original[i] = new Foo(100 + i, "foo array test");
264             }
265             Foo[] echoedBack = server.testValueArray(original);
266             assertEquals(original.length, echoedBack.length);
267             for (int i = 0; i < echoedBack.length; i++)
268             {
269                 //System.out.println(echoedBack[i].toString());
270
assertEquals(RMITestUtil.echoFoo(original[i]), echoedBack[i]);
271             }
272         }
273         catch (java.rmi.RemoteException JavaDoc re)
274         {
275             throw new RuntimeException JavaDoc(re.toString());
276         }
277     }
278
279     public void test_exception()
280     {
281         try
282         {
283             //System.out.println("testException" + " -----------------------");
284

285             //System.out.println(server.testException(0));
286
assertEquals("#0", server.testException(0));
287             //System.out.println(server.testException(1));
288
assertEquals("#1", server.testException(1));
289             //System.out.println(server.testException(2));
290
assertEquals("#2", server.testException(2));
291             try
292             {
293                 server.testException(-2);
294                 fail("NegativeArgumentException expected but not thrown.");
295             }
296             catch (NegativeArgumentException na)
297             {
298                 //System.out.println("Expected exception:\n" + na.toString());
299
assertEquals(-2, na.getNegativeArgument());
300             }
301             try
302             {
303                 server.testException(-1);
304                 fail("NegativeArgumentException expected but not thrown.");
305             }
306             catch (NegativeArgumentException na)
307             {
308                 //System.out.println("Expected exception:\n" + na.toString());
309
assertEquals(-1, na.getNegativeArgument());
310             }
311             //System.out.println(server.testException(0));
312
assertEquals("#0", server.testException(0));
313         }
314         catch (NegativeArgumentException na)
315         {
316             throw new RuntimeException JavaDoc(na.toString());
317         }
318         catch (java.rmi.RemoteException JavaDoc re)
319         {
320             throw new RuntimeException JavaDoc(re.toString());
321         }
322     }
323
324     public void test_FooValueToObject()
325     {
326         try
327         {
328             //System.out.println("fooValueToObject" + " --------------------");
329
Foo original = new Foo(9999, "foo test");
330             java.lang.Object JavaDoc echoedBack = server.fooValueToObject(original);
331             //System.out.println(echoedBack.toString());
332
assertEquals(RMITestUtil.echoFoo(original), echoedBack);
333         }
334         catch (java.rmi.RemoteException JavaDoc re)
335         {
336             throw new RuntimeException JavaDoc(re.toString());
337         }
338     }
339
340     public void test_BooValueToObject()
341     {
342         try
343         {
344             //System.out.println("booValueToObject" + " --------------------");
345
Boo original = new Boo("t1", "boo test");
346             java.lang.Object JavaDoc echoedBack = server.booValueToObject(original);
347             //System.out.println(echoedBack.toString());
348
assertEquals(RMITestUtil.echoBoo(original), echoedBack);
349         }
350         catch (java.rmi.RemoteException JavaDoc re)
351         {
352             throw new RuntimeException JavaDoc(re.toString());
353         }
354     }
355
356     public void test_valueArrayToVector()
357     {
358         try
359         {
360             //System.out.println("valueArrayToVector" + " ------------------");
361
Foo[] original = new Foo[4];
362             for (int i = 0; i < original.length; i++)
363             {
364                 original[i] = new Foo(100 + i, "foo vector test");
365             }
366             java.util.Vector JavaDoc v = server.valueArrayToVector(original);
367             java.lang.Object JavaDoc[] echoedBack = v.toArray();
368             assertEquals(original.length, echoedBack.length);
369             for (int i = 0; i < echoedBack.length; i++)
370             {
371                 //System.out.println(echoedBack[i].toString());
372
assertEquals(RMITestUtil.echoFoo(original[i]), echoedBack[i]);
373             }
374         }
375         catch (java.rmi.RemoteException JavaDoc re)
376         {
377             throw new RuntimeException JavaDoc(re.toString());
378         }
379     }
380
381     public void test_vectorToValueArray()
382     {
383         try
384         {
385             Foo[] original = new Foo[4];
386             for (int i = 0; i < original.length; i++)
387             {
388                 original[i] = new Foo(100 + i, "foo vector test");
389             }
390
391             //System.out.println("vectorToValueArray" + " ------------------");
392
java.util.Vector JavaDoc v = server.valueArrayToVector(original);
393             Foo[] echoedBack = server.vectorToValueArray(v);
394             assertEquals(original.length, echoedBack.length);
395             for (int i = 0; i < echoedBack.length; i++)
396             {
397                 //System.out.println(echoedBack[i].toString());
398
assertEquals(
399                         RMITestUtil.echoFoo(RMITestUtil.echoFoo(original[i])),
400                         echoedBack[i]);
401             }
402         }
403         catch (java.rmi.RemoteException JavaDoc re)
404         {
405             throw new RuntimeException JavaDoc(re.toString());
406         }
407     }
408
409     public void test_getException()
410     {
411         try
412         {
413             //System.out.println("getException" + " ------------------------");
414
java.lang.Object JavaDoc obj = server.getException();
415             //System.out.println(obj.toString());
416
NegativeArgumentException na = (NegativeArgumentException)obj;
417             assertEquals(-7777, na.getNegativeArgument());
418         }
419         catch (java.rmi.RemoteException JavaDoc re)
420         {
421             throw new RuntimeException JavaDoc(re.toString());
422         }
423     }
424
425     public void test_getZooValue()
426     {
427         try
428         {
429             //System.out.println("getZooValue" + " -------------------------");
430
java.lang.Object JavaDoc obj = server.getZooValue();
431             //System.out.println(obj.toString());
432
Assert.assertEquals(new Zoo("outer_zoo!",
433                                  "returned by getZooValue",
434                                  new Zoo("inner_zoo!", "inner")),
435                          obj);
436         }
437         catch (java.rmi.RemoteException JavaDoc re)
438         {
439             throw new RuntimeException JavaDoc(re.toString());
440         }
441     }
442
443     public void test_referenceSharingWithinArray()
444     {
445         try
446         {
447             //System.out.println("testRefSharingWithinArray" + " -----------");
448
int n = 100;
449             Object JavaDoc[] original = new Object JavaDoc[n];
450             for (int i = 0; i < n; i++)
451             {
452                 //original[i] = new Foo(100 + i, "foo array test");
453
original[i] = new Boo("t" + i, "boo array test");
454             }
455             Object JavaDoc[] echoedBack =
456                     server.testReferenceSharingWithinArray(original);
457             assertEquals(2 * n, echoedBack.length);
458
459             for (int i = 0; i < n; i++)
460             {
461                 assertEquals(original[i], echoedBack[i]);
462                 assertEquals(original[i], echoedBack[i + n]);
463                 assertSame(echoedBack[i], echoedBack[i + n]);
464             }
465         }
466         catch (java.rmi.RemoteException JavaDoc re)
467         {
468             throw new RuntimeException JavaDoc(re.toString());
469         }
470     }
471
472     public void test_referenceSharingWithinCollection()
473     {
474         try
475         {
476             //System.out.println("testRefSharingWithinCollection" + " ------");
477
java.util.Collection JavaDoc original = new java.util.ArrayList JavaDoc();
478             int n = 10;
479             for (int i = 0; i < n; i++)
480             {
481                 original.add(new Foo(100 + i, "foo collection test"));
482                 //original.add(new Boo("t" + i, "boo collection test"));
483
}
484             java.util.Collection JavaDoc echoedBack =
485                     server.testReferenceSharingWithinCollection(original);
486             assertEquals(2 * n, echoedBack.size());
487
488             java.util.ArrayList JavaDoc originalList = (java.util.ArrayList JavaDoc)original;
489             java.util.ArrayList JavaDoc echoedList = (java.util.ArrayList JavaDoc)echoedBack;
490
491
492             for (int i = 0; i < n; i++)
493             {
494                 //System.out.println(echoedList.get(i).toString());
495
assertEquals(originalList.get(i), echoedList.get(i));
496                 assertEquals(originalList.get(i), echoedList.get(i + n));
497                 assertSame(echoedList.get(i), echoedList.get(i + n));
498             }
499         }
500         catch (java.rmi.RemoteException JavaDoc re)
501         {
502             throw new RuntimeException JavaDoc(re.toString());
503         }
504     }
505
506     public void test_getVectorWithObjectArrayAsElement()
507     {
508         try
509         {
510             //System.out.println("getVectorWithObjectArrayAsElement" + " ---");
511
java.util.Vector JavaDoc vector =
512                 server.getVectorWithObjectArrayAsElement();
513             //System.out.println(vector.toString());
514
assertTrue(vector.size() == 1);
515             Object JavaDoc[] inner = (Object JavaDoc[]) vector.get(0);
516             assertEquals(new Integer JavaDoc(1), inner[0]);
517             assertEquals(new Integer JavaDoc(2), inner[1]);
518             assertEquals("Third Element", inner[2]);
519         }
520         catch (java.rmi.RemoteException JavaDoc re)
521         {
522             throw new RuntimeException JavaDoc(re.toString());
523         }
524     }
525
526     public void test_getVectorWithVectorAsElement()
527     {
528         try
529         {
530             //System.out.println("getVectorWithVectorAsElement" + " --------");
531
java.util.Vector JavaDoc vector =
532                 server.getVectorWithVectorAsElement();
533             //System.out.println(vector.toString());
534
assertTrue(vector.size() == 1);
535             java.util.Vector JavaDoc inner = (java.util.Vector JavaDoc) vector.get(0);
536             assertEquals(new Integer JavaDoc(1), inner.get(0));
537             assertEquals(new Integer JavaDoc(2), inner.get(1));
538             assertEquals("Third Element", inner.get(2));
539         }
540         catch (java.rmi.RemoteException JavaDoc re)
541         {
542             throw new RuntimeException JavaDoc(re.toString());
543         }
544     }
545
546     public void test_getVectorWithHashtableAsElement()
547     {
548         try
549         {
550             //System.out.println("getVectorWithHashtableAsElement" + " -----");
551
java.util.Vector JavaDoc vector =
552                 server.getVectorWithHashtableAsElement();
553             //System.out.println(vector.toString());
554
assertTrue(vector.size() == 1);
555             java.util.Hashtable JavaDoc inner = (java.util.Hashtable JavaDoc) vector.get(0);
556             assertEquals(new Integer JavaDoc(1), inner.get(new Integer JavaDoc(0)));
557             assertEquals(new Integer JavaDoc(2), inner.get(new Integer JavaDoc(1)));
558             assertEquals("Third Element", inner.get(new Integer JavaDoc(2)));
559         }
560         catch (java.rmi.RemoteException JavaDoc re)
561         {
562             throw new RuntimeException JavaDoc(re.toString());
563         }
564     }
565
566 }
567
Popular Tags