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