Back to index

salome-kernel  6.5.0
SALOMEDSTest_StudyBuilder.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00028 void SALOMEDSTest::testStudyBuilder()
00029 {
00030   //Create or find the Study manager
00031   _PTR(StudyManager) sm ( new SALOMEDS_StudyManager(_sm) );
00032 
00033   CPPUNIT_ASSERT(sm);
00034 
00035   //Create a new study
00036   _PTR(Study) study = sm->NewStudy("TestStudyBuilder");
00037 
00038   CPPUNIT_ASSERT(study);
00039 
00040   //Create Study Builder
00041   _PTR(StudyBuilder) studyBuilder = study->NewBuilder();
00042 
00043   //Check the StudyBuilder creation
00044   CPPUNIT_ASSERT(studyBuilder);
00045 
00046   //Check method NewComponent
00047   _PTR(SComponent) sco1 = studyBuilder->NewComponent("Test");
00048   CPPUNIT_ASSERT(sco1 && sco1->ComponentDataType() == "Test");
00049 
00050   //Check method DefineComponentInstance
00051   std::string ior = _orb->object_to_string(_sm);
00052   studyBuilder->DefineComponentInstance(sco1, ior);
00053   std::string newior;
00054   sco1->ComponentIOR(newior);
00055   CPPUNIT_ASSERT(newior == ior);
00056 
00057   //Check method RemoveComponent
00058   studyBuilder->RemoveComponent(sco1);
00059   _PTR(SComponent) sco2 = study->FindComponent("Test");
00060   CPPUNIT_ASSERT(!sco2);
00061 
00062   //Try to create and find the component with empty type
00063   _PTR(SComponent) sco_empty = studyBuilder->NewComponent(""); 
00064   CPPUNIT_ASSERT(!sco_empty);
00065 
00066   _PTR(SComponent) sco3 = studyBuilder->NewComponent("NewComp");
00067   CPPUNIT_ASSERT(sco3);
00068 
00069   //Check method NewObject
00070   _PTR(SObject) so1 = studyBuilder->NewObject(sco3);
00071   CPPUNIT_ASSERT(so1);
00072   std::string id1 = so1->GetID();
00073 
00074   //Check method NewObjectToTag
00075   _PTR(SObject) so2 = studyBuilder->NewObjectToTag(so1, 2);
00076   CPPUNIT_ASSERT(so2 && so2->Tag() == 2);
00077   std::string id2 = so2->GetID();
00078 
00079   //Check method FindOrCreateAttribute
00080   _PTR(SObject) so3 = studyBuilder->NewObject(sco3);
00081   CPPUNIT_ASSERT(so3);
00082   _PTR(AttributeName) an3 = studyBuilder->FindOrCreateAttribute(so3, "AttributeName");
00083   CPPUNIT_ASSERT(an3);
00084 
00085   //Try to create attribute with invalid type
00086   CPPUNIT_ASSERT(!studyBuilder->FindOrCreateAttribute(so3, "invalid type"));
00087 
00088   //Check method FindAttribute
00089   _PTR(GenericAttribute) ga;
00090   CPPUNIT_ASSERT(studyBuilder->FindAttribute(so3, ga, "AttributeName"));
00091 
00092   //Try to find attribute with invalid type
00093   CPPUNIT_ASSERT(!studyBuilder->FindAttribute(so3, ga, "invalid type"));
00094 
00095   //Check method RemoveObject
00096   studyBuilder->RemoveObject(so3);
00097   CPPUNIT_ASSERT(!studyBuilder->FindAttribute(so3, ga, "AttributeName"));
00098 
00099   //Check method RemoveObjectWithChildren
00100   _PTR(AttributeName) an2 = studyBuilder->FindOrCreateAttribute(so2, "AttributeName");
00101   CPPUNIT_ASSERT(an2);
00102   studyBuilder->RemoveObjectWithChildren(so1);
00103   CPPUNIT_ASSERT(!studyBuilder->FindAttribute(so2, ga, "AttributeName"));
00104 
00105   //Check method RemoveAttribute
00106   _PTR(AttributeName) an1 = studyBuilder->FindOrCreateAttribute(so1, "AttributeName");
00107   CPPUNIT_ASSERT(an1);
00108   CPPUNIT_ASSERT(studyBuilder->FindAttribute(so1, ga, "AttributeName"));
00109   studyBuilder->RemoveAttribute(so1, "AttributeName");
00110   CPPUNIT_ASSERT(!studyBuilder->FindAttribute(so1, ga, "AttributeName"));
00111 
00112   //Check method Addreference
00113   studyBuilder->Addreference(so2, so1);
00114   _PTR(SObject) refSO;
00115   CPPUNIT_ASSERT(so2->ReferencedObject(refSO) && refSO->GetID() == so1->GetID());
00116 
00117   //Try to set reference to NULL SObject
00118   bool isRaised = false;
00119   _PTR(SObject) empty_so;
00120   try {
00121     studyBuilder->Addreference(so2, empty_so);
00122   }
00123   catch(...) {
00124     isRaised = true;
00125   }
00126   CPPUNIT_ASSERT(isRaised);
00127 
00128   //Check method RemoveReference
00129   studyBuilder->RemoveReference(so2);
00130   CPPUNIT_ASSERT(!so2->ReferencedObject(refSO));
00131 
00132   //Check method SetGUID and IsGUID
00133   std::string value = "0e1c36e6-379b-4d90-ab3b-17a14310e648";
00134   studyBuilder->SetGUID(so1, value);
00135 
00136   CPPUNIT_ASSERT(studyBuilder->IsGUID(so1, value));
00137 
00138 /* Not implemented
00139   //Check method UndoLimit (set/get)
00140   studyBuilder->UndoLimit(10);
00141   CPPUNIT_ASSERT(studyBuilder->UndoLimit() == 10);
00142 */
00143 
00144   //Check method SetName
00145   studyBuilder->SetName(so1, "new name");
00146   CPPUNIT_ASSERT(so1->GetName() == "new name");
00147 
00148   //Try to set empty Name
00149   studyBuilder->SetName(so1, "");
00150   CPPUNIT_ASSERT(so1->GetName() == "");
00151 
00152   //Check method SetComment
00153   studyBuilder->SetComment(so1, "new comment");
00154   CPPUNIT_ASSERT(so1->GetComment() == "new comment");
00155 
00156   //Check method empty Comment
00157   studyBuilder->SetComment(so1, "");
00158   CPPUNIT_ASSERT(so1->GetComment() == "");
00159 
00160   //Try to set empty IOR
00161   studyBuilder->SetIOR(so1, "");
00162   CPPUNIT_ASSERT(so1->GetIOR() == "");
00163 
00164   //Check method SetIOR
00165   studyBuilder->SetIOR(so1, ior);
00166   CPPUNIT_ASSERT(so1->GetIOR() == ior);
00167 
00168   sm->Close(study);
00169 
00170   //Check method LoadWith
00171   _PTR(Study) study2 = sm->NewStudy("Study2");
00172   
00173   SALOME_NamingService NS(_orb);
00174   CORBA::Object_var obj = SALOME_LifeCycleCORBA(&NS).FindOrLoad_Component("FactoryServer", "SMESH");
00175   CPPUNIT_ASSERT(!CORBA::is_nil(obj));
00176 
00177   MESSAGE("Created a new SMESH component");
00178 
00179   SALOMEDS::Driver_var drv = SALOMEDS::Driver::_narrow(obj);
00180   CPPUNIT_ASSERT(!CORBA::is_nil(drv));
00181  
00182   _PTR(StudyBuilder) sb2 = study2->NewBuilder();
00183   _PTR(SComponent) sco = sb2->NewComponent("SMESH");
00184   
00185   ior = _orb->object_to_string(drv);
00186   sb2->DefineComponentInstance(sco, ior);
00187 
00188   sm->SaveAs("srn_SALOMEDS_UnitTests.hdf", study2, false);
00189   sm->Close(study2);
00190 
00191   _PTR(Study) study3 = sm->Open("srn_SALOMEDS_UnitTests.hdf");
00192   _PTR(StudyBuilder) sb3 = study3->NewBuilder();
00193   _PTR(SComponent) aComp = study3->FindComponent("SMESH");
00194   CPPUNIT_ASSERT(aComp);
00195 
00196   CORBA::Object_var obj2 = SALOME_LifeCycleCORBA(&NS).FindOrLoad_Component("FactoryServer", "SMESH");
00197   CPPUNIT_ASSERT(!CORBA::is_nil(obj2));
00198   SALOMEDS::Driver_var drv2 = SALOMEDS::Driver::_narrow(obj2);
00199   ior = _orb->object_to_string(drv2);
00200 
00201   isRaised = false;
00202   try {
00203     //getchar();
00204     sb3->LoadWith(aComp, ior);
00205   }
00206   catch(...) {
00207     isRaised = true;
00208   }
00209 
00210 
00211   CPPUNIT_ASSERT(!isRaised);
00212 
00213   ior = "";
00214   aComp->ComponentIOR(ior);
00215   CPPUNIT_ASSERT(!ior.empty());
00216 
00217   system("rm -f srn_SALOMEDS_UnitTests.hdf");
00218 
00219   //Check method AddDirectory
00220   _PTR(AttributeName) na1 = sb3->FindOrCreateAttribute(aComp, "AttributeName");
00221   na1->SetValue("Component");
00222 
00223   isRaised = false;
00224   try {
00225     sb3->AddDirectory("/Component/Dir1");
00226   } catch(...) {
00227     isRaised = true;
00228   }
00229 
00230 
00231   CPPUNIT_ASSERT(!isRaised);
00232   _PTR(SObject) so5 = study3->FindObjectByPath("/Component/Dir1");
00233   CPPUNIT_ASSERT(so5);
00234 
00235   isRaised = false;
00236   try {
00237     sb3->AddDirectory("/Component/Dir1"); //Attempt to create the same directory
00238   } catch(...) {
00239     isRaised = true;
00240   }
00241   CPPUNIT_ASSERT(isRaised);
00242 
00243   isRaised = false;
00244   try {
00245     sb3->AddDirectory("/MyComponent/Dir1"); //Attempt to create the invalid directory
00246   } catch(...) {
00247     isRaised = true;
00248   }
00249   CPPUNIT_ASSERT(isRaised);
00250 
00251   sm->Close(study3);  
00252 }