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