Salome HOME
Added a possibility to wait Supervisor component being launched, one needs to define...
[modules/kernel.git] / src / SALOMEDS / Test / TestSALOMEDS.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/ or email : webmaster.salome@opencascade.com
19 //
20
21 // --- include all SALOMEDS Test from basics until the present directory
22
23 #include "SALOMELocalTraceTest.hxx"
24 #include "SALOMEDSImplTest.hxx"
25 #include "SALOMEDSTest.hxx"
26
27 #include <TCollection_AsciiString.hxx>
28
29 // --- Registers the fixture into the 'registry'
30
31 CPPUNIT_TEST_SUITE_REGISTRATION( SALOMEDSTest );
32 CPPUNIT_TEST_SUITE_REGISTRATION( SALOMEDSTest_Embedded );
33
34 #include <cppunit/CompilerOutputter.h>
35 #include <cppunit/TestResult.h>
36 #include <cppunit/TestResultCollector.h>
37 #include <cppunit/TextTestProgressListener.h>
38 #include <cppunit/BriefTestProgressListener.h>
39 #include <cppunit/extensions/TestFactoryRegistry.h>
40 #include <cppunit/TestRunner.h>
41 #include <stdexcept>
42
43 #include <iostream>
44 #include <fstream>
45
46 #include <SALOMEconfig.h>
47 #include CORBA_SERVER_HEADER(SALOMEDS)
48 #include "utilities.h"
49 #include "Utils_SINGLETON.hxx"
50 #include "Utils_ORB_INIT.hxx"
51 #include "OpUtil.hxx"
52 #include "SALOME_NamingService.hxx"
53 #include "NamingService_WaitForServerReadiness.hxx"
54 #include "SALOMEDS_StudyManager_i.hxx"
55
56 // ============================================================================
57 /*!
58  *  Main program source for Unit Tests with cppunit package does not depend
59  *  on actual tests, so we use the same for all partial unit tests.
60  */
61 // ============================================================================
62
63 int main(int argc, char* argv[])
64 {
65   // --- Run Salome without GUI
66
67   setenv("SALOME_trace", "file:./traceUnitTest.log", 1); // 1: overwrite
68
69   system("runSalome -t &");
70
71   // --- Wait till SALOMEDS server is launched
72   
73   char hostname[511];
74   int size;
75   gethostname(hostname, size);
76   char* chr_port = getenv("SALOMEDS_UNITTESTS_PORT");
77   string port;
78   if(chr_port) port = chr_port;
79   if(port.empty()) port = "2810";
80   string cfg_file = string(getenv("HOME"))+"/.omniORB_"+string(hostname)+"_"+port+".cfg";
81   setenv("OMNIORB_CONFIG", cfg_file.c_str(), 1);
82
83   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
84   ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
85   CORBA::ORB_var orb = init(argc , argv ) ;
86
87   sleep(15);
88
89   string host; // = GetHostname();
90   char* wait_Superv = getenv("SALOMEDS_UNITTESTS_WAIT_SUPERVISOR");
91   if(wait_Superv) host = GetHostname(); 
92
93   SALOME_NamingService NS(orb);
94   if(host.empty())
95     NamingService_WaitForServerReadiness(&NS, "/myStudyManager");
96   else {
97     string serverName = "/Containers/"+host+"/SuperVisionContainer";
98     NamingService_WaitForServerReadiness(&NS, serverName);
99   }
100
101   CORBA::Object_var obj = NS.Resolve( "/myStudyManager" );
102   if(CORBA::is_nil(obj)) {
103      system("killSalome.py");
104      return 1;
105   }
106
107   //Set up the environement for Embedded case
108   string kernel_root = getenv("KERNEL_ROOT_DIR");
109   CPPUNIT_ASSERT(!kernel_root.empty());
110   kernel_root+="/share/salome/resources/kernel";
111   setenv("CSF_PluginDefaults", kernel_root.c_str(), 1);
112   setenv("CSF_SALOMEDS_ResourcesDefaults", kernel_root.c_str(), 1);
113
114   CORBA::Object_var poaObj = orb->resolve_initial_references("RootPOA");
115   if(!CORBA::is_nil(poaObj)) {
116                 PortableServer::POA_var poa = PortableServer::POA::_narrow(poaObj);
117
118     SALOMEDS_StudyManager_i * aStudyManager_i = new  SALOMEDS_StudyManager_i(orb, poa);
119     // Activate the objects.  This tells the POA that the objects are ready to accept requests.
120     PortableServer::ObjectId_var aStudyManager_iid =  poa->activate_object(aStudyManager_i);
121     aStudyManager_i->register_name("/myStudyManager_embedded");
122       
123     // Obtain a POAManager, and tell the POA to start accepting
124     // requests on its objects.
125     PortableServer::POAManager_var pman = poa->the_POAManager();
126     pman->activate();
127   }
128  
129
130   // --- Create the event manager and test controller
131   CPPUNIT_NS::TestResult controller;
132
133   // ---  Add a listener that colllects test result
134   CPPUNIT_NS::TestResultCollector result;
135   controller.addListener( &result );        
136
137   // ---  Add a listener that print dots as test run.
138 #ifdef WIN32
139   CPPUNIT_NS::TextTestProgressListener progress;
140 #else
141   CPPUNIT_NS::BriefTestProgressListener progress;
142 #endif
143   controller.addListener( &progress );      
144
145   // ---  Get the top level suite from the registry
146
147   CPPUNIT_NS::Test *suite =
148     CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest();
149
150   // ---  Adds the test to the list of test to run
151
152   CPPUNIT_NS::TestRunner runner;
153   runner.addTest( suite );
154   runner.run( controller);
155
156   // ---  Print test in a compiler compatible format.
157
158   std::ofstream testFile;
159   testFile.open("UnitTestsResult", std::ios::out |  std::ios::trunc);
160   //CPPUNIT_NS::CompilerOutputter outputter( &result, std::cerr );
161   CPPUNIT_NS::CompilerOutputter outputter( &result, testFile );
162   outputter.write(); 
163
164   // ---  Run the tests.
165
166   bool wasSucessful = result.wasSuccessful();
167   testFile.close();
168
169   // --- Kill all created Salome process
170
171   system("killSalome.py");
172   system("rm -f ./traceUnitTest.log");
173   // ---  Return error code 1 if the one of test failed.
174
175   return wasSucessful ? 0 : 1;
176 }