Salome HOME
PR: remove argument library name (always libCOMPONENTEngine.so)
[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_intance(Engines::Container_ptr iGenFact)
64 {
65   bool isLib =
66     iGenFact->load_component_Library("SalomeTestComponent");
67   ASSERT(isLib);
68   CORBA::Object_var obj =
69     iGenFact->create_component_instance("SalomeTestComponent",
70                                         0);
71   Engines::TestComponent_var anInstance = Engines::TestComponent::_narrow(obj);
72   MESSAGE("create anInstance");
73   SCRUTE(anInstance->instanceName());
74   return anInstance._retn();
75 }
76
77 int main (int argc, char * argv[])
78 {
79   // Initializing omniORB
80   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
81   CORBA::ORB_var &orb = init( argc , argv ) ;
82   SALOMETraceCollector *myThreadTrace = SALOMETraceCollector::instance(orb);
83
84   try
85     {
86       SALOME_NamingService _NS(orb) ;
87       string containerName = "/Containers/" ;
88       string hostName = GetHostname();
89       containerName += hostName + "/FactoryServer";
90       NamingService_WaitForServerReadiness(&_NS,containerName);
91
92       CORBA::Object_var obj = _NS.Resolve(containerName.c_str()) ;
93       Engines::Container_var iGenFact = Engines::Container::_narrow(obj);
94
95       int nbInstances = 5;
96
97       vector<Engines::TestComponent_var> instances(nbInstances);
98     
99       MESSAGE("------------------------------- create instances ");
100       for (int iter = 0; iter < nbInstances ; iter++)
101         {
102           instances[iter] = create_intance(iGenFact);
103         }
104
105       MESSAGE("------------------------------ set env instances ");
106       for (int iter = 0; iter < nbInstances ; iter++)
107         {
108           Engines::TestComponent_var anInstance = instances[iter];
109           SCRUTE(anInstance->instanceName());
110           Engines::FieldsDict_var dico = new Engines::FieldsDict;
111           dico->length(3);
112           dico[0].key=CORBA::string_dup("key_0");
113           dico[0].value <<="value_0";
114           dico[1].key=CORBA::string_dup("key_1");
115           dico[1].value <<=(CORBA::UShort)72;
116           dico[2].key=CORBA::string_dup("key_2");
117           dico[2].value <<=(CORBA::ULong)iter;
118           anInstance->setProperties(dico);
119           MESSAGE("Coucou " << anInstance->Coucou(iter));
120           anInstance->Setenv();
121         }
122
123       MESSAGE("---------------------------------- get instances ");
124       for (int iter = 0; iter < nbInstances ; iter++)
125         {
126           Engines::TestComponent_var anInstance = instances[iter];
127           SCRUTE(anInstance->instanceName());
128           Engines::FieldsDict_var dico2 =  anInstance->getProperties();
129           for (CORBA::ULong i=0; i<dico2->length(); i++)
130             {
131               MESSAGE("dico2["<<i<<"].key="<<dico2[i].key);
132               MESSAGE("dico2["<<i<<"].value type ="<<dico2[i].value.type()->kind());
133               if (dico2[i].value.type()->kind() == CORBA::tk_string)
134                 {
135                   const char* value;
136                   dico2[i].value >>= value;
137                   MESSAGE("dico2["<<i<<"].value="<<value);
138                 }
139             }
140         }
141
142       MESSAGE("------------------------------- remove instances ");
143       for (int iter = 0; iter < nbInstances ; iter++)
144         {
145           Engines::TestComponent_var anInstance = instances[iter];
146           SCRUTE(anInstance->instanceName());
147           iGenFact->remove_impl(anInstance) ;
148           //iGenFact->finalize_removal() ; // unpredictable results ...
149         } 
150    
151       // Clean-up.
152       iGenFact->finalize_removal() ;
153       orb->shutdown(0);
154     }
155   catch(CORBA::COMM_FAILURE& ex) {
156     INFOS("Caught system exception COMM_FAILURE -- unable to contact the object.")
157       }
158   catch(CORBA::SystemException& e) {
159     INFOS("Caught a CORBA::SystemException."<<e)
160       }
161   catch(CORBA::Exception& e) {
162     INFOS("Caught CORBA::Exception."<<e)
163       }
164   catch(...) {
165     INFOS("Caught unknown exception.")
166       }
167
168   delete myThreadTrace;
169   return 0;
170 }
171