1 // Copyright (C) 2006 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 * Check all methods of SALOMEDS_StudyBuilder
23 * Use code of SALOMEDS_StudyBuilder.cxx
26 void SALOMEDSTest::testStudyBuilder()
28 //Create or find the Study manager
29 _PTR(StudyManager) sm ( new SALOMEDS_StudyManager(_sm) );
34 _PTR(Study) study = sm->NewStudy("TestStudyBuilder");
36 CPPUNIT_ASSERT(study);
38 //Create Study Builder
39 _PTR(StudyBuilder) studyBuilder = study->NewBuilder();
41 //Check the StudyBuilder creation
42 CPPUNIT_ASSERT(studyBuilder);
44 //Check method NewComponent
45 _PTR(SComponent) sco1 = studyBuilder->NewComponent("Test");
46 CPPUNIT_ASSERT(sco1 && sco1->ComponentDataType() == "Test");
48 //Check method DefineComponentInstance
49 string ior = _orb->object_to_string(_sm);
50 studyBuilder->DefineComponentInstance(sco1, ior);
52 sco1->ComponentIOR(newior);
53 CPPUNIT_ASSERT(newior == ior);
55 //Check method RemoveComponent
56 studyBuilder->RemoveComponent(sco1);
57 _PTR(SComponent) sco2 = study->FindComponent("Test");
58 CPPUNIT_ASSERT(!sco2);
60 //Try to create and find the component with empty type
61 _PTR(SComponent) sco_empty = studyBuilder->NewComponent("");
62 CPPUNIT_ASSERT(!sco_empty);
64 _PTR(SComponent) sco3 = studyBuilder->NewComponent("NewComp");
67 //Check method NewObject
68 _PTR(SObject) so1 = studyBuilder->NewObject(sco3);
70 string id1 = so1->GetID();
72 //Check method NewObjectToTag
73 _PTR(SObject) so2 = studyBuilder->NewObjectToTag(so1, 2);
74 CPPUNIT_ASSERT(so2 && so2->Tag() == 2);
75 string id2 = so2->GetID();
77 //Check method FindOrCreateAttribute
78 _PTR(SObject) so3 = studyBuilder->NewObject(sco3);
80 _PTR(AttributeName) an3 = studyBuilder->FindOrCreateAttribute(so3, "AttributeName");
83 //Try to create attribute with invalid type
84 CPPUNIT_ASSERT(!studyBuilder->FindOrCreateAttribute(so3, "invalid type"));
86 //Check method FindAttribute
87 _PTR(GenericAttribute) ga;
88 CPPUNIT_ASSERT(studyBuilder->FindAttribute(so3, ga, "AttributeName"));
90 //Try to find attribute with invalid type
91 CPPUNIT_ASSERT(!studyBuilder->FindAttribute(so3, ga, "invalid type"));
93 //Check method RemoveObject
94 studyBuilder->RemoveObject(so3);
95 CPPUNIT_ASSERT(!studyBuilder->FindAttribute(so3, ga, "AttributeName"));
97 //Check method RemoveObjectWithChildren
98 _PTR(AttributeName) an2 = studyBuilder->FindOrCreateAttribute(so2, "AttributeName");
100 studyBuilder->RemoveObjectWithChildren(so1);
101 CPPUNIT_ASSERT(!studyBuilder->FindAttribute(so2, ga, "AttributeName"));
103 //Check method RemoveAttribute
104 _PTR(AttributeName) an1 = studyBuilder->FindOrCreateAttribute(so1, "AttributeName");
106 CPPUNIT_ASSERT(studyBuilder->FindAttribute(so1, ga, "AttributeName"));
107 studyBuilder->RemoveAttribute(so1, "AttributeName");
108 CPPUNIT_ASSERT(!studyBuilder->FindAttribute(so1, ga, "AttributeName"));
110 //Check method Addreference
111 studyBuilder->Addreference(so2, so1);
113 CPPUNIT_ASSERT(so2->ReferencedObject(refSO) && refSO->GetID() == so1->GetID());
115 //Try to set reference to NULL SObject
116 bool isRaised = false;
117 _PTR(SObject) empty_so;
119 studyBuilder->Addreference(so2, empty_so);
124 CPPUNIT_ASSERT(isRaised);
126 //Check method RemoveReference
127 studyBuilder->RemoveReference(so2);
128 CPPUNIT_ASSERT(!so2->ReferencedObject(refSO));
130 //Check method SetGUID and IsGUID
131 string value = "0e1c36e6-379b-4d90-ab3b-17a14310e648";
132 studyBuilder->SetGUID(so1, value);
134 CPPUNIT_ASSERT(studyBuilder->IsGUID(so1, value));
137 //Check method UndoLimit (set/get)
138 studyBuilder->UndoLimit(10);
139 CPPUNIT_ASSERT(studyBuilder->UndoLimit() == 10);
142 //Check method SetName
143 studyBuilder->SetName(so1, "new name");
144 CPPUNIT_ASSERT(so1->GetName() == "new name");
146 //Try to set empty Name
147 studyBuilder->SetName(so1, "");
148 CPPUNIT_ASSERT(so1->GetName() == "");
150 //Check method SetComment
151 studyBuilder->SetComment(so1, "new comment");
152 CPPUNIT_ASSERT(so1->GetComment() == "new comment");
154 //Check method empty Comment
155 studyBuilder->SetComment(so1, "");
156 CPPUNIT_ASSERT(so1->GetComment() == "");
158 //Try to set empty IOR
159 studyBuilder->SetIOR(so1, "");
160 CPPUNIT_ASSERT(so1->GetIOR() == "");
162 //Check method SetIOR
163 studyBuilder->SetIOR(so1, ior);
164 CPPUNIT_ASSERT(so1->GetIOR() == ior);
168 //Check method LoadWith
169 _PTR(Study) study2 = sm->NewStudy("Study2");
171 SALOME_NamingService NS(_orb);
172 CORBA::Object_var obj = SALOME_LifeCycleCORBA(&NS).FindOrLoad_Component("FactoryServer", "SMESH");
173 CPPUNIT_ASSERT(!CORBA::is_nil(obj));
175 MESSAGE("Created a new SMESH component");
177 SALOMEDS::Driver_var drv = SALOMEDS::Driver::_narrow(obj);
178 CPPUNIT_ASSERT(!CORBA::is_nil(drv));
180 _PTR(StudyBuilder) sb2 = study2->NewBuilder();
181 _PTR(SComponent) sco = sb2->NewComponent("SMESH");
183 ior = _orb->object_to_string(drv);
184 sb2->DefineComponentInstance(sco, ior);
186 sm->SaveAs("srn_SALOMEDS_UnitTests.hdf", study2, false);
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);
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);
202 sb3->LoadWith(aComp, ior);
209 CPPUNIT_ASSERT(!isRaised);
212 aComp->ComponentIOR(ior);
213 CPPUNIT_ASSERT(!ior.empty());
215 system("rm -f srn_SALOMEDS_UnitTests.hdf");
217 //Check method AddDirectory
218 _PTR(AttributeName) na1 = sb3->FindOrCreateAttribute(aComp, "AttributeName");
219 na1->SetValue("Component");
223 sb3->AddDirectory("/Component/Dir1");
229 CPPUNIT_ASSERT(!isRaised);
230 _PTR(SObject) so5 = study3->FindObjectByPath("/Component/Dir1");
235 sb3->AddDirectory("/Component/Dir1"); //Attempt to create the same directory
239 CPPUNIT_ASSERT(isRaised);
243 sb3->AddDirectory("/MyComponent/Dir1"); //Attempt to create the invalid directory
247 CPPUNIT_ASSERT(isRaised);