diff --git a/Modules/IGT/IO/mitkNavigationToolStorageSerializer.cpp b/Modules/IGT/IO/mitkNavigationToolStorageSerializer.cpp index 236db1c301..d01e91641d 100644 --- a/Modules/IGT/IO/mitkNavigationToolStorageSerializer.cpp +++ b/Modules/IGT/IO/mitkNavigationToolStorageSerializer.cpp @@ -1,99 +1,98 @@ /*=================================================================== 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. ===================================================================*/ -//Poco headers -#include -#include -#include - #include "mitkNavigationToolStorageSerializer.h" #include "mitkNavigationToolWriter.h" #include "mitkIGTException.h" #include "mitkIGTIOException.h" #include #include #include -#include +// Poco +#include +//ITKsys +#include +#include mitk::NavigationToolStorageSerializer::NavigationToolStorageSerializer() { //create temp directory - m_tempDirectory = mitk::IOUtil::CreateTemporaryDirectory("NavigationToolStorageSerializerTmp_XXXXXX",mitk::IOUtil::GetProgramPath()); + m_tempDirectory = mitk::IOUtil::CreateTemporaryDirectory("NavigationToolStorageSerializerTmp_XXXXXX"); } mitk::NavigationToolStorageSerializer::~NavigationToolStorageSerializer() { //remove temp directory - Poco::File myFile(m_tempDirectory); + try { - if (myFile.exists()) myFile.remove(true); + 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) { //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 + Poco::Path::separator() + "NavigationTool" + convertIntToString(i) + ".tool"; + 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 + Poco::Path::separator() + "NavigationTool" + convertIntToString(i) + ".tool"; + 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 + Poco::Path::separator() + "NavigationTool" + convertIntToString(i) + ".tool"; - zipper.addFile(fileName,myToolWriter->GetFileWithoutPath(fileName)); + 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/Testing/mitkNavigationToolStorageSerializerTest.cpp b/Modules/IGT/Testing/mitkNavigationToolStorageSerializerTest.cpp index 27b981c4fa..5902192086 100644 --- a/Modules/IGT/Testing/mitkNavigationToolStorageSerializerTest.cpp +++ b/Modules/IGT/Testing/mitkNavigationToolStorageSerializerTest.cpp @@ -1,141 +1,141 @@ /*=================================================================== 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 when deleting test file!"; + 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 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); } 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); } 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); } 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) +MITK_TEST_SUITE_REGISTRATION(mitkNavigationToolStorageSerializer) \ No newline at end of file