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