diff --git a/Modules/IGT/Testing/files.cmake b/Modules/IGT/Testing/files.cmake index 8fc4665475..a81c54c494 100644 --- a/Modules/IGT/Testing/files.cmake +++ b/Modules/IGT/Testing/files.cmake @@ -1,45 +1,51 @@ +set(H_FILES + mitkNavigationToolStorageTestHelper.h +) + set(MODULE_TESTS mitkCameraVisualizationTest.cpp mitkClaronInterfaceTest.cpp mitkClaronToolTest.cpp mitkClaronTrackingDeviceTest.cpp mitkInternalTrackingToolTest.cpp mitkNavigationDataDisplacementFilterTest.cpp mitkNavigationDataLandmarkTransformFilterTest.cpp mitkNavigationDataObjectVisualizationFilterTest.cpp mitkNavigationDataTest.cpp mitkNavigationDataRecorderTest.cpp mitkNavigationDataReferenceTransformFilterTest.cpp mitkNavigationDataSequentialPlayerTest.cpp mitkNavigationDataSourceTest.cpp mitkNavigationDataToMessageFilterTest.cpp mitkNavigationDataToNavigationDataFilterTest.cpp mitkNavigationDataToPointSetFilterTest.cpp mitkNavigationDataTransformFilterTest.cpp mitkNDIPassiveToolTest.cpp mitkNDIProtocolTest.cpp mitkNDITrackingDeviceTest.cpp mitkTimeStampTest.cpp mitkTrackingVolumeGeneratorTest.cpp mitkTrackingDeviceTest.cpp mitkTrackingToolTest.cpp mitkVirtualTrackingDeviceTest.cpp mitkNavigationDataPlayerTest.cpp # see bug 11636 (extend this test by microservices) mitkTrackingDeviceSourceTest.cpp mitkTrackingDeviceSourceConfiguratorTest.cpp mitkNavigationDataEvaluationFilterTest.cpp mitkTrackingTypesTest.cpp # ------------------ Navigation Tool Management Tests ------------------- mitkNavigationToolStorageTest.cpp mitkNavigationToolTest.cpp mitkNavigationToolReaderAndWriterTest.cpp - mitkNavigationToolStorageSerializerAndDeserializerTest.cpp + mitkNavigationToolStorageSerializerTest.cpp + mitkNavigationToolStorageDeserializerTest.cpp + mitkNavigationToolStorageSerializerAndDeserializerIntegrationTest.cpp # ----------------------------------------------------------------------- ) set(MODULE_CUSTOM_TESTS mitkNDIAuroraHardwareTest.cpp mitkNDIPolarisHardwareTest.cpp mitkClaronTrackingDeviceHardwareTest.cpp ) diff --git a/Modules/IGT/Testing/mitkNavigationToolStorageDeserializerTest.cpp b/Modules/IGT/Testing/mitkNavigationToolStorageDeserializerTest.cpp new file mode 100644 index 0000000000..f796722743 --- /dev/null +++ b/Modules/IGT/Testing/mitkNavigationToolStorageDeserializerTest.cpp @@ -0,0 +1,133 @@ +/*=================================================================== + +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 + +//headers of IGT classes releated to the tested class +#include +#include +#include + +class mitkNavigationToolStorageDeserializerTestSuite : public mitk::TestFixture +{ + CPPUNIT_TEST_SUITE(mitkNavigationToolStorageDeserializerTestSuite); + MITK_TEST(TestInstantiationDeserializer); + MITK_TEST(TestReadSimpleToolStorage); + MITK_TEST(TestReadComplexToolStorage); + MITK_TEST(TestReadNotExistingStorage); + MITK_TEST(TestReadStorageWithUnknownFiletype); + MITK_TEST(TestReadZipFileWithNoToolstorage); + MITK_TEST(TestDeserializerForExceptions); + CPPUNIT_TEST_SUITE_END(); + +private: + /** Members used inside the different test methods. All members are initialized via setUp().*/ + mitk::NavigationToolStorageDeserializer::Pointer m_Deserializer; + mitk::DataStorage::Pointer m_DataStorage; + +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() + { + m_DataStorage = dynamic_cast(mitk::StandaloneDataStorage::New().GetPointer()); //needed for deserializer! + m_Deserializer = mitk::NavigationToolStorageDeserializer::New(m_DataStorage); + } + + void tearDown() + { + m_DataStorage = NULL; + m_Deserializer = NULL; + } + + 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 TestReadSimpleToolStorage() + { + std::string testDataPath = GetTestDataFilePath("IGT-Data/SimpleIGTStorage.storage"); + mitk::NavigationToolStorage::Pointer readStorage = m_Deserializer->Deserialize(testDataPath); + CPPUNIT_ASSERT_MESSAGE("Testing deserialization of simple tool storage",readStorage.IsNotNull()); + CPPUNIT_ASSERT_MESSAGE(" ..Testing number of tools in storage",readStorage->GetToolCount()==3); + //TODO: why is the order of tools changed is save/load process?? + bool foundtool1 = false; + bool foundtool2 = false; + bool foundtool3 = false; + for(int i=0; i<3; i++) + { + if ((readStorage->GetTool(i)->GetIdentifier()=="001")) foundtool1 = true; + else if ((readStorage->GetTool(i)->GetIdentifier()=="002")) foundtool2 = true; + else if ((readStorage->GetTool(i)->GetIdentifier()=="003")) foundtool3 = true; + } + CPPUNIT_ASSERT_MESSAGE(" ..Testing if tool 1 was loaded successfully",foundtool1); + CPPUNIT_ASSERT_MESSAGE(" ..Testing if tool 2 was loaded successfully",foundtool2); + CPPUNIT_ASSERT_MESSAGE(" ..Testing if tool 3 was loaded successfully",foundtool2); + } + + void TestReadComplexToolStorage() + { + std::string testDataPath = GetTestDataFilePath("IGT-Data/ComplexIGTStorage.storage"); + mitk::NavigationToolStorage::Pointer readStorage = m_Deserializer->Deserialize(testDataPath); + CPPUNIT_ASSERT_MESSAGE("Testing deserialization of complex tool storage",readStorage.IsNotNull()); + CPPUNIT_ASSERT_MESSAGE(" ..Testing number of tools in storage",readStorage->GetToolCount()==2); + } + + void TestReadNotExistingStorage() + { + CPPUNIT_ASSERT_THROW_MESSAGE("Testing if exception is thrown if a non existing storage is given for deserialization.", + m_Deserializer->Deserialize("noStorage.tfl"), + mitk::IGTException); + } + + void TestReadStorageWithUnknownFiletype() + { + std::string testDataPath = GetTestDataFilePath("IGT-Data/ClaronTool.stl"); + + CPPUNIT_ASSERT_THROW_MESSAGE("Testing if exception is thrown if a wrong file type is given for deserialization.", + m_Deserializer->Deserialize(testDataPath), + mitk::IGTException); + } + + void TestReadZipFileWithNoToolstorage() + { + std::string testDataPath = GetTestDataFilePath("IGT-Data/Empty.zip"); + + CPPUNIT_ASSERT_THROW_MESSAGE("Testing if exception is thrown if a empty zip file is given for deserialization.", + m_Deserializer->Deserialize(testDataPath), + mitk::IGTException); + } + + //new tests for exception throwing of NavigationToolStorageDeserializer + void TestDeserializerForExceptions() + { + CPPUNIT_ASSERT_THROW_MESSAGE("Testing deserializer with invalid filename.", + m_Deserializer->Deserialize("InvalidName"), + mitk::IGTException); + + std::string testDataPath = GetTestDataFilePath("IGT-Data/Empty2.zip"); + CPPUNIT_ASSERT_THROW_MESSAGE("Testing deserializer with invalid filename.", + m_Deserializer->Deserialize(testDataPath), + mitk::IGTException); + } + + +}; +MITK_TEST_SUITE_REGISTRATION(mitkNavigationToolStorageDeserializer) diff --git a/Modules/IGT/Testing/mitkNavigationToolStorageSerializerAndDeserializerIntegrationTest.cpp b/Modules/IGT/Testing/mitkNavigationToolStorageSerializerAndDeserializerIntegrationTest.cpp new file mode 100644 index 0000000000..1c0db0d483 --- /dev/null +++ b/Modules/IGT/Testing/mitkNavigationToolStorageSerializerAndDeserializerIntegrationTest.cpp @@ -0,0 +1,137 @@ +/*=================================================================== + +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() + { + m_Serializer = mitk::NavigationToolStorageSerializer::New(); + mitk::DataStorage::Pointer DataStorage = dynamic_cast(mitk::StandaloneDataStorage::New().GetPointer()); //needed for deserializer! + m_Deserializer = mitk::NavigationToolStorageDeserializer::New(DataStorage); + try + { + m_FileName1 = mitk::IOUtil::CreateTemporaryFile(); + } + catch (std::exception& e) + { + MITK_ERROR << "File access Exception: " << e.what(); + MITK_ERROR <<"Could not create filename during setUp() method."; + } + } + + void tearDown() + { + m_Serializer = NULL; + m_Deserializer = NULL; + 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::CreateTestData_StorageWithOneTool(); + + //test serialization + bool success = m_Serializer->Serialize(m_FileName1,storage); + CPPUNIT_ASSERT_MESSAGE("Testing serialization of tool storage with tool registrations",success); + + //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)->GetToolRegistrationLandmarks(); + mitk::PointSet::Pointer readCalLandmarks = readStorage->GetTool(0)->GetToolCalibrationLandmarks(); + + 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)->GetToolTipOrientation(); + + 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/mitkNavigationToolStorageSerializerAndDeserializerTest.cpp b/Modules/IGT/Testing/mitkNavigationToolStorageSerializerAndDeserializerTest.cpp deleted file mode 100644 index 7a6bc032d5..0000000000 --- a/Modules/IGT/Testing/mitkNavigationToolStorageSerializerAndDeserializerTest.cpp +++ /dev/null @@ -1,468 +0,0 @@ -/*=================================================================== - -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 "mitkTestingConfig.h" - -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -//POCO -#include -#include -#include - -static void TestInstantiationSerializer() -{ - // let's create objects of our classes - mitk::NavigationToolStorageSerializer::Pointer testSerializer = mitk::NavigationToolStorageSerializer::New(); - MITK_TEST_CONDITION_REQUIRED(testSerializer.IsNotNull(),"Testing instantiation of NavigationToolStorageSerializer"); -} - -static void TestInstantiationDeserializer() -{ - mitk::DataStorage::Pointer tempStorage = dynamic_cast(mitk::StandaloneDataStorage::New().GetPointer()); //needed for deserializer! - mitk::NavigationToolStorageDeserializer::Pointer testDeserializer = mitk::NavigationToolStorageDeserializer::New(tempStorage); - MITK_TEST_CONDITION_REQUIRED(testDeserializer.IsNotNull(),"Testing instantiation of NavigationToolStorageDeserializer") -} - -static void TestWriteSimpleToolStorage() -{ - //create Tool Storage - mitk::NavigationToolStorage::Pointer myStorage = mitk::NavigationToolStorage::New(); - //first tool - mitk::NavigationTool::Pointer myTool1 = mitk::NavigationTool::New(); - myTool1->SetIdentifier("001"); - myStorage->AddTool(myTool1); - //second tool - mitk::NavigationTool::Pointer myTool2 = mitk::NavigationTool::New(); - myTool2->SetIdentifier("002"); - myStorage->AddTool(myTool2); - //third tool - mitk::NavigationTool::Pointer myTool3 = mitk::NavigationTool::New(); - myTool3->SetIdentifier("003"); - myStorage->AddTool(myTool3); - - //create Serializer - mitk::NavigationToolStorageSerializer::Pointer mySerializer = mitk::NavigationToolStorageSerializer::New(); - - //create filename - std::string filename = std::string( MITK_TEST_OUTPUT_DIR )+Poco::Path::separator()+"TestStorage.storage"; - - //test serialization - bool success = mySerializer->Serialize(filename,myStorage); - MITK_TEST_CONDITION_REQUIRED(success,"Testing serialization of simple tool storage"); -} - -static void TestWriteAndReadSimpleToolStorageWithToolLandmarks() -{ - //create Tool Storage - mitk::NavigationToolStorage::Pointer myStorage = mitk::NavigationToolStorage::New(); - - //first tool - mitk::NavigationTool::Pointer myTool1 = mitk::NavigationTool::New(); - myTool1->SetIdentifier("001"); - mitk::PointSet::Pointer CalLandmarks1 = mitk::PointSet::New(); - mitk::Point3D testPt1; - mitk::FillVector3D(testPt1,1,2,3); - CalLandmarks1->SetPoint(0,testPt1); - mitk::PointSet::Pointer RegLandmarks1 = mitk::PointSet::New(); - mitk::Point3D testPt2; - mitk::FillVector3D(testPt2,4,5,6); - RegLandmarks1->SetPoint(5,testPt2); - myTool1->SetToolCalibrationLandmarks(CalLandmarks1); - myTool1->SetToolRegistrationLandmarks(RegLandmarks1); - mitk::Point3D toolTipPos; - mitk::FillVector3D(toolTipPos,1.3423,2.323,4.332); - mitk::Quaternion toolTipRot = mitk::Quaternion(0.1,0.2,0.3,0.4); - myTool1->SetToolTipPosition(toolTipPos); - myTool1->SetToolTipOrientation(toolTipRot); - myStorage->AddTool(myTool1); - - //create Serializer - mitk::NavigationToolStorageSerializer::Pointer mySerializer = mitk::NavigationToolStorageSerializer::New(); - - //create filename - std::string filename = std::string( MITK_TEST_OUTPUT_DIR )+Poco::Path::separator()+"TestStorageToolReg.storage"; - - //test serialization - bool success = mySerializer->Serialize(filename,myStorage); - MITK_TEST_CONDITION_REQUIRED(success,"Testing serialization of tool storage with tool registrations"); - - mitk::DataStorage::Pointer tempStorage = dynamic_cast(mitk::StandaloneDataStorage::New().GetPointer()); //needed for deserializer! - mitk::NavigationToolStorageDeserializer::Pointer myDeserializer = mitk::NavigationToolStorageDeserializer::New(tempStorage); - mitk::NavigationToolStorage::Pointer readStorage = myDeserializer->Deserialize(std::string( MITK_TEST_OUTPUT_DIR )+Poco::Path::separator()+"TestStorageToolReg.storage"); - MITK_TEST_CONDITION_REQUIRED(readStorage.IsNotNull(),"Testing deserialization of tool storage with tool registrations"); - MITK_TEST_CONDITION_REQUIRED(readStorage->GetToolCount()==1," ..Testing number of tools in storage"); - - mitk::PointSet::Pointer readRegLandmarks = readStorage->GetTool(0)->GetToolRegistrationLandmarks(); - mitk::PointSet::Pointer readCalLandmarks = readStorage->GetTool(0)->GetToolCalibrationLandmarks(); - - int val1 = readRegLandmarks->GetPoint(5)[0]; - int val2 = readRegLandmarks->GetPoint(5)[1]; - int val3 = readRegLandmarks->GetPoint(5)[2]; - std::cout << val1 << val2 << val3 << std::endl; - MITK_TEST_CONDITION_REQUIRED(((readRegLandmarks->GetPoint(5)[0] == 4)&&(readRegLandmarks->GetPoint(5)[1] == 5)&&(readRegLandmarks->GetPoint(5)[2] == 6)),"..Testing if tool registration landmarks have been stored and loaded correctly."); - MITK_TEST_CONDITION_REQUIRED(((readCalLandmarks->GetPoint(0)[0] == 1)&&(readCalLandmarks->GetPoint(0)[1] == 2)&&(readCalLandmarks->GetPoint(0)[2] == 3)),"..Testing if tool calibration landmarks have been stored and loaded correctly."); - - mitk::Point3D readToolTipPos = readStorage->GetTool(0)->GetToolTipPosition(); - mitk::Quaternion readToolTipRot = readStorage->GetTool(0)->GetToolTipOrientation(); - - MITK_TEST_CONDITION_REQUIRED(((float(readToolTipPos[0]) == float(1.3423))&& - (float(readToolTipPos[1]) == float(2.323))&& - (float(readToolTipPos[2]) == float(4.332))), - "..Testing if tool tip position has been stored and loaded correctly."); - - MITK_TEST_CONDITION_REQUIRED(((float(readToolTipRot.x()) == float(0.1))&& - (float(readToolTipRot.y()) == float(0.2))&& - (float(readToolTipRot.z()) == float(0.3))&& - (float(readToolTipRot.r()) == float(0.4))), - "..Testing if tool tip orientation has been stored and loaded correctly."); -} - -static void TestReadSimpleToolStorage() -{ - mitk::DataStorage::Pointer tempStorage = dynamic_cast(mitk::StandaloneDataStorage::New().GetPointer()); //needed for deserializer! - mitk::NavigationToolStorageDeserializer::Pointer myDeserializer = mitk::NavigationToolStorageDeserializer::New(tempStorage); - mitk::NavigationToolStorage::Pointer readStorage = myDeserializer->Deserialize(std::string( MITK_TEST_OUTPUT_DIR )+Poco::Path::separator()+"TestStorage.storage"); - MITK_TEST_CONDITION_REQUIRED(readStorage.IsNotNull(),"Testing deserialization of simple tool storage"); - MITK_TEST_CONDITION_REQUIRED(readStorage->GetToolCount()==3," ..Testing number of tools in storage"); - //TODO: why is the order of tools changed is save/load process?? - bool foundtool1 = false; - bool foundtool2 = false; - bool foundtool3 = false; - for(int i=0; i<3; i++) - { - if ((readStorage->GetTool(i)->GetIdentifier()=="001")) foundtool1 = true; - else if ((readStorage->GetTool(i)->GetIdentifier()=="002")) foundtool2 = true; - else if ((readStorage->GetTool(i)->GetIdentifier()=="003")) foundtool3 = true; - } - MITK_TEST_CONDITION_REQUIRED(foundtool1&&foundtool2&&foundtool3," ..Testing if identifiers of tools where saved / loaded successfully"); -} - -static void CleanUp() -{ - try - { - std::remove((std::string( MITK_TEST_OUTPUT_DIR )+Poco::Path::separator()+"TestStorage.storage").c_str()); - std::remove((std::string( MITK_TEST_OUTPUT_DIR )+Poco::Path::separator()+"TestStorageToolReg.storage").c_str()); - std::remove((std::string( MITK_TEST_OUTPUT_DIR )+Poco::Path::separator()+"TestStorage2.storage").c_str()); - } - catch(...) - { - MITK_INFO << "Warning: Error occured when deleting test file!"; - } -} - -static void TestWriteComplexToolStorage() -{ - //create first tool - mitk::Surface::Pointer testSurface; - std::string toolFileName(MITK_IGT_DATA_DIR); - toolFileName.append("/ClaronTool"); - mitk::NavigationTool::Pointer myNavigationTool = mitk::NavigationTool::New(); - myNavigationTool->SetCalibrationFile(toolFileName); - - mitk::DataNode::Pointer myNode = mitk::DataNode::New(); - myNode->SetName("ClaronTool"); - - //load an stl File - std::string pathToClaronTool(MITK_IGT_DATA_DIR); - pathToClaronTool.append("/ClaronTool.stl"); - testSurface = mitk::IOUtil::LoadSurface(pathToClaronTool); - myNode->SetData(testSurface); - - myNavigationTool->SetDataNode(myNode); - myNavigationTool->SetIdentifier("ClaronTool#1"); - myNavigationTool->SetSerialNumber("0815"); - myNavigationTool->SetTrackingDeviceType(mitk::ClaronMicron); - myNavigationTool->SetType(mitk::NavigationTool::Fiducial); - - //create second tool - mitk::NavigationTool::Pointer myNavigationTool2 = mitk::NavigationTool::New(); - mitk::Surface::Pointer testSurface2; - - mitk::DataNode::Pointer myNode2 = mitk::DataNode::New(); - myNode2->SetName("AuroraTool"); - - //load an stl File - std::string pathToEMTool(MITK_IGT_DATA_DIR); - pathToEMTool.append("/EMTool.stl"); - testSurface2 = mitk::IOUtil::LoadSurface(pathToEMTool); - myNode2->SetData(testSurface2); - - myNavigationTool2->SetDataNode(myNode2); - myNavigationTool2->SetIdentifier("AuroraTool#1"); - myNavigationTool2->SetSerialNumber("0816"); - myNavigationTool2->SetTrackingDeviceType(mitk::NDIAurora); - myNavigationTool2->SetType(mitk::NavigationTool::Instrument); - - //create navigation tool storage - mitk::NavigationToolStorage::Pointer myStorage = mitk::NavigationToolStorage::New(); - myStorage->AddTool(myNavigationTool); - myStorage->AddTool(myNavigationTool2); - - //create Serializer - mitk::NavigationToolStorageSerializer::Pointer mySerializer = mitk::NavigationToolStorageSerializer::New(); - - //create filename - std::string filename = std::string( MITK_TEST_OUTPUT_DIR )+Poco::Path::separator()+"TestStorage2.storage"; - - //test serialization - bool success = mySerializer->Serialize(filename,myStorage); - MITK_TEST_CONDITION_REQUIRED(success,"Testing serialization of complex tool storage"); -} - -static void TestReadComplexToolStorage() -{ - mitk::DataStorage::Pointer tempStorage = dynamic_cast(mitk::StandaloneDataStorage::New().GetPointer()); //needed for deserializer! - mitk::NavigationToolStorageDeserializer::Pointer myDeserializer = mitk::NavigationToolStorageDeserializer::New(tempStorage); - mitk::NavigationToolStorage::Pointer readStorage = myDeserializer->Deserialize(std::string( MITK_TEST_OUTPUT_DIR )+Poco::Path::separator()+"TestStorage2.storage"); - MITK_TEST_CONDITION_REQUIRED(readStorage.IsNotNull(),"Testing deserialization of complex tool storage"); - MITK_TEST_CONDITION_REQUIRED(readStorage->GetToolCount()==2," ..Testing number of tools in storage"); -} - -static void TestReadNotExistingStorage() -{ - mitk::DataStorage::Pointer tempStorage = dynamic_cast(mitk::StandaloneDataStorage::New().GetPointer()); //needed for deserializer! - mitk::NavigationToolStorageDeserializer::Pointer myDeserializer = mitk::NavigationToolStorageDeserializer::New(tempStorage); - - bool exceptionThrown = false; - try - { - mitk::NavigationToolStorage::Pointer readStorage = myDeserializer->Deserialize("noStorage.tfl"); - } - catch (mitk::IGTException e) - { - exceptionThrown = true; - } - MITK_TEST_CONDITION_REQUIRED(exceptionThrown,"Testing if exception is thrown if a non existing storage is given for deserialization."); -} - -static void TestReadStorageWithUnknownFiletype() -{ - mitk::DataStorage::Pointer tempStorage = dynamic_cast(mitk::StandaloneDataStorage::New().GetPointer()); //needed for deserializer! - - std::string toolFileName(MITK_IGT_DATA_DIR); - toolFileName.append("/ClaronTool.stl"); - mitk::NavigationToolStorageDeserializer::Pointer myDeserializer = mitk::NavigationToolStorageDeserializer::New(tempStorage); - - bool exceptionThrown = false; - try - { - mitk::NavigationToolStorage::Pointer readStorage = myDeserializer->Deserialize(toolFileName); - } - catch (mitk::IGTException e) - { - exceptionThrown = true; - } - MITK_TEST_CONDITION_REQUIRED(exceptionThrown,"Testing if exception is thrown if a wrong file type is given for deserialization."); -} - - -static void TestReadZipFileWithNoToolstorage() -{ - mitk::DataStorage::Pointer tempStorage = dynamic_cast(mitk::StandaloneDataStorage::New().GetPointer()); //needed for deserializer! - - std::string toolFileName(MITK_IGT_DATA_DIR); - toolFileName.append("/Empty.zip"); - MITK_TEST_CONDITION(toolFileName.empty() == false, "Check if tool calibration of claron tool file exists"); - mitk::NavigationToolStorageDeserializer::Pointer myDeserializer = mitk::NavigationToolStorageDeserializer::New(tempStorage); - - bool exceptionThrown = false; - try - { - mitk::NavigationToolStorage::Pointer readStorage = myDeserializer->Deserialize(toolFileName); - } - catch (mitk::IGTException e) - { - exceptionThrown = true; - } - catch (std::exception& e) - { - MITK_ERROR << "Unexpected exception catched: " << e.what() << " / filename: " << toolFileName; - } - MITK_TEST_CONDITION_REQUIRED(exceptionThrown,"Testing if exception is thrown if a empty zip file is given for deserialization."); -} - - -static void TestWriteStorageToInvalidFile() -{ - //create Tool Storage - mitk::NavigationToolStorage::Pointer myStorage = mitk::NavigationToolStorage::New(); - //first tool - mitk::NavigationTool::Pointer myTool1 = mitk::NavigationTool::New(); - myTool1->SetIdentifier("001"); - myStorage->AddTool(myTool1); - //second tool - mitk::NavigationTool::Pointer myTool2 = mitk::NavigationTool::New(); - myTool2->SetIdentifier("002"); - myStorage->AddTool(myTool2); - //third tool - mitk::NavigationTool::Pointer myTool3 = mitk::NavigationTool::New(); - myTool3->SetIdentifier("003"); - myStorage->AddTool(myTool3); - - //create Serializer - mitk::NavigationToolStorageSerializer::Pointer mySerializer = mitk::NavigationToolStorageSerializer::New(); - - //create 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 - bool exceptionThrown = false; - try - { - mySerializer->Serialize(filename,myStorage); - } - catch(mitk::IGTException e) - { - exceptionThrown = true; - } - MITK_TEST_CONDITION_REQUIRED(exceptionThrown,"Testing if an exception is thrown if an invalid file is used."); -} - -static void TestWriteEmptyToolStorage() -{ - //create Tool Storage - mitk::NavigationToolStorage::Pointer myStorage = mitk::NavigationToolStorage::New(); - - //create Serializer - mitk::NavigationToolStorageSerializer::Pointer mySerializer = mitk::NavigationToolStorageSerializer::New(); - - std::string filename; - try - { - //create filename - filename = mitk::IOUtil::CreateTemporaryFile(); - } - catch (std::exception& e) { - MITK_ERROR << "File access Exception: " << e.what(); - MITK_TEST_FAILED_MSG(<<"Could not create filename for Exceptiontest"); - } - - //test serialization - bool success = mySerializer->Serialize(filename,myStorage); - MITK_TEST_CONDITION_REQUIRED(success,"Testing serialization of simple tool storage"); - - //clean up - Poco::File file = Poco::File(filename); - file.remove(); - -} - -//new tests for exception throwing of NavigationToolStorageSerializer -static void TestSerializerForExceptions() -{ - mitk::NavigationToolStorageSerializer::Pointer testSerializer = mitk::NavigationToolStorageSerializer::New(); - 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 - bool ExceptionThrown = false; - try - { - testSerializer->Serialize(filename,myStorage); - } - catch(mitk::IGTException) - { - ExceptionThrown = true; - } - MITK_TEST_CONDITION_REQUIRED(ExceptionThrown, "Testing serializer with invalid filename."); -} - -//new tests for exception throwing of NavigationToolStorageDeserializer -static void TestDeserializerForExceptions() -{ - // Desearializing file with invalid name - mitk::DataStorage::Pointer tempStorage = dynamic_cast(mitk::StandaloneDataStorage::New().GetPointer()); - mitk::NavigationToolStorageDeserializer::Pointer testDeseralizer= mitk::NavigationToolStorageDeserializer::New(tempStorage); - bool ExceptionThrown1 = false; - try - { - mitk::NavigationToolStorage::Pointer readStorage = testDeseralizer->Deserialize("InvalidName"); - } - catch(mitk::IGTException) - { - ExceptionThrown1 = true; - } - MITK_TEST_CONDITION_REQUIRED(ExceptionThrown1, "Testing deserializer with invalid filename."); - - bool ExceptionThrown2 = false; - - // Deserializing of empty zip file - mitk::NavigationToolStorageDeserializer::Pointer testDeseralizer2= mitk::NavigationToolStorageDeserializer::New(tempStorage); - try - { - std::string filename(MITK_IGT_DATA_DIR); - filename.append("/Empty2.zip"); - mitk::NavigationToolStorage::Pointer readStorage = testDeseralizer2->Deserialize(filename); - } - catch(mitk::IGTException) - { - ExceptionThrown2 = true; - } - MITK_TEST_CONDITION_REQUIRED(ExceptionThrown2, "Testing deserializer method with empty zip file."); -} - -/** This function is testing the TrackingVolume class. */ -int mitkNavigationToolStorageSerializerAndDeserializerTest(int /* argc */, char* /*argv*/[]) -{ - MITK_TEST_BEGIN("NavigationToolStorageSerializerAndDeserializer"); - try{ - TestInstantiationSerializer(); - TestInstantiationDeserializer(); - TestWriteSimpleToolStorage(); - TestWriteAndReadSimpleToolStorageWithToolLandmarks(); - TestReadSimpleToolStorage(); - TestWriteComplexToolStorage(); - TestReadComplexToolStorage(); - TestReadNotExistingStorage(); - TestReadStorageWithUnknownFiletype(); - TestReadZipFileWithNoToolstorage(); - TestWriteStorageToInvalidFile(); - TestWriteEmptyToolStorage(); - TestSerializerForExceptions(); - TestDeserializerForExceptions(); - } - catch (std::exception& e) { - MITK_ERROR << "exception:" << e.what(); - MITK_TEST_FAILED_MSG(<<"Exception occured, test failed!"); - } - catch (...) { - MITK_ERROR << "Unknown Exception?"; - MITK_TEST_FAILED_MSG(<<"Exception occured, test failed!"); - } - - CleanUp(); - - MITK_TEST_END(); -} diff --git a/Modules/IGT/Testing/mitkNavigationToolStorageSerializerTest.cpp b/Modules/IGT/Testing/mitkNavigationToolStorageSerializerTest.cpp new file mode 100644 index 0000000000..de4b8071ba --- /dev/null +++ b/Modules/IGT/Testing/mitkNavigationToolStorageSerializerTest.cpp @@ -0,0 +1,137 @@ +/*=================================================================== + +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() + { + try { + m_FileName1 = mitk::IOUtil::CreateTemporaryFile(); + } + 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() + { + m_Serializer = NULL; + 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 TestWriteSimpleToolStorage() + { + //create Tool Storage + mitk::NavigationToolStorage::Pointer myStorage = mitk::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::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::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) diff --git a/Modules/IGT/Testing/mitkNavigationToolStorageTestHelper.h b/Modules/IGT/Testing/mitkNavigationToolStorageTestHelper.h new file mode 100644 index 0000000000..eae9148d26 --- /dev/null +++ b/Modules/IGT/Testing/mitkNavigationToolStorageTestHelper.h @@ -0,0 +1,109 @@ +/*=================================================================== + +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 + +namespace mitk { + //help methods for test tool storages + static mitk::NavigationToolStorage::Pointer CreateTestData_SimpleStorage() + { + //create Tool Storage + mitk::NavigationToolStorage::Pointer myStorage = mitk::NavigationToolStorage::New(); + //first tool + mitk::NavigationTool::Pointer myTool1 = mitk::NavigationTool::New(); + myTool1->SetIdentifier("001"); + myStorage->AddTool(myTool1); + //second tool + mitk::NavigationTool::Pointer myTool2 = mitk::NavigationTool::New(); + myTool2->SetIdentifier("002"); + myStorage->AddTool(myTool2); + //third tool + mitk::NavigationTool::Pointer myTool3 = mitk::NavigationTool::New(); + myTool3->SetIdentifier("003"); + myStorage->AddTool(myTool3); + + return myStorage; + }; + + static mitk::NavigationToolStorage::Pointer CreateTestData_StorageWithOneTool() + { + //create Tool Storage + mitk::NavigationToolStorage::Pointer myStorage = mitk::NavigationToolStorage::New(); + + //first tool + mitk::NavigationTool::Pointer myTool1 = mitk::NavigationTool::New(); + myTool1->SetIdentifier("001"); + mitk::PointSet::Pointer CalLandmarks1 = mitk::PointSet::New(); + mitk::Point3D testPt1; + mitk::FillVector3D(testPt1,1,2,3); + CalLandmarks1->SetPoint(0,testPt1); + mitk::PointSet::Pointer RegLandmarks1 = mitk::PointSet::New(); + mitk::Point3D testPt2; + mitk::FillVector3D(testPt2,4,5,6); + RegLandmarks1->SetPoint(5,testPt2); + myTool1->SetToolCalibrationLandmarks(CalLandmarks1); + myTool1->SetToolRegistrationLandmarks(RegLandmarks1); + mitk::Point3D toolTipPos; + mitk::FillVector3D(toolTipPos,1.3423,2.323,4.332); + mitk::Quaternion toolTipRot = mitk::Quaternion(0.1,0.2,0.3,0.4); + myTool1->SetToolTipPosition(toolTipPos); + myTool1->SetToolTipOrientation(toolTipRot); + myStorage->AddTool(myTool1); + + return myStorage; + } + + static mitk::NavigationToolStorage::Pointer CreateTestData_ComplexStorage(std::string toolFilePath, std::string toolSurfacePath1, std::string toolSurfacePath2) + { + //create first tool + mitk::NavigationTool::Pointer myNavigationTool = mitk::NavigationTool::New(); + myNavigationTool->SetCalibrationFile(toolFilePath); + mitk::DataNode::Pointer myNode = mitk::DataNode::New(); + myNode->SetName("ClaronTool"); + myNode->SetData(mitk::IOUtil::LoadSurface(toolSurfacePath1)); //load an stl File + myNavigationTool->SetDataNode(myNode); + myNavigationTool->SetIdentifier("ClaronTool#1"); + myNavigationTool->SetSerialNumber("0815"); + myNavigationTool->SetTrackingDeviceType(mitk::ClaronMicron); + myNavigationTool->SetType(mitk::NavigationTool::Fiducial); + + //create second tool + mitk::NavigationTool::Pointer myNavigationTool2 = mitk::NavigationTool::New(); + mitk::Surface::Pointer testSurface2; + + mitk::DataNode::Pointer myNode2 = mitk::DataNode::New(); + myNode2->SetName("AuroraTool"); + + //load an stl File + testSurface2 = mitk::IOUtil::LoadSurface(toolSurfacePath2); + myNode2->SetData(testSurface2); + + myNavigationTool2->SetDataNode(myNode2); + myNavigationTool2->SetIdentifier("AuroraTool#1"); + myNavigationTool2->SetSerialNumber("0816"); + myNavigationTool2->SetTrackingDeviceType(mitk::NDIAurora); + myNavigationTool2->SetType(mitk::NavigationTool::Instrument); + + //create navigation tool storage + mitk::NavigationToolStorage::Pointer myStorage = mitk::NavigationToolStorage::New(); + myStorage->AddTool(myNavigationTool); + myStorage->AddTool(myNavigationTool2); + + return myStorage; + }; +}