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