Back to index

salome-kernel  6.5.0
SALOMEDSTest_Study.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::testStudy()
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("Test");
00037 
00038   //Check the creation of the study
00039   CPPUNIT_ASSERT(study);
00040 
00041   //Check method NewBuilder
00042   _PTR(StudyBuilder) studyBuilder = study->NewBuilder();
00043   CPPUNIT_ASSERT(studyBuilder);
00044 
00045   //Check method NewComponentIterator
00046   _PTR(SComponentIterator) componentIterator = study->NewComponentIterator();
00047 
00048   CPPUNIT_ASSERT(componentIterator);
00049 
00050   //Check method GetTransientReference
00051   CPPUNIT_ASSERT(!study->GetTransientReference().empty());
00052 
00053   //Check method StudyId
00054   CPPUNIT_ASSERT(study->StudyId() > 0);
00055 
00056   //Check method Name (get/set)
00057   CPPUNIT_ASSERT(study->Name() == "Test");
00058   study->Name("New name");
00059   CPPUNIT_ASSERT(study->Name() == "New name");
00060   study->Name("Test");
00061 
00062   //Check method URL (get/set)
00063   study->URL("");
00064   CPPUNIT_ASSERT(study->URL() == "");
00065   study->URL("some url");
00066   CPPUNIT_ASSERT(study->URL() == "some url");
00067 
00068   //Check method GetPersistentReference == URL
00069   CPPUNIT_ASSERT(study->GetPersistentReference() == "some url");
00070 
00071   //Check method IsSaved (get/set)
00072   study->IsSaved(true);
00073   CPPUNIT_ASSERT(study->IsSaved());
00074   study->IsSaved(false);
00075   CPPUNIT_ASSERT(!study->IsSaved());
00076 
00077   //Check method CreateObjectID
00078   _PTR(SObject) so = study->CreateObjectID("0:2:1:3");
00079   CPPUNIT_ASSERT(so);
00080 
00081   //Try to create SObject with empty and invalid entries
00082   CPPUNIT_ASSERT(!study->CreateObjectID(""));
00083   CPPUNIT_ASSERT(!study->CreateObjectID("entry"));
00084 
00085   //Check method NewChildIterator
00086   _PTR(ChildIterator) childIterator = study->NewChildIterator(so);
00087   CPPUNIT_ASSERT(childIterator);
00088 
00089   //Check method IsEmpty
00090   CPPUNIT_ASSERT(!study->IsEmpty());
00091 
00092   //Check method FindComponent
00093   _PTR(SComponent) sco1 = studyBuilder->NewComponent("test1");
00094   _PTR(SComponent) sco2 = study->FindComponent("test1");
00095   CPPUNIT_ASSERT(sco1 && sco2);
00096   CPPUNIT_ASSERT(sco1->GetID() == sco2->GetID());
00097   _PTR(AttributeName) name_attr_sco1 = studyBuilder->FindOrCreateAttribute(sco1, "AttributeName");
00098   CPPUNIT_ASSERT(name_attr_sco1);
00099   name_attr_sco1->SetValue("sco1");
00100 
00101   //Try to find component with empty type
00102   CPPUNIT_ASSERT(!study->FindComponent(""));
00103 
00104   //Check method GetComponentNames
00105   std::vector<std::string> components = study->GetComponentNames(""); //The context doesn't matter
00106   CPPUNIT_ASSERT(components.size() == 1 && components[0] == "sco1");
00107 
00108   //Check method FindComponentID
00109   _PTR(SComponent) sco3 = study->FindComponentID(sco1->GetID());
00110   CPPUNIT_ASSERT(sco3 && sco3->GetID() == sco1->GetID());
00111 
00112   //Try to find component with empty id
00113   CPPUNIT_ASSERT(!study->FindComponentID(""));
00114 
00115   _PTR(SObject) so1 = studyBuilder->NewObject(sco1);
00116   CPPUNIT_ASSERT(so1);
00117   _PTR(AttributeName) name_attr_so1 = studyBuilder->FindOrCreateAttribute(so1, "AttributeName");
00118   CPPUNIT_ASSERT(name_attr_so1);
00119   name_attr_so1->SetValue("so1");
00120   
00121    //Create an attribute AttributeIOR
00122   _PTR(AttributeIOR) ior_attr_so1 = studyBuilder->FindOrCreateAttribute(so1, "AttributeIOR");
00123   CPPUNIT_ASSERT(ior_attr_so1);
00124 
00125   std::string ior = _orb->object_to_string(_sm);
00126   ior_attr_so1->SetValue(ior);
00127   
00128   _PTR(SObject) so2 = studyBuilder->NewObject(so1);
00129   CPPUNIT_ASSERT(so2);
00130   _PTR(AttributeName) name_attr_so2 = studyBuilder->FindOrCreateAttribute(so2, "AttributeName");
00131   CPPUNIT_ASSERT(name_attr_so2);
00132   name_attr_so2->SetValue("so2");
00133 
00134   //Check method FindObject
00135   _PTR(SObject) so3 = study->FindObject("so1");
00136   CPPUNIT_ASSERT(so3 && so3->GetID() == so1->GetID());
00137 
00138   //Try to find SObject with empty name
00139   CPPUNIT_ASSERT(!study->FindObject(""));
00140 
00141   //Check method FindObjectID
00142   _PTR(SObject) so4 = study->FindObjectID(so1->GetID());
00143   CPPUNIT_ASSERT(so4 && so4->GetID() == so1->GetID());
00144 
00145   //Try to find SObject with empty ID
00146   CPPUNIT_ASSERT(!study->FindObjectID(""));
00147 
00148   //Check method FindObjectByName
00149   std::vector< _PTR(SObject) > v = study->FindObjectByName("so1", sco1->ComponentDataType());
00150   CPPUNIT_ASSERT(v.size()==1 && v[0]->GetID() == so1->GetID());
00151 
00152   //Try to find SObject with empty name and empty component type
00153   CPPUNIT_ASSERT((study->FindObjectByName("", "")).size() == 0);
00154 
00155   //Check method FindObjectByPath
00156   _PTR(SObject) path_so = study->FindObjectByPath("/"+sco1->GetName()+"/"+so1->GetName());
00157   CPPUNIT_ASSERT(path_so && path_so->GetID() == so1->GetID());
00158 
00159   //Check method FindObjectIOR
00160   _PTR(SObject) so5 = study->FindObjectIOR(ior);
00161   CPPUNIT_ASSERT(so5 && so5->GetID() == so1->GetID());
00162 
00163   //Try to find SObject with empty IOR
00164   CPPUNIT_ASSERT(!study->FindObjectIOR(""));
00165 
00166   //Check method GetObjectPath
00167   std::string path = study->GetObjectPath(so2);
00168 
00169   //Try to get path of NULL SObject
00170   _PTR(SObject) emptySO;
00171   path = study->GetObjectPath(emptySO);
00172   CPPUNIT_ASSERT(path.empty());
00173 
00174   //Check method SetContext
00175   study->SetContext("/sco1"); 
00176   CPPUNIT_ASSERT(study->GetContext() == "/sco1");
00177 
00178   //Check method FindObjectByPath
00179   _PTR(SObject) so6 = study->FindObjectByPath("so1");
00180   CPPUNIT_ASSERT(so6 && so6->GetID() == so1->GetID());
00181 
00182   
00183   //Try to find SObject with empty path
00184   _PTR(SObject) tmp = study->FindObjectByPath(""); //Must return the Context SObject
00185   CPPUNIT_ASSERT(tmp && tmp->GetID() == sco1->GetID());
00186 
00187   study->SetContext("/"); //Root
00188 
00189   //Check method GetObjectNames
00190   std::vector<std::string> vs = study->GetObjectNames("/sco1");  
00191   CPPUNIT_ASSERT(vs.size() == 2);
00192     
00193   //Check method GetDirectoryNames
00194   _PTR(AttributeLocalID) locid_attr_sco1 = studyBuilder->FindOrCreateAttribute(sco1, "AttributeLocalID");
00195   CPPUNIT_ASSERT(locid_attr_sco1);
00196   locid_attr_sco1->SetValue(16661); //DIRECTORYID
00197   _PTR(AttributeLocalID) locid_attr_so1 = studyBuilder->FindOrCreateAttribute(so1, "AttributeLocalID");
00198   CPPUNIT_ASSERT(locid_attr_so1);
00199   locid_attr_so1->SetValue(16661); //DIRECTORYID
00200   vs = study->GetDirectoryNames(""); //Empty context (the current is taken)
00201   CPPUNIT_ASSERT(vs.size() == 2);
00202 
00203   //Check method GetFileNames
00204   locid_attr_sco1->SetValue(26662); //FILELOCALID
00205   _PTR(AttributePersistentRef) persref_attr_sco1 = studyBuilder->FindOrCreateAttribute(sco1, "AttributePersistentRef");
00206   CPPUNIT_ASSERT(persref_attr_sco1);
00207   persref_attr_sco1->SetValue("FILE: filename1");
00208   locid_attr_so1->SetValue(26662); //FILELOCALID
00209   _PTR(AttributePersistentRef) persref_attr_so1 = studyBuilder->FindOrCreateAttribute(so1, "AttributePersistentRef");
00210   CPPUNIT_ASSERT(persref_attr_so1);
00211   persref_attr_so1->SetValue("FILE: filename2");
00212   vs = study->GetFileNames("");
00213   CPPUNIT_ASSERT(vs.size() == 2 && vs[0] == "filename1" && vs[1] == "filename2");
00214 
00215   //Check method StudyId (get/set)
00216   int id = study->StudyId();
00217   study->StudyId(-1);
00218   CPPUNIT_ASSERT(study->StudyId() == -1);
00219   study->StudyId(id);
00220 
00221   //Check method FindDependances
00222   studyBuilder->Addreference(so2, so1);
00223   studyBuilder->Addreference(sco1, so1);
00224   std::vector< _PTR(SObject) > vso = study->FindDependances(so1);
00225   CPPUNIT_ASSERT(vso.size() == 2 && vso[0]->GetID() == so2->GetID() && vso[1]->GetID() == sco1->GetID());
00226 
00227   //Check method GetProperties
00228   _PTR(AttributeStudyProperties) sp = study->GetProperties();
00229   CPPUNIT_ASSERT(sp);
00230 
00231   //Check Lock functionality
00232   sp->SetLocked(true);
00233   bool isLockRaised = false;
00234   try {
00235      name_attr_so1->SetValue("test");      
00236   }
00237   catch(...) {
00238     isLockRaised = true;
00239   }
00240   CPPUNIT_ASSERT(isLockRaised);
00241  
00242   sp->SetLocked(false);
00243   isLockRaised = false;
00244   try {
00245      name_attr_so1->SetValue("test");      
00246   }
00247   catch(...) {
00248     isLockRaised = true;
00249   }
00250   CPPUNIT_ASSERT(!isLockRaised);
00251 
00252   //Check method GetLastModificationDate
00253   sp->SetModification("srn", 1, 2, 3, 4, 5);  
00254   sp->SetModification("srn", 6, 7, 8, 9, 10);
00255   std::string date = study->GetLastModificationDate();  
00256 
00257   CPPUNIT_ASSERT(date == "08/09/0010 07:06");
00258 
00259   //Check method GetModificationsDate
00260   vs = study->GetModificationsDate();
00261   CPPUNIT_ASSERT(vs.size() == 2 && vs[0] == "03/04/0005 02:01" && vs[1] == "08/09/0010 07:06");
00262 
00263   //Check method GetCommonParameters
00264   _PTR(AttributeParameter) cp = study->GetCommonParameters("id", 1); //Save point = 1
00265   CPPUNIT_ASSERT(cp);
00266   cp->SetBool("test_true", true);
00267   cp->SetBool("test_false", false);
00268   _PTR(AttributeParameter) cp2 = study->GetCommonParameters("id", 1); //Save point = 1
00269   CPPUNIT_ASSERT(cp2);
00270   CPPUNIT_ASSERT(cp2->GetBool("test_true") && !cp2->GetBool("test_false"));
00271   _PTR(AttributeParameter) cp21 = study->GetCommonParameters("id", 2); //Save point = 2
00272   CPPUNIT_ASSERT(cp21);
00273   CPPUNIT_ASSERT(!cp21->IsSet("test_true", PT_BOOLEAN) && !cp21->IsSet("test_false", PT_BOOLEAN));
00274 
00275   //Check method GetModuleParameters
00276   _PTR(AttributeParameter) cp3 = study->GetModuleParameters("id", "module name", 1); //Save point = 1, name = "module name"
00277   CPPUNIT_ASSERT(cp3);
00278   cp3->SetBool("test_true", true);
00279   cp3->SetBool("test_false", false);
00280   _PTR(AttributeParameter) cp4 = study->GetModuleParameters("id", "module name", 1); //Save point = 1, name = "module name"
00281   CPPUNIT_ASSERT(cp4);
00282   CPPUNIT_ASSERT(cp4->GetBool("test_true") && !cp4->GetBool("test_false"));
00283   _PTR(AttributeParameter) cp5 = study->GetModuleParameters("id", "module name 2", 1); //Save point = 1, name = "module name 2"
00284   CPPUNIT_ASSERT(cp5);
00285   CPPUNIT_ASSERT(!cp5->IsSet("test_true", PT_BOOLEAN) && !cp5->IsSet("test_false", PT_BOOLEAN));
00286   _PTR(AttributeParameter) cp6 = study->GetModuleParameters("id", "module name", 2); //Save point = 2, name = "module name"
00287   CPPUNIT_ASSERT(cp6);
00288   CPPUNIT_ASSERT(!cp6->IsSet("test_true", PT_BOOLEAN) && !cp6->IsSet("test_false", PT_BOOLEAN));
00289 
00290   //Check method GetUseCaseBuilder
00291   _PTR(UseCaseBuilder) ub = study->GetUseCaseBuilder();
00292   CPPUNIT_ASSERT(ub);
00293 
00294   //Check method SetStudyLock
00295   study->SetStudyLock("locker1");
00296 
00297   //Check method IsStudyLocked
00298   CPPUNIT_ASSERT(study->IsStudyLocked());
00299 
00300   //Check method GetLockerID
00301   study->SetStudyLock("locker2");
00302   vs = study->GetLockerID();
00303   CPPUNIT_ASSERT(vs.size() == 2 && vs[0] == "locker1" && vs[1] == "locker2");
00304 
00305   //Check method UnLockStudy
00306   study->UnLockStudy("locker1");
00307   CPPUNIT_ASSERT(study->IsStudyLocked());
00308   vs = study->GetLockerID();
00309   CPPUNIT_ASSERT(vs.size() == 1 && vs[0] == "locker2");
00310   study->UnLockStudy("locker2");
00311   CPPUNIT_ASSERT(!study->IsStudyLocked());
00312   vs = study->GetLockerID();
00313   CPPUNIT_ASSERT(vs.size() == 0);
00314 
00315   //Check method EnableUseCaseAutoFilling
00316   study->EnableUseCaseAutoFilling(false);
00317   _PTR(SObject) uso1 = study->NewBuilder()->NewObject(sco1);
00318   std::vector< _PTR(GenericAttribute) > va1 = uso1->GetAllAttributes();
00319   CPPUNIT_ASSERT(va1.size() == 0); 
00320 
00321   study->EnableUseCaseAutoFilling(true);
00322   _PTR(SObject) uso2 = study->NewBuilder()->NewObject(sco1);
00323   std::vector< _PTR(GenericAttribute) > va2 = uso2->GetAllAttributes();
00324   CPPUNIT_ASSERT(va2.size() == 1); // +AttributeTreeNode
00325 
00326   //Check method DumpStudy
00327   study->DumpStudy(".", "SRN", false, false);
00328 
00329   std::fstream f("SRN.py");
00330   char buffer[128];
00331   buffer[81] = (char)0;
00332   f.getline(buffer, 80);
00333   std::string line(buffer);
00334  
00335   f.close();
00336   system("rm -f SRN.py");
00337   CPPUNIT_ASSERT(line == "### This file is generated by SALOME automatically by dump python functionality");
00338 
00339   //Check method Close 
00340   bool isException = false;
00341   try {
00342     sm->Close(study);  //Close is called inside StudyManager::Close
00343   }
00344   catch(...) {
00345     isException = true;
00346   }
00347   CPPUNIT_ASSERT(!isException);
00348 }
00349 
00350 
00351