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