Salome HOME
PR: merge from branch BR_UnitTests tag mergeto_trunk_17oct05
[modules/kernel.git] / src / TestContainer / TestContainer.cxx
1 //  SALOME TestContainer : test of container creation and its life cycle
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : TestContainer.cxx
25 //  Author : Paul RASCLE, EDF - MARC TAJCHMAN, CEA
26 //  Module : SALOME
27 //  $Header$
28
29 #include "utilities.h"
30 #include <iostream>
31 #include <unistd.h>
32 #include <SALOMEconfig.h>
33 #include CORBA_CLIENT_HEADER(SALOME_Component)
34 #include CORBA_CLIENT_HEADER(SALOME_TestComponent)
35
36 #include "SALOME_NamingService.hxx"
37 #include "NamingService_WaitForServerReadiness.hxx"
38 #include "OpUtil.hxx"
39 #include "Utils_ORB_INIT.hxx"
40 #include "Utils_SINGLETON.hxx"
41 #include "Utils_SALOME_Exception.hxx"
42 #include "Utils_CommException.hxx"
43 #include "SALOMETraceCollector.hxx"
44 using namespace std;
45
46 static ostream& operator<<(ostream& os, const CORBA::Exception& e)
47 {
48   CORBA::Any tmp;
49   tmp<<= e;
50   CORBA::TypeCode_var tc = tmp.type();
51   const char *p = tc->name();
52   os<<"Test blocking exception was catch of the kind : ";
53   if ( *p != '\0' ) {
54     os<<p;
55   } 
56   else  { 
57     os << tc->id();
58   }
59   
60   return os;
61 }
62
63 Engines::TestComponent_ptr create_instance(Engines::Container_ptr iGenFact,
64                                            string componenttName)
65 {
66   bool isLib =
67     iGenFact->load_component_Library(componenttName.c_str());
68   //    iGenFact->load_component_Library("SalomeTestComponent");
69   ASSERT(isLib);
70   CORBA::Object_var obj =
71     //    iGenFact->create_component_instance("SalomeTestComponent",
72     iGenFact->create_component_instance(componenttName.c_str(),
73                                         0);
74   Engines::TestComponent_var anInstance = Engines::TestComponent::_narrow(obj);
75   MESSAGE("create anInstance");
76   SCRUTE(anInstance->instanceName());
77   return anInstance._retn();
78 }
79
80 int main (int argc, char * argv[])
81 {
82   // Initializing omniORB
83   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
84   CORBA::ORB_var &orb = init( argc , argv ) ;
85   //  LocalTraceCollector *myThreadTrace = SALOMETraceCollector::instance(orb);
86
87   try
88     {
89       SALOME_NamingService _NS(orb) ;
90       string containerName = "/Containers/" ;
91       string hostName = GetHostname();
92       containerName += hostName + "/FactoryServer";
93       NamingService_WaitForServerReadiness(&_NS,containerName);
94
95       CORBA::Object_var obj = _NS.Resolve(containerName.c_str()) ;
96       Engines::Container_var iGenFact = Engines::Container::_narrow(obj);
97
98       int nbInstances = 5;
99
100       vector<Engines::TestComponent_var> instances(nbInstances);
101     
102       MESSAGE("------------------------------- create instances ");
103       for (int iter = 0; iter < nbInstances ; iter++)
104         {
105           instances[iter] = create_instance(iGenFact,"SalomeTestComponent");
106         }
107
108       MESSAGE("------------------------------ set env instances ");
109       for (int iter = 0; iter < nbInstances ; iter++)
110         {
111           Engines::TestComponent_var anInstance = instances[iter];
112           SCRUTE(anInstance->instanceName());
113           Engines::FieldsDict_var dico = new Engines::FieldsDict;
114           dico->length(3);
115           dico[0].key=CORBA::string_dup("key_0");
116           dico[0].value <<="value_0";
117           dico[1].key=CORBA::string_dup("key_1");
118           dico[1].value <<=(CORBA::UShort)72;
119           dico[2].key=CORBA::string_dup("key_2");
120           dico[2].value <<=(CORBA::ULong)iter;
121           anInstance->setProperties(dico);
122           MESSAGE("Coucou " << anInstance->Coucou(iter));
123           anInstance->Setenv();
124         }
125
126       MESSAGE("---------------------------------- get instances ");
127       for (int iter = 0; iter < nbInstances ; iter++)
128         {
129           Engines::TestComponent_var anInstance = instances[iter];
130           SCRUTE(anInstance->instanceName());
131           Engines::FieldsDict_var dico2 =  anInstance->getProperties();
132           for (CORBA::ULong i=0; i<dico2->length(); i++)
133             {
134               MESSAGE("dico2["<<i<<"].key="<<dico2[i].key);
135               MESSAGE("dico2["<<i<<"].value type ="<<dico2[i].value.type()->kind());
136               if (dico2[i].value.type()->kind() == CORBA::tk_string)
137                 {
138                   const char* value;
139                   dico2[i].value >>= value;
140                   MESSAGE("dico2["<<i<<"].value="<<value);
141                 }
142             }
143         }
144
145       MESSAGE("------------------------------- remove instances ");
146       for (int iter = 0; iter < nbInstances ; iter++)
147         {
148           Engines::TestComponent_var anInstance = instances[iter];
149           SCRUTE(anInstance->instanceName());
150           iGenFact->remove_impl(anInstance) ;
151           //iGenFact->finalize_removal() ; // unpredictable results ...
152         } 
153       MESSAGE("------------------------------- PYTHON ");
154       {
155 //      bool isLib =
156 //        iGenFact->load_component_Library("SALOME_TestComponentPy");
157 //      ASSERT(isLib);
158 //      CORBA::Object_var obj =
159 //        iGenFact->create_component_instance("SALOME_TestComponentPy",
160 //                                            0);
161 //      Engines::TestComponent_var anInstance =
162 //        Engines::TestComponent::_narrow(obj);
163 //      MESSAGE("create anInstance");
164 //      SCRUTE(anInstance->instanceName());
165       MESSAGE("------------------------------- create instances ");
166       for (int iter = 0; iter < nbInstances ; iter++)
167         {
168           instances[iter] = create_instance(iGenFact,"SALOME_TestComponentPy");
169         }
170
171       MESSAGE("---------------------------------- get instances ");
172       for (int iter = 0; iter < nbInstances ; iter++)
173         {
174           Engines::TestComponent_var anInstance = instances[iter];
175           SCRUTE(anInstance->instanceName());
176           MESSAGE("Coucou " << anInstance->Coucou(iter));
177         }
178       }
179    
180       // Clean-up.
181       iGenFact->finalize_removal() ;
182       orb->shutdown(0);
183     }
184   catch(CORBA::COMM_FAILURE& ex) {
185     INFOS("Caught system exception COMM_FAILURE -- unable to contact the object.")
186       }
187   catch(CORBA::SystemException& e) {
188     INFOS("Caught a CORBA::SystemException."<<e)
189       }
190   catch(CORBA::Exception& e) {
191     INFOS("Caught CORBA::Exception."<<e)
192       }
193   catch(...) {
194     INFOS("Caught unknown exception.")
195       }
196
197   //  delete myThreadTrace;
198   return 0;
199 }
200