Medical Imaging Interaction Toolkit  2018.4.99-6aa36ba9
Medical Imaging Interaction Toolkit
mitkIOUtilTest.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "mitkTestingMacros.h"
14 #include <mitkTestFixture.h>
15 #include <mitkTestingConfig.h>
16 
17 #include <mitkIOUtil.h>
18 #include <mitkImageGenerator.h>
19 
20 #include <itksys/SystemTools.hxx>
21 
22 class mitkIOUtilTestSuite : public mitk::TestFixture
23 {
24  CPPUNIT_TEST_SUITE(mitkIOUtilTestSuite);
25  MITK_TEST(TestTempMethods);
26  MITK_TEST(TestSaveEmptyData);
27  MITK_TEST(TestLoadAndSaveImage);
28  MITK_TEST(TestNullLoad);
29  MITK_TEST(TestNullSave);
30  MITK_TEST(TestLoadAndSavePointSet);
31  MITK_TEST(TestLoadAndSaveSurface);
32  MITK_TEST(TestTempMethodsForUniqueFilenames);
33  MITK_TEST(TestTempMethodsForUniqueFilenames);
34  CPPUNIT_TEST_SUITE_END();
35 
36 private:
37  std::string m_ImagePath;
38  std::string m_SurfacePath;
39  std::string m_PointSetPath;
40 
41 public:
42  void setUp() override
43  {
44  m_ImagePath = GetTestDataFilePath("Pic3D.nrrd");
45  m_SurfacePath = GetTestDataFilePath("binary.stl");
46  m_PointSetPath = GetTestDataFilePath("pointSet.mps");
47  }
48 
49  void TestSaveEmptyData()
50  {
52  CPPUNIT_ASSERT_THROW(mitk::IOUtil::Save(data, "/tmp/dummy"), mitk::Exception);
53  }
54 
55  void TestTempMethods()
56  {
57  std::string tmpPath = mitk::IOUtil::GetTempPath();
58  CPPUNIT_ASSERT(!tmpPath.empty());
59 
60  std::ofstream tmpFile;
61  std::string tmpFilePath = mitk::IOUtil::CreateTemporaryFile(tmpFile);
62  CPPUNIT_ASSERT(tmpFile && tmpFile.is_open());
63  CPPUNIT_ASSERT(tmpFilePath.size() > tmpPath.size());
64  CPPUNIT_ASSERT(tmpFilePath.substr(0, tmpPath.size()) == tmpPath);
65 
66  tmpFile.close();
67  CPPUNIT_ASSERT(std::remove(tmpFilePath.c_str()) == 0);
68 
69  std::string programPath = mitk::IOUtil::GetProgramPath();
70  CPPUNIT_ASSERT(!programPath.empty());
71  std::ofstream tmpFile2;
72  std::string tmpFilePath2 = mitk::IOUtil::CreateTemporaryFile(tmpFile2, "my-XXXXXX", programPath);
73  CPPUNIT_ASSERT(tmpFile2 && tmpFile2.is_open());
74  CPPUNIT_ASSERT(tmpFilePath2.size() > programPath.size());
75  CPPUNIT_ASSERT(tmpFilePath2.substr(0, programPath.size()) == programPath);
76  tmpFile2.close();
77  CPPUNIT_ASSERT(std::remove(tmpFilePath2.c_str()) == 0);
78 
79  std::ofstream tmpFile3;
80  std::string tmpFilePath3 =
81  mitk::IOUtil::CreateTemporaryFile(tmpFile3, std::ios_base::binary, "my-XXXXXX.TXT", programPath);
82  CPPUNIT_ASSERT(tmpFile3 && tmpFile3.is_open());
83  CPPUNIT_ASSERT(tmpFilePath3.size() > programPath.size());
84  CPPUNIT_ASSERT(tmpFilePath3.substr(0, programPath.size()) == programPath);
85  CPPUNIT_ASSERT(tmpFilePath3.substr(tmpFilePath3.size() - 13, 3) == "my-");
86  CPPUNIT_ASSERT(tmpFilePath3.substr(tmpFilePath3.size() - 4) == ".TXT");
87  tmpFile3.close();
88  // CPPUNIT_ASSERT(std::remove(tmpFilePath3.c_str()) == 0)
89 
90  std::string tmpFilePath4 = mitk::IOUtil::CreateTemporaryFile();
91  std::ofstream file;
92  file.open(tmpFilePath4.c_str());
93  CPPUNIT_ASSERT_MESSAGE("Testing if file exists after CreateTemporaryFile()", file.is_open());
94 
95  CPPUNIT_ASSERT_THROW(mitk::IOUtil::CreateTemporaryFile(tmpFile2, "XX"), mitk::Exception);
96 
97  std::string tmpDir = mitk::IOUtil::CreateTemporaryDirectory();
98  CPPUNIT_ASSERT(tmpDir.size() > tmpPath.size());
99  CPPUNIT_ASSERT(tmpDir.substr(0, tmpPath.size()) == tmpPath);
100  CPPUNIT_ASSERT(itksys::SystemTools::RemoveADirectory(tmpDir.c_str()));
101 
102  std::string tmpDir2 = mitk::IOUtil::CreateTemporaryDirectory("my-XXXXXX", programPath);
103  CPPUNIT_ASSERT(tmpDir2.size() > programPath.size());
104  CPPUNIT_ASSERT(tmpDir2.substr(0, programPath.size()) == programPath);
105  CPPUNIT_ASSERT(itksys::SystemTools::RemoveADirectory(tmpDir2.c_str()));
106  }
107 
108  void TestTempMethodsForUniqueFilenames()
109  {
110  int numberOfFiles = 100;
111 
112  // create 100 empty files
113  std::vector<std::string> v100filenames;
114  for (int i = 0; i < numberOfFiles; i++)
115  {
116  v100filenames.push_back(mitk::IOUtil::CreateTemporaryFile());
117  }
118 
119  // check if all of them are unique
120  for (int i = 0; i < numberOfFiles; i++)
121  for (int j = 0; j < numberOfFiles; j++)
122  {
123  if (i != j)
124  {
125  std::stringstream message;
126  message << "Checking if file " << i << " and file " << j
127  << " are different, which should be the case because each of them should be unique.";
128  CPPUNIT_ASSERT_MESSAGE(message.str(), (v100filenames.at(i) != v100filenames.at(j)));
129  }
130  }
131 
132  // delete all the files / clean up
133  for (int i = 0; i < numberOfFiles; i++)
134  {
135  std::remove(v100filenames.at(i).c_str());
136  }
137  }
138 
139  void TestLoadAndSaveImage()
140  {
141  mitk::Image::Pointer img1 = mitk::IOUtil::Load<mitk::Image>(m_ImagePath);
142  CPPUNIT_ASSERT(img1.IsNotNull());
143 
144  std::ofstream tmpStream;
145  std::string imagePath = mitk::IOUtil::CreateTemporaryFile(tmpStream, "diffpic3d-XXXXXX.nrrd");
146  tmpStream.close();
147  std::string imagePath2 = mitk::IOUtil::CreateTemporaryFile(tmpStream, "diffpic3d-XXXXXX.nii.gz");
148  tmpStream.close();
149 
150  // the cases where no exception should be thrown
151  CPPUNIT_ASSERT_NO_THROW(mitk::IOUtil::Save(img1, imagePath));
152  CPPUNIT_ASSERT_NO_THROW(mitk::IOUtil::Save(img1.GetPointer(), imagePath2));
153 
154  // load data which does not exist
155  CPPUNIT_ASSERT_THROW(mitk::IOUtil::Load("fileWhichDoesNotExist.nrrd"), mitk::Exception);
156 
157  // delete the files after the test is done
158  std::remove(imagePath.c_str());
159  std::remove(imagePath2.c_str());
160 
161  mitk::Image::Pointer relativImage = mitk::ImageGenerator::GenerateGradientImage<float>(4, 4, 4, 1);
162  std::string imagePath3 = mitk::IOUtil::CreateTemporaryFile(tmpStream, "XXXXXX.nrrd");
163  tmpStream.close();
164  mitk::IOUtil::Save(relativImage, imagePath3);
165  CPPUNIT_ASSERT_NO_THROW(mitk::IOUtil::Load(imagePath3));
166  std::remove(imagePath3.c_str());
167  }
168 
172  void TestNullLoad()
173  {
174  CPPUNIT_ASSERT_THROW(mitk::IOUtil::Load(""), mitk::Exception);
175  }
176 
181  void TestNullSave()
182  {
183  CPPUNIT_ASSERT_THROW(mitk::IOUtil::Save(nullptr, mitk::IOUtil::CreateTemporaryFile()), mitk::Exception);
184  CPPUNIT_ASSERT_THROW(mitk::IOUtil::Save(mitk::Image::New().GetPointer(), ""), mitk::Exception);
185  }
186 
187  void TestLoadAndSavePointSet()
188  {
189  mitk::PointSet::Pointer pointset = mitk::IOUtil::Load<mitk::PointSet>(m_PointSetPath);
190  CPPUNIT_ASSERT(pointset.IsNotNull());
191 
192  std::ofstream tmpStream;
193  std::string pointSetPath = mitk::IOUtil::CreateTemporaryFile(tmpStream, "XXXXXX.mps");
194  tmpStream.close();
195  std::string pointSetPathWithDefaultExtension = mitk::IOUtil::CreateTemporaryFile(tmpStream, "XXXXXX.mps");
196  tmpStream.close();
197  std::string pointSetPathWithoutDefaultExtension = mitk::IOUtil::CreateTemporaryFile(tmpStream);
198  tmpStream.close();
199 
200  // the cases where no exception should be thrown
201  CPPUNIT_ASSERT_NO_THROW(mitk::IOUtil::Save(pointset, pointSetPathWithDefaultExtension));
202 
203  // test if defaultextension is inserted if no extension is present
204  CPPUNIT_ASSERT_NO_THROW(mitk::IOUtil::Save(pointset, pointSetPathWithoutDefaultExtension.c_str()));
205 
206  // delete the files after the test is done
207  std::remove(pointSetPath.c_str());
208  std::remove(pointSetPathWithDefaultExtension.c_str());
209  std::remove(pointSetPathWithoutDefaultExtension.c_str());
210  }
211 
212  void TestLoadAndSaveSurface()
213  {
214  mitk::Surface::Pointer surface = mitk::IOUtil::Load<mitk::Surface>(m_SurfacePath);
215  CPPUNIT_ASSERT(surface.IsNotNull());
216 
217  std::ofstream tmpStream;
218  std::string surfacePath = mitk::IOUtil::CreateTemporaryFile(tmpStream, "diffsurface-XXXXXX.stl");
219 
220  // the cases where no exception should be thrown
221  CPPUNIT_ASSERT_NO_THROW(mitk::IOUtil::Save(surface, surfacePath));
222 
223  // test if exception is thrown as expected on unknown extsension
224  CPPUNIT_ASSERT_THROW(mitk::IOUtil::Save(surface, "testSurface.xXx"), mitk::Exception);
225 
226  // delete the files after the test is done
227  std::remove(surfacePath.c_str());
228  }
229 };
230 
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
static std::string GetTempPath()
Definition: mitkIOUtil.cpp:378
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
static std::string GetTestDataFilePath(const std::string &testData)
Get the absolute path for test data.
An object of this class represents an exception of MITK. Please don&#39;t instantiate exceptions manually...
Definition: mitkException.h:45
Test fixture for parameterized tests.
static Pointer New()
static std::string CreateTemporaryDirectory(const std::string &templateName="XXXXXX", std::string path=std::string())
Definition: mitkIOUtil.cpp:458
static std::string GetProgramPath()
Definition: mitkIOUtil.cpp:356
static void Save(const mitk::BaseData *data, const std::string &path, bool setPathProperty=false)
Save a mitk::BaseData instance.
Definition: mitkIOUtil.cpp:774
static std::string CreateTemporaryFile(std::ofstream &tmpStream, const std::string &templateName="XXXXXX", std::string path=std::string())
Definition: mitkIOUtil.cpp:413
static Pointer New()
static DataStorage::SetOfObjects::Pointer Load(const std::string &path, DataStorage &storage, const ReaderOptionsFunctorBase *optionsCallback=nullptr)
Load a file into the given DataStorage.
Definition: mitkIOUtil.cpp:489