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