Medical Imaging Interaction Toolkit  2018.4.99-eed36131
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>
20 #include <mitkVersion.h>
21 
22 #include <itkMetaDataObject.h>
23 #include <itkNrrdImageIO.h>
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  MITK_TEST(TestIOMetaInformation);
39  CPPUNIT_TEST_SUITE_END();
40 
41 private:
42  std::string m_ImagePath;
43  std::string m_SurfacePath;
44  std::string m_PointSetPath;
45 
46 public:
47  void setUp() override
48  {
49  m_ImagePath = GetTestDataFilePath("Pic3D.nrrd");
50  m_SurfacePath = GetTestDataFilePath("binary.stl");
51  m_PointSetPath = GetTestDataFilePath("pointSet.mps");
52  }
53 
54  void TestSaveEmptyData()
55  {
57  CPPUNIT_ASSERT_THROW(mitk::IOUtil::Save(data, "/tmp/dummy"), mitk::Exception);
58  }
59 
60  void TestTempMethods()
61  {
62  std::string tmpPath = mitk::IOUtil::GetTempPath();
63  CPPUNIT_ASSERT(!tmpPath.empty());
64 
65  std::ofstream tmpFile;
66  std::string tmpFilePath = mitk::IOUtil::CreateTemporaryFile(tmpFile);
67  CPPUNIT_ASSERT(tmpFile && tmpFile.is_open());
68  CPPUNIT_ASSERT(tmpFilePath.size() > tmpPath.size());
69  CPPUNIT_ASSERT(tmpFilePath.substr(0, tmpPath.size()) == tmpPath);
70 
71  tmpFile.close();
72  CPPUNIT_ASSERT(std::remove(tmpFilePath.c_str()) == 0);
73 
74  std::string programPath = mitk::IOUtil::GetProgramPath();
75  CPPUNIT_ASSERT(!programPath.empty());
76  std::ofstream tmpFile2;
77  std::string tmpFilePath2 = mitk::IOUtil::CreateTemporaryFile(tmpFile2, "my-XXXXXX", programPath);
78  CPPUNIT_ASSERT(tmpFile2 && tmpFile2.is_open());
79  CPPUNIT_ASSERT(tmpFilePath2.size() > programPath.size());
80  CPPUNIT_ASSERT(tmpFilePath2.substr(0, programPath.size()) == programPath);
81  tmpFile2.close();
82  CPPUNIT_ASSERT(std::remove(tmpFilePath2.c_str()) == 0);
83 
84  std::ofstream tmpFile3;
85  std::string tmpFilePath3 =
86  mitk::IOUtil::CreateTemporaryFile(tmpFile3, std::ios_base::binary, "my-XXXXXX.TXT", programPath);
87  CPPUNIT_ASSERT(tmpFile3 && tmpFile3.is_open());
88  CPPUNIT_ASSERT(tmpFilePath3.size() > programPath.size());
89  CPPUNIT_ASSERT(tmpFilePath3.substr(0, programPath.size()) == programPath);
90  CPPUNIT_ASSERT(tmpFilePath3.substr(tmpFilePath3.size() - 13, 3) == "my-");
91  CPPUNIT_ASSERT(tmpFilePath3.substr(tmpFilePath3.size() - 4) == ".TXT");
92  tmpFile3.close();
93  // CPPUNIT_ASSERT(std::remove(tmpFilePath3.c_str()) == 0)
94 
95  std::string tmpFilePath4 = mitk::IOUtil::CreateTemporaryFile();
96  std::ofstream file;
97  file.open(tmpFilePath4.c_str());
98  CPPUNIT_ASSERT_MESSAGE("Testing if file exists after CreateTemporaryFile()", file.is_open());
99 
100  CPPUNIT_ASSERT_THROW(mitk::IOUtil::CreateTemporaryFile(tmpFile2, "XX"), mitk::Exception);
101 
102  std::string tmpDir = mitk::IOUtil::CreateTemporaryDirectory();
103  CPPUNIT_ASSERT(tmpDir.size() > tmpPath.size());
104  CPPUNIT_ASSERT(tmpDir.substr(0, tmpPath.size()) == tmpPath);
105  CPPUNIT_ASSERT(itksys::SystemTools::RemoveADirectory(tmpDir.c_str()));
106 
107  std::string tmpDir2 = mitk::IOUtil::CreateTemporaryDirectory("my-XXXXXX", programPath);
108  CPPUNIT_ASSERT(tmpDir2.size() > programPath.size());
109  CPPUNIT_ASSERT(tmpDir2.substr(0, programPath.size()) == programPath);
110  CPPUNIT_ASSERT(itksys::SystemTools::RemoveADirectory(tmpDir2.c_str()));
111  }
112 
113  void TestTempMethodsForUniqueFilenames()
114  {
115  int numberOfFiles = 100;
116 
117  // create 100 empty files
118  std::vector<std::string> v100filenames;
119  for (int i = 0; i < numberOfFiles; i++)
120  {
121  v100filenames.push_back(mitk::IOUtil::CreateTemporaryFile());
122  }
123 
124  // check if all of them are unique
125  for (int i = 0; i < numberOfFiles; i++)
126  for (int j = 0; j < numberOfFiles; j++)
127  {
128  if (i != j)
129  {
130  std::stringstream message;
131  message << "Checking if file " << i << " and file " << j
132  << " are different, which should be the case because each of them should be unique.";
133  CPPUNIT_ASSERT_MESSAGE(message.str(), (v100filenames.at(i) != v100filenames.at(j)));
134  }
135  }
136 
137  // delete all the files / clean up
138  for (int i = 0; i < numberOfFiles; i++)
139  {
140  std::remove(v100filenames.at(i).c_str());
141  }
142  }
143 
144  void TestLoadAndSaveImage()
145  {
146  mitk::Image::Pointer img1 = mitk::IOUtil::Load<mitk::Image>(m_ImagePath);
147  CPPUNIT_ASSERT(img1.IsNotNull());
148 
149  std::ofstream tmpStream;
150  std::string imagePath = mitk::IOUtil::CreateTemporaryFile(tmpStream, "diffpic3d-XXXXXX.nrrd");
151  tmpStream.close();
152  std::string imagePath2 = mitk::IOUtil::CreateTemporaryFile(tmpStream, "diffpic3d-XXXXXX.nii.gz");
153  tmpStream.close();
154 
155  // the cases where no exception should be thrown
156  CPPUNIT_ASSERT_NO_THROW(mitk::IOUtil::Save(img1, imagePath));
157  CPPUNIT_ASSERT_NO_THROW(mitk::IOUtil::Save(img1.GetPointer(), imagePath2));
158 
159  // load data which does not exist
160  CPPUNIT_ASSERT_THROW(mitk::IOUtil::Load("fileWhichDoesNotExist.nrrd"), mitk::Exception);
161 
162  // delete the files after the test is done
163  std::remove(imagePath.c_str());
164  std::remove(imagePath2.c_str());
165 
166  mitk::Image::Pointer relativImage = mitk::ImageGenerator::GenerateGradientImage<float>(4, 4, 4, 1);
167  std::string imagePath3 = mitk::IOUtil::CreateTemporaryFile(tmpStream, "XXXXXX.nrrd");
168  tmpStream.close();
169  mitk::IOUtil::Save(relativImage, imagePath3);
170  CPPUNIT_ASSERT_NO_THROW(mitk::IOUtil::Load(imagePath3));
171  std::remove(imagePath3.c_str());
172  }
173 
177  void TestNullLoad()
178  {
179  CPPUNIT_ASSERT_THROW(mitk::IOUtil::Load(""), mitk::Exception);
180  }
181 
186  void TestNullSave()
187  {
188  CPPUNIT_ASSERT_THROW(mitk::IOUtil::Save(nullptr, mitk::IOUtil::CreateTemporaryFile()), mitk::Exception);
189  CPPUNIT_ASSERT_THROW(mitk::IOUtil::Save(mitk::Image::New().GetPointer(), ""), mitk::Exception);
190  }
191 
192  void TestLoadAndSavePointSet()
193  {
194  mitk::PointSet::Pointer pointset = mitk::IOUtil::Load<mitk::PointSet>(m_PointSetPath);
195  CPPUNIT_ASSERT(pointset.IsNotNull());
196 
197  std::ofstream tmpStream;
198  std::string pointSetPath = mitk::IOUtil::CreateTemporaryFile(tmpStream, "XXXXXX.mps");
199  tmpStream.close();
200  std::string pointSetPathWithDefaultExtension = mitk::IOUtil::CreateTemporaryFile(tmpStream, "XXXXXX.mps");
201  tmpStream.close();
202  std::string pointSetPathWithoutDefaultExtension = mitk::IOUtil::CreateTemporaryFile(tmpStream);
203  tmpStream.close();
204 
205  // the cases where no exception should be thrown
206  CPPUNIT_ASSERT_NO_THROW(mitk::IOUtil::Save(pointset, pointSetPathWithDefaultExtension));
207 
208  // test if defaultextension is inserted if no extension is present
209  CPPUNIT_ASSERT_NO_THROW(mitk::IOUtil::Save(pointset, pointSetPathWithoutDefaultExtension.c_str()));
210 
211  // delete the files after the test is done
212  std::remove(pointSetPath.c_str());
213  std::remove(pointSetPathWithDefaultExtension.c_str());
214  std::remove(pointSetPathWithoutDefaultExtension.c_str());
215  }
216 
217  void TestLoadAndSaveSurface()
218  {
219  mitk::Surface::Pointer surface = mitk::IOUtil::Load<mitk::Surface>(m_SurfacePath);
220  CPPUNIT_ASSERT(surface.IsNotNull());
221 
222  std::ofstream tmpStream;
223  std::string surfacePath = mitk::IOUtil::CreateTemporaryFile(tmpStream, "diffsurface-XXXXXX.stl");
224 
225  // the cases where no exception should be thrown
226  CPPUNIT_ASSERT_NO_THROW(mitk::IOUtil::Save(surface, surfacePath));
227 
228  // test if exception is thrown as expected on unknown extsension
229  CPPUNIT_ASSERT_THROW(mitk::IOUtil::Save(surface, "testSurface.xXx"), mitk::Exception);
230 
231  // delete the files after the test is done
232  std::remove(surfacePath.c_str());
233  }
234 
235  std::string GenerateMetaDictKey(const mitk::PropertyKeyPath& propKey)
236  {
237  auto result = mitk::PropertyKeyPathToPropertyName(propKey);
238  std::replace(result.begin(), result.end(), '.', '_');
239  return result;
240  }
241 
242  std::string GetValueFromMetaDict(const itk::MetaDataDictionary& dict, const mitk::PropertyKeyPath& propKey)
243  {
244  auto metaValueBase = dict.Get(GenerateMetaDictKey(propKey));
245  auto metaValue = dynamic_cast<const itk::MetaDataObject<std::string>*>(metaValueBase);
246  return metaValue->GetMetaDataObjectValue();
247  }
248 
249  void TestIOMetaInformation()
250  {
251  mitk::Image::Pointer img = mitk::IOUtil::Load<mitk::Image>(m_ImagePath);
252  CPPUNIT_ASSERT(img.IsNotNull());
253 
254  auto value = img->GetProperty(mitk::PropertyKeyPathToPropertyName(mitk::IOMetaInformationPropertyConstants::READER_DESCRIPTION()).c_str())->GetValueAsString();
255  CPPUNIT_ASSERT_EQUAL(std::string("ITK NrrdImageIO"), value);
256  value = img->GetProperty(mitk::PropertyKeyPathToPropertyName(mitk::IOMetaInformationPropertyConstants::READER_INPUTLOCATION()).c_str())->GetValueAsString();
257  CPPUNIT_ASSERT_EQUAL(m_ImagePath, value);
258  value = img->GetProperty(mitk::PropertyKeyPathToPropertyName(mitk::IOMetaInformationPropertyConstants::READER_MIME_CATEGORY()).c_str())->GetValueAsString();
259  CPPUNIT_ASSERT_EQUAL(std::string("Images"), value);
260  value = img->GetProperty(mitk::PropertyKeyPathToPropertyName(mitk::IOMetaInformationPropertyConstants::READER_MIME_NAME()).c_str())->GetValueAsString();
261  CPPUNIT_ASSERT_EQUAL(std::string("application/vnd.mitk.image.nrrd"), value);
262  value = img->GetProperty(mitk::PropertyKeyPathToPropertyName(mitk::IOMetaInformationPropertyConstants::READER_VERSION()).c_str())->GetValueAsString();
263  CPPUNIT_ASSERT_EQUAL(std::string(MITK_VERSION_STRING), value);
264 
265  //check if the information is persistet correctly on save.
266  std::ofstream tmpStream;
267  std::string imagePath = mitk::IOUtil::CreateTemporaryFile(tmpStream, "ioMeta_XXXXXX.nrrd");
268  tmpStream.close();
269  mitk::IOUtil::Save(img, imagePath);
270 
271  auto io = itk::NrrdImageIO::New();
272  io->SetFileName(imagePath);
273  io->ReadImageInformation();
274  auto metaDict = io->GetMetaDataDictionary();
275 
276  auto metaValue = GetValueFromMetaDict(metaDict, mitk::IOMetaInformationPropertyConstants::READER_DESCRIPTION());
277  CPPUNIT_ASSERT_EQUAL(std::string("ITK NrrdImageIO"), metaValue);
278  metaValue = GetValueFromMetaDict(metaDict, mitk::IOMetaInformationPropertyConstants::READER_INPUTLOCATION());
279  CPPUNIT_ASSERT_EQUAL(m_ImagePath, metaValue);
280  metaValue = GetValueFromMetaDict(metaDict, mitk::IOMetaInformationPropertyConstants::READER_MIME_CATEGORY());
281  CPPUNIT_ASSERT_EQUAL(std::string("Images"), metaValue);
282  metaValue = GetValueFromMetaDict(metaDict, mitk::IOMetaInformationPropertyConstants::READER_MIME_NAME());
283  CPPUNIT_ASSERT_EQUAL(std::string("application/vnd.mitk.image.nrrd"), metaValue);
284  metaValue = GetValueFromMetaDict(metaDict, mitk::IOMetaInformationPropertyConstants::READER_VERSION());
285  CPPUNIT_ASSERT_EQUAL(std::string(MITK_VERSION_STRING), metaValue);
286 
287  // delete the files after the test is done
288  std::remove(imagePath.c_str());
289  }
290 
291 };
292 
#define MITK_VERSION_STRING
Definition: mitkVersion.h:9
MITKCORE_EXPORT std::string PropertyKeyPathToPropertyName(const PropertyKeyPath &tagPath)
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 const char * replace[]
This is a dictionary to replace long names of classes, modules, etc. to shorter versions in the conso...
Class that can be used to specify nested or wild carded property keys. E.g. for the use in context of...
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