MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
mitkNavigationDataSetReaderWriterXMLTest.cpp
Go to the documentation of this file.
1/*============================================================================
2
3The Medical Imaging Interaction Toolkit (MITK)
4
5Copyright (c) German Cancer Research Center (DKFZ)
6All rights reserved.
7
8Use of this source code is governed by a 3-clause BSD license that can be
9found in the LICENSE file.
10
11============================================================================*/
12
13//testing headers
14//#include <mitkTestingMacros.h>
15#include <mitkTestFixture.h>
16
19#include <mitkNavigationData.h>
20#include <mitkStandardFileLocations.h>
21#include <mitkTestingMacros.h>
22#include <mitkIOUtil.h>
23
24#include <Poco/Path.h>
25#include <Poco/File.h>
26
27#include <iostream>
28#include <sstream>
29#include <fstream>
30
31//for exceptions
32#include "mitkIGTException.h"
33#include "mitkIGTIOException.h"
34
35class mitkNavigationDataSetReaderWriterXMLTestSuite : public mitk::TestFixture
36{
38 MITK_TEST(TestCompareFunction);
39 MITK_TEST(TestReadWrite);
41 CPPUNIT_TEST_SUITE_END();
42
43private:
44
45 std::string pathRead;
46 std::string pathWrite;
47 std::string pathWrong;
48 mitk::NavigationDataSet::Pointer set;
49
50public:
51
52 void setUp() override
53 {
54 pathRead = GetTestDataFilePath("IGT-Data/RecordedNavigationData.xml");
55
56 pathWrite = pathRead;
57 pathWrite.insert(pathWrite.end()-4,'2');;//Insert 2: IGT-Data/NavigationDataSet2.xml
58 std::ifstream FileTest(pathWrite.c_str());
59 if(FileTest){
60 //remove file if it already exists. TODO: Loeschen funktioniert nicht!!!! xxxxxxxxxxxxxxxx
61 FileTest.close();
62 std::remove(pathWrite.c_str());
63 }
64
65 pathWrong = GetTestDataFilePath("IGT-Data/NavigationDataTestData.xml");
66 }
67
68 void tearDown() override
69 {
70 }
71
73 {
74 // Aim is to read an xml into a pointset, write that xml again, and compare the output
75
76 set = dynamic_cast<mitk::NavigationDataSet*> (mitk::IOUtil::Load(pathRead)[0].GetPointer());
77 mitk::IOUtil::Save(set, pathWrite);
78
79 //FIXME: Commented out, because test fails under linux. binary comparison of files is probably not the wa to go
80 // See Bug 17775
81 //CPPUNIT_ASSERT_MESSAGE( "Testing if read/write cycle creates identical files", CompareFiles(pathRead, pathWrite));
82 remove(pathWrite.c_str());
83 }
84
85 bool CompareFiles(std::string file1, std::string file2)
86 {
87 FILE* f1 = fopen (file1.c_str() , "r");
88 FILE* f2 = fopen (file2.c_str() , "r");
89 char buf1[10000];
90 char buf2[10000];
91
92 do {
93 size_t r1 = fread(buf1, 1, 10000, f1);
94 size_t r2 = fread(buf2, 1, 10000, f2);
95
96 if (r1 != r2 ||
97 memcmp(buf1, buf2, r1)) {
98 fclose(f1);
99 fclose(f2);
100 return false; // Files are not equal
101 }
102 } while (!feof(f1) && !feof(f2));
103 bool returnValue = feof(f1) && feof(f2);
104 fclose(f1);
105 fclose(f2);
106 return returnValue;
107 }
108
110 {
111 bool exceptionThrown3=false;
112 try
113 {
114 std::string file = GetTestDataFilePath("IGT-Data/InvalidVersionNavigationDataTestData.xml");
115 mitk::NavigationDataSet::Pointer dataset = dynamic_cast<mitk::NavigationDataSet*> (mitk::IOUtil::Load(file)[0].GetPointer());
116 }
117 catch(mitk::Exception&)
118 {
119 exceptionThrown3=true;
120 }
121 MITK_TEST_CONDITION(exceptionThrown3, "Reading an invalid XML string and expecting a exception");
122 }
123
125 {
126 CPPUNIT_ASSERT_MESSAGE("Asserting that compare function for files works correctly - Positive Test", CompareFiles(pathRead,
127 pathRead));
128 CPPUNIT_ASSERT_MESSAGE("Asserting that compare function for files works correctly - Negative Test", ! CompareFiles(pathRead,
129 pathWrong) );
130 }
131};
132MITK_TEST_SUITE_REGISTRATION(mitkNavigationDataSetReaderWriterXML)
Data structure which stores streams of mitk::NavigationData for multiple tools.