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