Salome HOME
New CMake procedure.
[modules/kernel.git] / src / SALOMEDS / Test / SALOMEDSTest_StudyBuilder.cxx
1 // Copyright (C) 2007-2013  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 /*!
24  * Check all methods of SALOMEDS_StudyBuilder
25  * Use code of SALOMEDS_StudyBuilder.cxx
26  */
27
28 void SALOMEDSTest::testStudyBuilder()
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("TestStudyBuilder");
37
38   CPPUNIT_ASSERT(study);
39
40   //Create Study Builder
41   _PTR(StudyBuilder) studyBuilder = study->NewBuilder();
42
43   //Check the StudyBuilder creation
44   CPPUNIT_ASSERT(studyBuilder);
45
46   //Check method NewComponent
47   _PTR(SComponent) sco1 = studyBuilder->NewComponent("Test");
48   CPPUNIT_ASSERT(sco1 && sco1->ComponentDataType() == "Test");
49
50   //Check method DefineComponentInstance
51   std::string ior = _orb->object_to_string(_sm);
52   studyBuilder->DefineComponentInstance(sco1, ior);
53   std::string newior;
54   sco1->ComponentIOR(newior);
55   CPPUNIT_ASSERT(newior == ior);
56
57   //Check method RemoveComponent
58   studyBuilder->RemoveComponent(sco1);
59   _PTR(SComponent) sco2 = study->FindComponent("Test");
60   CPPUNIT_ASSERT(!sco2);
61
62   //Try to create and find the component with empty type
63   _PTR(SComponent) sco_empty = studyBuilder->NewComponent(""); 
64   CPPUNIT_ASSERT(!sco_empty);
65
66   _PTR(SComponent) sco3 = studyBuilder->NewComponent("NewComp");
67   CPPUNIT_ASSERT(sco3);
68
69   //Check method NewObject
70   _PTR(SObject) so1 = studyBuilder->NewObject(sco3);
71   CPPUNIT_ASSERT(so1);
72   std::string id1 = so1->GetID();
73
74   //Check method NewObjectToTag
75   _PTR(SObject) so2 = studyBuilder->NewObjectToTag(so1, 2);
76   CPPUNIT_ASSERT(so2 && so2->Tag() == 2);
77   std::string id2 = so2->GetID();
78
79   //Check method FindOrCreateAttribute
80   _PTR(SObject) so3 = studyBuilder->NewObject(sco3);
81   CPPUNIT_ASSERT(so3);
82   _PTR(AttributeName) an3 = studyBuilder->FindOrCreateAttribute(so3, "AttributeName");
83   CPPUNIT_ASSERT(an3);
84
85   //Try to create attribute with invalid type
86   CPPUNIT_ASSERT(!studyBuilder->FindOrCreateAttribute(so3, "invalid type"));
87
88   //Check method FindAttribute
89   _PTR(GenericAttribute) ga;
90   CPPUNIT_ASSERT(studyBuilder->FindAttribute(so3, ga, "AttributeName"));
91
92   //Try to find attribute with invalid type
93   CPPUNIT_ASSERT(!studyBuilder->FindAttribute(so3, ga, "invalid type"));
94
95   //Check method RemoveObject
96   studyBuilder->RemoveObject(so3);
97   CPPUNIT_ASSERT(!studyBuilder->FindAttribute(so3, ga, "AttributeName"));
98
99   //Check method RemoveObjectWithChildren
100   _PTR(AttributeName) an2 = studyBuilder->FindOrCreateAttribute(so2, "AttributeName");
101   CPPUNIT_ASSERT(an2);
102   studyBuilder->RemoveObjectWithChildren(so1);
103   CPPUNIT_ASSERT(!studyBuilder->FindAttribute(so2, ga, "AttributeName"));
104
105   //Check method RemoveAttribute
106   _PTR(AttributeName) an1 = studyBuilder->FindOrCreateAttribute(so1, "AttributeName");
107   CPPUNIT_ASSERT(an1);
108   CPPUNIT_ASSERT(studyBuilder->FindAttribute(so1, ga, "AttributeName"));
109   studyBuilder->RemoveAttribute(so1, "AttributeName");
110   CPPUNIT_ASSERT(!studyBuilder->FindAttribute(so1, ga, "AttributeName"));
111
112   //Check method Addreference
113   studyBuilder->Addreference(so2, so1);
114   _PTR(SObject) refSO;
115   CPPUNIT_ASSERT(so2->ReferencedObject(refSO) && refSO->GetID() == so1->GetID());
116
117   //Try to set reference to NULL SObject
118   bool isRaised = false;
119   _PTR(SObject) empty_so;
120   try {
121     studyBuilder->Addreference(so2, empty_so);
122   }
123   catch(...) {
124     isRaised = true;
125   }
126   CPPUNIT_ASSERT(isRaised);
127
128   //Check method RemoveReference
129   studyBuilder->RemoveReference(so2);
130   CPPUNIT_ASSERT(!so2->ReferencedObject(refSO));
131
132   //Check method SetGUID and IsGUID
133   std::string value = "0e1c36e6-379b-4d90-ab3b-17a14310e648";
134   studyBuilder->SetGUID(so1, value);
135
136   CPPUNIT_ASSERT(studyBuilder->IsGUID(so1, value));
137
138 /* Not implemented
139   //Check method UndoLimit (set/get)
140   studyBuilder->UndoLimit(10);
141   CPPUNIT_ASSERT(studyBuilder->UndoLimit() == 10);
142 */
143
144   //Check method SetName
145   studyBuilder->SetName(so1, "new name");
146   CPPUNIT_ASSERT(so1->GetName() == "new name");
147
148   //Try to set empty Name
149   studyBuilder->SetName(so1, "");
150   CPPUNIT_ASSERT(so1->GetName() == "");
151
152   //Check method SetComment
153   studyBuilder->SetComment(so1, "new comment");
154   CPPUNIT_ASSERT(so1->GetComment() == "new comment");
155
156   //Check method empty Comment
157   studyBuilder->SetComment(so1, "");
158   CPPUNIT_ASSERT(so1->GetComment() == "");
159
160   //Try to set empty IOR
161   studyBuilder->SetIOR(so1, "");
162   CPPUNIT_ASSERT(so1->GetIOR() == "");
163
164   //Check method SetIOR
165   studyBuilder->SetIOR(so1, ior);
166   CPPUNIT_ASSERT(so1->GetIOR() == ior);
167
168   sm->Close(study);
169
170   //Check method LoadWith
171   _PTR(Study) study2 = sm->NewStudy("Study2");
172   
173   SALOME_NamingService NS(_orb);
174   CORBA::Object_var obj = SALOME_LifeCycleCORBA(&NS).FindOrLoad_Component("FactoryServer", "SMESH");
175   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
176
177   MESSAGE("Created a new SMESH component");
178
179   SALOMEDS::Driver_var drv = SALOMEDS::Driver::_narrow(obj);
180   CPPUNIT_ASSERT(!CORBA::is_nil(drv));
181  
182   _PTR(StudyBuilder) sb2 = study2->NewBuilder();
183   _PTR(SComponent) sco = sb2->NewComponent("SMESH");
184   
185   ior = _orb->object_to_string(drv);
186   sb2->DefineComponentInstance(sco, ior);
187
188   sm->SaveAs("srn_SALOMEDS_UnitTests.hdf", study2, false);
189   sm->Close(study2);
190
191   _PTR(Study) study3 = sm->Open("srn_SALOMEDS_UnitTests.hdf");
192   _PTR(StudyBuilder) sb3 = study3->NewBuilder();
193   _PTR(SComponent) aComp = study3->FindComponent("SMESH");
194   CPPUNIT_ASSERT(aComp);
195
196   CORBA::Object_var obj2 = SALOME_LifeCycleCORBA(&NS).FindOrLoad_Component("FactoryServer", "SMESH");
197   CPPUNIT_ASSERT(!CORBA::is_nil(obj2));
198   SALOMEDS::Driver_var drv2 = SALOMEDS::Driver::_narrow(obj2);
199   ior = _orb->object_to_string(drv2);
200
201   isRaised = false;
202   try {
203     //getchar();
204     sb3->LoadWith(aComp, ior);
205   }
206   catch(...) {
207     isRaised = true;
208   }
209
210
211   CPPUNIT_ASSERT(!isRaised);
212
213   ior = "";
214   aComp->ComponentIOR(ior);
215   CPPUNIT_ASSERT(!ior.empty());
216
217   system("rm -f srn_SALOMEDS_UnitTests.hdf");
218
219   //Check method AddDirectory
220   _PTR(AttributeName) na1 = sb3->FindOrCreateAttribute(aComp, "AttributeName");
221   na1->SetValue("Component");
222
223   isRaised = false;
224   try {
225     sb3->AddDirectory("/Component/Dir1");
226   } catch(...) {
227     isRaised = true;
228   }
229
230
231   CPPUNIT_ASSERT(!isRaised);
232   _PTR(SObject) so5 = study3->FindObjectByPath("/Component/Dir1");
233   CPPUNIT_ASSERT(so5);
234
235   isRaised = false;
236   try {
237     sb3->AddDirectory("/Component/Dir1"); //Attempt to create the same directory
238   } catch(...) {
239     isRaised = true;
240   }
241   CPPUNIT_ASSERT(isRaised);
242
243   isRaised = false;
244   try {
245     sb3->AddDirectory("/MyComponent/Dir1"); //Attempt to create the invalid directory
246   } catch(...) {
247     isRaised = true;
248   }
249   CPPUNIT_ASSERT(isRaised);
250
251   sm->Close(study3);  
252 }