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