1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/
21 #include "LifeCycleCORBATest.hxx"
22 #include "SALOME_LifeCycleCORBA.hxx"
23 #include "Utils_ORB_INIT.hxx"
24 #include "Utils_SINGLETON.hxx"
34 // --- uncomment to have some traces on standard error
35 // (useful only when adding new tests...)
39 #define MYDEVTRACE {std::cerr << __FILE__ << " [" << __LINE__ << "] : ";}
40 #define DEVTRACE(msg) {MYDEVTRACE; std::cerr<<msg<<std::endl<<std::flush;}
46 #define TRACEFILE "/tmp/traceUnitTest.log"
48 // ============================================================================
51 * - delete preexisting trace classes if any
53 * Get or initialize the orb
54 * Create a SALOME_NamingService instance
56 // ============================================================================
59 LifeCycleCORBATest::setUp()
61 LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
63 bp1->deleteInstance(bp1);
66 char *theFileName = TRACEFILE;
72 CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
75 // traceFile.open(theFileName, ios::out | ios::trunc);
76 traceFile.open(theFileName, ios::out | ios::app);
77 CPPUNIT_ASSERT(traceFile); // file created empty, then closed
80 bp1 = LocalTraceBufferPool::instance();
83 // --- Get or initialize the orb
87 ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
88 ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
89 _orb = init(_argc , _argv ) ;
91 // --- Create a SALOME_NamingService instance
96 // ============================================================================
98 * - delete trace classes
100 // ============================================================================
103 LifeCycleCORBATest::tearDown()
106 LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
108 bp1->deleteInstance(bp1);
111 // ============================================================================
113 * Check FindOrLoad_Component.
114 * - get a local container (no hostname given),
115 * load an engine, check that the CORBA object is not null.
118 // ============================================================================
121 LifeCycleCORBATest::testFindOrLoad_Component_LaunchContainer()
123 SALOME_LifeCycleCORBA _LCC(&_NS);
125 // --- get a local container,
126 // load an engine, check that the CORBA object is not null
128 string containerName = "myContainer";
129 Engines::Component_var mycompo =
130 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
131 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
135 Engines::TestComponent_var m1;
136 m1 = Engines::TestComponent::_narrow(mycompo);
137 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
140 // ============================================================================
142 * Check FindOrLoad_Component.
143 * - Call 2 times FindOrLoad_Component with the same parameters
144 * - check if we get the same engine
146 // ============================================================================
149 LifeCycleCORBATest::testFindOrLoad_Component_SameInstance()
151 SALOME_LifeCycleCORBA _LCC(&_NS);
153 // --- get a local container,
154 // load an engine, check that the CORBA object is not null
156 string containerName = "myContainer";
158 Engines::Component_var mycompo1 =
159 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
160 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo1));
162 Engines::Component_var mycompo2 =
163 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
164 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo2));
168 Engines::TestComponent_var m1;
169 m1 = Engines::TestComponent::_narrow(mycompo1);
170 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
172 Engines::TestComponent_var m2;
173 m2 = Engines::TestComponent::_narrow(mycompo2);
174 CPPUNIT_ASSERT(!CORBA::is_nil(m2));
176 // --- check equality of instance names
178 string name1 = m1->instanceName();
179 string name2 = m2->instanceName();
180 CPPUNIT_ASSERT_EQUAL(name1, name2);
183 // ============================================================================
185 * Check FindOrLoad_Component with Python Component on C++ Container
186 * load an engine, check that the CORBA object is not null.
189 // ============================================================================
192 LifeCycleCORBATest::testFindOrLoad_Component_PythonInCppContainer()
194 SALOME_LifeCycleCORBA _LCC(&_NS);
196 // --- get a local container,
197 // load an engine, check that the CORBA object is not null
199 string containerName = "myContainer";
201 Engines::Component_var mycompo1 =
202 _LCC.FindOrLoad_Component(containerName.c_str(),"SALOME_TestComponentPy");
203 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo1));
207 Engines::TestComponent_var m1;
208 m1 = Engines::TestComponent::_narrow(mycompo1);
209 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
212 // ============================================================================
214 * Check FindOrLoad_Component with Python Component on C++ Container
215 * - Call 2 times FindOrLoad_Component with the same parameters
216 * - check if we get the same engine
218 // ============================================================================
221 LifeCycleCORBATest::testFindOrLoad_Component_PythonSameInstance()
223 SALOME_LifeCycleCORBA _LCC(&_NS);
225 // --- get a local container (with a name based on local hostname),
226 // load an engine, check that the CORBA object is not null
228 string containerName = "myContainer";
230 Engines::Component_var mycompo1 =
231 _LCC.FindOrLoad_Component(containerName.c_str(),"SALOME_TestComponentPy");
232 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo1));
234 Engines::Component_var mycompo2 =
235 _LCC.FindOrLoad_Component(containerName.c_str(),"SALOME_TestComponentPy");
236 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo2));
240 Engines::TestComponent_var m1;
241 m1 = Engines::TestComponent::_narrow(mycompo1);
242 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
244 Engines::TestComponent_var m2;
245 m2 = Engines::TestComponent::_narrow(mycompo2);
246 CPPUNIT_ASSERT(!CORBA::is_nil(m2));
248 // --- check equality of instance names
250 string name1 = m1->instanceName();
251 string name2 = m2->instanceName();
252 CPPUNIT_ASSERT_EQUAL(name1, name2);
255 // ============================================================================
257 * Check FindOrLoad_Component with a component name not in catalog.
258 * See list of catalog given to module catalog server.
259 * Here, we work with KERNEL_SRC/resources/KERNELCatalog.xml that contains
260 * only KERNEL, SalomeTestComponent and SALOME_TestComponentPy
262 // ============================================================================
265 LifeCycleCORBATest::testFindOrLoad_Component_UnknownInCatalog()
267 SALOME_LifeCycleCORBA _LCC(&_NS);
269 // --- get a local container (with a name based on local hostname),
270 // load an engine, check that the CORBA object is not null
272 string containerName = "myContainer";
274 Engines::Component_var mycompo1 =
275 _LCC.FindOrLoad_Component(containerName.c_str(),"MyNewComponent");
276 CPPUNIT_ASSERT(CORBA::is_nil(mycompo1));
279 // ============================================================================
281 * Check FindOrLoad_Component with hostname given
282 * - get a local container : getHostName()/componentName,
283 * load an engine, check that the CORBA object is not null.
286 // ============================================================================
289 LifeCycleCORBATest::testFindOrLoad_Component_LaunchContainerHostname()
291 SALOME_LifeCycleCORBA _LCC(&_NS);
293 // --- get a local container (with a name based on local hostname),
294 // load an engine, check that the CORBA object is not null
296 string containerName = GetHostname();
297 containerName += "/theContainer";
298 DEVTRACE("containerName = " << containerName);
299 Engines::Component_var mycompo =
300 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
301 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
305 Engines::TestComponent_var m1;
306 m1 = Engines::TestComponent::_narrow(mycompo);
307 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
310 // ============================================================================
312 * Check FindOrLoad_Component with and without local hostname given.
313 * We must get the same container, the same instance of component
315 // ============================================================================
318 LifeCycleCORBATest::testFindOrLoad_Component_SameContainer()
320 SALOME_LifeCycleCORBA _LCC(&_NS);
322 // --- get a local container (with a name based on local hostname),
323 // load an engine, check that the CORBA object is not null
325 string containerName = "aContainer";
327 Engines::Component_var mycompo1 =
328 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
329 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo1));
331 containerName = GetHostname();
332 containerName += "/aContainer";
333 DEVTRACE("containerName = " << containerName);
334 Engines::Component_var mycompo2 =
335 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
336 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo2));
340 Engines::TestComponent_var m1;
341 m1 = Engines::TestComponent::_narrow(mycompo1);
342 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
344 Engines::TestComponent_var m2;
345 m2 = Engines::TestComponent::_narrow(mycompo2);
346 CPPUNIT_ASSERT(!CORBA::is_nil(m2));
348 // --- check equality of instance names
350 string name1 = m1->instanceName();
351 string name2 = m2->instanceName();
352 CPPUNIT_ASSERT_EQUAL(name1, name2);
354 // --- check containers are the same servant (same container name+hostname)
356 Engines::Container_var c1 = m1->GetContainerRef();
357 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
358 Engines::Container_var c2 = m2->GetContainerRef();
359 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
360 string cname1 = c1->name();
361 string cname2 = c2->name();
362 CPPUNIT_ASSERT_EQUAL(cname1, cname2);
363 string hostname1 = c1->getHostName();
364 string hostname2 = c2->getHostName();
365 CPPUNIT_ASSERT_EQUAL(hostname1, hostname2);
366 CORBA::Long pidc1 = c1->getPID();
367 CORBA::Long pidc2 = c2->getPID();
368 CPPUNIT_ASSERT_EQUAL(pidc1, pidc2);
371 // ============================================================================
373 * Check FindOrLoad_Component: check behaviour when ask for an unknown computer
374 * We must catch a Salome Exception with "unknown host" message
376 // ============================================================================
379 LifeCycleCORBATest::testFindOrLoad_Component_UnknownMachine()
381 SALOME_LifeCycleCORBA _LCC(&_NS);
383 // --- try to get a distant container on an unknown machine (not existing)
384 // check that the CORBA object is null
386 string containerName = "aFarAwayComputer";
387 containerName += "/theContainer";
388 // CPPUNIT_ASSERT_THROW(Engines::Component_var mycompo =
389 // _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");,SALOME::SALOME_Exception);
392 Engines::Component_var mycompo =
393 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
395 catch(const SALOME::SALOME_Exception &ex)
397 CPPUNIT_ASSERT(true);
398 // string expectedMessage = "BAD PARAM";
401 // string actualMessage = os.str();
402 // DEVTRACE("actual Exception Message = " << actualMessage);
403 // CPPUNIT_ASSERT(actualMessage.find(expectedMessage) != string::npos);
407 // ============================================================================
409 * Check FindOrLoad_Component, empty params must give an instance
411 // ============================================================================
414 LifeCycleCORBATest::testFindOrLoad_Component_ParamsEmpty()
416 SALOME_LifeCycleCORBA _LCC(&_NS);
418 Engines::MachineParameters params;
420 Engines::Component_var mycompo =
421 _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
422 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
426 Engines::TestComponent_var m1;
427 m1 = Engines::TestComponent::_narrow(mycompo);
430 // ============================================================================
432 * Check FindOrLoad_Component params = local container
434 // ============================================================================
437 LifeCycleCORBATest::testFindOrLoad_Component_ParamsLocalContainer()
439 SALOME_LifeCycleCORBA _LCC(&_NS);
441 Engines::MachineParameters params;
443 string hostname=GetHostname();
444 params.hostname=hostname.c_str();
445 Engines::Component_var mycompo =
446 _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
447 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
451 Engines::TestComponent_var m1;
452 m1 = Engines::TestComponent::_narrow(mycompo);
454 // --- check that container is on local computer
456 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
457 Engines::Container_var c1 = m1->GetContainerRef();
458 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
459 string hostname1 = c1->getHostName();
460 CPPUNIT_ASSERT_EQUAL(hostname1, GetHostname());
464 // ============================================================================
466 * Check FindOrLoad_Component params = containerName
468 // ============================================================================
471 LifeCycleCORBATest::testFindOrLoad_Component_ParamsContainerName()
473 SALOME_LifeCycleCORBA _LCC(&_NS);
475 Engines::MachineParameters params;
477 string containerName = "myContainer";
478 params.container_name = containerName.c_str();
479 Engines::Component_var mycompo =
480 _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
481 CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
485 Engines::TestComponent_var m1;
486 m1 = Engines::TestComponent::_narrow(mycompo);
488 // --- check that container has good name
490 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
491 Engines::Container_var c1 = m1->GetContainerRef();
492 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
493 string hostname1 = c1->getHostName();
494 CPPUNIT_ASSERT_EQUAL(hostname1, GetHostname());
495 string cname1 = c1->name();
496 CPPUNIT_ASSERT(cname1.find(containerName) != string::npos);
499 // ============================================================================
501 * Check FindOrLoad_Component on remote computer
503 // ============================================================================
506 LifeCycleCORBATest::testFindOrLoad_Component_RemoteComputer()
508 SALOME_LifeCycleCORBA _LCC(&_NS);
510 string remoteHost = GetRemoteHost();
512 string containerName = remoteHost;
513 containerName += "/aContainer";
514 DEVTRACE("containerName = " << containerName);
515 Engines::Component_var mycompo1 =
516 _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
517 CPPUNIT_ASSERT_MESSAGE("Test validity requires connection to remote "\
518 "computers from ResourcesCatalog",
519 !CORBA::is_nil(mycompo1));
523 Engines::TestComponent_var m1;
524 m1 = Engines::TestComponent::_narrow(mycompo1);
526 // --- check that container is on good computer
528 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
529 Engines::Container_var c1 = m1->GetContainerRef();
530 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
531 string hostname1 = c1->getHostName();
532 CPPUNIT_ASSERT_EQUAL(hostname1, remoteHost);
535 // ============================================================================
537 * Check FindOrLoad_Component with params on remote computer
538 * params empty except hostname
540 // ============================================================================
543 LifeCycleCORBATest::testFindOrLoad_Component_ParamsRemoteComputer()
545 SALOME_LifeCycleCORBA _LCC(&_NS);
547 string remoteHost = GetRemoteHost();
549 Engines::MachineParameters params;
551 params.hostname = remoteHost.c_str();
553 Engines::Component_var mycompo1 =
554 _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
555 CPPUNIT_ASSERT_MESSAGE("Test validity requires connection to remote "\
556 "computers from ResourcesCatalog",
557 !CORBA::is_nil(mycompo1));
561 Engines::TestComponent_var m1;
562 m1 = Engines::TestComponent::_narrow(mycompo1);
564 // --- check that container is on good computer
566 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
567 Engines::Container_var c1 = m1->GetContainerRef();
568 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
569 string hostname1 = c1->getHostName();
570 CPPUNIT_ASSERT_EQUAL(hostname1, remoteHost);
573 // ============================================================================
575 * Check FindOrLoad_Component with params on remote computer
576 * params empty except hostname and container_name
578 // ============================================================================
581 LifeCycleCORBATest::testFindOrLoad_Component_ParamsRemoteComputer2()
583 SALOME_LifeCycleCORBA _LCC(&_NS);
585 string remoteHost = GetRemoteHost();
587 Engines::MachineParameters params;
589 params.hostname = remoteHost.c_str();
590 params.container_name = "anotherContainer";
592 Engines::Component_var mycompo1 =
593 _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
594 CPPUNIT_ASSERT_MESSAGE("Test validity requires connection to remote "\
595 "computers from ResourcesCatalog",
596 !CORBA::is_nil(mycompo1));
600 Engines::TestComponent_var m1;
601 m1 = Engines::TestComponent::_narrow(mycompo1);
603 // --- check that container is on good computer
605 CPPUNIT_ASSERT(!CORBA::is_nil(m1));
606 Engines::Container_var c1 = m1->GetContainerRef();
607 CPPUNIT_ASSERT(!CORBA::is_nil(c1));
608 string hostname1 = c1->getHostName();
609 CPPUNIT_ASSERT_EQUAL(hostname1, remoteHost);
614 // ============================================================================
616 * Check FindOrLoad_Component params = containerName
618 // ============================================================================
621 // LifeCycleCORBATest::testFindOrLoad_Component_()
632 // ============================================================================
634 * Get a remote HostName in the Resource Catalog
636 // ============================================================================
638 string LifeCycleCORBATest::GetRemoteHost()
640 SALOME_LifeCycleCORBA _LCC(&_NS);
642 CORBA::Object_var obj = _NS.Resolve("/ContainerManager");
643 CPPUNIT_ASSERT(!CORBA::is_nil(obj));
644 Engines::ContainerManager_var containerManager =
645 Engines::ContainerManager::_narrow(obj);
646 CPPUNIT_ASSERT(!CORBA::is_nil(containerManager));
648 Engines::MachineParameters params;
649 _LCC.preSet(params); // empty params to get all the machines
651 Engines::MachineList_var hostList =
652 containerManager->GetFittingResources(params,"SalomeTestComponent");
653 CPPUNIT_ASSERT(hostList->length() > 1);
655 string localHost = GetHostname();
657 for (unsigned int i=0; i < hostList->length(); i++)
659 const char* aMachine = hostList[i];
660 string machine(aMachine);
661 if (machine != localHost)
663 remoteHost = machine;
667 CPPUNIT_ASSERT(remoteHost != "");