Salome HOME
Issue 0020194: EDF 977 ALL: Get rid of warnings PACKAGE_VERSION already defined
[modules/kernel.git] / src / SALOMEDS / Test / SALOMEDSTest_Study.cxx
1 //  Copyright (C) 2007-2008  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.
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  * Check all methods of SALOMEDS_Study
24  * Use code of SALOMEDS_Study.cxx
25  */
26
27 void SALOMEDSTest::testStudy()
28 {
29   //Create or find the Study manager
30   _PTR(StudyManager) sm ( new SALOMEDS_StudyManager(_sm) );
31
32   CPPUNIT_ASSERT(sm);
33
34   //Create a new study
35   _PTR(Study) study = sm->NewStudy("Test");
36
37   //Check the creation of the study
38   CPPUNIT_ASSERT(study);
39
40   //Check method NewBuilder
41   _PTR(StudyBuilder) studyBuilder = study->NewBuilder();
42   CPPUNIT_ASSERT(studyBuilder);
43
44   //Check method NewComponentIterator
45   _PTR(SComponentIterator) componentIterator = study->NewComponentIterator();
46
47   CPPUNIT_ASSERT(componentIterator);
48
49   //Check method GetTransientReference
50   CPPUNIT_ASSERT(!study->GetTransientReference().empty());
51
52   //Check method StudyId
53   CPPUNIT_ASSERT(study->StudyId() > 0);
54
55   //Check method Name (get/set)
56   CPPUNIT_ASSERT(study->Name() == "Test");
57   study->Name("New name");
58   CPPUNIT_ASSERT(study->Name() == "New name");
59   study->Name("Test");
60
61   //Check method URL (get/set)
62   study->URL("");
63   CPPUNIT_ASSERT(study->URL() == "");
64   study->URL("some url");
65   CPPUNIT_ASSERT(study->URL() == "some url");
66
67   //Check method GetPersistentReference == URL
68   CPPUNIT_ASSERT(study->GetPersistentReference() == "some url");
69
70   //Check method IsSaved (get/set)
71   study->IsSaved(true);
72   CPPUNIT_ASSERT(study->IsSaved());
73   study->IsSaved(false);
74   CPPUNIT_ASSERT(!study->IsSaved());
75
76   //Check method CreateObjectID
77   _PTR(SObject) so = study->CreateObjectID("0:2:1:3");
78   CPPUNIT_ASSERT(so);
79
80   //Try to create SObject with empty and invalid entries
81   CPPUNIT_ASSERT(!study->CreateObjectID(""));
82   CPPUNIT_ASSERT(!study->CreateObjectID("entry"));
83
84   //Check method NewChildIterator
85   _PTR(ChildIterator) childIterator = study->NewChildIterator(so);
86   CPPUNIT_ASSERT(childIterator);
87
88   //Check method IsEmpty
89   CPPUNIT_ASSERT(!study->IsEmpty());
90
91   //Check method FindComponent
92   _PTR(SComponent) sco1 = studyBuilder->NewComponent("test1");
93   _PTR(SComponent) sco2 = study->FindComponent("test1");
94   CPPUNIT_ASSERT(sco1 && sco2);
95   CPPUNIT_ASSERT(sco1->GetID() == sco2->GetID());
96   _PTR(AttributeName) name_attr_sco1 = studyBuilder->FindOrCreateAttribute(sco1, "AttributeName");
97   CPPUNIT_ASSERT(name_attr_sco1);
98   name_attr_sco1->SetValue("sco1");
99
100   //Try to find component with empty type
101   CPPUNIT_ASSERT(!study->FindComponent(""));
102
103   //Check method GetComponentNames
104   vector<string> components = study->GetComponentNames(""); //The context doesn't matter
105   CPPUNIT_ASSERT(components.size() == 1 && components[0] == "sco1");
106
107   //Check method FindComponentID
108   _PTR(SComponent) sco3 = study->FindComponentID(sco1->GetID());
109   CPPUNIT_ASSERT(sco3 && sco3->GetID() == sco1->GetID());
110
111   //Try to find component with empty id
112   CPPUNIT_ASSERT(!study->FindComponentID(""));
113
114   _PTR(SObject) so1 = studyBuilder->NewObject(sco1);
115   CPPUNIT_ASSERT(so1);
116   _PTR(AttributeName) name_attr_so1 = studyBuilder->FindOrCreateAttribute(so1, "AttributeName");
117   CPPUNIT_ASSERT(name_attr_so1);
118   name_attr_so1->SetValue("so1");
119   
120    //Create an attribute AttributeIOR
121   _PTR(AttributeIOR) ior_attr_so1 = studyBuilder->FindOrCreateAttribute(so1, "AttributeIOR");
122   CPPUNIT_ASSERT(ior_attr_so1);
123
124   string ior = _orb->object_to_string(_sm);
125   ior_attr_so1->SetValue(ior);
126   
127   _PTR(SObject) so2 = studyBuilder->NewObject(so1);
128   CPPUNIT_ASSERT(so2);
129   _PTR(AttributeName) name_attr_so2 = studyBuilder->FindOrCreateAttribute(so2, "AttributeName");
130   CPPUNIT_ASSERT(name_attr_so2);
131   name_attr_so2->SetValue("so2");
132
133   //Check method FindObject
134   _PTR(SObject) so3 = study->FindObject("so1");
135   CPPUNIT_ASSERT(so3 && so3->GetID() == so1->GetID());
136
137   //Try to find SObject with empty name
138   CPPUNIT_ASSERT(!study->FindObject(""));
139
140   //Check method FindObjectID
141   _PTR(SObject) so4 = study->FindObjectID(so1->GetID());
142   CPPUNIT_ASSERT(so4 && so4->GetID() == so1->GetID());
143
144   //Try to find SObject with empty ID
145   CPPUNIT_ASSERT(!study->FindObjectID(""));
146
147   //Check method FindObjectByName
148   vector< _PTR(SObject) > v = study->FindObjectByName("so1", sco1->ComponentDataType());
149   CPPUNIT_ASSERT(v.size()==1 && v[0]->GetID() == so1->GetID());
150
151   //Try to find SObject with empty name and empty component type
152   CPPUNIT_ASSERT((study->FindObjectByName("", "")).size() == 0);
153
154   //Check method FindObjectByPath
155   _PTR(SObject) path_so = study->FindObjectByPath("/"+sco1->GetName()+"/"+so1->GetName());
156   CPPUNIT_ASSERT(path_so && path_so->GetID() == so1->GetID());
157
158   //Check method FindObjectIOR
159   _PTR(SObject) so5 = study->FindObjectIOR(ior);
160   CPPUNIT_ASSERT(so5 && so5->GetID() == so1->GetID());
161
162   //Try to find SObject with empty IOR
163   CPPUNIT_ASSERT(!study->FindObjectIOR(""));
164
165   //Check method GetObjectPath
166   string path = study->GetObjectPath(so2);
167
168   //Try to get path of NULL SObject
169   _PTR(SObject) emptySO;
170   path = study->GetObjectPath(emptySO);
171   CPPUNIT_ASSERT(path.empty());
172
173   //Check method SetContext
174   study->SetContext("/sco1"); 
175   CPPUNIT_ASSERT(study->GetContext() == "/sco1");
176
177   //Check method FindObjectByPath
178   _PTR(SObject) so6 = study->FindObjectByPath("so1");
179   CPPUNIT_ASSERT(so6 && so6->GetID() == so1->GetID());
180
181   
182   //Try to find SObject with empty path
183   _PTR(SObject) tmp = study->FindObjectByPath(""); //Must return the Context SObject
184   CPPUNIT_ASSERT(tmp && tmp->GetID() == sco1->GetID());
185
186   study->SetContext("/"); //Root
187
188   //Check method GetObjectNames
189   vector<string> vs = study->GetObjectNames("/sco1");  
190   CPPUNIT_ASSERT(vs.size() == 2);
191     
192   //Check method GetDirectoryNames
193   _PTR(AttributeLocalID) locid_attr_sco1 = studyBuilder->FindOrCreateAttribute(sco1, "AttributeLocalID");
194   CPPUNIT_ASSERT(locid_attr_sco1);
195   locid_attr_sco1->SetValue(16661); //DIRECTORYID
196   _PTR(AttributeLocalID) locid_attr_so1 = studyBuilder->FindOrCreateAttribute(so1, "AttributeLocalID");
197   CPPUNIT_ASSERT(locid_attr_so1);
198   locid_attr_so1->SetValue(16661); //DIRECTORYID
199   vs = study->GetDirectoryNames(""); //Empty context (the current is taken)
200   CPPUNIT_ASSERT(vs.size() == 2);
201
202   //Check method GetFileNames
203   locid_attr_sco1->SetValue(26662); //FILELOCALID
204   _PTR(AttributePersistentRef) persref_attr_sco1 = studyBuilder->FindOrCreateAttribute(sco1, "AttributePersistentRef");
205   CPPUNIT_ASSERT(persref_attr_sco1);
206   persref_attr_sco1->SetValue("FILE: filename1");
207   locid_attr_so1->SetValue(26662); //FILELOCALID
208   _PTR(AttributePersistentRef) persref_attr_so1 = studyBuilder->FindOrCreateAttribute(so1, "AttributePersistentRef");
209   CPPUNIT_ASSERT(persref_attr_so1);
210   persref_attr_so1->SetValue("FILE: filename2");
211   vs = study->GetFileNames("");
212   CPPUNIT_ASSERT(vs.size() == 2 && vs[0] == "filename1" && vs[1] == "filename2");
213
214   //Check method StudyId (get/set)
215   int id = study->StudyId();
216   study->StudyId(-1);
217   CPPUNIT_ASSERT(study->StudyId() == -1);
218   study->StudyId(id);
219
220   //Check method FindDependances
221   studyBuilder->Addreference(so2, so1);
222   studyBuilder->Addreference(sco1, so1);
223   vector< _PTR(SObject) > vso = study->FindDependances(so1);
224   CPPUNIT_ASSERT(vso.size() == 2 && vso[0]->GetID() == so2->GetID() && vso[1]->GetID() == sco1->GetID());
225
226   //Check method GetProperties
227   _PTR(AttributeStudyProperties) sp = study->GetProperties();
228   CPPUNIT_ASSERT(sp);
229
230   //Check Lock functionality
231   sp->SetLocked(true);
232   bool isLockRaised = false;
233   try {
234      name_attr_so1->SetValue("test");      
235   }
236   catch(...) {
237     isLockRaised = true;
238   }
239   CPPUNIT_ASSERT(isLockRaised);
240  
241   sp->SetLocked(false);
242   isLockRaised = false;
243   try {
244      name_attr_so1->SetValue("test");      
245   }
246   catch(...) {
247     isLockRaised = true;
248   }
249   CPPUNIT_ASSERT(!isLockRaised);
250
251   //Check method GetLastModificationDate
252   sp->SetModification("srn", 1, 2, 3, 4, 5);  
253   sp->SetModification("srn", 6, 7, 8, 9, 10);
254   string date = study->GetLastModificationDate();  
255
256   CPPUNIT_ASSERT(date == "08/09/0010 07:06");
257
258   //Check method GetModificationsDate
259   vs = study->GetModificationsDate();
260   CPPUNIT_ASSERT(vs.size() == 2 && vs[0] == "03/04/0005 02:01" && vs[1] == "08/09/0010 07:06");
261
262   //Check method GetCommonParameters
263   _PTR(AttributeParameter) cp = study->GetCommonParameters("id", 1); //Save point = 1
264   CPPUNIT_ASSERT(cp);
265   cp->SetBool("test_true", true);
266   cp->SetBool("test_false", false);
267   _PTR(AttributeParameter) cp2 = study->GetCommonParameters("id", 1); //Save point = 1
268   CPPUNIT_ASSERT(cp2);
269   CPPUNIT_ASSERT(cp2->GetBool("test_true") && !cp2->GetBool("test_false"));
270   _PTR(AttributeParameter) cp21 = study->GetCommonParameters("id", 2); //Save point = 2
271   CPPUNIT_ASSERT(cp21);
272   CPPUNIT_ASSERT(!cp21->IsSet("test_true", PT_BOOLEAN) && !cp21->IsSet("test_false", PT_BOOLEAN));
273
274   //Check method GetModuleParameters
275   _PTR(AttributeParameter) cp3 = study->GetModuleParameters("id", "module name", 1); //Save point = 1, name = "module name"
276   CPPUNIT_ASSERT(cp3);
277   cp3->SetBool("test_true", true);
278   cp3->SetBool("test_false", false);
279   _PTR(AttributeParameter) cp4 = study->GetModuleParameters("id", "module name", 1); //Save point = 1, name = "module name"
280   CPPUNIT_ASSERT(cp4);
281   CPPUNIT_ASSERT(cp4->GetBool("test_true") && !cp4->GetBool("test_false"));
282   _PTR(AttributeParameter) cp5 = study->GetModuleParameters("id", "module name 2", 1); //Save point = 1, name = "module name 2"
283   CPPUNIT_ASSERT(cp5);
284   CPPUNIT_ASSERT(!cp5->IsSet("test_true", PT_BOOLEAN) && !cp5->IsSet("test_false", PT_BOOLEAN));
285   _PTR(AttributeParameter) cp6 = study->GetModuleParameters("id", "module name", 2); //Save point = 2, name = "module name"
286   CPPUNIT_ASSERT(cp6);
287   CPPUNIT_ASSERT(!cp6->IsSet("test_true", PT_BOOLEAN) && !cp6->IsSet("test_false", PT_BOOLEAN));
288
289   //Check method GetUseCaseBuilder
290   _PTR(UseCaseBuilder) ub = study->GetUseCaseBuilder();
291   CPPUNIT_ASSERT(ub);
292
293   //Check method SetStudyLock
294   study->SetStudyLock("locker1");
295
296   //Check method IsStudyLocked
297   CPPUNIT_ASSERT(study->IsStudyLocked());
298
299   //Check method GetLockerID
300   study->SetStudyLock("locker2");
301   vs = study->GetLockerID();
302   CPPUNIT_ASSERT(vs.size() == 2 && vs[0] == "locker1" && vs[1] == "locker2");
303
304   //Check method UnLockStudy
305   study->UnLockStudy("locker1");
306   CPPUNIT_ASSERT(study->IsStudyLocked());
307   vs = study->GetLockerID();
308   CPPUNIT_ASSERT(vs.size() == 1 && vs[0] == "locker2");
309   study->UnLockStudy("locker2");
310   CPPUNIT_ASSERT(!study->IsStudyLocked());
311   vs = study->GetLockerID();
312   CPPUNIT_ASSERT(vs.size() == 0);
313
314   //Check method EnableUseCaseAutoFilling
315   study->EnableUseCaseAutoFilling(false);
316   _PTR(SObject) uso1 = study->NewBuilder()->NewObject(sco1);
317   vector< _PTR(GenericAttribute) > va1 = uso1->GetAllAttributes();
318   CPPUNIT_ASSERT(va1.size() == 0); 
319
320   study->EnableUseCaseAutoFilling(true);
321   _PTR(SObject) uso2 = study->NewBuilder()->NewObject(sco1);
322   vector< _PTR(GenericAttribute) > va2 = uso2->GetAllAttributes();
323   CPPUNIT_ASSERT(va2.size() == 1); // +AttributeTreeNode
324
325   //Check method DumpStudy
326   study->DumpStudy(".", "SRN", false);
327
328   fstream f("SRN.py");
329   char buffer[128];
330   buffer[81] = (char)0;
331   f.getline(buffer, 80);
332   string line(buffer);
333  
334   f.close();
335   system("rm -f SRN.py");
336   CPPUNIT_ASSERT(line == "### This file is generated by SALOME automatically by dump python functionality");
337
338   //Check method Close 
339   bool isException = false;
340   try {
341     sm->Close(study);  //Close is called inside StudyManager::Close
342   }
343   catch(...) {
344     isException = true;
345   }
346   CPPUNIT_ASSERT(!isException);
347 }
348
349
350