Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkFileWriterRegistryTest.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 <mitkAbstractFileWriter.h>
14 #include <mitkBaseData.h>
15 #include <mitkFileReaderRegistry.h>
16 #include <mitkFileWriterRegistry.h>
17 #include <mitkIFileWriter.h>
18 #include <mitkIOUtil.h>
19 #include <mitkTestingMacros.h>
20 
21 class DummyBaseData : public mitk::BaseData
22 {
23 public:
24  mitkClassMacro(DummyBaseData, mitk::BaseData) itkNewMacro(Self)
25 
26  void SetRequestedRegion(const itk::DataObject * /*data*/)
27  {
28  }
30  bool RequestedRegionIsOutsideOfTheBufferedRegion() { return false; }
31  bool VerifyRequestedRegion() { return true; }
32 };
33 
34 class DummyWriter : public mitk::AbstractFileWriter
35 {
36 public:
37  DummyWriter(const DummyWriter &other) : mitk::AbstractFileWriter(other), m_Content("Hi there stream") {}
38  DummyWriter(const std::string &basedataType, const std::string &extension, int ranking)
39  : mitk::AbstractFileWriter(basedataType, extension, "This is a dummy description"), m_Content("Hi there stream")
40  {
41  this->SetRanking(ranking);
42  m_ServiceReg = this->RegisterService();
43  }
44 
45  ~DummyWriter()
46  {
47  if (m_ServiceReg)
48  m_ServiceReg.Unregister();
49  }
50 
51  using AbstractFileWriter::Write;
52 
53  virtual void Write(const mitk::BaseData *data, std::ostream &stream)
54  {
55  MITK_TEST_CONDITION_REQUIRED(dynamic_cast<const DummyBaseData *>(data), "Correct data type")
56  stream << m_Content;
57  }
58 
59  std::string m_Content;
60 
61 private:
62  DummyWriter *Clone() const { return new DummyWriter(*this); }
64 
65 }; // End of internal dummy Writer
66 
67 class DummyWriter2 : public mitk::AbstractFileWriter
68 {
69 public:
70  DummyWriter2(const DummyWriter2 &other) : mitk::AbstractFileWriter(other), m_Content("hi there file path") {}
71  DummyWriter2(const std::string &basedataType, const std::string &extension, int ranking)
72  : mitk::AbstractFileWriter(basedataType, extension, "This is a dummy description"), m_Content("hi there file path")
73  {
74  this->SetRanking(ranking);
75  m_ServiceReg = this->RegisterService();
76  }
77 
78  ~DummyWriter2()
79  {
80  if (m_ServiceReg)
81  m_ServiceReg.Unregister();
82  }
83 
84  using AbstractFileWriter::Write;
85 
86  virtual void Write(const mitk::BaseData *data, const std::string &filePath)
87  {
88  MITK_TEST_CONDITION_REQUIRED(dynamic_cast<const DummyBaseData *>(data), "Correct data type")
89  std::ofstream fileStream(filePath.c_str());
90  fileStream << m_Content;
91  }
92 
93  virtual void Write(const mitk::BaseData *data, std::ostream &stream)
94  {
95  mitk::AbstractFileWriter::Write(data, stream);
96  }
97 
98  virtual bool CanWrite(const mitk::BaseData *data) const { return dynamic_cast<const DummyBaseData *>(data); }
99  std::string m_Content;
100 
101 private:
102  DummyWriter2 *Clone() const { return new DummyWriter2(*this); }
104 
105 }; // End of internal dummy Writer 2
106 
108 {
109  DummyWriter dummyWriter(DummyBaseData::GetStaticNameOfClass(), "stream", 100);
110  DummyWriter2 dummyWriter2(DummyBaseData::GetStaticNameOfClass(), "file", 50);
111  mitk::FileWriterRegistry writerRegistry;
112 
113  // Test DummyWriter, which always uses a ostream for writing, even
114  // when a file path is used
115  DummyBaseData dummyData;
116  std::stringstream oss;
117  writerRegistry.Write(&dummyData, oss);
118  MITK_TEST_CONDITION_REQUIRED(dummyWriter.m_Content == oss.str(), "Dummy stream writer")
119 
120  std::string content;
121  {
122  std::ofstream tmpStream;
123  std::string tmpFileName = mitk::IOUtil::CreateTemporaryFile(tmpStream);
124  writerRegistry.Write(&dummyData, tmpFileName);
125 
126  std::ifstream tmpInput(tmpFileName.c_str());
127  std::getline(tmpInput, content);
128  tmpInput.close();
129  tmpStream.close();
130  std::remove(tmpFileName.c_str());
131  }
132  MITK_TEST_CONDITION_REQUIRED(dummyWriter.m_Content == content, "Dummy stream writer")
133 
134  // Test DummyWriter2, which always uses a real file for writing, even
135  // when a std::ostream object is given
136  std::stringstream oss2;
137  dummyWriter2.Write(&dummyData, oss2);
138  MITK_TEST_CONDITION_REQUIRED(dummyWriter2.m_Content == oss2.str(), "Dummy 2 stream writer")
139 
140  std::string content2;
141  {
142  std::ofstream tmpStream;
143  std::string tmpFileName = mitk::IOUtil::CreateTemporaryFile(tmpStream, "XXXXXX.file");
144  writerRegistry.Write(&dummyData, tmpFileName);
145 
146  std::ifstream tmpInput(tmpFileName.c_str());
147  std::getline(tmpInput, content2);
148  tmpInput.close();
149  std::remove(tmpFileName.c_str());
150  }
151  MITK_TEST_CONDITION_REQUIRED(dummyWriter2.m_Content == content2, "Dummy 2 stream writer")
152 }
153 
157 int mitkFileWriterRegistryTest(int /*argc*/, char * /*argv*/ [])
158 {
159  // always start with this!
160  MITK_TEST_BEGIN("FileWriterRegistry");
161 
163 
164  // mitk::FileWriterRegistry::Pointer frm = mitk::FileWriterRegistry::New();
165  // MITK_TEST_CONDITION_REQUIRED(argc == 2,"Testing FileWriterRegistry instantiation");
166 
167  DummyWriter testDR("testdata", "test", 1);
168  DummyWriter otherDR("testdata", "other", 1);
169 
170  // MITK_TEST_CONDITION_REQUIRED(testDR->CanWrite("/this/is/a/folder/file.test"),"Positive test of default CanRead()
171  // implementation");
172  // MITK_TEST_CONDITION_REQUIRED(!testDR->CanWrite("/this/is/a/folder/file.tes"),"Negative test of default CanRead()
173  // implementation");
174 
176  mitk::IFileWriter *returned = writerRegistry->GetWriter("", "test");
177 
178  MITK_TEST_CONDITION_REQUIRED(returned && &static_cast<mitk::IFileWriter &>(testDR) != returned,
179  "Testing correct retrieval of FileWriter 1/2");
180 
181  returned = writerRegistry->GetWriter("", "other");
182 
183  MITK_TEST_CONDITION_REQUIRED(returned && &static_cast<mitk::IFileWriter &>(otherDR) != returned,
184  "Testing correct retrieval of FileWriter 2/2");
185 
186  DummyWriter mediocreTestDR("testdata", "test", 20);
187  DummyWriter prettyFlyTestDR("testdata", "test", 50);
188  DummyWriter2 awesomeTestDR("testdata", "test", 100);
189 
190  returned = writerRegistry->GetWriter("test");
191  MITK_TEST_CONDITION_REQUIRED(dynamic_cast<DummyWriter2 *>(returned),
192  "Testing correct priorized retrieval of FileWriter: Best Writer");
193 
194  // Now to give those Writers some options, then we will try again
195  mitk::IFileWriter::OptionList options;
196  options.push_back(std::make_pair("isANiceGuy", true));
197  mediocreTestDR.SetOptions(options);
198  options.clear();
199  options.push_back(std::make_pair("canFly", true));
200  prettyFlyTestDR.SetOptions(options);
201  options.push_back(std::make_pair("isAwesome", true));
202  awesomeTestDR.SetOptions(options); // note: awesomeWriter canFly and isAwesome
203 
204  // Reset Options, use to define what we want the Writer to do
205  mitk::IFileWriter::OptionNames optionFilter;
206  optionFilter.push_back("canFly");
207  returned = writerRegistry->GetWriter("", "test", optionFilter);
208  MITK_TEST_CONDITION_REQUIRED(returned && &static_cast<mitk::IFileWriter &>(awesomeTestDR) != returned,
209  "Testing correct retrieval of FileWriter with Options: Best Writer with options");
210 
211  optionFilter.push_back("isAwesome");
212  returned = writerRegistry->GetWriter("", "test", optionFilter);
214  returned && &static_cast<mitk::IFileWriter &>(awesomeTestDR) != returned,
215  "Testing correct retrieval of FileWriter with multiple Options: Best Writer with options");
216 
217  optionFilter.clear();
218  optionFilter.push_back("isANiceGuy");
219  returned = writerRegistry->GetWriter("", "test", optionFilter);
221  returned && &static_cast<mitk::IFileWriter &>(mediocreTestDR) != returned,
222  "Testing correct retrieval of specific FileWriter with Options: Low priority Writer with specific option");
223 
224  optionFilter.push_back("canFly");
225  returned = writerRegistry->GetWriter("", "test", optionFilter);
226  MITK_TEST_CONDITION_REQUIRED(returned == nullptr, "Testing correct return of 0 value when no matching Writer was found");
227 
228  // Onward to test the retrieval of multiple Writers
229 
230  std::vector<mitk::IFileWriter *> returnedList;
231  returnedList = writerRegistry->GetWriters("", "test", optionFilter);
233  returnedList.empty(),
234  "Testing correct return of zero Writers when no matching Writer was found, asking for all compatibles");
235 
236  optionFilter.clear();
237  optionFilter.push_back("canFly");
238  returnedList = writerRegistry->GetWriters("", "test", optionFilter);
240  returnedList.size() == 2,
241  "Testing correct return of two Writers when two matching Writer was found, asking for all compatibles");
242  MITK_TEST_CONDITION_REQUIRED(dynamic_cast<DummyWriter2 *>(returnedList.front()),
243  "Testing correct priorization of returned Writers with options 1/2");
244 
245  optionFilter.clear();
246  optionFilter.push_back("isAwesome");
247  returnedList = writerRegistry->GetWriters("", "test", optionFilter);
249  returnedList.size() == 1,
250  "Testing correct return of one Writers when one matching Writer was found, asking for all compatibles");
251  MITK_TEST_CONDITION_REQUIRED(dynamic_cast<DummyWriter2 *>(returnedList.front()),
252  "Testing correctness of result from former query");
253 
254  // mitk::CoreObjectFactory::GetInstance();
255  // mitk::FileReaderRegistry readerRegistry;
256  // mitk::Image::Pointer image = readerRegistry.Read<mitk::Image>("F://Build//MITK-Data//Pic2DplusT.nrrd");
257 
258  // writerRegistry->Write(image.GetPointer(), "F://Build//MITK-Data//Pic2DplusTcopy.nrrd");
259 
261  // mitk::PointSetWriter::Pointer psr = mitk::PointSetWriter::New();
262  // mitk::BaseData::Pointer basedata;
263  // basedata = mitk::FileWriterRegistry::Read("F://Build//MITK-Data//pointSet.mps");
264  // MITK_TEST_CONDITION_REQUIRED(basedata.IsNotNull(), "Testing correct read of PointSet");
265 
267  // mitk::PointSet::Pointer pointset = mitk::FileWriterRegistry::Read< mitk::PointSet
268  // >("F://Build//MITK-Data//pointSet.mps");
269  // MITK_TEST_CONDITION_REQUIRED(pointset.IsNotNull(), "Testing templated call of Read()");
270 
272  // mitk::LegacyFileWriterService::Pointer lfr = mitk::LegacyFileWriterService::New(".nrrd", "Nearly Raw Raster Data");
273  // returned = mitk::FileWriterRegistry::GetWriter(".nrrd");
274  // MITK_TEST_CONDITION_REQUIRED(lfr == returned, "Testing correct retrieval of specific FileWriter with Options: Low
275  // priority Writer with specific option");
276 
277  // mitk::BaseData::Pointer image = mitk::FileWriterRegistry::Read("F://Build//MITK-Data//Pic2DplusT.nrrd");
278  // MITK_TEST_CONDITION_REQUIRED(image.IsNotNull(), "Testing whether BaseData is empty or not");
279  // mitk::Image::Pointer image2 = dynamic_cast<mitk::Image*> (image.GetPointer());
280  // MITK_TEST_CONDITION_REQUIRED(image2.IsNotNull(), "Testing if BaseData is an image");
281 
282  // Delete this here because it will call the PrototypeServiceFactory::Unget() method
283  // of the dummy writers.
284  delete writerRegistry;
285 
287  MITK_TEST_END()
288 }
Base of all data objects.
Definition: mitkBaseData.h:37
void Write() override=0
Write the base data to the specified location or output stream.
#define MITK_TEST_CONDITION_REQUIRED(COND, MSG)
std::vector< IFileWriter * > GetWriters(const BaseData *baseData, const std::string &mimeType, us::ModuleContext *context=us::GetModuleContext())
STL namespace.
DataCollection - Class to facilitate loading/accessing structured data.
bool RequestedRegionIsOutsideOfTheBufferedRegion() override=0
Determine whether the RequestedRegion is outside of the BufferedRegion.
section GeneralTestsDeprecatedOldTestingStyle Deprecated macros All tests with MITK_TEST_BEGIN()
void TestStreamMethods()
bool VerifyRequestedRegion() override=0
Verify that the RequestedRegion is within the LargestPossibleRegion.
void SetRequestedRegionToLargestPossibleRegion() override=0
Set the RequestedRegion to the LargestPossibleRegion.
#define mitkClassMacro(className, SuperClassName)
Definition: mitkCommon.h:40
void SetRequestedRegion(const itk::DataObject *data) override=0
Set the requested region from this data object to match the requested region of the data object passe...
IFileWriter * GetWriter(const WriterReference &ref, us::ModuleContext *context=us::GetModuleContext())
static std::string CreateTemporaryFile(std::ofstream &tmpStream, const std::string &templateName="XXXXXX", std::string path=std::string())
Definition: mitkIOUtil.cpp:413
int mitkFileWriterRegistryTest(int, char *[])
and MITK_TEST_END()
The common interface of all MITK file writers.
static mitk::PlanarFigure::Pointer Clone(mitk::PlanarFigure::Pointer original)
Base class for writing mitk::BaseData objects to files or streams.