Salome HOME
PR: merge from branch BR_auto_V310 tag mergefrom_OCC_development_for_3_2_0a2_10mar06
[modules/yacs.git] / src / SALOMEDS / Test / SALOMEDSTest.cxx
1 // Copyright (C) 2006  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/
19 //
20
21 #include "SALOMEDSTest.hxx"
22
23 #include <iostream>
24 #include <fstream>
25 #include <string>
26 #include <vector>
27 #include <cstdlib>
28 #include "utilities.h"
29 #include "Utils_SALOME_Exception.hxx"
30 #include "Utils_ORB_INIT.hxx"
31 #include "Utils_SINGLETON.hxx"
32 #include "OpUtil.hxx"
33
34 #include "SALOMEDSClient.hxx"
35 #include "SALOMEDSClient_ClientFactory.hxx"
36
37 #include <TCollection_AsciiString.hxx>
38
39 using namespace std;
40
41 #define PT_INTEGER 0
42 #define PT_REAL    1
43 #define PT_BOOLEAN 2
44 #define PT_STRING  3
45 #define PT_REALARRAY 4
46 #define PT_INTARRAY  5
47 #define PT_STRARRAY  6
48
49
50 #define TRACEFILE "/tmp/traceUnitTest.log"
51
52 // ============================================================================
53 /*!
54  * Set up the environment
55  */
56 // ============================================================================
57
58 void SALOMEDSTest::setUp()
59 {
60   TCollection_AsciiString kernel(getenv("KERNEL_ROOT_DIR"));
61   TCollection_AsciiString subPath("/share/salome/resources");
62   TCollection_AsciiString csf_var = (kernel+subPath);
63   setenv("CSF_PluginDefaults", csf_var.ToCString(), 0);
64   setenv("CSF_SALOMEDS_ResourcesDefaults", csf_var.ToCString(), 0);
65
66   // --- trace on file
67   char *theFileName = TRACEFILE;
68
69   string s = "file:";
70   s += theFileName;
71   CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite
72
73   ofstream traceFile;
74   traceFile.open(theFileName, ios::out | ios::app);
75   CPPUNIT_ASSERT(traceFile); // file created empty, then closed
76   traceFile.close();
77
78   LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
79   CPPUNIT_ASSERT(bp1);
80
81   // --- Get or initialize the orb
82
83   int _argc = 1;
84   char* _argv[] = {""};
85   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
86   ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
87   _orb = init(_argc , _argv ) ;
88
89   sleep(2);
90
91   // --- Create a SALOME_NamingService instance
92
93   _NS = new SALOME_NamingService;
94
95   sleep(2);
96
97   _NS->init_orb(_orb) ;
98 }
99
100 // ============================================================================
101 /*!
102  *  - delete trace classes
103  */
104 // ============================================================================
105
106 void 
107 SALOMEDSTest::tearDown()
108 {
109   LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
110   CPPUNIT_ASSERT(bp1);
111   bp1->deleteInstance(bp1);
112
113   delete _NS;
114 }
115
116 // ============================================================================
117 /*!
118  * Check AttributeParameter
119  */
120 // ============================================================================
121 void SALOMEDSTest::testAttributeParameter()
122 {
123   CORBA::Object_var obj = _orb->resolve_initial_references( "RootPOA" );
124   PortableServer::POA_var poa = PortableServer::POA::_narrow( obj );
125
126   PortableServer::POAManager_var pman = poa->the_POAManager();
127   pman->activate() ;
128
129   _PTR(StudyManager) sm = ClientFactory::createStudyManager(_orb, poa);
130
131   CPPUNIT_ASSERT(sm);
132
133   _PTR(Study) study = sm->NewStudy("Test");
134
135   CPPUNIT_ASSERT(study);
136
137   _PTR(AttributeParameter) _ap = study->GetCommonParameters("TestComp", 0);
138
139   CPPUNIT_ASSERT(_ap);
140
141   _ap->SetInt("IntValue", 1);
142   CPPUNIT_ASSERT(_ap->IsSet("IntValue", PT_INTEGER));
143   CPPUNIT_ASSERT(_ap->GetInt("IntValue") == 1);
144
145   _ap->SetReal("RealValue", 1.2);
146   CPPUNIT_ASSERT(_ap->IsSet("RealValue", PT_REAL));
147   CPPUNIT_ASSERT(_ap->GetReal("RealValue") == 1.2);
148
149   _ap->SetString("StringValue", "hello");
150   CPPUNIT_ASSERT(_ap->IsSet("StringValue", PT_STRING));
151   CPPUNIT_ASSERT(_ap->GetString("StringValue") == "hello");
152
153   _ap->SetBool("BoolValue", 0);
154   CPPUNIT_ASSERT(_ap->IsSet("BoolValue", PT_BOOLEAN));
155   CPPUNIT_ASSERT(!_ap->GetBool("BoolValue"));
156
157   _ap->SetBool("BoolValue", 0);
158   CPPUNIT_ASSERT(_ap->IsSet("BoolValue", PT_BOOLEAN));
159   CPPUNIT_ASSERT(!_ap->GetBool("BoolValue"));
160
161   vector<int> intArray;
162   intArray.push_back(0);
163   intArray.push_back(1);
164
165   _ap->SetIntArray("IntArray", intArray);
166   CPPUNIT_ASSERT(_ap->IsSet("IntArray", PT_INTARRAY));
167   CPPUNIT_ASSERT(_ap->GetIntArray("IntArray")[0] == 0);
168   CPPUNIT_ASSERT(_ap->GetIntArray("IntArray")[1] == 1); 
169
170   vector<double> realArray;
171   realArray.push_back(0.0);
172   realArray.push_back(1.1);
173   
174   _ap->SetRealArray("RealArray", realArray);
175   CPPUNIT_ASSERT(_ap->IsSet("RealArray", PT_REALARRAY));
176   CPPUNIT_ASSERT(_ap->GetRealArray("RealArray")[0] == 0.0);
177   CPPUNIT_ASSERT(_ap->GetRealArray("RealArray")[1] == 1.1); 
178
179   vector<string> strArray;
180   strArray.push_back("hello");
181   strArray.push_back("world");
182   
183   _ap->SetStrArray("StrArray", strArray);
184   CPPUNIT_ASSERT(_ap->IsSet("StrArray", PT_STRARRAY));
185   CPPUNIT_ASSERT(_ap->GetStrArray("StrArray")[0] == "hello");
186   CPPUNIT_ASSERT(_ap->GetStrArray("StrArray")[1] == "world"); 
187 }
188
189
190