1 // Copyright (C) 2007-2021 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "NamingServiceTest.hxx"
25 #include "Basics_Utils.hxx"
26 #include "Basics_DirUtils.hxx"
27 #include "SALOME_LifeCycleCORBA.hxx"
28 #include "utilities.h"
37 // --- uncomment to have some traces on standard error
38 // (useful only when adding new tests...)
42 #define MYDEVTRACE {std::cerr << __FILE__ << " [" << __LINE__ << "] : ";}
43 #define DEVTRACE(msg) {MYDEVTRACE; std::cerr<<msg<<std::endl<<std::flush;}
50 #define setenv Kernel_Utils::setenv
53 // ============================================================================
55 * a basic CORBA object implementation for use with namingService tests
57 // ============================================================================
59 NSTEST_echo_i::NSTEST_echo_i()
64 NSTEST_echo_i::NSTEST_echo_i(CORBA::Long num)
69 NSTEST_echo_i::~NSTEST_echo_i()
73 CORBA::Long NSTEST_echo_i::getId()
78 // ============================================================================
80 * a factory of CORBA objects for use with namingService tests
82 // ============================================================================
84 NSTEST_aFactory_i::NSTEST_aFactory_i()
89 NSTEST_aFactory_i::~NSTEST_aFactory_i()
93 NSTEST::echo_ptr NSTEST_aFactory_i::createInstance()
95 NSTEST_echo_i * anEcho = new NSTEST_echo_i(_num);
97 NSTEST::echo_var anEchoRef = anEcho->_this();
98 anEcho->_remove_ref();
99 return anEchoRef._retn();
102 // ============================================================================
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
110 // ============================================================================
113 NamingServiceTest::_getTraceFileName()
115 std::string dir = Kernel_Utils::GetTmpDir();
116 return dir + "traceUnitTest-NamingServiceTest.log";
120 NamingServiceTest::setUp()
122 LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
124 bp1->deleteInstance(bp1);
127 std::string theFileName = _getTraceFileName();
129 std::string s = "file:";
133 CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
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
141 bp1 = LocalTraceBufferPool::instance();
144 // --- Get or initialize the orb
146 _orb = KERNEL::GetRefToORB();
148 // --- Create a SALOME_NamingService instance
152 // --- Create an NSTEST::factory
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();
162 _myFactory->_remove_ref();
166 // ============================================================================
168 * - delete trace classes
170 // ============================================================================
173 NamingServiceTest::tearDown()
176 LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
178 bp1->deleteInstance(bp1);
181 // ============================================================================
183 * Test default constructor: must be followed by a call to init_orb(ORB)
185 // ============================================================================
188 NamingServiceTest::testConstructorDefault()
190 SALOME_NamingService NS;
191 //CPPUNIT_ASSERT_THROW(NS.getIORaddr(),CORBA::Exception);
194 CORBA::String_var root = NS.getIORaddr();
195 CORBA::Object_var obj = _orb->string_to_object(root);
196 CPPUNIT_ASSERT(!CORBA::is_nil(obj));
198 CosNaming::NamingContext_var rootContext =
199 CosNaming::NamingContext::_narrow(obj);
200 CPPUNIT_ASSERT(!CORBA::is_nil(rootContext));
203 // ============================================================================
205 * Test constructor with ORB parameter
207 // ============================================================================
210 NamingServiceTest::testConstructorOrb()
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));
217 CosNaming::NamingContext_var rootContext =
218 CosNaming::NamingContext::_narrow(obj);
219 CPPUNIT_ASSERT(!CORBA::is_nil(rootContext));
222 // ============================================================================
224 * Test Register and resolve of a single CORBA object with absolute pathname,
225 * without subdirectories
227 // ============================================================================
230 NamingServiceTest::testRegisterResolveAbsNoPath()
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));
239 // ============================================================================
241 * Test Register and resolve of a single CORBA object with relative pathname,
242 * without subdirectories
244 // ============================================================================
247 NamingServiceTest::testRegisterResolveRelativeNoPath()
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));
254 _NS.Create_Directory("/myContext");
255 _NS.Change_Directory("/myContext");
257 NSTEST::echo_var anEchoRef = myFactory->createInstance();
258 _NS.Register(anEchoRef,"echo_0");
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());
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());
273 // ============================================================================
275 * Test Register and resolve of a single CORBA object with absolute pathname,
278 // ============================================================================
281 NamingServiceTest::testRegisterResolveAbsWithPath()
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));
288 NSTEST::echo_var anEchoRef = myFactory->createInstance();
289 _NS.Register(anEchoRef,"/nstest/echo_0");
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());
297 NSTEST::echo_var anEchoRef1 = myFactory->createInstance();
298 _NS.Register(anEchoRef1,"/nstest2/rep2/rep3/echo_1");
299 CPPUNIT_ASSERT(anEchoRef->getId() != anEchoRef1->getId());
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());
307 NSTEST::echo_var anEchoRef2 = myFactory->createInstance();
308 _NS.Register(anEchoRef2,"/nstest2/1/2/3/4/echo_1");
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());
317 // ============================================================================
319 * Test Register and resolve of a single CORBA object with relative pathname,
321 * Relative Path is changed to the created subdirectory when Register()
323 // ============================================================================
326 NamingServiceTest::testRegisterResolveRelativeWithPath()
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");
335 NSTEST::echo_var anEchoRef = myFactory->createInstance();
336 _NS.Register(anEchoRef,"subdir/echo_0");
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));
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());
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());
357 // ============================================================================
359 * Test resolve with a name not known
361 // ============================================================================
364 NamingServiceTest::testResolveBadName()
366 CORBA::Object_var obj = _NS.Resolve("/notRegisteredName");
367 CPPUNIT_ASSERT(CORBA::is_nil(obj));
369 obj = _NS.Resolve("/nstest/notRegisteredName");
370 CPPUNIT_ASSERT(CORBA::is_nil(obj));
372 obj = _NS.Resolve("/unknownPath/notRegisteredName");
373 CPPUNIT_ASSERT(CORBA::is_nil(obj));
375 obj = _NS.Resolve("/anUnknown/ComplicatedPath/notRegisteredName");
376 CPPUNIT_ASSERT(CORBA::is_nil(obj));
379 // ============================================================================
381 * Test resolve with a name not known, with a relative path
383 // ============================================================================
386 NamingServiceTest::testResolveBadNameRelative()
388 _NS.Create_Directory("/myContext");
389 _NS.Change_Directory("/myContext");
391 CORBA::Object_var obj = _NS.Resolve("notRegisteredName");
392 CPPUNIT_ASSERT(CORBA::is_nil(obj));
394 obj = _NS.Resolve("unknownPath/notRegisteredName");
395 CPPUNIT_ASSERT(CORBA::is_nil(obj));
397 obj = _NS.Resolve("anUnknown/ComplicatedPath/notRegisteredName");
398 CPPUNIT_ASSERT(CORBA::is_nil(obj));
401 // ============================================================================
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.
409 // ============================================================================
414 NamingServiceTest::testResolveFirst()
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));
423 for (int i=0; i<NB_OBJS; i++)
425 NSTEST::echo_var anEchoRef = myFactory->createInstance();
426 ref[i] = anEchoRef->getId();
427 std::string name = "/nstestfirst/echo_";
429 sprintf(anum,"%d",ref[i]);
431 _NS.Register(anEchoRef,name.c_str());
434 for (int i=0; i<NB_OBJS; i++)
436 std::string name = "/nstestfirst/echo_";
438 sprintf(anum,"%d",ref[i]);
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]);
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]);
455 // ============================================================================
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.
463 // ============================================================================
466 NamingServiceTest::testResolveFirstRelative()
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));
475 for (int i=0; i<NB_OBJS; i++)
477 NSTEST::echo_var anEchoRef = myFactory->createInstance();
478 ref[i] = anEchoRef->getId();
479 std::string name = "/nstestfirstrel/echo_";
481 sprintf(anum,"%d",ref[i]);
483 _NS.Register(anEchoRef,name.c_str());
486 for (int i=0; i<NB_OBJS; i++)
488 _NS.Change_Directory("/nstestfirstrel");
489 std::string name = "echo_";
491 sprintf(anum,"%d",ref[i]);
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]);
500 _NS.Change_Directory("/nstestfirstrel");
501 std::string name = "echo";
502 obj = _NS.ResolveFirst(name.c_str());
503 CPPUNIT_ASSERT(!CORBA::is_nil(obj));
506 // ============================================================================
508 * Test resolveFirst with unknown name
510 // ============================================================================
513 NamingServiceTest::testResolveFirstUnknown()
515 std::string name = "/notYeyRegistered";
516 CORBA::Object_var obj= _NS.ResolveFirst(name.c_str());
517 CPPUNIT_ASSERT(CORBA::is_nil(obj));
519 name = "/nstestfirst/notYeyRegistered";
520 obj = _NS.ResolveFirst(name.c_str());
521 CPPUNIT_ASSERT(CORBA::is_nil(obj));
523 name = "/rrr/sss/ttt/notYeyRegistered";
524 obj = _NS.ResolveFirst(name.c_str());
525 CPPUNIT_ASSERT(CORBA::is_nil(obj));
528 // ============================================================================
530 * Test resolveFirst with unknown name, relative Path
532 // ============================================================================
535 NamingServiceTest::testResolveFirstUnknownRelative()
537 _NS.Create_Directory("/myContext");
538 _NS.Change_Directory("/myContext");
540 std::string name = "RelnotYeyRegistered";
541 CORBA::Object_var obj = _NS.ResolveFirst(name.c_str());
542 CPPUNIT_ASSERT(CORBA::is_nil(obj));
544 name = "Relnstestfirst/notYeyRegistered";
545 obj = _NS.ResolveFirst(name.c_str());
546 CPPUNIT_ASSERT(CORBA::is_nil(obj));
548 name = "Relrrr/sss/ttt/notYeyRegistered";
549 obj = _NS.ResolveFirst(name.c_str());
550 CPPUNIT_ASSERT(CORBA::is_nil(obj));
553 // ============================================================================
555 * Test ResolveComponent works as specified
557 // ============================================================================
560 NamingServiceTest::testResolveComponentOK()
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));
567 NSTEST::echo_var anEchoRef = myFactory->createInstance();
568 _NS.Register(anEchoRef,
569 "/Containers/theHostName/theContainerName/theComponentName");
571 obj = _NS.ResolveComponent("theHostName",
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());
580 NSTEST::echo_var anEchoRef2 = myFactory->createInstance();
581 _NS.Register(anEchoRef2,
582 "/Containers/theHostName/theContainerName_2/theComponentName");
584 obj = _NS.ResolveComponent("theHostName",
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());
594 // ============================================================================
596 * Test ResolveComponent gives nil pointer if hostname is not given (empty)
598 // ============================================================================
601 NamingServiceTest::testResolveComponentEmptyHostname()
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));
608 NSTEST::echo_var anEchoRef = myFactory->createInstance();
609 _NS.Register(anEchoRef,
610 "/Containers/theHostName/theContainerName/theComponentName");
612 obj = _NS.ResolveComponent("",
615 CPPUNIT_ASSERT(CORBA::is_nil(obj));
618 // ============================================================================
620 * Test ResolveComponent gives nil pointer if hostname is unknown
622 // ============================================================================
625 NamingServiceTest::testResolveComponentUnknownHostname()
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));
632 NSTEST::echo_var anEchoRef = myFactory->createInstance();
633 _NS.Register(anEchoRef,
634 "/Containers/theHostName/theContainerName/theComponentName");
636 obj = _NS.ResolveComponent("anUnknownHostName",
639 CPPUNIT_ASSERT(CORBA::is_nil(obj));
642 // ============================================================================
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.
650 // ============================================================================
653 NamingServiceTest::testResolveComponentEmptyContainerName()
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));
660 NSTEST::echo_var anEchoRef = myFactory->createInstance();
661 _NS.Register(anEchoRef,
662 "/Containers/theHostName/theContainerName/theComponentName");
664 NSTEST::echo_var anEchoRef2 = myFactory->createInstance();
665 _NS.Register(anEchoRef2,
666 "/Containers/theHostName/aContainerName/aComponentName");
668 NSTEST::echo_var anEchoRef3 = myFactory->createInstance();
669 _NS.Register(anEchoRef3,
670 "/Containers/theHostName/otherContainerName/theComponentName");
672 obj = _NS.ResolveComponent("anUnknownHostName",
675 CPPUNIT_ASSERT(CORBA::is_nil(obj));
677 obj = _NS.ResolveComponent("theHostName",
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());
686 // ============================================================================
690 // ============================================================================
693 NamingServiceTest::testResolveComponentUnknownContainerName()
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));
700 NSTEST::echo_var anEchoRef = myFactory->createInstance();
701 _NS.Register(anEchoRef,
702 "/Containers/theHostName/theContainerName/theComponentName");
704 NSTEST::echo_var anEchoRef2 = myFactory->createInstance();
705 _NS.Register(anEchoRef2,
706 "/Containers/theHostName/aContainerName/aComponentName");
708 NSTEST::echo_var anEchoRef3 = myFactory->createInstance();
709 _NS.Register(anEchoRef3,
710 "/Containers/theHostName/otherContainerName/theComponentName");
712 obj = _NS.ResolveComponent("theHostName",
713 "anUnknownContainerName",
715 CPPUNIT_ASSERT(CORBA::is_nil(obj));
718 // ============================================================================
722 // ============================================================================
725 NamingServiceTest::testResolveComponentEmptyComponentName()
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));
732 NSTEST::echo_var anEchoRef = myFactory->createInstance();
733 _NS.Register(anEchoRef,
734 "/Containers/theHostName/theContainerName/theComponentName");
736 NSTEST::echo_var anEchoRef2 = myFactory->createInstance();
737 _NS.Register(anEchoRef2,
738 "/Containers/theHostName/EmptyContainerName/");
740 obj = _NS.ResolveComponent("theHostName",
741 "EmptyContainerName",
743 CPPUNIT_ASSERT(CORBA::is_nil(obj));
746 // ============================================================================
750 // ============================================================================
753 NamingServiceTest::testResolveComponentUnknownComponentName()
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));
760 NSTEST::echo_var anEchoRef = myFactory->createInstance();
761 _NS.Register(anEchoRef,
762 "/Containers/theHostName/theContainerName/theComponentName");
764 obj = _NS.ResolveComponent("theHostName",
766 "anUnknownComponentName");
767 CPPUNIT_ASSERT(CORBA::is_nil(obj));
770 // ============================================================================
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.
776 // ============================================================================
779 NamingServiceTest::testResolveComponentFalseNbproc()
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));
786 NSTEST::echo_var anEchoRef = myFactory->createInstance();
787 _NS.Register(anEchoRef,
788 "/Containers/theHostName/theContainerName/theComponentName");
790 obj = _NS.ResolveComponent("theHostName",
794 CPPUNIT_ASSERT(CORBA::is_nil(obj));
796 obj = _NS.ResolveComponent("theHostName",
800 CPPUNIT_ASSERT(! CORBA::is_nil(obj));
803 // ============================================================================
807 // ============================================================================
810 NamingServiceTest::testContainerName()
812 std::string ref0 = "FactoryServer";
813 std::string ret = SALOME_NamingService_Abstract::ContainerName("");
814 CPPUNIT_ASSERT(ret == ref0);
816 ref0 = "MyContainerName";
817 ret = SALOME_NamingService_Abstract::ContainerName(ref0.c_str());
818 CPPUNIT_ASSERT(ret == ref0);
821 // ============================================================================
825 // ============================================================================
828 NamingServiceTest::testContainerNameParams()
830 Engines::ContainerParameters params;
831 SALOME_LifeCycleCORBA::preSet(params);
833 std::string ref0 = "FactoryServer";
834 std::string ret = _NS.ContainerName(params);
835 CPPUNIT_ASSERT(ret == ref0);
837 ref0 = "MyContainerName";
838 params.container_name = ref0.c_str();
839 ret = _NS.ContainerName(params);
840 CPPUNIT_ASSERT(ret == ref0);
843 // ============================================================================
847 // ============================================================================
850 NamingServiceTest::testBuildContainerNameForNS()
852 std::string ref0 = "/Containers/theHostName/theContainerName";
853 std::string ret = SALOME_NamingService_Abstract::BuildContainerNameForNS("theContainerName","theHostName");
854 CPPUNIT_ASSERT(ret == ref0);
856 ref0 = "/Containers/theHostName/FactoryServer";
857 ret = SALOME_NamingService_Abstract::BuildContainerNameForNS("","theHostName");
858 CPPUNIT_ASSERT(ret == ref0);
861 // ============================================================================
865 // ============================================================================
868 NamingServiceTest::testBuildContainerNameForNSParams()
870 Engines::ContainerParameters params;
871 SALOME_LifeCycleCORBA::preSet(params);
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);
878 params.container_name = "";
879 ref0 = "/Containers/theHostName/FactoryServer";
880 ret = _NS.BuildContainerNameForNS(params,"theHostName");
881 CPPUNIT_ASSERT(ret == ref0);
884 // ============================================================================
888 // ============================================================================
891 NamingServiceTest::testFind()
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));
898 NSTEST::echo_var anEchoRef = myFactory->createInstance();
899 _NS.Register(anEchoRef,
900 "/Containers/theHostName/theContainerName/theComponentName");
902 NSTEST::echo_var anEchoRef2 = myFactory->createInstance();
903 _NS.Register(anEchoRef2,
904 "/Containers/theHostName/aContainerName/aComponentName");
906 NSTEST::echo_var anEchoRef3 = myFactory->createInstance();
907 _NS.Register(anEchoRef3,
908 "/Containers/theHostName/otherContainerName/theComponentName");
910 NSTEST::echo_var anEchoRef4 = myFactory->createInstance();
911 _NS.Register(anEchoRef4,
912 "/Containers/anHostName/oneContainerName/theComponentName");
914 _NS.Change_Directory("/Containers");
915 int occ= _NS.Find("theComponentName");
916 CPPUNIT_ASSERT(occ >= 3); // see previous tests
918 _NS.Change_Directory("/Containers");
919 occ= _NS.Find("aComponentName");
920 CPPUNIT_ASSERT(occ == 1);
922 _NS.Change_Directory("/Containers");
923 occ= _NS.Find("UnknownCompnentName");
924 CPPUNIT_ASSERT(occ == 0);
927 // ============================================================================
931 // ============================================================================
934 NamingServiceTest::testCreateDirectory()
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));
941 bool ret = _NS.Create_Directory("/aaa/bbb/ccc/ddd/eee");
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_";
949 sprintf(anum,"%d",val);
951 _NS.Register(anEchoRef,name.c_str());
953 std::string dirname = "/aaa/bbb/ccc/ddd/eee/";
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());
961 ret = _NS.Create_Directory("/aaa/bbb/ccc/ddd/eee");
964 _NS.Change_Directory("/aaa/bbb");
965 ret = _NS.Create_Directory("cccccc/dddddd/eeeeee");
966 _NS.Register(anEchoRef,"echo_abcde");
969 _NS.Change_Directory("/aaa/bbb/cccccc/dddddd/eeeeee");
970 obj = _NS.Resolve("echo_abcde");
971 CPPUNIT_ASSERT(!CORBA::is_nil(obj));
974 // ============================================================================
978 // ============================================================================
981 NamingServiceTest::testChangeDirectory()
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));
988 NSTEST::echo_var anEchoRef = myFactory->createInstance();
989 _NS.Register(anEchoRef,
990 "/Containers/theHostName/theContainerName/theComponentName");
992 NSTEST::echo_var anEchoRef2 = myFactory->createInstance();
993 _NS.Register(anEchoRef2,
994 "/Containers/theHostName/aContainerName/aComponentName");
996 NSTEST::echo_var anEchoRef3 = myFactory->createInstance();
997 _NS.Register(anEchoRef3,
998 "/Containers/theHostName/otherContainerName/theComponentName");
1000 NSTEST::echo_var anEchoRef4 = myFactory->createInstance();
1001 _NS.Register(anEchoRef4,
1002 "/Containers/anHostName/oneContainerName/theComponentName");
1004 _NS.Change_Directory("/Containers/theHostName/otherContainerName");
1005 obj = _NS.Resolve("theComponentName");
1006 CPPUNIT_ASSERT(!CORBA::is_nil(obj));
1008 NSTEST::echo_var anEchoRefa = NSTEST::echo::_narrow(obj);
1009 CPPUNIT_ASSERT(!CORBA::is_nil(anEchoRefa));
1010 CPPUNIT_ASSERT(anEchoRefa->getId() == anEchoRef3->getId());
1013 // ============================================================================
1017 // ============================================================================
1020 NamingServiceTest::testCurrentDirectory()
1022 std::string path = "/aaa/bbb/ccc/ddd/eee";
1023 bool ret = _NS.Create_Directory(path.c_str());
1024 CPPUNIT_ASSERT(ret);
1026 _NS.Change_Directory(path.c_str());
1027 char* acurdir = _NS.Current_Directory();
1028 std::string curdir = acurdir;
1030 CPPUNIT_ASSERT(curdir == path);
1033 // ============================================================================
1037 // ============================================================================
1040 NamingServiceTest::testList()
1042 _NS.Change_Directory("/Containers/theHostName/theContainerName");
1044 _NS.Change_Directory("/Containers");
1048 // ============================================================================
1052 // ============================================================================
1055 NamingServiceTest::testListDirectory()
1057 _NS.Change_Directory("/Containers/theHostName/theContainerName");
1058 _NS.list_directory();
1059 _NS.Change_Directory("/Containers");
1060 _NS.list_directory();
1063 // ============================================================================
1067 // ============================================================================
1070 NamingServiceTest::testListDirectoryRecurs()
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();
1081 // ============================================================================
1085 // ============================================================================
1088 NamingServiceTest::testListSubdirs()
1090 _NS.Change_Directory("/Containers/theHostName/theContainerName");
1092 _NS.Change_Directory("/Containers");
1094 _NS.Change_Directory("/");
1097 // ============================================================================
1101 // ============================================================================
1104 NamingServiceTest::testDestroyName()
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));
1111 std::string path = "/Containers/theHostName/theContainerName/theComponentName";
1113 NSTEST::echo_var anEchoRef = myFactory->createInstance();
1114 _NS.Register(anEchoRef, path.c_str());
1116 obj=_NS.Resolve(path.c_str());
1117 CPPUNIT_ASSERT(!CORBA::is_nil(obj));
1119 _NS.Destroy_Name(path.c_str());
1120 obj=_NS.Resolve(path.c_str());
1121 CPPUNIT_ASSERT(CORBA::is_nil(obj));
1124 // ============================================================================
1128 // ============================================================================
1131 NamingServiceTest::testDestroyDirectory()
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));
1138 std::string path = "/Containers/theHostName/theContainerName/theComponentName";
1140 NSTEST::echo_var anEchoRef = myFactory->createInstance();
1141 _NS.Register(anEchoRef, path.c_str());
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
1147 _NS.Destroy_Name(path.c_str());
1148 _NS.Destroy_Directory("/Containers/theHostName/theContainerName");
1149 _NS.Change_Directory("/Containers/theHostName");
1153 // ============================================================================
1155 * DestroyFullDirectory is not recursive
1156 * Need Housekeeping of /Containers for further tests !
1158 // ============================================================================
1160 void NamingServiceTest::_destroyDirectoryRecurs(std::string path)
1162 std::string current = path;
1164 if (_NS.Change_Directory(path.c_str()))
1166 std::vector<std::string> subdirs = _NS.list_subdirs();
1167 for (size_t i=0; i<subdirs.size(); i++)
1169 std::string subpath=path + "/" +subdirs[i];
1170 _destroyDirectoryRecurs(subpath);
1172 if (_NS.Change_Directory(path.c_str()))
1174 _NS.Destroy_FullDirectory(path.c_str());
1180 NamingServiceTest::testDestroyFullDirectory()
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("/");
1192 _NS.list_directory_recurs();
1195 // ============================================================================
1199 // ============================================================================
1202 NamingServiceTest::testGetIorAddr()
1204 CORBA::String_var root = _NS.getIORaddr();
1205 CORBA::Object_var obj = _orb->string_to_object(root);
1206 CPPUNIT_ASSERT(!CORBA::is_nil(obj));
1209 // ============================================================================
1213 // ============================================================================
1216 // NamingServiceTest::()
1218 // CPPUNIT_ASSERT(0);