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