KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > jtests > conform > basic > clients > MultiProtocolTests


1 /**
2  * Copyright (C) 2002,2005 - INRIA (www.inria.fr)
3  *
4  * CAROL: Common Architecture for RMI ObjectWeb Layer
5  *
6  * This library is developed inside the ObjectWeb Consortium,
7  * http://www.objectweb.org
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22  * USA
23  *
24  * --------------------------------------------------------------------------
25  * $Id: MultiProtocolTests.java,v 1.14 2005/04/07 15:07:08 benoitf Exp $
26  * --------------------------------------------------------------------------
27  */

28 package org.objectweb.carol.jtests.conform.basic.clients;
29
30 import java.util.Hashtable JavaDoc;
31
32 import javax.naming.Binding JavaDoc;
33 import javax.naming.Context JavaDoc;
34 import javax.naming.InitialContext JavaDoc;
35 import javax.naming.NameClassPair JavaDoc;
36 import javax.naming.NameNotFoundException JavaDoc;
37 import javax.naming.NamingEnumeration JavaDoc;
38 import javax.naming.NamingException JavaDoc;
39 import javax.rmi.PortableRemoteObject JavaDoc;
40 import javax.rmi.CORBA.Stub JavaDoc;
41
42 import junit.framework.Test;
43 import junit.framework.TestCase;
44 import junit.framework.TestSuite;
45
46 import org.omg.CORBA.ORB JavaDoc;
47
48 import org.objectweb.carol.jtests.conform.basic.server.BasicMultiObjectItf;
49 import org.objectweb.carol.jtests.conform.basic.server.BasicObjectItf;
50 import org.objectweb.carol.jtests.conform.basic.server.BasicRemoteObject;
51 import org.objectweb.carol.jtests.conform.basic.server.BasicSerializableObject;
52 import org.objectweb.carol.util.configuration.CarolDefaultValues;
53 import org.objectweb.carol.util.configuration.ConfigurationRepository;
54
55 /**
56  * Class <code>MultiProtocolTests</code> is a Junit BasicTest Test : Test The
57  * InitialContext and the PortableRemoteObject situation with remote object
58  * @author Guillaume Riviere
59  * @author Florent Benoit
60  */

61 public class MultiProtocolTests extends TestCase {
62
63     /**
64      * Name of the basic remote object (in all name services)
65      */

66     private String JavaDoc basicName = null;
67
68     /**
69      * Name of the basic multi remote object (in all name services)
70      */

71     private String JavaDoc basicMultiName = null;
72
73     /**
74      * Initial Contexts
75      */

76     private InitialContext JavaDoc ic = null;
77
78     /**
79      * TheBasicObject
80      */

81     private BasicObjectItf ba = null;
82
83     /**
84      * TheBasicMultiObject
85      */

86     private BasicMultiObjectItf bma = null;
87
88     /**
89      * Constructor
90      * @param name the name of the test
91      */

92     public MultiProtocolTests(String JavaDoc name) {
93         super(name);
94     }
95
96     /**
97      * Setup Method
98      * @throws Exception if setup fails (could be narrow)
99      */

100     public void setUp() throws Exception JavaDoc {
101         super.setUp();
102
103         ConfigurationRepository.init();
104
105         ic = new InitialContext JavaDoc();
106
107         // set the object name
108
basicName = "basicname";
109         basicMultiName = "basicmultiname";
110
111         // lookup to the remote objects
112
ba = (BasicObjectItf) PortableRemoteObject.narrow(ic.lookup(basicName), BasicObjectItf.class);
113         bma = (BasicMultiObjectItf) PortableRemoteObject.narrow(ic.lookup(basicMultiName), BasicMultiObjectItf.class);
114     }
115
116     /**
117      * tearDown method
118      * @throws Exception if super method fails
119      */

120     public void tearDown() throws Exception JavaDoc {
121         basicName = null;
122         basicMultiName = null;
123         ba = null;
124         bma = null;
125         ic.close();
126         super.tearDown();
127     }
128
129     /**
130      * Test Method , Test an access on a remote object The default orb is used
131      * for this access
132      */

133     public void testString() {
134         try {
135             String JavaDoc expected = ba.getString();
136             assertEquals(expected, "string");
137         } catch (Exception JavaDoc e) {
138             e.printStackTrace();
139             fail("Can't get string" + e);
140         }
141     }
142
143     /**
144      * Test Method , Test an access on a remote object which also access to
145      * remote object, This tests use 2 call via default protocol The default orb
146      * is used for this access
147      */

148     public void testMultiString() {
149         try {
150             String JavaDoc expected = bma.getMultiString();
151             assertEquals(expected, "multi string call: " + "string");
152         } catch (Exception JavaDoc e) {
153             e.printStackTrace();
154             fail("Can't get multi string" + e);
155         }
156     }
157
158     /**
159      * Test Method , Test an access on a refearence
160      */

161     public void testReferenceString() {
162         try {
163             String JavaDoc expected = bma.getBasicRefString();
164             assertEquals(expected, "string2");
165         } catch (Exception JavaDoc e) {
166             e.printStackTrace();
167             fail("Can't get ref string" + e);
168         }
169     }
170
171     /**
172      * Test Method , Test an access on a remote object The default orb is used
173      * for this access
174      */

175     public void testStub() {
176         try {
177             BasicObjectItf ob = (BasicObjectItf) PortableRemoteObject
178                     .narrow(bma.getBasicObject(), BasicObjectItf.class);
179             String JavaDoc expected = ob.getString();
180             assertEquals(expected, "string");
181         } catch (Exception JavaDoc e) {
182             e.printStackTrace();
183             fail("Can't narrow Remote Object :" + e);
184         }
185     }
186
187     /**
188      * Try to bind/lookup a serializable object
189      */

190     public void testSerializable() {
191         BasicSerializableObject bso = new BasicSerializableObject("test1");
192         BasicSerializableObject bsoResult = null;
193         try {
194             ic.bind("testSerializable", bso);
195         } catch (Exception JavaDoc e) {
196             fail("Can't bind object : " + e);
197         }
198         Object JavaDoc lookupObj = null;
199         try {
200             lookupObj = ic.lookup("testSerializable");
201         } catch (Exception JavaDoc e) {
202             fail("Can't lookup object : " + e);
203         }
204
205         try {
206             bsoResult = (BasicSerializableObject) PortableRemoteObject.narrow(lookupObj, BasicSerializableObject.class);
207         } catch (Exception JavaDoc e) {
208             fail("Can't narrow object : " + e);
209         }
210
211         assertTrue(bso.equals(bsoResult));
212
213     }
214
215     /**
216      * test context methods
217      */

218     public void testContextCommonContextMethods() {
219         try {
220             ic.bind("testContextCommonContextMethods", bma);
221         } catch (Exception JavaDoc e) {
222             fail("Can't bind object : " + e);
223         }
224         Object JavaDoc lookupObj = null;
225         try {
226             lookupObj = ic.lookup("testContextCommonContextMethods");
227         } catch (Exception JavaDoc e) {
228             fail("Can't lookup object : " + e);
229         }
230
231         // Now rebind object
232
try {
233             ic.rebind("testContextCommonContextMethods", bma);
234         } catch (Exception JavaDoc e) {
235             fail("Can't rebind object : " + e);
236         }
237
238         // Now unbind object
239
try {
240             ic.unbind("testContextCommonContextMethods");
241         } catch (Exception JavaDoc e) {
242             fail("Can't unbind object : " + e);
243         }
244
245         // verify the unbind
246
try {
247             lookupObj = ic.lookup("testContextCommonContextMethods");
248             fail("lookup of the object should fail as the object has been unbind");
249         } catch (Exception JavaDoc e) {
250             assertTrue("exception is : " + e.getMessage(), NamingException JavaDoc.class.isAssignableFrom(e.getClass()));
251         }
252
253         // bind a new object to test the rename
254
BasicSerializableObject bso = new BasicSerializableObject("testContextCommonContextMethods2");
255         try {
256             ic.bind("testContextCommonContextMethods2", bso);
257             ic.rename("testContextCommonContextMethods2", "testContextCommonContextMethods2renamed");
258         } catch (Exception JavaDoc e) {
259             fail("Can't bind object : " + e);
260         }
261         // verify the rename
262
try {
263             lookupObj = ic.lookup("testContextCommonContextMethods2");
264             fail("lookup of the object should fail as the object has been renamed");
265         } catch (Exception JavaDoc e) {
266             assertTrue("exception is : " + e.getMessage(), NamingException JavaDoc.class.isAssignableFrom(e.getClass()));
267         }
268         try {
269             lookupObj = ic.lookup("testContextCommonContextMethods2renamed");
270             BasicSerializableObject bsoResult = (BasicSerializableObject) PortableRemoteObject.narrow(lookupObj,
271                     BasicSerializableObject.class);
272             assertTrue(bso.equals(bsoResult));
273         } catch (Exception JavaDoc e) {
274             fail("lookup of the object should not fail as a previous object has been renamed");
275         }
276
277         // List context (and verify object is present)
278
try {
279             ic.bind("testContextCommonContextMethods4", bso);
280             NamingEnumeration JavaDoc ne = ic.list("");
281             boolean found = false;
282             while (ne.hasMore() && !found) {
283                 NameClassPair JavaDoc ncp = (NameClassPair JavaDoc) ne.next();
284                 String JavaDoc n = ncp.getName();
285                 if (n.equals("testContextCommonContextMethods4")) {
286                     found = true;
287                 }
288             }
289             if (!found) {
290                 fail("object bind was not find in ic.list(\"\")");
291             }
292
293             // Use listBindings method now
294
ne = ic.listBindings("");
295             found = false;
296             while (ne.hasMore() && !found) {
297                 Binding JavaDoc binding = (Binding JavaDoc) ne.next();
298                 String JavaDoc n = binding.getName();
299                 if (n.equals("testContextCommonContextMethods4")) {
300                     found = true;
301                 }
302             }
303             if (!found) {
304                 fail("object bind was not find in ic.listBindings(\"\")");
305             }
306
307             // unbind object
308
ic.unbind("testContextCommonContextMethods4");
309         } catch (Exception JavaDoc e) {
310             fail("list() method fails");
311         }
312
313         // test unbind a name not bind
314
try {
315             ic.unbind("testContextCommonContextMethods4");
316         } catch (Exception JavaDoc e) {
317             assertTrue(e instanceof NameNotFoundException JavaDoc);
318         }
319
320     }
321
322     /**
323      * Test Method , Test an access on a remote object The default orb is used
324      * for this access
325      */

326     public void testPortableRemoteObject() {
327         BasicRemoteObject bro = new BasicRemoteObject("testPortableRemoteObject");
328
329         // exportObject() method
330
try {
331             PortableRemoteObject.exportObject(bro);
332         } catch (Exception JavaDoc e) {
333             e.printStackTrace();
334             fail("Cannot export object '" + bro + "' : " + e.getMessage());
335         }
336
337         // toStub() method
338
try {
339             Object JavaDoc remoteVal = PortableRemoteObject.toStub(bro);
340
341             // in case of IIOP, connect stub as it use POA model and stub should
342
// be connected
343
if (remoteVal instanceof Stub JavaDoc) {
344                 ((Stub JavaDoc) remoteVal).connect(ORB.init(new String JavaDoc[0], null));
345             }
346         } catch (Exception JavaDoc e) {
347             e.printStackTrace();
348             fail("Cannot use toStub() method on object '" + bro + "' : " + e.getMessage());
349         }
350
351         // unexportObject() method
352
try {
353             PortableRemoteObject.unexportObject(bro);
354         } catch (Exception JavaDoc e) {
355             e.printStackTrace();
356             fail("Cannot unexportObject object '" + bro + "' : " + e.getMessage());
357         }
358
359         // test narrow() method
360
try {
361             Object JavaDoc o = ic.lookup("basicmultiname");
362             PortableRemoteObject.narrow(o, BasicMultiObjectItf.class);
363         } catch (Exception JavaDoc e) {
364             e.printStackTrace();
365             fail("Cannot narrow object '" + bro + "'.");
366         }
367
368     }
369
370     /**
371      * Check that the bind/lookup works on flat registry like JRMP registry Bind
372      * objects/obj1 and objects2/obj2 and check that lookup returns different
373      * objects On flat registry lookup return same object as it returns only
374      * value before / Carol should handle this case to make it working even if
375      * this is a flat registry
376      */

377     public void testCompositeNameId() {
378
379         final String JavaDoc id1 = "objects/testCompositeNameId1";
380         final String JavaDoc id2 = "objects/testCompositeNameId2";
381
382         BasicSerializableObject bso1 = new BasicSerializableObject(id1);
383         BasicSerializableObject bso2 = new BasicSerializableObject(id2);
384
385         // bind them
386
try {
387             ic.bind(id1, bso1);
388         } catch (Exception JavaDoc e) {
389             fail("Can't bind object 1: " + e);
390         }
391
392         try {
393             ic.bind(id2, bso2);
394         } catch (Exception JavaDoc e) {
395             fail("Can't bind object 1: " + e);
396         }
397
398         // lookup objects
399
BasicSerializableObject bsoResult1 = null;
400         BasicSerializableObject bsoResult2 = null;
401
402         try {
403             bsoResult1 = (BasicSerializableObject) PortableRemoteObject.narrow(ic.lookup(id1),
404                     BasicSerializableObject.class);
405             bsoResult2 = (BasicSerializableObject) PortableRemoteObject.narrow(ic.lookup(id2),
406                     BasicSerializableObject.class);
407         } catch (Exception JavaDoc e) {
408             fail("Can't lookup object : " + e);
409         }
410
411         // Now compare objects and they should be different !
412
assertFalse("Should be different : obj1 = " + bsoResult1 + ", obj2 = " + bsoResult2 + ".", bsoResult1
413                 .equals(bsoResult2));
414
415         // Another check. List context and see if there is our two names
416
try {
417             NamingEnumeration JavaDoc ne = ic.list("");
418             boolean found = false;
419             boolean found1 = false;
420             boolean found2 = false;
421             String JavaDoc listNames = "";
422             while (ne.hasMore() && !found) {
423                 NameClassPair JavaDoc ncp = (NameClassPair JavaDoc) ne.next();
424                 String JavaDoc n = ncp.getName();
425                 listNames += n + " : ";
426                 if (n.equals(id1)) {
427                     found1 = true;
428                     continue;
429                 }
430                 if (n.equals(id2)) {
431                     found2 = true;
432                     continue;
433                 }
434
435                 found = found1 && found2;
436             }
437             if (!found1) {
438                 fail("object named " + id1 + " was not find in the list. Names found were : " + listNames);
439             }
440             if (!found2) {
441                 fail("object named " + id2 + " was not find in the list. Names found were : " + listNames);
442             }
443
444         } catch (Exception JavaDoc e) {
445             fail("Can't list registry : " + e);
446         }
447
448         // unbind
449
try {
450             ic.unbind(id1);
451             ic.unbind(id2);
452         } catch (Exception JavaDoc e) {
453             fail("Can't unbind object : " + e);
454         }
455
456     }
457
458     /**
459      * Test basic commands on java:comp/env (ENC) or java:comp environment
460      */

461     public void testJavaCompEnvironment() {
462
463         String JavaDoc tstId = "testJavaEnvironment";
464         // bind
465
try {
466             ic.bind("java:comp/env/testJavaEnvironment", tstId);
467         } catch (Exception JavaDoc e) {
468             fail("Can't bind java:comp/env/testJavaEnvironment " + e);
469         }
470
471         Context JavaDoc javaComp = null;
472         try {
473             javaComp = (Context JavaDoc) ic.lookup("java:comp/");
474         } catch (NamingException JavaDoc e) {
475             fail("Cannot lookup java:comp : " + e);
476         }
477
478         try {
479             javaComp.lookup("env");
480         } catch (NamingException JavaDoc e) {
481             fail("Cannot lookup javaComp.lookup(\"env\") : " + e);
482         }
483
484         Context JavaDoc javaCompEnv = null;
485         try {
486             javaCompEnv = (Context JavaDoc) ic.lookup("java:comp/env");
487         } catch (NamingException JavaDoc e) {
488             fail("Cannot lookup java:comp/env : " + e);
489         }
490
491         try {
492             javaCompEnv.createSubcontext("ejb");
493         } catch (NamingException JavaDoc e) {
494             fail("Cannot use lookup javaCompEnv.createSubcontext(\"ejb\") " + e);
495         }
496
497         // lookup String
498
String JavaDoc resId = null;
499         try {
500             resId = (String JavaDoc) ic.lookup("java:comp/env/testJavaEnvironment");
501         } catch (Exception JavaDoc e) {
502             fail("Can't lookup object java:comp/env/testJavaEnvironment : " + e);
503         }
504
505         assertEquals(tstId, resId);
506
507     }
508
509     /**
510      * Test InitialContext with specific Factory / URL
511      */

512     public void testSingleInitialContext() {
513         Hashtable JavaDoc testEnv = new Hashtable JavaDoc();
514         String JavaDoc providerURL = ConfigurationRepository.getCurrentConfiguration().getProviderURL();
515         testEnv.put(Context.PROVIDER_URL, providerURL);
516
517         Context JavaDoc tmpContext = null;
518         try {
519             tmpContext = new InitialContext JavaDoc(testEnv);
520         } catch (NamingException JavaDoc e) {
521             e.printStackTrace();
522             fail("Cannot build a new initial context with provider url = " + providerURL);
523         }
524
525         String JavaDoc id = "testSingleInitialContext";
526         BasicSerializableObject bso1 = new BasicSerializableObject(id);
527         // bind it
528
try {
529             tmpContext.bind(id, bso1);
530         } catch (Exception JavaDoc e) {
531             fail("Can't bind object 1: " + e);
532         }
533
534         // lookup
535
BasicSerializableObject bsoResult1 = null;
536         try {
537             bsoResult1 = (BasicSerializableObject) PortableRemoteObject.narrow(tmpContext.lookup(id),
538                     BasicSerializableObject.class);
539         } catch (Exception JavaDoc e) {
540             fail("Can't lookup object : " + e);
541         }
542
543         // Now compare objects and they should be the same
544
assertTrue("Should be the same", bso1.equals(bsoResult1));
545
546         // test with adding the JNDI factory now
547
Hashtable JavaDoc testEnv2 = new Hashtable JavaDoc();
548         String JavaDoc initFactory = ConfigurationRepository.getCurrentConfiguration().getProtocol().getInitialContextFactoryClassName();
549         testEnv2.put(Context.PROVIDER_URL, providerURL);
550         testEnv2.put(Context.INITIAL_CONTEXT_FACTORY, initFactory);
551
552         Context JavaDoc tmpContext2 = null;
553         try {
554             tmpContext2 = new InitialContext JavaDoc(testEnv2);
555         } catch (NamingException JavaDoc e) {
556             e.printStackTrace();
557             fail("Cannot build a new initial context with provider url = " + providerURL + " and factory = "
558                     + initFactory);
559         }
560
561         String JavaDoc id2 = "testSingleInitialContext2";
562         BasicSerializableObject bso2 = new BasicSerializableObject(id);
563         // bind it
564
try {
565             tmpContext2.bind(id2, bso2);
566         } catch (Exception JavaDoc e) {
567             fail("Can't bind object 1: " + e);
568         }
569
570         // lookup
571
BasicSerializableObject bsoResult2 = null;
572         try {
573             bsoResult2 = (BasicSerializableObject) PortableRemoteObject.narrow(tmpContext2.lookup(id2),
574                     BasicSerializableObject.class);
575         } catch (Exception JavaDoc e) {
576             fail("Can't lookup object : " + e);
577         }
578
579         // Now compare objects and they should be the same
580
assertTrue("Should be the same", bso2.equals(bsoResult2));
581
582         // test that it fails with invalid properties
583
Hashtable JavaDoc env3 = new Hashtable JavaDoc();
584         env3.put(Context.PROVIDER_URL, "dummy://123.123.123.123:9876");
585         try {
586             new InitialContext JavaDoc(env3);
587             fail("The context should fail with invalid properties");
588         } catch (NamingException JavaDoc e) {
589             // awaited result
590
e.printStackTrace();
591         }
592
593     }
594
595     /**
596      * Test of environment Method
597      * bug #300979
598      */

599     public void testIctxEnvironment() {
600         Hashtable JavaDoc testEnv = null;
601
602         try {
603             testEnv = ic.getEnvironment();
604         } catch (NamingException JavaDoc e) {
605             e.printStackTrace();
606             fail("Cannot get environment");
607         }
608
609         Context JavaDoc newIctx = null;
610         try {
611             newIctx = new InitialContext JavaDoc(testEnv);
612         } catch (NamingException JavaDoc e) {
613             e.printStackTrace();
614             fail("Cannot get ICTX environment");
615         }
616
617         // test factory is the multi factory
618
try {
619             assertTrue(newIctx.getEnvironment().get(Context.INITIAL_CONTEXT_FACTORY).equals(
620                     CarolDefaultValues.MULTI_JNDI));
621         } catch (NamingException JavaDoc ne) {
622             ne.printStackTrace();
623             fail("Should be able to get the environment : " + ne.getMessage());
624         }
625
626         String JavaDoc id = "testIctxEnvironment";
627         BasicSerializableObject bso1 = new BasicSerializableObject(id);
628         // bind it
629
try {
630             newIctx.bind(id, bso1);
631         } catch (Exception JavaDoc e) {
632             fail("Can't bind object 1: " + e);
633         }
634
635         // lookup
636
BasicSerializableObject bsoResult1 = null;
637         try {
638             bsoResult1 = (BasicSerializableObject) PortableRemoteObject.narrow(newIctx.lookup(id),
639                     BasicSerializableObject.class);
640         } catch (Exception JavaDoc e) {
641             fail("Can't lookup object : " + e);
642         }
643
644         // Now compare objects and they should be the same
645
assertTrue("Should be the same", bso1.equals(bsoResult1));
646
647     }
648
649     /**
650      * Suite method
651      * @return the test suite to launch
652      */

653     public static Test suite() {
654         return new TestSuite(MultiProtocolTests.class);
655         // In case of launching only one test
656
/*TestSuite testSuite = new TestSuite();
657         testSuite.addTest(new MultiProtocolTests("testStub"));
658         reurn testSuite;*/

659     }
660 }
Popular Tags