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