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