]> SALOME platform Git repositories - modules/yacs.git/blob - src/LifeCycleCORBA/Test/LifeCycleCORBATest.cxx
Salome HOME
PR: merge from branch BR_UnitTests tag mergeto_trunk_17oct05
[modules/yacs.git] / src / LifeCycleCORBA / Test / LifeCycleCORBATest.cxx
1
2 #include "LifeCycleCORBATest.hxx"
3 #include "SALOME_LifeCycleCORBA.hxx"
4 #include "Utils_ORB_INIT.hxx"
5 #include "Utils_SINGLETON.hxx"
6 #include "OpUtil.hxx"
7
8 #include <iostream>
9 #include <fstream>
10 #include <string>
11 #include <cstdlib>
12
13 using namespace std;
14
15 // --- uncomment to have some traces on standard error
16 //     (useful only when adding new tests...)
17 //#define _DEVDEBUG_
18
19 #ifdef _DEVDEBUG_
20 #define MYDEVTRACE {std::cerr << __FILE__ << " [" << __LINE__ << "] : ";}
21 #define DEVTRACE(msg) {MYDEVTRACE; std::cerr<<msg<<std::endl<<std::flush;}
22 #else
23 #define MYDEVTRACE
24 #define DEVTRACE(msg)
25 #endif
26
27 #define TRACEFILE "/tmp/traceUnitTest.log"
28
29 // ============================================================================
30 /*!
31  * Set Trace mecanism
32  * - delete preexisting trace classes if any
33  * - set trace on file
34  * Get or initialize the orb
35  * Create a SALOME_NamingService instance
36  */
37 // ============================================================================
38
39 void 
40 LifeCycleCORBATest::setUp()
41 {
42   LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
43   CPPUNIT_ASSERT(bp1);
44   bp1->deleteInstance(bp1);
45
46   // --- trace on file
47   char *theFileName = TRACEFILE;
48
49   string s = "file:";
50   s += theFileName;
51   //s="local";
52   //s="with_logger";
53   CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
54
55   ofstream traceFile;
56   //  traceFile.open(theFileName, ios::out | ios::trunc);
57   traceFile.open(theFileName, ios::out | ios::app);
58   CPPUNIT_ASSERT(traceFile); // file created empty, then closed
59   traceFile.close();
60
61   bp1 = LocalTraceBufferPool::instance();
62   CPPUNIT_ASSERT(bp1);
63
64   // --- Get or initialize the orb
65
66   int _argc = 1;
67   char* _argv[] = {""};
68   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
69   ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
70   _orb = init(_argc , _argv ) ;
71
72   // --- Create a SALOME_NamingService instance
73
74   _NS.init_orb(_orb) ;
75 }
76
77 // ============================================================================
78 /*!
79  *  - delete trace classes
80  */
81 // ============================================================================
82
83 void 
84 LifeCycleCORBATest::tearDown()
85 {
86
87   LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
88   CPPUNIT_ASSERT(bp1);
89   bp1->deleteInstance(bp1);
90 }
91
92 // ============================================================================
93 /*!
94  * Check FindOrLoad_Component.
95  * - get a local container (no hostname given),
96  *       load an engine, check that the CORBA object is not null.
97  * - check narrow
98  */
99 // ============================================================================
100
101 void
102 LifeCycleCORBATest::testFindOrLoad_Component_LaunchContainer()
103 {
104   SALOME_LifeCycleCORBA _LCC(&_NS);
105
106   // --- get a local container,
107   //     load an engine, check that the CORBA object is not null
108
109   string containerName = "myContainer";
110   Engines::Component_var mycompo =
111     _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
112   CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
113
114   // --- check narrow
115
116   Engines::TestComponent_var m1;
117   m1 = Engines::TestComponent::_narrow(mycompo);
118   CPPUNIT_ASSERT(!CORBA::is_nil(m1));
119 }
120
121 // ============================================================================
122 /*!
123  * Check FindOrLoad_Component.
124  * - Call 2 times FindOrLoad_Component with the same parameters
125  * - check if we get the same engine
126  */
127 // ============================================================================
128
129 void
130 LifeCycleCORBATest::testFindOrLoad_Component_SameInstance()
131 {
132   SALOME_LifeCycleCORBA _LCC(&_NS);
133
134   // --- get a local container,
135   //     load an engine, check that the CORBA object is not null
136
137   string containerName = "myContainer";
138
139   Engines::Component_var mycompo1 =
140     _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
141   CPPUNIT_ASSERT(!CORBA::is_nil(mycompo1));
142
143   Engines::Component_var mycompo2 =
144     _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
145   CPPUNIT_ASSERT(!CORBA::is_nil(mycompo2));
146
147   // --- check narrow
148
149   Engines::TestComponent_var m1;
150   m1 = Engines::TestComponent::_narrow(mycompo1);
151   CPPUNIT_ASSERT(!CORBA::is_nil(m1));
152
153   Engines::TestComponent_var m2;
154   m2 = Engines::TestComponent::_narrow(mycompo2);
155   CPPUNIT_ASSERT(!CORBA::is_nil(m2));
156
157   // --- check equality of instance names
158
159   string name1 = m1->instanceName();
160   string name2 = m2->instanceName();
161   CPPUNIT_ASSERT_EQUAL(name1, name2);
162 }
163
164 // ============================================================================
165 /*!
166  * Check FindOrLoad_Component with Python Component on C++ Container
167  *       load an engine, check that the CORBA object is not null.
168  * - check narrow
169  */
170 // ============================================================================
171
172 void
173 LifeCycleCORBATest::testFindOrLoad_Component_PythonInCppContainer()
174 {
175   SALOME_LifeCycleCORBA _LCC(&_NS);
176
177   // --- get a local container,
178   //     load an engine, check that the CORBA object is not null
179
180   string containerName = "myContainer";
181
182   Engines::Component_var mycompo1 =
183     _LCC.FindOrLoad_Component(containerName.c_str(),"SALOME_TestComponentPy");
184   CPPUNIT_ASSERT(!CORBA::is_nil(mycompo1));
185
186   // --- check narrow
187
188   Engines::TestComponent_var m1;
189   m1 = Engines::TestComponent::_narrow(mycompo1);
190   CPPUNIT_ASSERT(!CORBA::is_nil(m1));
191 }
192
193 // ============================================================================
194 /*!
195  * Check FindOrLoad_Component with Python Component on C++ Container
196  * - Call 2 times FindOrLoad_Component with the same parameters
197  * - check if we get the same engine
198  */
199 // ============================================================================
200
201 void
202 LifeCycleCORBATest::testFindOrLoad_Component_PythonSameInstance()
203 {
204   SALOME_LifeCycleCORBA _LCC(&_NS);
205
206   // --- get a local container (with a name based on local hostname),
207   //     load an engine, check that the CORBA object is not null
208
209   string containerName = "myContainer";
210
211   Engines::Component_var mycompo1 =
212     _LCC.FindOrLoad_Component(containerName.c_str(),"SALOME_TestComponentPy");
213   CPPUNIT_ASSERT(!CORBA::is_nil(mycompo1));
214
215   Engines::Component_var mycompo2 =
216     _LCC.FindOrLoad_Component(containerName.c_str(),"SALOME_TestComponentPy");
217   CPPUNIT_ASSERT(!CORBA::is_nil(mycompo2));
218
219   // --- check narrow
220
221   Engines::TestComponent_var m1;
222   m1 = Engines::TestComponent::_narrow(mycompo1);
223   CPPUNIT_ASSERT(!CORBA::is_nil(m1));
224
225   Engines::TestComponent_var m2;
226   m2 = Engines::TestComponent::_narrow(mycompo2);
227   CPPUNIT_ASSERT(!CORBA::is_nil(m2));
228
229   // --- check equality of instance names
230
231   string name1 = m1->instanceName();
232   string name2 = m2->instanceName();
233   CPPUNIT_ASSERT_EQUAL(name1, name2);
234 }
235
236 // ============================================================================
237 /*!
238  * Check FindOrLoad_Component with a component name not in catalog.
239  * See list of catalog given to module catalog server.
240  * Here, we work with KERNEL_SRC/resources/KERNELCatalog.xml that contains 
241  * only KERNEL, SalomeTestComponent and SALOME_TestComponentPy
242  */
243 // ============================================================================
244
245 void
246 LifeCycleCORBATest::testFindOrLoad_Component_UnknownInCatalog()
247 {
248   SALOME_LifeCycleCORBA _LCC(&_NS);
249
250   // --- get a local container (with a name based on local hostname),
251   //     load an engine, check that the CORBA object is not null
252
253   string containerName = "myContainer";
254
255   Engines::Component_var mycompo1 =
256     _LCC.FindOrLoad_Component(containerName.c_str(),"MyNewComponent");
257   CPPUNIT_ASSERT(CORBA::is_nil(mycompo1));
258 }
259
260 // ============================================================================
261 /*!
262  * Check FindOrLoad_Component with hostname given
263  * - get a local container : getHostName()/componentName,
264  *       load an engine, check that the CORBA object is not null.
265  * - check narrow
266  */
267 // ============================================================================
268
269 void
270 LifeCycleCORBATest::testFindOrLoad_Component_LaunchContainerHostname()
271 {
272   SALOME_LifeCycleCORBA _LCC(&_NS);
273
274   // --- get a local container (with a name based on local hostname),
275   //     load an engine, check that the CORBA object is not null
276
277   string containerName = GetHostname();
278   containerName += "/theContainer";
279   DEVTRACE("containerName = " << containerName);
280   Engines::Component_var mycompo =
281     _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
282   CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
283
284   // --- check narrow
285
286   Engines::TestComponent_var m1;
287   m1 = Engines::TestComponent::_narrow(mycompo);
288   CPPUNIT_ASSERT(!CORBA::is_nil(m1));
289 }
290
291 // ============================================================================
292 /*!
293  * Check FindOrLoad_Component with and without local hostname given.
294  * We must get the same container, the same instance of component
295  */
296 // ============================================================================
297
298 void
299 LifeCycleCORBATest::testFindOrLoad_Component_SameContainer()
300 {
301   SALOME_LifeCycleCORBA _LCC(&_NS);
302
303   // --- get a local container (with a name based on local hostname),
304   //     load an engine, check that the CORBA object is not null
305
306   string containerName = "aContainer";
307
308   Engines::Component_var mycompo1 =
309     _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
310   CPPUNIT_ASSERT(!CORBA::is_nil(mycompo1));
311
312   containerName = GetHostname();
313   containerName += "/aContainer";
314   DEVTRACE("containerName = " << containerName);
315   Engines::Component_var mycompo2 =
316     _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
317   CPPUNIT_ASSERT(!CORBA::is_nil(mycompo2));
318
319   // --- check narrow
320
321   Engines::TestComponent_var m1;
322   m1 = Engines::TestComponent::_narrow(mycompo1);
323   CPPUNIT_ASSERT(!CORBA::is_nil(m1));
324
325   Engines::TestComponent_var m2;
326   m2 = Engines::TestComponent::_narrow(mycompo2);
327   CPPUNIT_ASSERT(!CORBA::is_nil(m2));
328
329   // --- check equality of instance names
330
331   string name1 = m1->instanceName();
332   string name2 = m2->instanceName();
333   CPPUNIT_ASSERT_EQUAL(name1, name2);
334
335   // --- check containers are the same servant (same container name+hostname)
336
337   Engines::Container_var c1 = m1->GetContainerRef();
338   CPPUNIT_ASSERT(!CORBA::is_nil(c1));
339   Engines::Container_var c2 = m2->GetContainerRef();
340   CPPUNIT_ASSERT(!CORBA::is_nil(c1));
341   string cname1 = c1->name();
342   string cname2 = c2->name();
343   CPPUNIT_ASSERT_EQUAL(cname1, cname2);
344   string hostname1 = c1->getHostName();
345   string hostname2 = c2->getHostName();
346   CPPUNIT_ASSERT_EQUAL(hostname1, hostname2);
347   CORBA::Long pidc1 = c1->getPID();
348   CORBA::Long pidc2 = c2->getPID();
349   CPPUNIT_ASSERT_EQUAL(pidc1, pidc2);
350 }
351
352 // ============================================================================
353 /*!
354  * Check FindOrLoad_Component: check behaviour when ask for an unknown computer
355  * We must catch a Salome Exception with "unknown host" message
356  */
357 // ============================================================================
358
359 void
360 LifeCycleCORBATest::testFindOrLoad_Component_UnknownMachine()
361 {
362   SALOME_LifeCycleCORBA _LCC(&_NS);
363
364   // --- try to get a distant container on an unknown machine (not existing)
365   //     check that the CORBA object is null
366
367   string containerName = "aFarAwayComputer";
368   containerName += "/theContainer";
369 //   CPPUNIT_ASSERT_THROW(Engines::Component_var mycompo =
370 //                     _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");,SALOME::SALOME_Exception);
371   try
372     {
373       Engines::Component_var mycompo =
374         _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
375     }
376   catch(const SALOME::SALOME_Exception &ex)
377     {
378       CPPUNIT_ASSERT(true);
379 //       string expectedMessage = "BAD PARAM";
380 //       std::ostream os;
381 //       os << ex;
382 //       string actualMessage = os.str();
383 //       DEVTRACE("actual Exception Message = " << actualMessage);
384 //       CPPUNIT_ASSERT(actualMessage.find(expectedMessage) != string::npos);
385     }
386 }
387
388 // ============================================================================
389 /*!
390  * Check FindOrLoad_Component, empty params must give an instance
391  */
392 // ============================================================================
393
394 void
395 LifeCycleCORBATest::testFindOrLoad_Component_ParamsEmpty()
396 {
397   SALOME_LifeCycleCORBA _LCC(&_NS);
398
399   Engines::MachineParameters params;
400   _LCC.preSet(params);
401   Engines::Component_var mycompo =
402     _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
403   CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
404
405   // --- check narrow
406
407   Engines::TestComponent_var m1;
408   m1 = Engines::TestComponent::_narrow(mycompo);
409 }
410
411 // ============================================================================
412 /*!
413  * Check FindOrLoad_Component params = local container 
414  */
415 // ============================================================================
416
417 void
418 LifeCycleCORBATest::testFindOrLoad_Component_ParamsLocalContainer()
419 {
420   SALOME_LifeCycleCORBA _LCC(&_NS);
421
422   Engines::MachineParameters params;
423   _LCC.preSet(params);
424   string hostname=GetHostname();
425   params.hostname=hostname.c_str();
426   Engines::Component_var mycompo =
427     _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
428   CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
429
430   // --- check narrow
431
432   Engines::TestComponent_var m1;
433   m1 = Engines::TestComponent::_narrow(mycompo);
434
435   // --- check that container is on local computer
436
437   CPPUNIT_ASSERT(!CORBA::is_nil(m1));
438   Engines::Container_var c1 = m1->GetContainerRef();
439   CPPUNIT_ASSERT(!CORBA::is_nil(c1));
440   string hostname1 = c1->getHostName();
441   CPPUNIT_ASSERT_EQUAL(hostname1, GetHostname());
442 }
443
444
445 // ============================================================================
446 /*!
447  * Check FindOrLoad_Component params = containerName
448  */
449 // ============================================================================
450
451 void
452 LifeCycleCORBATest::testFindOrLoad_Component_ParamsContainerName()
453 {
454   SALOME_LifeCycleCORBA _LCC(&_NS);
455
456   Engines::MachineParameters params;
457   _LCC.preSet(params);
458   string containerName = "myContainer";
459   params.container_name = containerName.c_str();
460   Engines::Component_var mycompo =
461     _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
462   CPPUNIT_ASSERT(!CORBA::is_nil(mycompo));
463
464   // --- check narrow
465
466   Engines::TestComponent_var m1;
467   m1 = Engines::TestComponent::_narrow(mycompo);
468
469   // --- check that container has good name
470
471   CPPUNIT_ASSERT(!CORBA::is_nil(m1));
472   Engines::Container_var c1 = m1->GetContainerRef();
473   CPPUNIT_ASSERT(!CORBA::is_nil(c1));
474   string hostname1 = c1->getHostName();
475   CPPUNIT_ASSERT_EQUAL(hostname1, GetHostname());
476   string cname1 = c1->name();
477   CPPUNIT_ASSERT(cname1.find(containerName) != string::npos);
478 }
479
480 // ============================================================================
481 /*!
482  * Check FindOrLoad_Component on remote computer
483  */
484 // ============================================================================
485
486 void
487 LifeCycleCORBATest::testFindOrLoad_Component_RemoteComputer()
488 {
489   SALOME_LifeCycleCORBA _LCC(&_NS);
490
491   string remoteHost = GetRemoteHost();
492
493   string containerName = remoteHost;
494   containerName += "/aContainer";
495   DEVTRACE("containerName = " << containerName);
496   Engines::Component_var mycompo1 =
497     _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent");
498   CPPUNIT_ASSERT_MESSAGE("Test validity requires connection to remote "\
499                          "computers from ResourcesCatalog",
500                          !CORBA::is_nil(mycompo1));
501
502   // --- check narrow
503
504   Engines::TestComponent_var m1;
505   m1 = Engines::TestComponent::_narrow(mycompo1);
506
507   // --- check that container is on good computer
508
509   CPPUNIT_ASSERT(!CORBA::is_nil(m1));
510   Engines::Container_var c1 = m1->GetContainerRef();
511   CPPUNIT_ASSERT(!CORBA::is_nil(c1));
512   string hostname1 = c1->getHostName();
513   CPPUNIT_ASSERT_EQUAL(hostname1, remoteHost);
514 }
515
516 // ============================================================================
517 /*!
518  * Check FindOrLoad_Component with params on remote computer
519  * params empty except hostname 
520  */
521 // ============================================================================
522
523 void
524 LifeCycleCORBATest::testFindOrLoad_Component_ParamsRemoteComputer()
525 {
526   SALOME_LifeCycleCORBA _LCC(&_NS);
527
528   string remoteHost = GetRemoteHost();
529
530   Engines::MachineParameters params;
531   _LCC.preSet(params); 
532   params.hostname = remoteHost.c_str();
533
534   Engines::Component_var mycompo1 =
535     _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
536   CPPUNIT_ASSERT_MESSAGE("Test validity requires connection to remote "\
537                          "computers from ResourcesCatalog",
538                          !CORBA::is_nil(mycompo1));
539
540   // --- check narrow
541
542   Engines::TestComponent_var m1;
543   m1 = Engines::TestComponent::_narrow(mycompo1);
544
545   // --- check that container is on good computer
546
547   CPPUNIT_ASSERT(!CORBA::is_nil(m1));
548   Engines::Container_var c1 = m1->GetContainerRef();
549   CPPUNIT_ASSERT(!CORBA::is_nil(c1));
550   string hostname1 = c1->getHostName();
551   CPPUNIT_ASSERT_EQUAL(hostname1, remoteHost);
552 }
553
554 // ============================================================================
555 /*!
556  * Check FindOrLoad_Component with params on remote computer
557  * params empty except hostname and container_name
558  */
559 // ============================================================================
560
561 void
562 LifeCycleCORBATest::testFindOrLoad_Component_ParamsRemoteComputer2()
563 {
564   SALOME_LifeCycleCORBA _LCC(&_NS);
565
566   string remoteHost = GetRemoteHost();
567
568   Engines::MachineParameters params;
569   _LCC.preSet(params); 
570   params.hostname = remoteHost.c_str();
571   params.container_name = "anotherContainer";
572
573   Engines::Component_var mycompo1 =
574     _LCC.FindOrLoad_Component(params,"SalomeTestComponent");
575   CPPUNIT_ASSERT_MESSAGE("Test validity requires connection to remote "\
576                          "computers from ResourcesCatalog",
577                          !CORBA::is_nil(mycompo1));
578
579   // --- check narrow
580
581   Engines::TestComponent_var m1;
582   m1 = Engines::TestComponent::_narrow(mycompo1);
583
584   // --- check that container is on good computer
585
586   CPPUNIT_ASSERT(!CORBA::is_nil(m1));
587   Engines::Container_var c1 = m1->GetContainerRef();
588   CPPUNIT_ASSERT(!CORBA::is_nil(c1));
589   string hostname1 = c1->getHostName();
590   CPPUNIT_ASSERT_EQUAL(hostname1, remoteHost);
591 }
592
593
594
595 // ============================================================================
596 /*!
597  * Check FindOrLoad_Component params = containerName
598  */
599 // ============================================================================
600
601 // void
602 // LifeCycleCORBATest::testFindOrLoad_Component_()
603 // {
604 // }
605
606
607
608
609
610
611
612
613 // ============================================================================
614 /*!
615  *  Get a remote HostName in the Resource Catalog
616  */
617 // ============================================================================
618
619 string LifeCycleCORBATest::GetRemoteHost()
620 {
621   SALOME_LifeCycleCORBA _LCC(&_NS);
622
623   CORBA::Object_var obj = _NS.Resolve("/ContainerManager");
624   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
625   Engines::ContainerManager_var containerManager =
626     Engines::ContainerManager::_narrow(obj);
627   CPPUNIT_ASSERT(!CORBA::is_nil(containerManager));
628
629   Engines::MachineParameters params;
630   _LCC.preSet(params);               // empty params to get all the machines
631
632   Engines::MachineList_var hostList =
633     containerManager->GetFittingResources(params,"SalomeTestComponent");
634   CPPUNIT_ASSERT(hostList->length() > 1);
635
636   string localHost = GetHostname();
637   string remoteHost;
638   for (unsigned int i=0; i < hostList->length(); i++)
639     {
640       const char* aMachine = hostList[i];
641       string machine(aMachine);
642       if (machine != localHost)
643         {
644           remoteHost = machine;
645           break;
646         }
647     }
648   CPPUNIT_ASSERT(remoteHost != "");
649   return remoteHost;
650 }