Salome HOME
Merge from V6_main (04/10/2012)
[modules/kernel.git] / src / NamingService / Test / NamingServiceTest.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "NamingServiceTest.hxx"
24 #include "Utils_ORB_INIT.hxx"
25 #include "Utils_SINGLETON.hxx"
26 #include "Basics_Utils.hxx"
27
28 #include <iostream>
29 #include <fstream>
30 #include <string>
31 #include <cstdlib>
32 #include <cstdio>
33
34
35 // --- uncomment to have some traces on standard error
36 //     (useful only when adding new tests...)
37 //#define _DEVDEBUG_
38
39 #ifdef _DEVDEBUG_
40 #define MYDEVTRACE {std::cerr << __FILE__ << " [" << __LINE__ << "] : ";}
41 #define DEVTRACE(msg) {MYDEVTRACE; std::cerr<<msg<<std::endl<<std::flush;}
42 #else
43 #define MYDEVTRACE
44 #define DEVTRACE(msg)
45 #endif
46
47 #ifdef WIN32
48 #define setenv Kernel_Utils::setenv
49 #endif 
50
51 #define TRACEFILE "/tmp/traceUnitTest.log"
52
53 // ============================================================================
54 /*!
55  * a basic CORBA object implementation for use with namingService tests
56  */
57 // ============================================================================
58
59 NSTEST_echo_i::NSTEST_echo_i()
60 {
61   _num=-1;
62 }
63
64 NSTEST_echo_i::NSTEST_echo_i(CORBA::Long num)
65 {
66   _num=num;
67 }
68
69 NSTEST_echo_i::~NSTEST_echo_i()
70 {
71 }
72
73 CORBA::Long NSTEST_echo_i::getId()
74 {
75   return _num;
76 }
77
78 // ============================================================================
79 /*!
80  * a factory of CORBA objects for use with namingService tests
81  */
82 // ============================================================================
83
84 NSTEST_aFactory_i::NSTEST_aFactory_i()
85 {
86   _num=0;
87 }
88
89 NSTEST_aFactory_i::~NSTEST_aFactory_i()
90 {
91 }
92
93 NSTEST::echo_ptr NSTEST_aFactory_i::createInstance()
94 {
95   NSTEST_echo_i * anEcho = new NSTEST_echo_i(_num);
96   _num++;
97   NSTEST::echo_var anEchoRef = anEcho->_this();
98   anEcho->_remove_ref();
99   return anEchoRef._retn();
100 }
101
102 // ============================================================================
103 /*!
104  * Set Trace mecanism
105  * - delete preexisting trace classes if any
106  * - set trace on file
107  * Get or initialize the orb
108  * Create a SALOME_NamingService instance
109  */
110 // ============================================================================
111
112 void 
113 NamingServiceTest::setUp()
114 {
115   LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
116   CPPUNIT_ASSERT(bp1);
117   bp1->deleteInstance(bp1);
118
119   // --- trace on file
120   const char *theFileName = TRACEFILE;
121
122   std::string s = "file:";
123   s += theFileName;
124   //s="local";
125   //s="with_logger";
126   CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
127
128   std::ofstream traceFile;
129   //  traceFile.open(theFileName, ios::out | ios::trunc);
130   traceFile.open(theFileName, std::ios::out | std::ios::app);
131   CPPUNIT_ASSERT(traceFile); // file created empty, then closed
132   traceFile.close();
133
134   bp1 = LocalTraceBufferPool::instance();
135   CPPUNIT_ASSERT(bp1);
136
137   // --- Get or initialize the orb
138
139   int _argc = 1;
140   char* _argv[] = {(char*)""};
141   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
142   ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
143   _orb = init(_argc , _argv ) ;
144
145   // --- Create a SALOME_NamingService instance
146
147   _NS.init_orb(_orb) ;
148
149   // --- Create an NSTEST::factory
150
151   CORBA::Object_var obj = _orb->resolve_initial_references("RootPOA");
152   ASSERT(!CORBA::is_nil(obj));
153   _root_poa = PortableServer::POA::_narrow(obj);
154   _pman = _root_poa->the_POAManager();
155   _myFactory  = new NSTEST_aFactory_i();
156   _myFactoryId = _root_poa->activate_object(_myFactory);
157   _factoryRef = _myFactory->_this();
158   _pman->activate();
159   _myFactory->_remove_ref();
160   
161 }
162
163 // ============================================================================
164 /*!
165  *  - delete trace classes
166  */
167 // ============================================================================
168
169 void 
170 NamingServiceTest::tearDown()
171 {
172
173   LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
174   CPPUNIT_ASSERT(bp1);
175   bp1->deleteInstance(bp1);
176 }
177
178 // ============================================================================
179 /*!
180  *  Test default constructor: must be followed by a call to init_orb(ORB)
181  */
182 // ============================================================================
183
184 void
185 NamingServiceTest::testConstructorDefault()
186 {
187   SALOME_NamingService  NS;
188   //CPPUNIT_ASSERT_THROW(NS.getIORaddr(),CORBA::Exception);
189   NS.init_orb(_orb);
190
191   CORBA::String_var root = NS.getIORaddr();
192   CORBA::Object_var obj = _orb->string_to_object(root);
193   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
194
195   CosNaming::NamingContext_var rootContext =
196     CosNaming::NamingContext::_narrow(obj);
197   CPPUNIT_ASSERT(!CORBA::is_nil(rootContext));
198 }
199
200 // ============================================================================
201 /*!
202  *  Test constructor with ORB parameter
203  */
204 // ============================================================================
205
206 void
207 NamingServiceTest::testConstructorOrb()
208 {
209   SALOME_NamingService  NS(_orb);
210   CORBA::String_var root = NS.getIORaddr();
211   CORBA::Object_var obj = _orb->string_to_object(root);
212   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
213
214   CosNaming::NamingContext_var rootContext =
215     CosNaming::NamingContext::_narrow(obj);
216   CPPUNIT_ASSERT(!CORBA::is_nil(rootContext));
217 }
218
219 // ============================================================================
220 /*!
221  * Test Register and resolve of a single CORBA object with absolute pathname,
222  * without subdirectories
223  */
224 // ============================================================================
225
226 void
227 NamingServiceTest::testRegisterResolveAbsNoPath()
228 {
229   _NS.Register(_factoryRef,"/nstest_factory");
230   CORBA::Object_var obj = _NS.Resolve("/nstest_factory");
231   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
232   NSTEST::aFactory_var myFactory = NSTEST::aFactory::_narrow(obj);
233   CPPUNIT_ASSERT(!CORBA::is_nil(myFactory));
234 }
235
236 // ============================================================================
237 /*!
238  * Test Register and resolve of a single CORBA object with relative pathname,
239  * without subdirectories
240  */
241 // ============================================================================
242
243 void
244 NamingServiceTest::testRegisterResolveRelativeNoPath()
245 {
246   CORBA::Object_var obj = _NS.Resolve("/nstest_factory");
247   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
248   NSTEST::aFactory_var myFactory = NSTEST::aFactory::_narrow(obj);
249   CPPUNIT_ASSERT(!CORBA::is_nil(myFactory));
250
251   _NS.Create_Directory("/myContext");
252   _NS.Change_Directory("/myContext");
253
254   NSTEST::echo_var anEchoRef = myFactory->createInstance();
255   _NS.Register(anEchoRef,"echo_0");
256
257   obj = _NS.Resolve("echo_0");
258   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
259   NSTEST::echo_var anEchoRef2 = NSTEST::echo::_narrow(obj);
260   CPPUNIT_ASSERT(!CORBA::is_nil(anEchoRef2));
261   CPPUNIT_ASSERT(anEchoRef->getId() == anEchoRef2->getId());
262
263   obj = _NS.Resolve("/myContext/echo_0");
264   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
265   NSTEST::echo_var anEchoRef3 = NSTEST::echo::_narrow(obj);
266   CPPUNIT_ASSERT(!CORBA::is_nil(anEchoRef3));
267   CPPUNIT_ASSERT(anEchoRef->getId() == anEchoRef3->getId());
268 }
269
270 // ============================================================================
271 /*!
272  * Test Register and resolve of a single CORBA object with absolute pathname,
273  * in a subdirectory
274  */
275 // ============================================================================
276
277 void
278 NamingServiceTest::testRegisterResolveAbsWithPath()
279 {
280   CORBA::Object_var obj = _NS.Resolve("/nstest_factory");
281   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
282   NSTEST::aFactory_var myFactory = NSTEST::aFactory::_narrow(obj);
283   CPPUNIT_ASSERT(!CORBA::is_nil(myFactory));
284
285   NSTEST::echo_var anEchoRef = myFactory->createInstance();
286   _NS.Register(anEchoRef,"/nstest/echo_0");
287
288   obj = _NS.Resolve("/nstest/echo_0");
289   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
290   NSTEST::echo_var anEchoRefa = NSTEST::echo::_narrow(obj);
291   CPPUNIT_ASSERT(!CORBA::is_nil(anEchoRefa));
292   CPPUNIT_ASSERT(anEchoRef->getId() == anEchoRefa->getId());
293
294   NSTEST::echo_var anEchoRef1 = myFactory->createInstance();
295   _NS.Register(anEchoRef1,"/nstest2/rep2/rep3/echo_1");
296   CPPUNIT_ASSERT(anEchoRef->getId() != anEchoRef1->getId());
297
298   obj = _NS.Resolve("/nstest2/rep2/rep3/echo_1");
299   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
300   NSTEST::echo_var anEchoRef1a = NSTEST::echo::_narrow(obj);
301   CPPUNIT_ASSERT(!CORBA::is_nil(anEchoRef1a));
302   CPPUNIT_ASSERT(anEchoRef1->getId() == anEchoRef1a->getId());
303
304   NSTEST::echo_var anEchoRef2 = myFactory->createInstance();
305   _NS.Register(anEchoRef2,"/nstest2/1/2/3/4/echo_1");
306
307   obj = _NS.Resolve("/nstest2/1/2/3/4/echo_1");
308   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
309   NSTEST::echo_var anEchoRef2a = NSTEST::echo::_narrow(obj);
310   CPPUNIT_ASSERT(!CORBA::is_nil(anEchoRef2a));
311   CPPUNIT_ASSERT(anEchoRef2->getId() == anEchoRef2a->getId());
312 }
313
314 // ============================================================================
315 /*!
316  * Test Register and resolve of a single CORBA object with relative pathname,
317  * in a subdirectory.
318  * Relative Path is changed to the created subdirectory when Register()
319  */
320 // ============================================================================
321
322 void
323 NamingServiceTest::testRegisterResolveRelativeWithPath()
324 {
325   CORBA::Object_var obj = _NS.Resolve("/nstest_factory");
326   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
327   NSTEST::aFactory_var myFactory = NSTEST::aFactory::_narrow(obj);
328   CPPUNIT_ASSERT(!CORBA::is_nil(myFactory));
329   _NS.Create_Directory("/myContext");
330   _NS.Change_Directory("/myContext");
331
332   NSTEST::echo_var anEchoRef = myFactory->createInstance();
333   _NS.Register(anEchoRef,"subdir/echo_0");
334
335   obj = _NS.Resolve("echo_0");
336   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
337   NSTEST::echo_var anEchoRef2 = NSTEST::echo::_narrow(obj);
338   CPPUNIT_ASSERT(!CORBA::is_nil(anEchoRef2));
339
340   obj = _NS.Resolve("/myContext/subdir/echo_0");
341   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
342   NSTEST::echo_var anEchoRef3 = NSTEST::echo::_narrow(obj);
343   CPPUNIT_ASSERT(!CORBA::is_nil(anEchoRef3));
344   CPPUNIT_ASSERT(anEchoRef->getId() == anEchoRef3->getId());
345
346   _NS.Change_Directory("/myContext");
347   obj = _NS.Resolve("subdir/echo_0");
348   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
349   NSTEST::echo_var anEchoRef4 = NSTEST::echo::_narrow(obj);
350   CPPUNIT_ASSERT(!CORBA::is_nil(anEchoRef4));
351   CPPUNIT_ASSERT(anEchoRef->getId() == anEchoRef4->getId());
352 }
353
354 // ============================================================================
355 /*!
356  * Test resolve with a name not known
357  */
358 // ============================================================================
359
360 void
361 NamingServiceTest::testResolveBadName()
362 {
363   CORBA::Object_var obj = _NS.Resolve("/notRegisteredName");
364   CPPUNIT_ASSERT(CORBA::is_nil(obj));
365
366   obj = _NS.Resolve("/nstest/notRegisteredName");
367   CPPUNIT_ASSERT(CORBA::is_nil(obj));
368
369   obj = _NS.Resolve("/unknownPath/notRegisteredName");
370   CPPUNIT_ASSERT(CORBA::is_nil(obj));
371
372   obj = _NS.Resolve("/anUnknown/ComplicatedPath/notRegisteredName");
373   CPPUNIT_ASSERT(CORBA::is_nil(obj));
374 }
375
376 // ============================================================================
377 /*!
378  * Test resolve with a name not known, with a relative path
379  */
380 // ============================================================================
381
382 void
383 NamingServiceTest::testResolveBadNameRelative()
384 {
385   _NS.Create_Directory("/myContext");
386   _NS.Change_Directory("/myContext");
387
388   CORBA::Object_var obj = _NS.Resolve("notRegisteredName");
389   CPPUNIT_ASSERT(CORBA::is_nil(obj));
390
391   obj = _NS.Resolve("unknownPath/notRegisteredName");
392   CPPUNIT_ASSERT(CORBA::is_nil(obj));
393
394   obj = _NS.Resolve("anUnknown/ComplicatedPath/notRegisteredName");
395   CPPUNIT_ASSERT(CORBA::is_nil(obj));
396 }
397
398 // ============================================================================
399 /*!
400  * Test register and resolve multiple objects, test resolveFirst.
401  * Register a few objects in /nstestfirst/echo_n where n is the object id.
402  * Resolve all the objects.
403  * ResolveFirst /nstestfirst/echo must give /nstestfirst/echo_i, corresponding
404  * to the first object.
405  */
406 // ============================================================================
407
408 #define NB_OBJS 10
409
410 void
411 NamingServiceTest::testResolveFirst()
412 {
413   CORBA::Object_var obj = _NS.Resolve("/nstest_factory");
414   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
415   NSTEST::aFactory_var myFactory = NSTEST::aFactory::_narrow(obj);
416   CPPUNIT_ASSERT(!CORBA::is_nil(myFactory));
417
418   int ref[NB_OBJS];
419
420   for (int i=0; i<NB_OBJS; i++)
421     {
422       NSTEST::echo_var anEchoRef = myFactory->createInstance();
423       ref[i] = anEchoRef->getId();
424       std::string name = "/nstestfirst/echo_";
425       char anum[10];
426       sprintf(anum,"%d",ref[i]);
427       name += anum;
428       _NS.Register(anEchoRef,name.c_str());
429     }
430
431   for (int i=0; i<NB_OBJS; i++)
432     {
433       std::string name = "/nstestfirst/echo_";
434       char anum[10];
435       sprintf(anum,"%d",ref[i]);
436       name += anum;
437       obj = _NS.Resolve(name.c_str());
438       CPPUNIT_ASSERT(!CORBA::is_nil(obj));
439       NSTEST::echo_var anEchoRef = NSTEST::echo::_narrow(obj);
440       CPPUNIT_ASSERT(!CORBA::is_nil(anEchoRef));
441       CPPUNIT_ASSERT(anEchoRef->getId() == ref[i]);
442     }
443
444   std::string name = "/nstestfirst/echo";
445   obj = _NS.ResolveFirst(name.c_str());
446   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
447   NSTEST::echo_var anEchoRef = NSTEST::echo::_narrow(obj);
448   CPPUNIT_ASSERT(!CORBA::is_nil(anEchoRef));
449   CPPUNIT_ASSERT(anEchoRef->getId() == ref[0]);
450 }
451
452 // ============================================================================
453 /*!
454  * Test register and resolve multiple objects, test resolveFirst, relative path
455  * Register a few objects in /nstestfirstrel/echo_n where n is the object id.
456  * Resolve all the objects.
457  * ResolveFirst echo with a relative path /nstestfirstrel must give 
458  * /nstestfirst/echo_i, corresponding to the first object.
459  */
460 // ============================================================================
461
462 void
463 NamingServiceTest::testResolveFirstRelative()
464 {
465   CORBA::Object_var obj = _NS.Resolve("/nstest_factory");
466   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
467   NSTEST::aFactory_var myFactory = NSTEST::aFactory::_narrow(obj);
468   CPPUNIT_ASSERT(!CORBA::is_nil(myFactory));
469
470   int ref[NB_OBJS];
471
472   for (int i=0; i<NB_OBJS; i++)
473     {
474       NSTEST::echo_var anEchoRef = myFactory->createInstance();
475       ref[i] = anEchoRef->getId();
476       std::string name = "/nstestfirstrel/echo_";
477       char anum[10];
478       sprintf(anum,"%d",ref[i]);
479       name += anum;
480       _NS.Register(anEchoRef,name.c_str());
481     }
482
483   for (int i=0; i<NB_OBJS; i++)
484     {
485       _NS.Change_Directory("/nstestfirstrel");
486       std::string name = "echo_";
487       char anum[10];
488       sprintf(anum,"%d",ref[i]);
489       name += anum;
490       obj = _NS.Resolve(name.c_str());
491       CPPUNIT_ASSERT(!CORBA::is_nil(obj));
492       NSTEST::echo_var anEchoRef = NSTEST::echo::_narrow(obj);
493       CPPUNIT_ASSERT(!CORBA::is_nil(anEchoRef));
494       CPPUNIT_ASSERT(anEchoRef->getId() == ref[i]);
495     }
496
497   _NS.Change_Directory("/nstestfirstrel");
498   std::string name = "echo";
499   obj = _NS.ResolveFirst(name.c_str());
500   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
501 }
502
503 // ============================================================================
504 /*!
505  * Test resolveFirst with unknown name
506  */
507 // ============================================================================
508
509 void
510 NamingServiceTest::testResolveFirstUnknown()
511 {
512   std::string name = "/notYeyRegistered";
513   CORBA::Object_var obj= _NS.ResolveFirst(name.c_str());
514   CPPUNIT_ASSERT(CORBA::is_nil(obj));
515
516   name = "/nstestfirst/notYeyRegistered";
517   obj = _NS.ResolveFirst(name.c_str());
518   CPPUNIT_ASSERT(CORBA::is_nil(obj));
519
520   name = "/rrr/sss/ttt/notYeyRegistered";
521   obj = _NS.ResolveFirst(name.c_str());
522   CPPUNIT_ASSERT(CORBA::is_nil(obj));
523 }
524
525 // ============================================================================
526 /*!
527  * Test resolveFirst with unknown name, relative Path
528  */
529 // ============================================================================
530
531 void
532 NamingServiceTest::testResolveFirstUnknownRelative()
533 {
534   _NS.Create_Directory("/myContext");
535   _NS.Change_Directory("/myContext");
536
537   std::string name = "RelnotYeyRegistered";
538   CORBA::Object_var obj = _NS.ResolveFirst(name.c_str());
539   CPPUNIT_ASSERT(CORBA::is_nil(obj));
540
541   name = "Relnstestfirst/notYeyRegistered";
542   obj = _NS.ResolveFirst(name.c_str());
543   CPPUNIT_ASSERT(CORBA::is_nil(obj));
544
545   name = "Relrrr/sss/ttt/notYeyRegistered";
546   obj = _NS.ResolveFirst(name.c_str());
547   CPPUNIT_ASSERT(CORBA::is_nil(obj));
548 }
549
550 // ============================================================================
551 /*!
552  * Test ResolveComponent works as specified
553  */
554 // ============================================================================
555
556 void
557 NamingServiceTest::testResolveComponentOK()
558 {
559   CORBA::Object_var obj = _NS.Resolve("/nstest_factory");
560   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
561   NSTEST::aFactory_var myFactory = NSTEST::aFactory::_narrow(obj);
562   CPPUNIT_ASSERT(!CORBA::is_nil(myFactory));
563
564   NSTEST::echo_var anEchoRef = myFactory->createInstance();
565   _NS.Register(anEchoRef,
566                "/Containers/theHostName/theContainerName/theComponentName");
567
568   obj = _NS.ResolveComponent("theHostName",
569                              "theContainerName",
570                              "theComponentName");
571   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
572   NSTEST::echo_var anEchoRefa = NSTEST::echo::_narrow(obj);
573   CPPUNIT_ASSERT(!CORBA::is_nil(anEchoRefa));
574   CPPUNIT_ASSERT(anEchoRefa->getId() == anEchoRef->getId());
575
576
577   NSTEST::echo_var anEchoRef2 = myFactory->createInstance();
578   _NS.Register(anEchoRef2,
579                "/Containers/theHostName/theContainerName_2/theComponentName");
580
581   obj = _NS.ResolveComponent("theHostName",
582                              "theContainerName",
583                              "theComponentName",
584                              2);
585   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
586   NSTEST::echo_var anEchoRefb = NSTEST::echo::_narrow(obj);
587   CPPUNIT_ASSERT(!CORBA::is_nil(anEchoRefb));
588   CPPUNIT_ASSERT(anEchoRefb->getId() == anEchoRef2->getId());
589 }
590
591 // ============================================================================
592 /*!
593  * Test ResolveComponent gives nil pointer if hostname is not given (empty)
594  */
595 // ============================================================================
596
597 void
598 NamingServiceTest::testResolveComponentEmptyHostname()
599 {
600   CORBA::Object_var obj = _NS.Resolve("/nstest_factory");
601   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
602   NSTEST::aFactory_var myFactory = NSTEST::aFactory::_narrow(obj);
603   CPPUNIT_ASSERT(!CORBA::is_nil(myFactory));
604
605   NSTEST::echo_var anEchoRef = myFactory->createInstance();
606   _NS.Register(anEchoRef,
607                "/Containers/theHostName/theContainerName/theComponentName");
608
609   obj = _NS.ResolveComponent("",
610                              "theContainerName",
611                              "theComponentName");
612   CPPUNIT_ASSERT(CORBA::is_nil(obj));
613 }
614
615 // ============================================================================
616 /*!
617  * Test ResolveComponent gives nil pointer if hostname is unknown
618  */
619 // ============================================================================
620
621 void
622 NamingServiceTest::testResolveComponentUnknownHostname()
623 {
624   CORBA::Object_var obj = _NS.Resolve("/nstest_factory");
625   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
626   NSTEST::aFactory_var myFactory = NSTEST::aFactory::_narrow(obj);
627   CPPUNIT_ASSERT(!CORBA::is_nil(myFactory));
628
629   NSTEST::echo_var anEchoRef = myFactory->createInstance();
630   _NS.Register(anEchoRef,
631                "/Containers/theHostName/theContainerName/theComponentName");
632
633   obj = _NS.ResolveComponent("anUnknownHostName",
634                              "theContainerName",
635                              "theComponentName");
636   CPPUNIT_ASSERT(CORBA::is_nil(obj));
637 }
638
639 // ============================================================================
640 /*!
641  * Test ResolveComponent when containerName is empty.
642  * check bad hostname gives nil pointer.
643  * If componentName registered on a container from hostname, a component
644  * reference is found (the first one).
645  * Else give nil pointer.
646  */
647 // ============================================================================
648
649 void
650 NamingServiceTest::testResolveComponentEmptyContainerName()
651 {
652   CORBA::Object_var obj = _NS.Resolve("/nstest_factory");
653   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
654   NSTEST::aFactory_var myFactory = NSTEST::aFactory::_narrow(obj);
655   CPPUNIT_ASSERT(!CORBA::is_nil(myFactory));
656
657   NSTEST::echo_var anEchoRef = myFactory->createInstance();
658   _NS.Register(anEchoRef,
659                "/Containers/theHostName/theContainerName/theComponentName");
660
661   NSTEST::echo_var anEchoRef2 = myFactory->createInstance();
662   _NS.Register(anEchoRef2,
663                "/Containers/theHostName/aContainerName/aComponentName");
664
665   NSTEST::echo_var anEchoRef3 = myFactory->createInstance();
666   _NS.Register(anEchoRef3,
667                "/Containers/theHostName/otherContainerName/theComponentName");
668
669   obj = _NS.ResolveComponent("anUnknownHostName",
670                              "",
671                              "theComponentName");
672   CPPUNIT_ASSERT(CORBA::is_nil(obj));
673
674   obj = _NS.ResolveComponent("theHostName",
675                              "",
676                              "theComponentName");
677   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
678   NSTEST::echo_var anEchoRefa = NSTEST::echo::_narrow(obj);
679   CPPUNIT_ASSERT(!CORBA::is_nil(anEchoRefa));
680   CPPUNIT_ASSERT(anEchoRefa->getId() == anEchoRef->getId());
681 }
682
683 // ============================================================================
684 /*!
685  * Test 
686  */
687 // ============================================================================
688
689 void
690 NamingServiceTest::testResolveComponentUnknownContainerName()
691 {
692   CORBA::Object_var obj = _NS.Resolve("/nstest_factory");
693   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
694   NSTEST::aFactory_var myFactory = NSTEST::aFactory::_narrow(obj);
695   CPPUNIT_ASSERT(!CORBA::is_nil(myFactory));
696
697   NSTEST::echo_var anEchoRef = myFactory->createInstance();
698   _NS.Register(anEchoRef,
699                "/Containers/theHostName/theContainerName/theComponentName");
700
701   NSTEST::echo_var anEchoRef2 = myFactory->createInstance();
702   _NS.Register(anEchoRef2,
703                "/Containers/theHostName/aContainerName/aComponentName");
704
705   NSTEST::echo_var anEchoRef3 = myFactory->createInstance();
706   _NS.Register(anEchoRef3,
707                "/Containers/theHostName/otherContainerName/theComponentName");
708
709   obj = _NS.ResolveComponent("theHostName",
710                              "anUnknownContainerName",
711                              "theComponentName");
712   CPPUNIT_ASSERT(CORBA::is_nil(obj));
713 }
714
715 // ============================================================================
716 /*!
717  * Test 
718  */
719 // ============================================================================
720
721 void
722 NamingServiceTest::testResolveComponentEmptyComponentName()
723 {
724   CORBA::Object_var obj = _NS.Resolve("/nstest_factory");
725   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
726   NSTEST::aFactory_var myFactory = NSTEST::aFactory::_narrow(obj);
727   CPPUNIT_ASSERT(!CORBA::is_nil(myFactory));
728
729   NSTEST::echo_var anEchoRef = myFactory->createInstance();
730   _NS.Register(anEchoRef,
731                "/Containers/theHostName/theContainerName/theComponentName");
732
733   NSTEST::echo_var anEchoRef2 = myFactory->createInstance();
734   _NS.Register(anEchoRef2,
735                "/Containers/theHostName/EmptyContainerName/");
736
737   obj = _NS.ResolveComponent("theHostName",
738                              "EmptyContainerName",
739                              "");
740   CPPUNIT_ASSERT(CORBA::is_nil(obj));
741 }
742
743 // ============================================================================
744 /*!
745  * Test 
746  */
747 // ============================================================================
748
749 void
750 NamingServiceTest::testResolveComponentUnknownComponentName()
751 {
752   CORBA::Object_var obj = _NS.Resolve("/nstest_factory");
753   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
754   NSTEST::aFactory_var myFactory = NSTEST::aFactory::_narrow(obj);
755   CPPUNIT_ASSERT(!CORBA::is_nil(myFactory));
756
757   NSTEST::echo_var anEchoRef = myFactory->createInstance();
758   _NS.Register(anEchoRef,
759                "/Containers/theHostName/theContainerName/theComponentName");
760
761   obj = _NS.ResolveComponent("theHostName",
762                              "theContainerName",
763                              "anUnknownComponentName");
764   CPPUNIT_ASSERT(CORBA::is_nil(obj));
765 }
766
767 // ============================================================================
768 /*!
769  * Test with a false number nbproc. 
770  * A positive number not corresponding to a registered component gives nil ref.
771  * A negative number is not taken into account and may give a non nil ref.
772  */
773 // ============================================================================
774
775 void
776 NamingServiceTest::testResolveComponentFalseNbproc()
777 {
778   CORBA::Object_var obj = _NS.Resolve("/nstest_factory");
779   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
780   NSTEST::aFactory_var myFactory = NSTEST::aFactory::_narrow(obj);
781   CPPUNIT_ASSERT(!CORBA::is_nil(myFactory));
782
783   NSTEST::echo_var anEchoRef = myFactory->createInstance();
784   _NS.Register(anEchoRef,
785                "/Containers/theHostName/theContainerName/theComponentName");
786
787   obj = _NS.ResolveComponent("theHostName",
788                              "theContainerName",
789                              "theComponentName",
790                              25);
791   CPPUNIT_ASSERT(CORBA::is_nil(obj));
792
793   obj = _NS.ResolveComponent("theHostName",
794                              "theContainerName",
795                              "theComponentName",
796                              -25);
797   CPPUNIT_ASSERT(! CORBA::is_nil(obj));
798 }
799
800 // ============================================================================
801 /*!
802  * Test 
803  */
804 // ============================================================================
805
806 void
807 NamingServiceTest::testContainerName()
808 {
809   std::string ref0 = "FactoryServer";
810   std::string ret = _NS.ContainerName("");
811   CPPUNIT_ASSERT(ret == ref0);
812
813   ref0 = "MyContainerName";
814   ret = _NS.ContainerName(ref0.c_str());
815   CPPUNIT_ASSERT(ret == ref0);
816 }
817
818 // ============================================================================
819 /*!
820  * Test 
821  */
822 // ============================================================================
823
824 void
825 NamingServiceTest::testContainerNameParams()
826 {
827   Engines::MachineParameters params;
828   params.container_name = "";
829   params.hostname = "";
830   params.OS = "";
831   params.mem_mb = 0;
832   params.cpu_clock = 0;
833   params.nb_proc_per_node = 0;
834   params.nb_node = 0;
835   params.isMPI = false;
836
837   std::string ref0 = "FactoryServer";
838   std::string ret = _NS.ContainerName(params);
839   CPPUNIT_ASSERT(ret == ref0);
840
841   ref0 = "MyContainerName";
842   params.container_name = ref0.c_str();
843   ret = _NS.ContainerName(params);
844   CPPUNIT_ASSERT(ret == ref0);
845 }
846
847 // ============================================================================
848 /*!
849  * Test 
850  */
851 // ============================================================================
852
853 void
854 NamingServiceTest::testBuildContainerNameForNS()
855 {
856   std::string ref0 = "/Containers/theHostName/theContainerName";
857   std::string ret = _NS.BuildContainerNameForNS("theContainerName","theHostName");
858   CPPUNIT_ASSERT(ret == ref0);
859
860   ref0 = "/Containers/theHostName/FactoryServer";
861   ret = _NS.BuildContainerNameForNS("","theHostName");
862   CPPUNIT_ASSERT(ret == ref0);
863 }
864
865 // ============================================================================
866 /*!
867  * Test 
868  */
869 // ============================================================================
870
871 void
872 NamingServiceTest::testBuildContainerNameForNSParams()
873 {
874   Engines::MachineParameters params;
875   params.container_name = "";
876   params.hostname = "";
877   params.OS = "";
878   params.mem_mb = 0;
879   params.cpu_clock = 0;
880   params.nb_proc_per_node = 0;
881   params.nb_node = 0;
882   params.isMPI = false;
883
884   params.container_name = "theContainerName";
885   std::string ref0 = "/Containers/theHostName/theContainerName";
886   std::string ret = _NS.BuildContainerNameForNS(params,"theHostName");
887   CPPUNIT_ASSERT(ret == ref0);
888
889   params.container_name = "";
890   ref0 = "/Containers/theHostName/FactoryServer";
891   ret = _NS.BuildContainerNameForNS(params,"theHostName");
892   CPPUNIT_ASSERT(ret == ref0);
893 }
894
895 // ============================================================================
896 /*!
897  * Test 
898  */
899 // ============================================================================
900
901 void
902 NamingServiceTest::testFind()
903 {
904   CORBA::Object_var obj = _NS.Resolve("/nstest_factory");
905   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
906   NSTEST::aFactory_var myFactory = NSTEST::aFactory::_narrow(obj);
907   CPPUNIT_ASSERT(!CORBA::is_nil(myFactory));
908
909   NSTEST::echo_var anEchoRef = myFactory->createInstance();
910   _NS.Register(anEchoRef,
911                "/Containers/theHostName/theContainerName/theComponentName");
912
913   NSTEST::echo_var anEchoRef2 = myFactory->createInstance();
914   _NS.Register(anEchoRef2,
915                "/Containers/theHostName/aContainerName/aComponentName");
916
917   NSTEST::echo_var anEchoRef3 = myFactory->createInstance();
918   _NS.Register(anEchoRef3,
919                "/Containers/theHostName/otherContainerName/theComponentName");
920
921   NSTEST::echo_var anEchoRef4 = myFactory->createInstance();
922   _NS.Register(anEchoRef4,
923                "/Containers/anHostName/oneContainerName/theComponentName");
924
925   _NS.Change_Directory("/Containers");
926   int occ= _NS.Find("theComponentName");
927   CPPUNIT_ASSERT(occ >= 3); // see previous tests
928
929   _NS.Change_Directory("/Containers");
930   occ= _NS.Find("aComponentName");
931   CPPUNIT_ASSERT(occ == 1);
932
933   _NS.Change_Directory("/Containers");
934   occ= _NS.Find("UnknownCompnentName");
935   CPPUNIT_ASSERT(occ == 0);
936 }
937
938 // ============================================================================
939 /*!
940  * Test 
941  */
942 // ============================================================================
943
944 void
945 NamingServiceTest::testCreateDirectory()
946 {
947   CORBA::Object_var obj = _NS.Resolve("/nstest_factory");
948   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
949   NSTEST::aFactory_var myFactory = NSTEST::aFactory::_narrow(obj);
950   CPPUNIT_ASSERT(!CORBA::is_nil(myFactory));
951
952   bool ret = _NS.Create_Directory("/aaa/bbb/ccc/ddd/eee");
953   CPPUNIT_ASSERT(ret);
954
955   _NS.Change_Directory("/aaa/bbb/ccc/ddd/eee");
956   NSTEST::echo_var anEchoRef = myFactory->createInstance();
957   int val = anEchoRef->getId();
958   std::string name = "echo_";
959   char anum[10];
960   sprintf(anum,"%d",val);
961   name += anum;
962   _NS.Register(anEchoRef,name.c_str());
963
964   std::string dirname = "/aaa/bbb/ccc/ddd/eee/";
965   dirname += name;
966   obj = _NS.Resolve(dirname.c_str());
967   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
968   NSTEST::echo_var anEchoRef2 = NSTEST::echo::_narrow(obj);
969   CPPUNIT_ASSERT(!CORBA::is_nil(anEchoRef2));
970   CPPUNIT_ASSERT(anEchoRef->getId() == anEchoRef2->getId());
971
972   ret = _NS.Create_Directory("/aaa/bbb/ccc/ddd/eee");
973   CPPUNIT_ASSERT(ret);
974
975   _NS.Change_Directory("/aaa/bbb");
976   ret = _NS.Create_Directory("cccccc/dddddd/eeeeee");
977   _NS.Register(anEchoRef,"echo_abcde");
978
979   CPPUNIT_ASSERT(ret);
980   _NS.Change_Directory("/aaa/bbb/cccccc/dddddd/eeeeee");
981   obj = _NS.Resolve("echo_abcde");
982   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
983 }
984
985 // ============================================================================
986 /*!
987  * Test 
988  */
989 // ============================================================================
990
991 void
992 NamingServiceTest::testChangeDirectory()
993 {
994   CORBA::Object_var obj = _NS.Resolve("/nstest_factory");
995   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
996   NSTEST::aFactory_var myFactory = NSTEST::aFactory::_narrow(obj);
997   CPPUNIT_ASSERT(!CORBA::is_nil(myFactory));
998
999   NSTEST::echo_var anEchoRef = myFactory->createInstance();
1000   _NS.Register(anEchoRef,
1001                "/Containers/theHostName/theContainerName/theComponentName");
1002
1003   NSTEST::echo_var anEchoRef2 = myFactory->createInstance();
1004   _NS.Register(anEchoRef2,
1005                "/Containers/theHostName/aContainerName/aComponentName");
1006
1007   NSTEST::echo_var anEchoRef3 = myFactory->createInstance();
1008   _NS.Register(anEchoRef3,
1009                "/Containers/theHostName/otherContainerName/theComponentName");
1010
1011   NSTEST::echo_var anEchoRef4 = myFactory->createInstance();
1012   _NS.Register(anEchoRef4,
1013                "/Containers/anHostName/oneContainerName/theComponentName");
1014   
1015   _NS.Change_Directory("/Containers/theHostName/otherContainerName");
1016   obj = _NS.Resolve("theComponentName");
1017   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
1018   
1019   NSTEST::echo_var anEchoRefa = NSTEST::echo::_narrow(obj);
1020   CPPUNIT_ASSERT(!CORBA::is_nil(anEchoRefa));
1021   CPPUNIT_ASSERT(anEchoRefa->getId() == anEchoRef3->getId());
1022 }
1023
1024 // ============================================================================
1025 /*!
1026  * Test 
1027  */
1028 // ============================================================================
1029
1030 void
1031 NamingServiceTest::testCurrentDirectory()
1032 {
1033   std::string path = "/aaa/bbb/ccc/ddd/eee";
1034   bool ret = _NS.Create_Directory(path.c_str());
1035   CPPUNIT_ASSERT(ret);
1036
1037   _NS.Change_Directory(path.c_str());
1038   char* acurdir = _NS.Current_Directory();
1039   std::string curdir = acurdir;
1040   free(acurdir);
1041   CPPUNIT_ASSERT(curdir == path);
1042 }
1043
1044 // ============================================================================
1045 /*!
1046  * Test 
1047  */
1048 // ============================================================================
1049
1050 void
1051 NamingServiceTest::testList()
1052 {
1053   _NS.Change_Directory("/Containers/theHostName/theContainerName");
1054   _NS.list();
1055   _NS.Change_Directory("/Containers");
1056   _NS.list();
1057 }
1058
1059 // ============================================================================
1060 /*!
1061  * Test 
1062  */
1063 // ============================================================================
1064
1065 void
1066 NamingServiceTest::testListDirectory()
1067 {
1068   _NS.Change_Directory("/Containers/theHostName/theContainerName");
1069   _NS.list_directory();
1070   _NS.Change_Directory("/Containers");
1071   _NS.list_directory();
1072 }
1073
1074 // ============================================================================
1075 /*!
1076  * Test 
1077  */
1078 // ============================================================================
1079
1080 void
1081 NamingServiceTest::testListDirectoryRecurs()
1082 {
1083   _NS.Change_Directory("/Containers/theHostName/theContainerName");
1084   _NS.list_directory_recurs();
1085   _NS.Change_Directory("/Containers");
1086   _NS.list_directory_recurs();
1087   _NS.Change_Directory("/");
1088   _NS.list_directory_recurs();
1089 }
1090
1091
1092 // ============================================================================
1093 /*!
1094  * Test 
1095  */
1096 // ============================================================================
1097
1098 void
1099 NamingServiceTest::testListSubdirs()
1100 {
1101   _NS.Change_Directory("/Containers/theHostName/theContainerName");
1102   _NS.list_subdirs();
1103   _NS.Change_Directory("/Containers");
1104   _NS.list_subdirs();
1105   _NS.Change_Directory("/");
1106   _NS.list_subdirs();
1107 }
1108 // ============================================================================
1109 /*!
1110  * Test 
1111  */
1112 // ============================================================================
1113
1114 void
1115 NamingServiceTest::testDestroyName()
1116 {
1117   CORBA::Object_var obj = _NS.Resolve("/nstest_factory");
1118   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
1119   NSTEST::aFactory_var myFactory = NSTEST::aFactory::_narrow(obj);
1120   CPPUNIT_ASSERT(!CORBA::is_nil(myFactory));
1121
1122   std::string path = "/Containers/theHostName/theContainerName/theComponentName";
1123
1124   NSTEST::echo_var anEchoRef = myFactory->createInstance();
1125   _NS.Register(anEchoRef, path.c_str());
1126
1127   obj=_NS.Resolve(path.c_str());
1128   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
1129
1130   _NS.Destroy_Name(path.c_str());
1131   obj=_NS.Resolve(path.c_str());
1132   CPPUNIT_ASSERT(CORBA::is_nil(obj));
1133 }
1134
1135 // ============================================================================
1136 /*!
1137  * Test 
1138  */
1139 // ============================================================================
1140
1141 void
1142 NamingServiceTest::testDestroyDirectory()
1143 {
1144   CORBA::Object_var obj = _NS.Resolve("/nstest_factory");
1145   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
1146   NSTEST::aFactory_var myFactory = NSTEST::aFactory::_narrow(obj);
1147   CPPUNIT_ASSERT(!CORBA::is_nil(myFactory));
1148
1149   std::string path = "/Containers/theHostName/theContainerName/theComponentName";
1150
1151   NSTEST::echo_var anEchoRef = myFactory->createInstance();
1152   _NS.Register(anEchoRef, path.c_str());
1153
1154   _NS.Destroy_Directory("/Containers/theHostName/theContainerName");
1155   obj=_NS.Resolve(path.c_str());
1156   CPPUNIT_ASSERT(!CORBA::is_nil(obj)); // directory not empty: not destroyed
1157
1158   _NS.Destroy_Name(path.c_str());
1159   _NS.Destroy_Directory("/Containers/theHostName/theContainerName");
1160   _NS.Change_Directory("/Containers/theHostName");
1161   _NS.list_subdirs();
1162 }
1163
1164 // ============================================================================
1165 /*!
1166  * DestroyFullDirectory is not recursive
1167  * Need Housekeeping of /Containers for further tests !
1168  */
1169 // ============================================================================
1170
1171 void NamingServiceTest::_destroyDirectoryRecurs(std::string path)
1172 {
1173   std::string current = path;
1174   SCRUTE(path);
1175   if (_NS.Change_Directory(path.c_str()))
1176     {
1177       std::vector<std::string> subdirs = _NS.list_subdirs();
1178       for (int i=0; i<subdirs.size(); i++)
1179         {
1180           std::string subpath=path + "/" +subdirs[i];
1181           _destroyDirectoryRecurs(subpath);
1182         }
1183       if (_NS.Change_Directory(path.c_str()))
1184         {
1185           _NS.Destroy_FullDirectory(path.c_str());
1186         }
1187     }
1188 }
1189
1190 void
1191 NamingServiceTest::testDestroyFullDirectory()
1192 {
1193   _NS.Destroy_FullDirectory("/Containers");
1194   CPPUNIT_ASSERT(_NS.Change_Directory("/Containers"));
1195   std::vector<std::string> subdirs = _NS.list_subdirs();
1196   CPPUNIT_ASSERT(subdirs.size() >0);
1197   _NS.list_directory_recurs();
1198   std::string path = "/Containers";
1199   _destroyDirectoryRecurs(path);
1200   CPPUNIT_ASSERT( ! _NS.Change_Directory("/Containers"));
1201   _NS.Change_Directory("/");
1202   _NS.list_subdirs();
1203   _NS.list_directory_recurs();
1204 }
1205
1206 // ============================================================================
1207 /*!
1208  * Test 
1209  */
1210 // ============================================================================
1211
1212 void
1213 NamingServiceTest::testGetIorAddr()
1214 {
1215   CORBA::String_var root = _NS.getIORaddr();
1216   CORBA::Object_var obj = _orb->string_to_object(root);
1217   CPPUNIT_ASSERT(!CORBA::is_nil(obj)); 
1218 }
1219
1220 // ============================================================================
1221 /*!
1222  * Test 
1223  */
1224 // ============================================================================
1225
1226 // void
1227 // NamingServiceTest::()
1228 // {
1229 //   CPPUNIT_ASSERT(0);
1230 // }
1231