diff --git a/Modules/IGT/IO/mitkNavigationToolStorageSerializer.cpp b/Modules/IGT/IO/mitkNavigationToolStorageSerializer.cpp index d01e91641d..551bfe10f5 100644 --- a/Modules/IGT/IO/mitkNavigationToolStorageSerializer.cpp +++ b/Modules/IGT/IO/mitkNavigationToolStorageSerializer.cpp @@ -1,98 +1,96 @@ /*=================================================================== 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 "mitkNavigationToolStorageSerializer.h" #include "mitkNavigationToolWriter.h" #include "mitkIGTException.h" #include "mitkIGTIOException.h" #include #include #include // Poco #include //ITKsys #include #include mitk::NavigationToolStorageSerializer::NavigationToolStorageSerializer() { //create temp directory m_tempDirectory = mitk::IOUtil::CreateTemporaryDirectory("NavigationToolStorageSerializerTmp_XXXXXX"); } mitk::NavigationToolStorageSerializer::~NavigationToolStorageSerializer() { //remove temp directory try { if (itksys::SystemTools::FileExists(m_tempDirectory.c_str())) std::remove(m_tempDirectory.c_str()); } catch(...) { MITK_ERROR << "Can't remove temp directory " << m_tempDirectory << "!"; } } -bool mitk::NavigationToolStorageSerializer::Serialize(std::string filename, mitk::NavigationToolStorage::Pointer storage) +void mitk::NavigationToolStorageSerializer::Serialize(const std::string& filename, mitk::NavigationToolStorage::Pointer storage) { //save every tool to temp directory mitk::NavigationToolWriter::Pointer myToolWriter = mitk::NavigationToolWriter::New(); for(unsigned int i=0; iGetToolCount();i++) { std::string tempFileName = m_tempDirectory + mitk::IOUtil::GetDirectorySeparator() + "NavigationTool" + convertIntToString(i) + ".tool"; if (!myToolWriter->DoWrite(tempFileName,storage->GetTool(i))) { mitkThrowException(mitk::IGTIOException) << "Could not write tool to tempory directory: " << tempFileName; } } //add all files to zip archive std::ofstream file( filename.c_str(), std::ios::binary | std::ios::out); if (!file.good()) //test if the zip archive is ready for writing { //first: clean up for (unsigned int i=0; iGetToolCount();i++) { std::string tempFileName = m_tempDirectory + mitk::IOUtil::GetDirectorySeparator() + "NavigationTool" + convertIntToString(i) + ".tool"; std::remove(tempFileName.c_str()); } //then: throw an exception mitkThrowException(mitk::IGTIOException) << "Could not open a file for writing: " << filename; } Poco::Zip::Compress zipper( file, true ); for (unsigned int i=0; iGetToolCount();i++) { std::string fileName = m_tempDirectory + mitk::IOUtil::GetDirectorySeparator() + "NavigationTool" + convertIntToString(i) + ".tool"; zipper.addFile(fileName, myToolWriter->GetFileWithoutPath(fileName)); std::remove(fileName.c_str()); //delete file } zipper.close(); file.close(); - - return true; - } +} std::string mitk::NavigationToolStorageSerializer::convertIntToString(int i) { std::string s; std::stringstream out; out << i; s = out.str(); return s; - } \ No newline at end of file + } diff --git a/Modules/IGT/IO/mitkNavigationToolStorageSerializer.h b/Modules/IGT/IO/mitkNavigationToolStorageSerializer.h index af7af7b960..9ea4470ec8 100644 --- a/Modules/IGT/IO/mitkNavigationToolStorageSerializer.h +++ b/Modules/IGT/IO/mitkNavigationToolStorageSerializer.h @@ -1,69 +1,59 @@ /*=================================================================== 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 NAVIGATIONTOOLSTORAGESERIALIZER_H_INCLUDED #define NAVIGATIONTOOLSTORAGESERIALIZER_H_INCLUDED //itk headers #include //mitk headers #include #include "mitkNavigationToolStorage.h" #include namespace mitk { /**Documentation * \brief This class offers methods to save an object of the class NavigationToolStorage * to the harddisc. * * \ingroup IGT */ class MITKIGT_EXPORT NavigationToolStorageSerializer : public itk::Object { public: mitkClassMacroItkParent(NavigationToolStorageSerializer,itk::Object); itkFactorylessNewMacro(Self) itkCloneMacro(Self) /** - * @brief Saves a mitk navigation tool storage to a file. - * @return Returns true always true since error handling was converted to exception handling. - * The return value is decrepated. Will be changed to void. + * @brief Saves a mitk navigation tool storage to a file. * @throw mitk::IGTIOException Throws an exception if the given filename cannot be opened for writing or * if the temp directory is not accessible. */ - bool Serialize(std::string filename, mitk::NavigationToolStorage::Pointer storage); - - /** - * @brief This method is decrepated. Exceptions are used for error handling now! - * @return Returns always an empty string since error handling was converted to exception handling. - */ - itkGetMacro(ErrorMessage,std::string); + void Serialize(const std::string& filename, mitk::NavigationToolStorage::Pointer storage); protected: NavigationToolStorageSerializer(); ~NavigationToolStorageSerializer() override; - std::string m_ErrorMessage; - std::string convertIntToString(int i); std::string m_tempDirectory; }; } // namespace mitk #endif //NAVIGATIONTOOLSTORAGESERIALIZER diff --git a/Modules/IGT/Testing/mitkNavigationToolStorageSerializerAndDeserializerIntegrationTest.cpp b/Modules/IGT/Testing/mitkNavigationToolStorageSerializerAndDeserializerIntegrationTest.cpp index cfdca9a993..98a4f3f936 100644 --- a/Modules/IGT/Testing/mitkNavigationToolStorageSerializerAndDeserializerIntegrationTest.cpp +++ b/Modules/IGT/Testing/mitkNavigationToolStorageSerializerAndDeserializerIntegrationTest.cpp @@ -1,139 +1,138 @@ /*=================================================================== 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. ===================================================================*/ //testing headers #include #include #include #include //some general mitk headers #include #include #include //headers of IGT classes releated to the tested class #include #include #include #include #include #include //POCO headers for file handling #include #include #include class mitkNavigationToolStorageSerializerAndDeserializerIntegrationTestSuite : public mitk::TestFixture { CPPUNIT_TEST_SUITE(mitkNavigationToolStorageSerializerAndDeserializerIntegrationTestSuite); MITK_TEST(TestInstantiationSerializer); MITK_TEST(TestInstantiationDeserializer); MITK_TEST(TestWriteAndReadSimpleToolStorageWithToolLandmarks); CPPUNIT_TEST_SUITE_END(); private: /** Members used inside the different test methods. All members are initialized via setUp().*/ mitk::NavigationToolStorageSerializer::Pointer m_Serializer; mitk::NavigationToolStorageDeserializer::Pointer m_Deserializer; std::string m_FileName1; public: /**@brief Setup Always call this method before each Test-case to ensure correct and new intialization of the used members for a new test case. (If the members are not used in a test, the method does not need to be called).*/ void setUp() override { try { m_FileName1 = mitk::IOUtil::CreateTemporaryFile(); std::ofstream file; file.open(m_FileName1.c_str()); if (!file.good()) {MITK_ERROR <<"Could not create a valid file during setUp() method.";} file.close(); } catch (std::exception& e) { MITK_ERROR << "File access Exception: " << e.what(); MITK_ERROR <<"Could not create filename during setUp() method."; } m_Serializer = mitk::NavigationToolStorageSerializer::New(); mitk::DataStorage::Pointer DataStorage = dynamic_cast(mitk::StandaloneDataStorage::New().GetPointer()); //needed for deserializer! m_Deserializer = mitk::NavigationToolStorageDeserializer::New(DataStorage); } void tearDown() override { m_Serializer = nullptr; m_Deserializer = nullptr; try { std::remove(m_FileName1.c_str()); } catch(...) { MITK_ERROR << "Warning: Error occured when deleting test file!"; } } void TestInstantiationSerializer() { // let's create objects of our classes mitk::NavigationToolStorageSerializer::Pointer testSerializer = mitk::NavigationToolStorageSerializer::New(); CPPUNIT_ASSERT_MESSAGE("Testing instantiation of NavigationToolStorageSerializer",testSerializer.IsNotNull()); } void TestInstantiationDeserializer() { mitk::DataStorage::Pointer tempStorage = dynamic_cast(mitk::StandaloneDataStorage::New().GetPointer()); //needed for deserializer! mitk::NavigationToolStorageDeserializer::Pointer testDeserializer = mitk::NavigationToolStorageDeserializer::New(tempStorage); CPPUNIT_ASSERT_MESSAGE("Testing instantiation of NavigationToolStorageDeserializer",testDeserializer.IsNotNull()); } void TestWriteAndReadSimpleToolStorageWithToolLandmarks() { //create Tool Storage mitk::NavigationToolStorage::Pointer storage = mitk::NavigationToolStorageTestHelper::CreateTestData_StorageWithOneTool(); //test serialization - bool success = m_Serializer->Serialize(m_FileName1,storage); - CPPUNIT_ASSERT_MESSAGE("Testing serialization of tool storage with tool registrations",success); + CPPUNIT_ASSERT_NO_THROW_MESSAGE("Testing serialization of tool storage with tool registrations", m_Serializer->Serialize(m_FileName1,storage)); //test deserialization of the same file mitk::NavigationToolStorage::Pointer readStorage = m_Deserializer->Deserialize(m_FileName1); CPPUNIT_ASSERT_MESSAGE("Testing deserialization of tool storage with tool registrations",readStorage.IsNotNull()); CPPUNIT_ASSERT_MESSAGE(" ..Testing number of tools in storage",readStorage->GetToolCount()==1); mitk::PointSet::Pointer readRegLandmarks = readStorage->GetTool(0)->GetToolLandmarks(); mitk::PointSet::Pointer readCalLandmarks = readStorage->GetTool(0)->GetToolControlPoints(); CPPUNIT_ASSERT_MESSAGE("..Testing if tool registration landmarks have been stored and loaded correctly.",((readRegLandmarks->GetPoint(5)[0] == 4)&&(readRegLandmarks->GetPoint(5)[1] == 5)&&(readRegLandmarks->GetPoint(5)[2] == 6))); CPPUNIT_ASSERT_MESSAGE("..Testing if tool calibration landmarks have been stored and loaded correctly.",((readCalLandmarks->GetPoint(0)[0] == 1)&&(readCalLandmarks->GetPoint(0)[1] == 2)&&(readCalLandmarks->GetPoint(0)[2] == 3))); mitk::Point3D readToolTipPos = readStorage->GetTool(0)->GetToolTipPosition(); mitk::Quaternion readToolTipRot = readStorage->GetTool(0)->GetToolAxisOrientation(); CPPUNIT_ASSERT_MESSAGE("..Testing if tool tip position has been stored and loaded correctly.", ((float(readToolTipPos[0]) == float(1.3423))&& (float(readToolTipPos[1]) == float(2.323))&& (float(readToolTipPos[2]) == float(4.332)))); CPPUNIT_ASSERT_MESSAGE("..Testing if tool tip orientation has been stored and loaded correctly.", ((float(readToolTipRot.x()) == float(0.1))&& (float(readToolTipRot.y()) == float(0.2))&& (float(readToolTipRot.z()) == float(0.3))&& (float(readToolTipRot.r()) == float(0.4)))); } }; MITK_TEST_SUITE_REGISTRATION(mitkNavigationToolStorageSerializerAndDeserializerIntegration) diff --git a/Modules/IGT/Testing/mitkNavigationToolStorageSerializerTest.cpp b/Modules/IGT/Testing/mitkNavigationToolStorageSerializerTest.cpp index 5902192086..250735b59f 100644 --- a/Modules/IGT/Testing/mitkNavigationToolStorageSerializerTest.cpp +++ b/Modules/IGT/Testing/mitkNavigationToolStorageSerializerTest.cpp @@ -1,141 +1,138 @@ /*=================================================================== 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. ===================================================================*/ //testing headers #include #include #include #include #include //headers of IGT classes releated to the tested class #include #include class mitkNavigationToolStorageSerializerTestSuite : public mitk::TestFixture { CPPUNIT_TEST_SUITE(mitkNavigationToolStorageSerializerTestSuite); MITK_TEST(TestInstantiationSerializer); MITK_TEST(TestWriteSimpleToolStorage); MITK_TEST(TestWriteComplexToolStorage); MITK_TEST(TestWriteStorageToInvalidFile); MITK_TEST(TestWriteEmptyToolStorage); MITK_TEST(TestSerializerForExceptions); CPPUNIT_TEST_SUITE_END(); private: /** Members used inside the different test methods. All members are initialized via setUp().*/ std::string m_FileName1; mitk::NavigationToolStorageSerializer::Pointer m_Serializer; public: /**@brief Setup Always call this method before each Test-case to ensure correct and new intialization of the used members for a new test case. (If the members are not used in a test, the method does not need to be called).*/ void setUp() override { try { m_FileName1 = mitk::IOUtil::CreateTemporaryFile("NavigationToolStorageSerializerTestTmp_XXXXXX.IGTToolStorage",mitk::IOUtil::GetProgramPath()); std::ofstream file; file.open(m_FileName1.c_str()); if (!file.good()) {MITK_ERROR <<"Could not create a valid file during setUp() method.";} file.close(); } catch (std::exception& e) { MITK_ERROR << "File access Exception: " << e.what(); MITK_ERROR <<"Could not create filename during setUp() method."; } m_Serializer = mitk::NavigationToolStorageSerializer::New(); } void tearDown() override { m_Serializer = nullptr; try { std::remove(m_FileName1.c_str()); } catch(...) { MITK_ERROR << "Warning: Error occured while deleting test file!"; } } void TestInstantiationSerializer() { // let's create objects of our classes mitk::NavigationToolStorageSerializer::Pointer testSerializer = mitk::NavigationToolStorageSerializer::New(); CPPUNIT_ASSERT_MESSAGE("Testing instantiation of NavigationToolStorageSerializer",testSerializer.IsNotNull()); } void TestWriteSimpleToolStorage() { - //create Tool Storage + //create Tool Storage mitk::NavigationToolStorage::Pointer myStorage = mitk::NavigationToolStorageTestHelper::CreateTestData_SimpleStorage(); - //test serialization - bool success = m_Serializer->Serialize(m_FileName1,myStorage); - CPPUNIT_ASSERT_MESSAGE("Testing serialization of simple tool storage",success); + //test serialization + CPPUNIT_ASSERT_NO_THROW_MESSAGE("Testing serialization of simple tool storage", m_Serializer->Serialize(m_FileName1, myStorage)); } void TestWriteComplexToolStorage() { //create navigation tool storage mitk::NavigationToolStorage::Pointer myStorage = mitk::NavigationToolStorageTestHelper::CreateTestData_ComplexStorage(GetTestDataFilePath("ClaronTool"),GetTestDataFilePath("IGT-Data/ClaronTool.stl"),GetTestDataFilePath("IGT-Data/EMTool.stl")); //test serialization - bool success = m_Serializer->Serialize(m_FileName1,myStorage); - CPPUNIT_ASSERT_MESSAGE("Testing serialization of complex tool storage",success); + CPPUNIT_ASSERT_NO_THROW_MESSAGE("Testing serialization of complex tool storage", m_Serializer->Serialize(m_FileName1, myStorage)); } void TestWriteStorageToInvalidFile() { //create Tool Storage mitk::NavigationToolStorage::Pointer myStorage = mitk::NavigationToolStorageTestHelper::CreateTestData_SimpleStorage(); //create invalid filename #ifdef WIN32 std::string filename = "C:\342INVALIDFILE<>.storage"; //invalid filename for windows #else std::string filename = "/dsfdsf:$�$342INVALIDFILE.storage"; //invalid filename for linux #endif //test serialization (should throw exception) CPPUNIT_ASSERT_THROW_MESSAGE("Test serialization with simple storage and invalid filename, an exception is expected.",m_Serializer->Serialize(filename,myStorage),mitk::IGTException); } void TestWriteEmptyToolStorage() { //create Tool Storage mitk::NavigationToolStorage::Pointer myStorage = mitk::NavigationToolStorage::New(); //test serialization - bool success = m_Serializer->Serialize(m_FileName1,myStorage); - CPPUNIT_ASSERT_MESSAGE("Testing serialization of simple tool storage",success); + CPPUNIT_ASSERT_NO_THROW_MESSAGE("Testing serialization of simple tool storage", m_Serializer->Serialize(m_FileName1, myStorage)); } void TestSerializerForExceptions() { mitk::NavigationToolStorage::Pointer myStorage = mitk::NavigationToolStorage::New(); //create an invalid filename std::string filename = std::string( MITK_TEST_OUTPUT_DIR )+Poco::Path::separator()+""; //now try to serialize an check if an exception is thrown CPPUNIT_ASSERT_THROW_MESSAGE("Test serialization with empty storage and invalid filename, an exception is expected.",m_Serializer->Serialize(filename,myStorage),mitk::IGTException); } }; -MITK_TEST_SUITE_REGISTRATION(mitkNavigationToolStorageSerializer) \ No newline at end of file +MITK_TEST_SUITE_REGISTRATION(mitkNavigationToolStorageSerializer)