Medical Imaging Interaction Toolkit  2016.11.0
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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "mitkTestingMacros.h"
18 #include <mitkTestFixture.h>
19 #include <mitkTestingConfig.h>
20 
21 #include <mitkIOUtil.h>
22 #include <mitkImageGenerator.h>
23 
24 #include <itksys/SystemTools.hxx>
25 
26 class mitkIOUtilTestSuite : public mitk::TestFixture
27 {
28  CPPUNIT_TEST_SUITE(mitkIOUtilTestSuite);
29  MITK_TEST(TestTempMethods);
30  MITK_TEST(TestSaveEmptyData);
31  MITK_TEST(TestLoadAndSaveImage);
32  MITK_TEST(TestNullLoad);
33  MITK_TEST(TestNullSave);
34  MITK_TEST(TestLoadAndSavePointSet);
35  MITK_TEST(TestLoadAndSaveSurface);
36  MITK_TEST(TestTempMethodsForUniqueFilenames);
37  MITK_TEST(TestTempMethodsForUniqueFilenames);
38  CPPUNIT_TEST_SUITE_END();
39 
40 private:
41  std::string m_ImagePath;
42  std::string m_SurfacePath;
43  std::string m_PointSetPath;
44 
45 public:
46  void setUp() override
47  {
48  m_ImagePath = GetTestDataFilePath("Pic3D.nrrd");
49  m_SurfacePath = GetTestDataFilePath("binary.stl");
50  m_PointSetPath = GetTestDataFilePath("pointSet.mps");
51  }
52 
53  void TestSaveEmptyData()
54  {
56  CPPUNIT_ASSERT_THROW(mitk::IOUtil::Save(data, "/tmp/dummy"), mitk::Exception);
57  }
58 
59  void TestTempMethods()
60  {
61  std::string tmpPath = mitk::IOUtil::GetTempPath();
62  CPPUNIT_ASSERT(!tmpPath.empty());
63 
64  std::ofstream tmpFile;
65  std::string tmpFilePath = mitk::IOUtil::CreateTemporaryFile(tmpFile);
66  CPPUNIT_ASSERT(tmpFile && tmpFile.is_open());
67  CPPUNIT_ASSERT(tmpFilePath.size() > tmpPath.size());
68  CPPUNIT_ASSERT(tmpFilePath.substr(0, tmpPath.size()) == tmpPath);
69 
70  tmpFile.close();
71  CPPUNIT_ASSERT(std::remove(tmpFilePath.c_str()) == 0);
72 
73  std::string programPath = mitk::IOUtil::GetProgramPath();
74  CPPUNIT_ASSERT(!programPath.empty());
75  std::ofstream tmpFile2;
76  std::string tmpFilePath2 = mitk::IOUtil::CreateTemporaryFile(tmpFile2, "my-XXXXXX", programPath);
77  CPPUNIT_ASSERT(tmpFile2 && tmpFile2.is_open());
78  CPPUNIT_ASSERT(tmpFilePath2.size() > programPath.size());
79  CPPUNIT_ASSERT(tmpFilePath2.substr(0, programPath.size()) == programPath);
80  tmpFile2.close();
81  CPPUNIT_ASSERT(std::remove(tmpFilePath2.c_str()) == 0);
82 
83  std::ofstream tmpFile3;
84  std::string tmpFilePath3 =
85  mitk::IOUtil::CreateTemporaryFile(tmpFile3, std::ios_base::binary, "my-XXXXXX.TXT", programPath);
86  CPPUNIT_ASSERT(tmpFile3 && tmpFile3.is_open());
87  CPPUNIT_ASSERT(tmpFilePath3.size() > programPath.size());
88  CPPUNIT_ASSERT(tmpFilePath3.substr(0, programPath.size()) == programPath);
89  CPPUNIT_ASSERT(tmpFilePath3.substr(tmpFilePath3.size() - 13, 3) == "my-");
90  CPPUNIT_ASSERT(tmpFilePath3.substr(tmpFilePath3.size() - 4) == ".TXT");
91  tmpFile3.close();
92  // CPPUNIT_ASSERT(std::remove(tmpFilePath3.c_str()) == 0)
93 
94  std::string tmpFilePath4 = mitk::IOUtil::CreateTemporaryFile();
95  std::ofstream file;
96  file.open(tmpFilePath4.c_str());
97  CPPUNIT_ASSERT_MESSAGE("Testing if file exists after CreateTemporaryFile()", file.is_open());
98 
99  CPPUNIT_ASSERT_THROW(mitk::IOUtil::CreateTemporaryFile(tmpFile2, "XX"), mitk::Exception);
100 
101  std::string tmpDir = mitk::IOUtil::CreateTemporaryDirectory();
102  CPPUNIT_ASSERT(tmpDir.size() > tmpPath.size());
103  CPPUNIT_ASSERT(tmpDir.substr(0, tmpPath.size()) == tmpPath);
104  CPPUNIT_ASSERT(itksys::SystemTools::RemoveADirectory(tmpDir.c_str()));
105 
106  std::string tmpDir2 = mitk::IOUtil::CreateTemporaryDirectory("my-XXXXXX", programPath);
107  CPPUNIT_ASSERT(tmpDir2.size() > programPath.size());
108  CPPUNIT_ASSERT(tmpDir2.substr(0, programPath.size()) == programPath);
109  CPPUNIT_ASSERT(itksys::SystemTools::RemoveADirectory(tmpDir2.c_str()));
110  }
111 
112  void TestTempMethodsForUniqueFilenames()
113  {
114  int numberOfFiles = 100;
115 
116  // create 100 empty files
117  std::vector<std::string> v100filenames;
118  for (int i = 0; i < numberOfFiles; i++)
119  {
120  v100filenames.push_back(mitk::IOUtil::CreateTemporaryFile());
121  }
122 
123  // check if all of them are unique
124  for (int i = 0; i < numberOfFiles; i++)
125  for (int j = 0; j < numberOfFiles; j++)
126  {
127  if (i != j)
128  {
129  std::stringstream message;
130  message << "Checking if file " << i << " and file " << j
131  << " are different, which should be the case because each of them should be unique.";
132  CPPUNIT_ASSERT_MESSAGE(message.str(), (v100filenames.at(i) != v100filenames.at(j)));
133  }
134  }
135 
136  // delete all the files / clean up
137  for (int i = 0; i < numberOfFiles; i++)
138  {
139  std::remove(v100filenames.at(i).c_str());
140  }
141  }
142 
143  void TestLoadAndSaveImage()
144  {
145  mitk::Image::Pointer img1 = mitk::IOUtil::LoadImage(m_ImagePath);
146  CPPUNIT_ASSERT(img1.IsNotNull());
147 
148  std::ofstream tmpStream;
149  std::string imagePath = mitk::IOUtil::CreateTemporaryFile(tmpStream, "diffpic3d-XXXXXX.nrrd");
150  tmpStream.close();
151  std::string imagePath2 = mitk::IOUtil::CreateTemporaryFile(tmpStream, "diffpic3d-XXXXXX.nii.gz");
152  tmpStream.close();
153 
154  // the cases where no exception should be thrown
155  CPPUNIT_ASSERT_NO_THROW(mitk::IOUtil::Save(img1, imagePath));
156  CPPUNIT_ASSERT_NO_THROW(mitk::IOUtil::Save(img1.GetPointer(), imagePath2));
157 
158  // load data which does not exist
159  CPPUNIT_ASSERT_THROW(mitk::IOUtil::LoadImage("fileWhichDoesNotExist.nrrd"), mitk::Exception);
160 
161  // delete the files after the test is done
162  std::remove(imagePath.c_str());
163  std::remove(imagePath2.c_str());
164 
165  mitk::Image::Pointer relativImage = mitk::ImageGenerator::GenerateGradientImage<float>(4, 4, 4, 1);
166  std::string imagePath3 = mitk::IOUtil::CreateTemporaryFile(tmpStream, "XXXXXX.nrrd");
167  tmpStream.close();
168  mitk::IOUtil::Save(relativImage, imagePath3);
169  CPPUNIT_ASSERT_NO_THROW(mitk::IOUtil::LoadImage(imagePath3));
170  std::remove(imagePath3.c_str());
171  }
172 
176  void TestNullLoad()
177  {
178  CPPUNIT_ASSERT_THROW(mitk::IOUtil::LoadImage(""), mitk::Exception);
179  CPPUNIT_ASSERT_THROW(mitk::IOUtil::LoadSurface(""), mitk::Exception);
180  CPPUNIT_ASSERT_THROW(mitk::IOUtil::LoadPointSet(""), mitk::Exception);
181  CPPUNIT_ASSERT_THROW(mitk::IOUtil::Load(""), mitk::Exception);
182  }
183 
188  void TestNullSave()
189  {
191  CPPUNIT_ASSERT_THROW(mitk::IOUtil::SaveImage(mitk::Image::New().GetPointer(), ""), mitk::Exception);
193  CPPUNIT_ASSERT_THROW(mitk::IOUtil::Save(mitk::Image::New().GetPointer(), ""), mitk::Exception);
194  }
195 
196  void TestLoadAndSavePointSet()
197  {
198  mitk::PointSet::Pointer pointset = mitk::IOUtil::LoadPointSet(m_PointSetPath);
199  CPPUNIT_ASSERT(pointset.IsNotNull());
200 
201  std::ofstream tmpStream;
202  std::string pointSetPath = mitk::IOUtil::CreateTemporaryFile(tmpStream, "XXXXXX.mps");
203  tmpStream.close();
204  std::string pointSetPathWithDefaultExtension = mitk::IOUtil::CreateTemporaryFile(tmpStream, "XXXXXX.mps");
205  tmpStream.close();
206  std::string pointSetPathWithoutDefaultExtension = mitk::IOUtil::CreateTemporaryFile(tmpStream);
207  tmpStream.close();
208 
209  // the cases where no exception should be thrown
210  CPPUNIT_ASSERT_NO_THROW(mitk::IOUtil::Save(pointset, pointSetPathWithDefaultExtension));
211 
212  // test if defaultextension is inserted if no extension is present
213  CPPUNIT_ASSERT_NO_THROW(mitk::IOUtil::Save(pointset, pointSetPathWithoutDefaultExtension.c_str()));
214 
215  // delete the files after the test is done
216  std::remove(pointSetPath.c_str());
217  std::remove(pointSetPathWithDefaultExtension.c_str());
218  std::remove(pointSetPathWithoutDefaultExtension.c_str());
219  }
220 
221  void TestLoadAndSaveSurface()
222  {
223  mitk::Surface::Pointer surface = mitk::IOUtil::LoadSurface(m_SurfacePath);
224  CPPUNIT_ASSERT(surface.IsNotNull());
225 
226  std::ofstream tmpStream;
227  std::string surfacePath = mitk::IOUtil::CreateTemporaryFile(tmpStream, "diffsurface-XXXXXX.stl");
228 
229  // the cases where no exception should be thrown
230  CPPUNIT_ASSERT_NO_THROW(mitk::IOUtil::Save(surface, surfacePath));
231 
232  // test if exception is thrown as expected on unknown extsension
233  CPPUNIT_ASSERT_THROW(mitk::IOUtil::Save(surface, "testSurface.xXx"), mitk::Exception);
234 
235  // delete the files after the test is done
236  std::remove(surfacePath.c_str());
237  }
238 };
239 
static mitk::Surface::Pointer LoadSurface(const std::string &path)
LoadSurface Convenience method to load an arbitrary mitkSurface.
Definition: mitkIOUtil.cpp:608
static void Save(const mitk::BaseData *data, const std::string &path)
Save a mitk::BaseData instance.
Definition: mitkIOUtil.cpp:824
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
static std::string GetTempPath()
Definition: mitkIOUtil.cpp:372
#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.
static bool SaveImage(mitk::Image::Pointer image, const std::string &path)
SaveImage Convenience method to save an arbitrary mitkImage.
Definition: mitkIOUtil.cpp:870
An object of this class represents an exception of MITK. Please don't instantiate exceptions manually...
Definition: mitkException.h:49
Test fixture for parameterized tests.
static mitk::PointSet::Pointer LoadPointSet(const std::string &path)
LoadPointSet Convenience method to load an arbitrary mitkPointSet.
Definition: mitkIOUtil.cpp:619
static Pointer New()
static std::string CreateTemporaryDirectory(const std::string &templateName="XXXXXX", std::string path=std::string())
Definition: mitkIOUtil.cpp:452
static std::string GetProgramPath()
Definition: mitkIOUtil.cpp:350
static std::string CreateTemporaryFile(std::ofstream &tmpStream, const std::string &templateName="XXXXXX", std::string path=std::string())
Definition: mitkIOUtil.cpp:407
static DataStorage::SetOfObjects::Pointer Load(const std::string &path, DataStorage &storage)
Load a file into the given DataStorage.
Definition: mitkIOUtil.cpp:483
static Pointer New()
static mitk::Image::Pointer LoadImage(const std::string &path)
LoadImage Convenience method to load an arbitrary mitkImage.
Definition: mitkIOUtil.cpp:597