Salome HOME
fed326885c506e4d5bf84ce60b6d64a3120fe0eb
[modules/kernel.git] / src / LifeCycleCORBA / Test / LifeCycleCORBATest.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
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.
8 // 
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.
13 //
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
17 //
18 // See http://www.salome-platform.org/
19 //
20
21 #include "LifeCycleCORBATest.hxx"
22 #include "SALOME_LifeCycleCORBA.hxx"
23 #include "Utils_ORB_INIT.hxx"
24 #include "Utils_SINGLETON.hxx"
25 #include "OpUtil.hxx"
26
27 #include <iostream>
28 #include <fstream>
29 #include <string>
30 #include <cstdlib>
31
32 using namespace std;
33
34 // --- uncomment to have some traces on standard error
35 //     (useful only when adding new tests...)
36 //#define _DEVDEBUG_
37
38 #ifdef _DEVDEBUG_
39 #define MYDEVTRACE {std::cerr << __FILE__ << " [" << __LINE__ << "] : ";}
40 #define DEVTRACE(msg) {MYDEVTRACE; std::cerr<<msg<<std::endl<<std::flush;}
41 #else
42 #define MYDEVTRACE
43 #define DEVTRACE(msg)
44 #endif
45
46 #define TRACEFILE "/tmp/traceUnitTest.log"
47
48 // ============================================================================
49 /*!
50  * Set Trace mecanism
51  * - delete preexisting trace classes if any
52  * - set trace on file
53  * Get or initialize the orb
54  * Create a SALOME_NamingService instance
55  */
56 // ============================================================================
57
58 void 
59 LifeCycleCORBATest::setUp()
60 {
61   LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
62   CPPUNIT_ASSERT(bp1);
63   bp1->deleteInstance(bp1);
64
65   // --- trace on file
66   char *theFileName = TRACEFILE;
67
68   string s = "file:";
69   s += theFileName;
70   //s="local";
71   //s="with_logger";
72   CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
73
74   ofstream traceFile;
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
78   traceFile.close();
79
80   bp1 = LocalTraceBufferPool::instance();
81   CPPUNIT_ASSERT(bp1);
82
83   // --- Get or initialize the orb
84
85   int _argc = 1;
86   char* _argv[] = {""};
87   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
88   ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
89   _orb = init(_argc , _argv ) ;
90
91   // --- Create a SALOME_NamingService instance
92
93   _NS.init_orb(_orb) ;
94 }
95
96 // ============================================================================
97 /*!
98  *  - delete trace classes
99  */
100 // ============================================================================
101
102 void 
103 LifeCycleCORBATest::tearDown()
104 {
105
106   LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
107   CPPUNIT_ASSERT(bp1);
108   bp1->deleteInstance(bp1);
109 }
110
111 // ============================================================================
112 /*!
113  * Check FindOrLoad_Component.
114  * - get a local container (no hostname given),
115  *       load an engine, check that the CORBA object is not null.
116  * - check narrow
117  */
118 // ============================================================================
119
120 void
121 LifeCycleCORBATest::testFindOrLoad_Component_LaunchContainer()
122 {
123   SALOME_LifeCycleCORBA _LCC(&_NS);
124
125   // --- get a local container,
126   //     load an engine, check that the CORBA object is not null
127
128   string containerName = "myContainer";
129   Engines::Component_var mycompo =
130     _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
131   CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
132
133   // --- check narrow
134
135   Engines::TestComponent_var m1;
136   m1 = Engines::TestComponent::_narrow(mycompo);
137   CPPUNIT_ASSERT(!CORBA::is_nil(m1));
138 }
139
140 // ============================================================================
141 /*!
142  * Check FindOrLoad_Component.
143  * - Call 2 times FindOrLoad_Component with the same parameters
144  * - check if we get the same engine
145  */
146 // ============================================================================
147
148 void
149 LifeCycleCORBATest::testFindOrLoad_Component_SameInstance()
150 {
151   SALOME_LifeCycleCORBA _LCC(&_NS);
152
153   // --- get a local container,
154   //     load an engine, check that the CORBA object is not null
155
156   string containerName = "myContainer";
157
158   Engines::Component_var mycompo1 =
159     _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
160   CPPUNIT_ASSERT(!CORBA::is_nil(mycompo1));
161
162   Engines::Component_var mycompo2 =
163     _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
164   CPPUNIT_ASSERT(!CORBA::is_nil(mycompo2));
165
166   // --- check narrow
167
168   Engines::TestComponent_var m1;
169   m1 = Engines::TestComponent::_narrow(mycompo1);
170   CPPUNIT_ASSERT(!CORBA::is_nil(m1));
171
172   Engines::TestComponent_var m2;
173   m2 = Engines::TestComponent::_narrow(mycompo2);
174   CPPUNIT_ASSERT(!CORBA::is_nil(m2));
175
176   // --- check equality of instance names
177
178   string name1 = m1->instanceName();
179   string name2 = m2->instanceName();
180   CPPUNIT_ASSERT_EQUAL(name1, name2);
181 }
182
183 // ============================================================================
184 /*!
185  * Check FindOrLoad_Component with Python Component on C++ Container
186  *       load an engine, check that the CORBA object is not null.
187  * - check narrow
188  */
189 // ============================================================================
190
191 void
192 LifeCycleCORBATest::testFindOrLoad_Component_PythonInCppContainer()
193 {
194   SALOME_LifeCycleCORBA _LCC(&_NS);
195
196   // --- get a local container,
197   //     load an engine, check that the CORBA object is not null
198
199   string containerName = "myContainer";
200
201   Engines::Component_var mycompo1 =
202     _LCC.FindOrLoad_Component(containerName.c_str(),"SALOME_TestComponentPy");
203   CPPUNIT_ASSERT(!CORBA::is_nil(mycompo1));
204
205   // --- check narrow
206
207   Engines::TestComponent_var m1;
208   m1 = Engines::TestComponent::_narrow(mycompo1);
209   CPPUNIT_ASSERT(!CORBA::is_nil(m1));
210 }
211
212 // ============================================================================
213 /*!
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
217  */
218 // ============================================================================
219
220 void
221 LifeCycleCORBATest::testFindOrLoad_Component_PythonSameInstance()
222 {
223   SALOME_LifeCycleCORBA _LCC(&_NS);
224
225   // --- get a local container (with a name based on local hostname),
226   //     load an engine, check that the CORBA object is not null
227
228   string containerName = "myContainer";
229
230   Engines::Component_var mycompo1 =
231     _LCC.FindOrLoad_Component(containerName.c_str(),"SALOME_TestComponentPy");
232   CPPUNIT_ASSERT(!CORBA::is_nil(mycompo1));
233
234   Engines::Component_var mycompo2 =
235     _LCC.FindOrLoad_Component(containerName.c_str(),"SALOME_TestComponentPy");
236   CPPUNIT_ASSERT(!CORBA::is_nil(mycompo2));
237
238   // --- check narrow
239
240   Engines::TestComponent_var m1;
241   m1 = Engines::TestComponent::_narrow(mycompo1);
242   CPPUNIT_ASSERT(!CORBA::is_nil(m1));
243
244   Engines::TestComponent_var m2;
245   m2 = Engines::TestComponent::_narrow(mycompo2);
246   CPPUNIT_ASSERT(!CORBA::is_nil(m2));
247
248   // --- check equality of instance names
249
250   string name1 = m1->instanceName();
251   string name2 = m2->instanceName();
252   CPPUNIT_ASSERT_EQUAL(name1, name2);
253 }
254
255 // ============================================================================
256 /*!
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
261  */
262 // ============================================================================
263
264 void
265 LifeCycleCORBATest::testFindOrLoad_Component_UnknownInCatalog()
266 {
267   SALOME_LifeCycleCORBA _LCC(&_NS);
268
269   // --- get a local container (with a name based on local hostname),
270   //     load an engine, check that the CORBA object is not null
271
272   string containerName = "myContainer";
273
274   Engines::Component_var mycompo1 =
275     _LCC.FindOrLoad_Component(containerName.c_str(),"MyNewComponent");
276   CPPUNIT_ASSERT(CORBA::is_nil(mycompo1));
277 }
278
279 // ============================================================================
280 /*!
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.
284  * - check narrow
285  */
286 // ============================================================================
287
288 void
289 LifeCycleCORBATest::testFindOrLoad_Component_LaunchContainerHostname()
290 {
291   SALOME_LifeCycleCORBA _LCC(&_NS);
292
293   // --- get a local container (with a name based on local hostname),
294   //     load an engine, check that the CORBA object is not null
295
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));
302
303   // --- check narrow
304
305   Engines::TestComponent_var m1;
306   m1 = Engines::TestComponent::_narrow(mycompo);
307   CPPUNIT_ASSERT(!CORBA::is_nil(m1));
308 }
309
310 // ============================================================================
311 /*!
312  * Check FindOrLoad_Component with and without local hostname given.
313  * We must get the same container, the same instance of component
314  */
315 // ============================================================================
316
317 void
318 LifeCycleCORBATest::testFindOrLoad_Component_SameContainer()
319 {
320   SALOME_LifeCycleCORBA _LCC(&_NS);
321
322   // --- get a local container (with a name based on local hostname),
323   //     load an engine, check that the CORBA object is not null
324
325   string containerName = "aContainer";
326
327   Engines::Component_var mycompo1 =
328     _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
329   CPPUNIT_ASSERT(!CORBA::is_nil(mycompo1));
330
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));
337
338   // --- check narrow
339
340   Engines::TestComponent_var m1;
341   m1 = Engines::TestComponent::_narrow(mycompo1);
342   CPPUNIT_ASSERT(!CORBA::is_nil(m1));
343
344   Engines::TestComponent_var m2;
345   m2 = Engines::TestComponent::_narrow(mycompo2);
346   CPPUNIT_ASSERT(!CORBA::is_nil(m2));
347
348   // --- check equality of instance names
349
350   string name1 = m1->instanceName();
351   string name2 = m2->instanceName();
352   CPPUNIT_ASSERT_EQUAL(name1, name2);
353
354   // --- check containers are the same servant (same container name+hostname)
355
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);
369 }
370
371 // ============================================================================
372 /*!
373  * Check FindOrLoad_Component: check behaviour when ask for an unknown computer
374  * We must catch a Salome Exception with "unknown host" message
375  */
376 // ============================================================================
377
378 void
379 LifeCycleCORBATest::testFindOrLoad_Component_UnknownMachine()
380 {
381   SALOME_LifeCycleCORBA _LCC(&_NS);
382
383   // --- try to get a distant container on an unknown machine (not existing)
384   //     check that the CORBA object is null
385
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);
390   try
391     {
392       Engines::Component_var mycompo =
393         _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
394     }
395   catch(const SALOME::SALOME_Exception &ex)
396     {
397       CPPUNIT_ASSERT(true);
398 //       string expectedMessage = "BAD PARAM";
399 //       std::ostream os;
400 //       os << ex;
401 //       string actualMessage = os.str();
402 //       DEVTRACE("actual Exception Message = " << actualMessage);
403 //       CPPUNIT_ASSERT(actualMessage.find(expectedMessage) != string::npos);
404     }
405 }
406
407 // ============================================================================
408 /*!
409  * Check FindOrLoad_Component, empty params must give an instance
410  */
411 // ============================================================================
412
413 void
414 LifeCycleCORBATest::testFindOrLoad_Component_ParamsEmpty()
415 {
416   SALOME_LifeCycleCORBA _LCC(&_NS);
417
418   Engines::MachineParameters params;
419   _LCC.preSet(params);
420   Engines::Component_var mycompo =
421     _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
422   CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
423
424   // --- check narrow
425
426   Engines::TestComponent_var m1;
427   m1 = Engines::TestComponent::_narrow(mycompo);
428 }
429
430 // ============================================================================
431 /*!
432  * Check FindOrLoad_Component params = local container 
433  */
434 // ============================================================================
435
436 void
437 LifeCycleCORBATest::testFindOrLoad_Component_ParamsLocalContainer()
438 {
439   SALOME_LifeCycleCORBA _LCC(&_NS);
440
441   Engines::MachineParameters params;
442   _LCC.preSet(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));
448
449   // --- check narrow
450
451   Engines::TestComponent_var m1;
452   m1 = Engines::TestComponent::_narrow(mycompo);
453
454   // --- check that container is on local computer
455
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());
461 }
462
463
464 // ============================================================================
465 /*!
466  * Check FindOrLoad_Component params = containerName
467  */
468 // ============================================================================
469
470 void
471 LifeCycleCORBATest::testFindOrLoad_Component_ParamsContainerName()
472 {
473   SALOME_LifeCycleCORBA _LCC(&_NS);
474
475   Engines::MachineParameters params;
476   _LCC.preSet(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));
482
483   // --- check narrow
484
485   Engines::TestComponent_var m1;
486   m1 = Engines::TestComponent::_narrow(mycompo);
487
488   // --- check that container has good name
489
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);
497 }
498
499 // ============================================================================
500 /*!
501  * Check FindOrLoad_Component on remote computer
502  */
503 // ============================================================================
504
505 void
506 LifeCycleCORBATest::testFindOrLoad_Component_RemoteComputer()
507 {
508   SALOME_LifeCycleCORBA _LCC(&_NS);
509
510   string remoteHost = GetRemoteHost();
511
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));
520
521   // --- check narrow
522
523   Engines::TestComponent_var m1;
524   m1 = Engines::TestComponent::_narrow(mycompo1);
525
526   // --- check that container is on good computer
527
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);
533 }
534
535 // ============================================================================
536 /*!
537  * Check FindOrLoad_Component with params on remote computer
538  * params empty except hostname 
539  */
540 // ============================================================================
541
542 void
543 LifeCycleCORBATest::testFindOrLoad_Component_ParamsRemoteComputer()
544 {
545   SALOME_LifeCycleCORBA _LCC(&_NS);
546
547   string remoteHost = GetRemoteHost();
548
549   Engines::MachineParameters params;
550   _LCC.preSet(params); 
551   params.hostname = remoteHost.c_str();
552
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));
558
559   // --- check narrow
560
561   Engines::TestComponent_var m1;
562   m1 = Engines::TestComponent::_narrow(mycompo1);
563
564   // --- check that container is on good computer
565
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);
571 }
572
573 // ============================================================================
574 /*!
575  * Check FindOrLoad_Component with params on remote computer
576  * params empty except hostname and container_name
577  */
578 // ============================================================================
579
580 void
581 LifeCycleCORBATest::testFindOrLoad_Component_ParamsRemoteComputer2()
582 {
583   SALOME_LifeCycleCORBA _LCC(&_NS);
584
585   string remoteHost = GetRemoteHost();
586
587   Engines::MachineParameters params;
588   _LCC.preSet(params); 
589   params.hostname = remoteHost.c_str();
590   params.container_name = "anotherContainer";
591
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));
597
598   // --- check narrow
599
600   Engines::TestComponent_var m1;
601   m1 = Engines::TestComponent::_narrow(mycompo1);
602
603   // --- check that container is on good computer
604
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);
610 }
611
612
613
614 // ============================================================================
615 /*!
616  * Check FindOrLoad_Component params = containerName
617  */
618 // ============================================================================
619
620 // void
621 // LifeCycleCORBATest::testFindOrLoad_Component_()
622 // {
623 // }
624
625
626
627
628
629
630
631
632 // ============================================================================
633 /*!
634  *  Get a remote HostName in the Resource Catalog
635  */
636 // ============================================================================
637
638 string LifeCycleCORBATest::GetRemoteHost()
639 {
640   SALOME_LifeCycleCORBA _LCC(&_NS);
641
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));
647
648   Engines::MachineParameters params;
649   _LCC.preSet(params);               // empty params to get all the machines
650
651   Engines::MachineList_var hostList =
652     containerManager->GetFittingResources(params,"SalomeTestComponent");
653   CPPUNIT_ASSERT(hostList->length() > 1);
654
655   string localHost = GetHostname();
656   string remoteHost;
657   for (unsigned int i=0; i < hostList->length(); i++)
658     {
659       const char* aMachine = hostList[i];
660       string machine(aMachine);
661       if (machine != localHost)
662         {
663           remoteHost = machine;
664           break;
665         }
666     }
667   CPPUNIT_ASSERT(remoteHost != "");
668   return remoteHost;
669 }