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