diff --git a/Core/Code/Interfaces/mitkIPersistenceService.h b/Core/Code/Interfaces/mitkIPersistenceService.h index 8abd88010d..568cefc235 100644 --- a/Core/Code/Interfaces/mitkIPersistenceService.h +++ b/Core/Code/Interfaces/mitkIPersistenceService.h @@ -1,344 +1,339 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef mitkIPersistenceService_h #define mitkIPersistenceService_h // mitk #include "mitkPropertyListReplacedObserver.h" #include "usServiceReference.h" #include "usModuleContext.h" #include "usGetModuleContext.h" //for microservices #include #include "mitkDataStorage.h" namespace mitk { - /// - /// The central service for the persistence module - /// Basic idea is to create PropertyLists with a unique id using AddPropertyList(). A consumer - /// of this interface can write arbitrary information into this propertylist - /// Calling Save() and Load() will cause the Service to save and load the current set of propertlists from - /// a file in the user directory. - /// Using SetAutoLoadAndSave(true) will cause the service to load/save the property lists at application - /// start/stop. - /// Moreover, depending on the backend type, the service is connected to the SceneSerialization module, i.e. - /// the user will be asked whether to save/load the propertlists in/from the current ".mitk" file that is selected - /// by the user. - /// + /** + * The central service for the persistence module + * Basic idea is to create PropertyLists with a unique id using AddPropertyList(). A consumer + * of this interface can write arbitrary information into this propertylist + * Calling Save() and Load() will cause the Service to save and load the current set of propertlists from + * a file in the user directory. + * Using SetAutoLoadAndSave(true) will cause the service to load/save the property lists at application + * start/stop. + * Moreover, depending on the backend type, the service is connected to the SceneSerialization module, i.e. + * the user will be asked whether to save/load the propertlists in/from the current ".mitk" file that is selected + * by the user. + */ class MITK_CORE_EXPORT IPersistenceService { public: - /// - /// If PropertyList with the given id exists, returns it. Otherwise creates a new one and returns it. - /// If id is empty a UUID will be created and set on the variable - /// If existed was passed, it is true if the PropertyList with that id existed, false otherwise - /// \return a valid PropertyList with a StringProperty "Id" containing the passed id - /// + /** + * If PropertyList with the given id exists, returns it. Otherwise creates a new one and returns it. + * If id is empty a UUID will be created and set on the variable + * If existed was passed, it is true if the PropertyList with that id existed, false otherwise + * \return a valid PropertyList with a StringProperty "Id" containing the passed id + */ virtual mitk::PropertyList::Pointer GetPropertyList( std::string& id, bool* existed=0 ) = 0; - /// - /// removes the PropertyList with the given id - /// \return true if PropertyList existed and could be removed, false otherwise - /// + /** + * removes the PropertyList with the given id + * \return true if PropertyList existed and could be removed, false otherwise + */ virtual bool RemovePropertyList( std::string& id ) = 0; - /// - /// Get the default name of the PersistenceFile (the one that is loaded at startup) - /// + /** + * Get the default name of the PersistenceFile (the one that is loaded at startup) + */ virtual std::string GetDefaultPersistenceFile() const = 0; - /// - /// \return The name of the Bool Property that specifies whether a DataNode is a Node carrying Persistence PropertyLists - /// + /** + * \return The name of the Bool Property that specifies whether a DataNode is a Node carrying Persistence PropertyLists + */ virtual std::string GetPersistenceNodePropertyName() const = 0; - /// - /// Creates a vector of DataNodes that contain all PropertyLists. Additionally, the DataNodes - /// will have the property name set to the PropertyList's id and a BoolProperty equal to GetPersistenceNodePropertyName() set to true. If ds is set the returned DataNodes will also be added to that DS. - /// \return vector of DataNodes with the described attributes - /// + /** + * Creates a vector of DataNodes that contain all PropertyLists. Additionally, the DataNodes + * will have the property name set to the PropertyList's id and a BoolProperty equal to GetPersistenceNodePropertyName() set to true. If ds is set the returned DataNodes will also be added to that DS. + * \return vector of DataNodes with the described attributes + */ virtual DataStorage::SetOfObjects::Pointer GetDataNodes(DataStorage* ds=0) const = 0; - /// - /// Searches storage for persistent DataNodes, extracts and inserts the appended property lists to this service - /// \return true if at least one node was found from which a PropertyList could be restored - /// + /** + * Searches storage for persistent DataNodes, extracts and inserts the appended property lists to this service + * \return true if at least one node was found from which a PropertyList could be restored + */ virtual bool RestorePropertyListsFromPersistentDataNodes(const DataStorage* storage) = 0; - /// - /// Save the current PropertyLists to fileName. If fileName is empty, a special file in the users home directory will be used. - /// if appendchanges is true, the file will not replaced but first loaded, then overwritten and then replaced - /// \return false if an error occured (cannot write to file), true otherwise - /// + /** + * Save the current PropertyLists to fileName. If fileName is empty, a special file in the users home directory will be used. + * if appendchanges is true, the file will not replaced but first loaded, then overwritten and then replaced + * \return false if an error occured (cannot write to file), true otherwise + */ virtual bool Save(const std::string& fileName="", bool appendChanges=false) = 0; - /// - /// Load PropertyLists from fileName. If fileName is empty, a special file in the users home directory will be used. - /// If enforeReload is false, the service will take care of modified time flags, i.e. it will not load a file - /// that was loaded before and did not change in the meantime or that was modified by the service itself - /// *ATTENTION*: If there are PropertyLists with the same id contained in the file, existing PropertyLists will be overwritten! - /// \see AddPropertyListReplacedObserver() - /// \return false if an error occured (cannot load from file), true otherwise - /// + /** + * Load PropertyLists from fileName. If fileName is empty, a special file in the users home directory will be used. + * If enforeReload is false, the service will take care of modified time flags, i.e. it will not load a file + * that was loaded before and did not change in the meantime or that was modified by the service itself + * *ATTENTION*: If there are PropertyLists with the same id contained in the file, existing PropertyLists will be overwritten! + * \see AddPropertyListReplacedObserver() + * \return false if an error occured (cannot load from file), true otherwise + */ virtual bool Load(const std::string& fileName="", bool enforeReload=true) = 0; - /// - /// Using SetAutoLoadAndSave(true) will cause the service to load/save the property lists at application - /// start/stop. - /// + /** + * Using SetAutoLoadAndSave(true) will cause the service to load/save the property lists at application + * start/stop. + */ virtual void SetAutoLoadAndSave(bool autoLoadAndSave) = 0; - /// - /// \return whether AutoLoading is activated or not - /// + /** + * \return whether AutoLoading is activated or not + */ virtual bool GetAutoLoadAndSave() const = 0; - /// - /// adds a observer which is informed if a propertyList gets replaced during a Load() procedure - /// + /** + * adds a observer which is informed if a propertyList gets replaced during a Load() procedure + */ virtual void AddPropertyListReplacedObserver( PropertyListReplacedObserver* observer ) = 0; - /// - /// removes a specific observer - /// + /** + * removes a specific observer + */ virtual void RemovePropertyListReplacedObserver( PropertyListReplacedObserver* observer ) = 0; - /// - /// nothing to do here - /// + /** + * nothing to do here + */ virtual ~IPersistenceService(); }; } -/// MACROS FOR AUTOMATIC SAVE FUNCTION +// MACROS FOR AUTOMATIC SAVE FUNCTION #define PERSISTENCE_GET_MODULE_CONTEXT_FUNCTION\ us::GetModuleContext() #define PERSISTENCE_GET_SERVICE_MACRO\ mitk::IPersistenceService* persistenceService = 0;\ us::ModuleContext* context = PERSISTENCE_GET_MODULE_CONTEXT_FUNCTION;\ if( context )\ {\ us::ServiceReference persistenceServiceRef = context->GetServiceReference();\ if( persistenceServiceRef )\ {\ persistenceService\ = dynamic_cast ( context->GetService(persistenceServiceRef) );\ }\ } #define PERSISTENCE_GET_SERVICE_METHOD_MACRO\ mitk::IPersistenceService* GetPeristenceService() const\ {\ - static mitk::IPersistenceService* staticPersistenceService = 0;\ - if( staticPersistenceService == 0 )\ - {\ - PERSISTENCE_GET_SERVICE_MACRO\ - staticPersistenceService = persistenceService;\ - }\ - return staticPersistenceService;\ + PERSISTENCE_GET_SERVICE_MACRO\ + return persistenceService;\ } #define PERSISTENCE_MACRO_START_PART(ID_MEMBER_NAME)\ public:\ bool Save(const std::string& fileName="", bool appendChanges=false)\ {\ mitk::IPersistenceService* persistenceService = this->GetPeristenceService();\ bool noError = persistenceService != 0;\ if( noError )\ this->ToPropertyList();\ if(noError)\ noError = persistenceService->Save(fileName, appendChanges);\ return noError;\ }\ bool Load(const std::string& fileName="", bool enforeReload=true)\ {\ mitk::IPersistenceService* persistenceService = this->GetPeristenceService();\ bool noError = persistenceService != 0 && persistenceService->Load(fileName, enforeReload);\ if( noError )\ {\ this->FromPropertyList();\ }\ return noError;\ }\ void ToPropertyList()\ {\ mitk::IPersistenceService* persistenceService = this->GetPeristenceService();\ this->InitializePropertyListReplacedObserver(persistenceService);\ if( !persistenceService )\ return;\ mitk::PropertyList::Pointer propList = persistenceService->GetPropertyList(ID_MEMBER_NAME); #define PERSISTENCE_MACRO_MIDDLE_PART(ID_MEMBER_NAME)\ }\ void FromPropertyList()\ {\ mitk::IPersistenceService* persistenceService = this->GetPeristenceService();\ this->InitializePropertyListReplacedObserver(persistenceService);\ if( !persistenceService )\ return;\ mitk::PropertyList::Pointer propList = persistenceService->GetPropertyList(ID_MEMBER_NAME); #define PERSISTENCE_MACRO_END_PART(THE_CLASS_NAME, ID_MEMBER_NAME)\ }\ std::string GetId() const\ {\ return ID_MEMBER_NAME;\ }\ private:\ PERSISTENCE_GET_SERVICE_METHOD_MACRO\ struct MyPropertyListReplacedObserver: public mitk::PropertyListReplacedObserver\ {\ MyPropertyListReplacedObserver()\ : m_##THE_CLASS_NAME(0), m_PersistenceService(0)\ {\ }\ ~MyPropertyListReplacedObserver()\ {\ if( m_PersistenceService )\ m_PersistenceService->RemovePropertyListReplacedObserver(this);\ }\ void AfterPropertyListReplaced( const std::string& id, mitk::PropertyList* propertyList )\ {\ if( m_##THE_CLASS_NAME && m_##THE_CLASS_NAME->GetId() == id )\ m_##THE_CLASS_NAME->FromPropertyList();\ }\ void Initialize( THE_CLASS_NAME * _##THE_CLASS_NAME, mitk::IPersistenceService* persistenceService )\ {\ m_##THE_CLASS_NAME = _##THE_CLASS_NAME;\ m_PersistenceService = persistenceService;\ if( m_PersistenceService )\ m_PersistenceService->AddPropertyListReplacedObserver(this);\ }\ private:\ THE_CLASS_NAME * m_##THE_CLASS_NAME;\ mitk::IPersistenceService* m_PersistenceService;\ };\ MyPropertyListReplacedObserver m_MyPropertyListReplacedObserver;\ void InitializePropertyListReplacedObserver(mitk::IPersistenceService* persistenceService)\ {\ static bool observerInitialized = false;\ if( observerInitialized == false && persistenceService )\ {\ m_MyPropertyListReplacedObserver.Initialize( this, persistenceService );\ observerInitialized = true;\ }\ } #define PERSISTENCE_CREATE(THE_CLASS_NAME, ID_MEMBER_NAME, PARAM_MEMBER_NAME)\ PERSISTENCE_MACRO_START_PART(ID_MEMBER_NAME)\ propList->Set( #PARAM_MEMBER_NAME, PARAM_MEMBER_NAME );\ PERSISTENCE_MACRO_MIDDLE_PART(ID_MEMBER_NAME)\ propList->Get( #PARAM_MEMBER_NAME, PARAM_MEMBER_NAME );\ PERSISTENCE_MACRO_END_PART(THE_CLASS_NAME, ID_MEMBER_NAME) #define PERSISTENCE_CREATE2(THE_CLASS_NAME, ID_MEMBER_NAME, PARAM_MEMBER_NAME, PARAM2_MEMBER_NAME)\ PERSISTENCE_MACRO_START_PART(ID_MEMBER_NAME)\ propList->Set( #PARAM_MEMBER_NAME, PARAM_MEMBER_NAME );\ propList->Set( #PARAM2_MEMBER_NAME, PARAM2_MEMBER_NAME );\ PERSISTENCE_MACRO_MIDDLE_PART(ID_MEMBER_NAME)\ propList->Get( #PARAM_MEMBER_NAME, PARAM_MEMBER_NAME );\ propList->Get( #PARAM2_MEMBER_NAME, PARAM2_MEMBER_NAME );\ PERSISTENCE_MACRO_END_PART(THE_CLASS_NAME, ID_MEMBER_NAME) #define PERSISTENCE_CREATE3(THE_CLASS_NAME, ID_MEMBER_NAME, PARAM_MEMBER_NAME, PARAM2_MEMBER_NAME, PARAM3_MEMBER_NAME)\ PERSISTENCE_MACRO_START_PART(ID_MEMBER_NAME)\ propList->Set( #PARAM_MEMBER_NAME, PARAM_MEMBER_NAME );\ propList->Set( #PARAM2_MEMBER_NAME, PARAM2_MEMBER_NAME );\ propList->Set( #PARAM3_MEMBER_NAME, PARAM3_MEMBER_NAME );\ PERSISTENCE_MACRO_MIDDLE_PART(ID_MEMBER_NAME)\ propList->Get( #PARAM_MEMBER_NAME, PARAM_MEMBER_NAME );\ propList->Get( #PARAM2_MEMBER_NAME, PARAM2_MEMBER_NAME );\ propList->Get( #PARAM3_MEMBER_NAME, PARAM3_MEMBER_NAME );\ PERSISTENCE_MACRO_END_PART(THE_CLASS_NAME, ID_MEMBER_NAME) #define PERSISTENCE_CREATE4(THE_CLASS_NAME, ID_MEMBER_NAME, PARAM_MEMBER_NAME, PARAM2_MEMBER_NAME, PARAM3_MEMBER_NAME, PARAM4_MEMBER_NAME)\ PERSISTENCE_MACRO_START_PART(ID_MEMBER_NAME)\ propList->Set( #PARAM_MEMBER_NAME, PARAM_MEMBER_NAME );\ propList->Set( #PARAM2_MEMBER_NAME, PARAM2_MEMBER_NAME );\ propList->Set( #PARAM3_MEMBER_NAME, PARAM3_MEMBER_NAME );\ propList->Set( #PARAM4_MEMBER_NAME, PARAM4_MEMBER_NAME );\ PERSISTENCE_MACRO_MIDDLE_PART(ID_MEMBER_NAME)\ propList->Get( #PARAM_MEMBER_NAME, PARAM_MEMBER_NAME );\ propList->Get( #PARAM2_MEMBER_NAME, PARAM2_MEMBER_NAME );\ propList->Get( #PARAM3_MEMBER_NAME, PARAM3_MEMBER_NAME );\ propList->Get( #PARAM4_MEMBER_NAME, PARAM4_MEMBER_NAME );\ PERSISTENCE_MACRO_END_PART(THE_CLASS_NAME, ID_MEMBER_NAME) #define PERSISTENCE_CREATE5(THE_CLASS_NAME, ID_MEMBER_NAME, PARAM_MEMBER_NAME, PARAM2_MEMBER_NAME, PARAM3_MEMBER_NAME, PARAM4_MEMBER_NAME, PARAM5_MEMBER_NAME)\ PERSISTENCE_MACRO_START_PART(ID_MEMBER_NAME)\ propList->Set( #PARAM_MEMBER_NAME, PARAM_MEMBER_NAME );\ propList->Set( #PARAM2_MEMBER_NAME, PARAM2_MEMBER_NAME );\ propList->Set( #PARAM3_MEMBER_NAME, PARAM3_MEMBER_NAME );\ propList->Set( #PARAM4_MEMBER_NAME, PARAM4_MEMBER_NAME );\ propList->Set( #PARAM5_MEMBER_NAME, PARAM5_MEMBER_NAME );\ PERSISTENCE_MACRO_MIDDLE_PART(ID_MEMBER_NAME)\ propList->Get( #PARAM_MEMBER_NAME, PARAM_MEMBER_NAME );\ propList->Get( #PARAM2_MEMBER_NAME, PARAM2_MEMBER_NAME );\ propList->Get( #PARAM3_MEMBER_NAME, PARAM3_MEMBER_NAME );\ propList->Get( #PARAM4_MEMBER_NAME, PARAM4_MEMBER_NAME );\ propList->Get( #PARAM5_MEMBER_NAME, PARAM5_MEMBER_NAME );\ PERSISTENCE_MACRO_END_PART(THE_CLASS_NAME, ID_MEMBER_NAME) #define PERSISTENCE_CREATE6(THE_CLASS_NAME, ID_MEMBER_NAME, PARAM_MEMBER_NAME, PARAM2_MEMBER_NAME, PARAM3_MEMBER_NAME, PARAM4_MEMBER_NAME, PARAM5_MEMBER_NAME, PARAM6_MEMBER_NAME)\ PERSISTENCE_MACRO_START_PART(ID_MEMBER_NAME)\ propList->Set( #PARAM_MEMBER_NAME, PARAM_MEMBER_NAME );\ propList->Set( #PARAM2_MEMBER_NAME, PARAM2_MEMBER_NAME );\ propList->Set( #PARAM3_MEMBER_NAME, PARAM3_MEMBER_NAME );\ propList->Set( #PARAM4_MEMBER_NAME, PARAM4_MEMBER_NAME );\ propList->Set( #PARAM5_MEMBER_NAME, PARAM5_MEMBER_NAME );\ propList->Set( #PARAM6_MEMBER_NAME, PARAM6_MEMBER_NAME );\ PERSISTENCE_MACRO_MIDDLE_PART(ID_MEMBER_NAME)\ propList->Get( #PARAM_MEMBER_NAME, PARAM_MEMBER_NAME );\ propList->Get( #PARAM2_MEMBER_NAME, PARAM2_MEMBER_NAME );\ propList->Get( #PARAM3_MEMBER_NAME, PARAM3_MEMBER_NAME );\ propList->Get( #PARAM4_MEMBER_NAME, PARAM4_MEMBER_NAME );\ propList->Get( #PARAM5_MEMBER_NAME, PARAM5_MEMBER_NAME );\ propList->Get( #PARAM6_MEMBER_NAME, PARAM6_MEMBER_NAME );\ PERSISTENCE_MACRO_END_PART(THE_CLASS_NAME, ID_MEMBER_NAME) #define PERSISTENCE_CREATE7(THE_CLASS_NAME, ID_MEMBER_NAME, PARAM_MEMBER_NAME, PARAM2_MEMBER_NAME, PARAM3_MEMBER_NAME, PARAM4_MEMBER_NAME, PARAM5_MEMBER_NAME, PARAM6_MEMBER_NAME, PARAM7_MEMBER_NAME)\ PERSISTENCE_MACRO_START_PART(ID_MEMBER_NAME)\ propList->Set( #PARAM_MEMBER_NAME, PARAM_MEMBER_NAME );\ propList->Set( #PARAM2_MEMBER_NAME, PARAM2_MEMBER_NAME );\ propList->Set( #PARAM3_MEMBER_NAME, PARAM3_MEMBER_NAME );\ propList->Set( #PARAM4_MEMBER_NAME, PARAM4_MEMBER_NAME );\ propList->Set( #PARAM5_MEMBER_NAME, PARAM5_MEMBER_NAME );\ propList->Set( #PARAM6_MEMBER_NAME, PARAM6_MEMBER_NAME );\ propList->Set( #PARAM7_MEMBER_NAME, PARAM7_MEMBER_NAME );\ PERSISTENCE_MACRO_MIDDLE_PART(ID_MEMBER_NAME)\ propList->Get( #PARAM_MEMBER_NAME, PARAM_MEMBER_NAME );\ propList->Get( #PARAM2_MEMBER_NAME, PARAM2_MEMBER_NAME );\ propList->Get( #PARAM3_MEMBER_NAME, PARAM3_MEMBER_NAME );\ propList->Get( #PARAM4_MEMBER_NAME, PARAM4_MEMBER_NAME );\ propList->Get( #PARAM5_MEMBER_NAME, PARAM5_MEMBER_NAME );\ propList->Get( #PARAM6_MEMBER_NAME, PARAM6_MEMBER_NAME );\ propList->Get( #PARAM7_MEMBER_NAME, PARAM7_MEMBER_NAME );\ PERSISTENCE_MACRO_END_PART(THE_CLASS_NAME, ID_MEMBER_NAME) #define PERSISTENCE_CREATE8(THE_CLASS_NAME, ID_MEMBER_NAME, PARAM_MEMBER_NAME, PARAM2_MEMBER_NAME, PARAM3_MEMBER_NAME, PARAM4_MEMBER_NAME, PARAM5_MEMBER_NAME, PARAM6_MEMBER_NAME, PARAM7_MEMBER_NAME, PARAM8_MEMBER_NAME)\ PERSISTENCE_MACRO_START_PART(ID_MEMBER_NAME)\ propList->Set( #PARAM_MEMBER_NAME, PARAM_MEMBER_NAME );\ propList->Set( #PARAM2_MEMBER_NAME, PARAM2_MEMBER_NAME );\ propList->Set( #PARAM3_MEMBER_NAME, PARAM3_MEMBER_NAME );\ propList->Set( #PARAM4_MEMBER_NAME, PARAM4_MEMBER_NAME );\ propList->Set( #PARAM5_MEMBER_NAME, PARAM5_MEMBER_NAME );\ propList->Set( #PARAM6_MEMBER_NAME, PARAM6_MEMBER_NAME );\ propList->Set( #PARAM7_MEMBER_NAME, PARAM7_MEMBER_NAME );\ propList->Set( #PARAM8_MEMBER_NAME, PARAM8_MEMBER_NAME );\ PERSISTENCE_MACRO_MIDDLE_PART(ID_MEMBER_NAME)\ propList->Get( #PARAM_MEMBER_NAME, PARAM_MEMBER_NAME );\ propList->Get( #PARAM2_MEMBER_NAME, PARAM2_MEMBER_NAME );\ propList->Get( #PARAM3_MEMBER_NAME, PARAM3_MEMBER_NAME );\ propList->Get( #PARAM4_MEMBER_NAME, PARAM4_MEMBER_NAME );\ propList->Get( #PARAM5_MEMBER_NAME, PARAM5_MEMBER_NAME );\ propList->Get( #PARAM6_MEMBER_NAME, PARAM6_MEMBER_NAME );\ propList->Get( #PARAM7_MEMBER_NAME, PARAM7_MEMBER_NAME );\ propList->Get( #PARAM8_MEMBER_NAME, PARAM8_MEMBER_NAME );\ PERSISTENCE_MACRO_END_PART(THE_CLASS_NAME, ID_MEMBER_NAME) US_DECLARE_SERVICE_INTERFACE(mitk::IPersistenceService, "org.mitk.services.IPersistenceService") #endif diff --git a/Modules/Persistence/CMakeLists.txt b/Modules/Persistence/CMakeLists.txt index b0e15b403a..66b52eaabc 100644 --- a/Modules/Persistence/CMakeLists.txt +++ b/Modules/Persistence/CMakeLists.txt @@ -1,6 +1,6 @@ MITK_CREATE_MODULE( Persistence INCLUDE_DIRS DEPENDS SceneSerialization - AUTOLOAD_WITH Mitk + AUTOLOAD_WITH SceneSerialization ) add_subdirectory(Testing) diff --git a/Modules/Persistence/Testing/mitkPersistenceTest.cpp b/Modules/Persistence/Testing/mitkPersistenceTest.cpp index 62eb2e9498..b53004bd18 100644 --- a/Modules/Persistence/Testing/mitkPersistenceTest.cpp +++ b/Modules/Persistence/Testing/mitkPersistenceTest.cpp @@ -1,164 +1,164 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include #include #include #include #include #include -//#include +#include #include struct PersistenceTestClass { PersistenceTestClass() : id(""), param1(1), param2(2), param3(false) { } std::string id; int param1; double param2; bool param3; PERSISTENCE_CREATE3(PersistenceTestClass, id, param1, param2, param3) }; struct TestPropertyListReplacedObserver: public mitk::PropertyListReplacedObserver { TestPropertyListReplacedObserver(): counter(0) {} virtual void BeforePropertyListReplaced( const std::string& id, mitk::PropertyList* propertyList ) { if( id == m_Id ) counter++; } virtual void AfterPropertyListReplaced( const std::string& id, mitk::PropertyList* propertyList ) { if( id == m_Id ) counter++; } int counter; std::string m_Id; }; std::string testClassId = "testClass"; int param1 = 100; double param2 = 201.56; bool param3 = true; void testParams( const PersistenceTestClass& testClass, const std::string& testClassName ) { MITK_INFO << "Testing parameters of " << testClassName; MITK_TEST_CONDITION( testClass.id == testClassId, "testClass.id (" << testClass.id << ") != testClassId (" << testClassId << ")" ); MITK_TEST_CONDITION( testClass.param1 == param1, "testClass.param1 (" << testClass.param1 << ") != param1 (" << param1 << ")" ); MITK_TEST_CONDITION( testClass.param2 == param2, "testClass.param2 (" << testClass.param2 << ") != param2 (" << param2 << ")" ); MITK_TEST_CONDITION( testClass.param3 == param3, "testClass.param3 (" << testClass.param3 << ") != param3 (" << param3 << ")" ); } int mitkPersistenceTest(int /*argc*/, char* /*argv*/[]) { MITK_TEST_BEGIN("PersistenceTest") // dummy load of SceneIO, otherwise PersistenceService won't be available - // mitk::SceneIO::Pointer sceneIO = mitk::SceneIO::New(); + mitk::SceneIO::Pointer sceneIO = mitk::SceneIO::New(); MITK_INFO << "Testing availability of the PersistenceService."; PERSISTENCE_GET_SERVICE_MACRO MITK_TEST_CONDITION_REQUIRED(persistenceService, "IPersistenceService available") MITK_INFO << "Initialize testable parameter values."; Poco::File defaultPersistenceFile(persistenceService->GetDefaultPersistenceFile()); if( defaultPersistenceFile.exists() && persistenceService->GetAutoLoadAndSave() ) { MITK_INFO << "Testing auto load/save of the PersistenceService."; PersistenceTestClass autoLoadTestClass; autoLoadTestClass.id = testClassId; autoLoadTestClass.FromPropertyList(); testParams( autoLoadTestClass, "autoLoadTestClass" ); } MITK_INFO << "Removing left-over test files."; Poco::File testTempFile("PersistenceTestFile.mitk"); if( testTempFile.exists() ) testTempFile.remove(false); Poco::File testXmlTempFile("PersistenceTestFile.xml"); if( testXmlTempFile.exists() ) testXmlTempFile.remove(false); MITK_INFO << "Testing standard write to scene file/xml file."; PersistenceTestClass testClass; testClass.id = testClassId; testClass.param1 = param1; testClass.param2 = param2; testClass.param3 = param3; MITK_TEST_CONDITION_REQUIRED( testClass.Save(testTempFile.path()), "testClass.Save(testTempFile.path())"); MITK_TEST_CONDITION_REQUIRED( testClass.Save(testXmlTempFile.path()), "testClass.Save(testTempFile.path())"); MITK_INFO << "Testing read from scene file."; MITK_TEST_CONDITION_REQUIRED( persistenceService->RemovePropertyList(testClassId), "persistenceService->RemovePropertyList(testClassId)"); PersistenceTestClass testClass2; testClass2.id = testClassId; MITK_TEST_CONDITION_REQUIRED( testClass2.Load(testTempFile.path()), "testClass2.Load(testTempFile.path())"); testParams( testClass2, "testClass2" ); MITK_INFO << "Testing read from xml file."; MITK_TEST_CONDITION_REQUIRED( persistenceService->RemovePropertyList(testClassId), "persistenceService->RemovePropertyList(testClassId)"); PersistenceTestClass testClass3; testClass3.id = testClassId; MITK_TEST_CONDITION_REQUIRED( testClass3.Load(testXmlTempFile.path()), "testClass3.Load(testXmlTempFile.path())"); testParams( testClass3, "testClass3" ); MITK_INFO << "Testing appendChanges functionality with scene load/write."; MITK_TEST_CONDITION_REQUIRED( persistenceService->RemovePropertyList(testClassId), "persistenceService->RemovePropertyList(testClassId)"); MITK_TEST_CONDITION_REQUIRED( persistenceService->Save(testTempFile.path(), true), "persistenceService->Save(testTempFile.path())"); MITK_TEST_CONDITION_REQUIRED( persistenceService->Load(testTempFile.path()), "persistenceService->Load(testTempFile.path())"); PersistenceTestClass testClass4; testClass4.id = testClassId; testClass4.FromPropertyList(); testParams( testClass4, "testClass4" ); MITK_INFO << "Testing appendChanges functionality with xml load/write."; MITK_TEST_CONDITION_REQUIRED( persistenceService->RemovePropertyList(testClassId), "persistenceService->RemovePropertyList(testClassId)"); MITK_TEST_CONDITION_REQUIRED( persistenceService->Save(testXmlTempFile.path(), true), "persistenceService->Save(testXmlTempFile.path())"); MITK_TEST_CONDITION_REQUIRED( persistenceService->Load(testXmlTempFile.path()), "persistenceService->Load(testXmlTempFile.path())"); PersistenceTestClass testClass5; testClass5.id = testClassId; testClass5.FromPropertyList(); testParams( testClass5, "testClass5" ); MITK_INFO << "Testing observer functionality."; TestPropertyListReplacedObserver testObserver; testObserver.m_Id = testClassId; persistenceService->AddPropertyListReplacedObserver( &testObserver ); persistenceService->Load(testTempFile.path()); MITK_TEST_CONDITION( testObserver.counter == 2, "testObserver.counter == 2, testObserver.counter is " << testObserver.counter ); MITK_INFO << "Cleaning test files."; if( testXmlTempFile.exists() ) testXmlTempFile.remove(false); if( testTempFile.exists() ) testTempFile.remove(false); MITK_TEST_END() }