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