Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
mitkFileReaderRegistryTest.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 "mitkAbstractFileReader.h"
18 #include "mitkFileReaderRegistry.h"
19 #include "mitkIFileReader.h"
20 #include "mitkTestingMacros.h"
21 #include <mitkBaseData.h>
22 #include <mitkCustomMimeType.h>
23 #include <mitkImage.h>
24 
25 class DummyReader : public mitk::AbstractFileReader
26 {
27 public:
28  DummyReader(const DummyReader &other) : mitk::AbstractFileReader(other) {}
29  DummyReader(const std::string &mimeTypeName, const std::string &extension, int priority) : mitk::AbstractFileReader()
30  {
31  mitk::CustomMimeType mimeType(mimeTypeName);
32  mimeType.AddExtension(extension);
33  mimeType.SetComment("This is a dummy description");
34 
35  this->SetMimeType(mimeType);
36 
37  this->SetRanking(priority);
38  m_ServiceReg = this->RegisterService();
39  }
40 
41  ~DummyReader()
42  {
43  if (m_ServiceReg)
44  m_ServiceReg.Unregister();
45  }
46 
48 
49  virtual std::vector<itk::SmartPointer<mitk::BaseData>> Read() override
50  {
51  std::vector<mitk::BaseData::Pointer> result;
52  return result;
53  }
54 
55 private:
56  DummyReader *Clone() const override { return new DummyReader(*this); }
58 }; // End of internal dummy reader
59 
60 class DummyReader2 : public mitk::AbstractFileReader
61 {
62 public:
63  DummyReader2(const DummyReader2 &other) : mitk::AbstractFileReader(other) {}
64  DummyReader2(const std::string &mimeTypeName, const std::string &extension, int priority) : mitk::AbstractFileReader()
65  {
66  mitk::CustomMimeType mimeType(mimeTypeName);
67  mimeType.AddExtension(extension);
68  mimeType.SetComment("This is a second dummy description");
69  this->SetMimeType(mimeType);
70 
71  this->SetRanking(priority);
72  m_ServiceReg = this->RegisterService();
73  }
74 
75  ~DummyReader2()
76  {
77  if (m_ServiceReg)
78  m_ServiceReg.Unregister();
79  }
80 
82 
83  virtual std::vector<itk::SmartPointer<mitk::BaseData>> Read() override
84  {
85  std::vector<mitk::BaseData::Pointer> result;
86  return result;
87  }
88 
89 private:
90  DummyReader2 *Clone() const override { return new DummyReader2(*this); }
92 }; // End of internal dummy reader 2
93 
97 int mitkFileReaderRegistryTest(int /*argc*/, char * /*argv*/ [])
98 {
99  // always start with this!
100  MITK_TEST_BEGIN("FileReaderRegistry");
101  // mitk::FileReaderRegistry::Pointer frm = mitk::FileReaderRegistry::New();
102  // MITK_TEST_CONDITION_REQUIRED(argc == 2,"Testing FileReaderRegistry instantiation");
103 
104  // DummyReader testDR("application/dummy", "test",1);
105  // DummyReader otherDR("application/dummy2", "other",1);
106 
107  // MITK_TEST_CONDITION_REQUIRED(!testDR.CanRead("/this/is/a/folder/file.tes"),"Negative test of default CanRead()
108  // implementation");
109 
110  // mitk::FileReaderRegistry* readerRegistry = new mitk::FileReaderRegistry;
111  // mitk::IFileReader* returned = readerRegistry->GetReader("bla.test");
112 
113  // MITK_TEST_CONDITION_REQUIRED(returned && &static_cast<mitk::IFileReader&>(testDR) != returned,"Testing correct
114  // retrieval of FileReader 1/2");
115 
116  // returned = readerRegistry->GetReader("other");
117 
118  // MITK_TEST_CONDITION_REQUIRED(returned && &static_cast<mitk::IFileReader&>(otherDR) != returned,"Testing correct
119  // retrieval of FileReader 2/2");
120 
121  // DummyReader mediocreTestDR("application/dummy", "test", 20);
122  // DummyReader prettyFlyTestDR("application/dummy", "test", 50);
123  // DummyReader2 awesomeTestDR("application/dummy", "test", 100);
124 
125  // returned = readerRegistry->GetReader("test");
126  // MITK_TEST_CONDITION_REQUIRED(dynamic_cast<DummyReader2*>(returned), "Testing correct priorized retrieval of
127  // FileReader: Best reader");
128 
129  // Now to give those readers some options, then we will try again
130 
131  // mitk::IFileReader::OptionList options;
132  // options.push_back(std::make_pair("isANiceGuy", true));
133  // mediocreTestDR.SetOptions(options);
134  // options.clear();
135  // options.push_back(std::make_pair("canFly", true));
136  // prettyFlyTestDR.SetOptions(options);
137  // options.push_back(std::make_pair("isAwesome", true));
138  // awesomeTestDR.SetOptions(options); //note: awesomeReader canFly and isAwesome
139 
140  // // Reset Options, use to define what we want the reader to do
141  // options.clear();
142  // mitk::IFileReader::OptionNames optionsFilter;
143  // optionsFilter.push_back("canFly");
144  // returned = readerRegistry->GetReader("test", optionsFilter);
145  // MITK_TEST_CONDITION_REQUIRED(returned && &static_cast<mitk::IFileReader&>(awesomeTestDR) != returned, "Testing
146  // correct retrieval of FileReader with Options: Best reader with options");
147 
148  // optionsFilter.push_back("isAwesome");
149  // returned = readerRegistry->GetReader("test", optionsFilter);
150  // MITK_TEST_CONDITION_REQUIRED(returned && &static_cast<mitk::IFileReader&>(awesomeTestDR) != returned, "Testing
151  // correct retrieval of FileReader with multiple Options: Best reader with options");
152 
153  // optionsFilter.clear();
154  // optionsFilter.push_back("isANiceGuy");
155  // returned = readerRegistry->GetReader("test", optionsFilter);
156  // MITK_TEST_CONDITION_REQUIRED(returned && &static_cast<mitk::IFileReader&>(mediocreTestDR) != returned, "Testing
157  // correct retrieval of specific FileReader with Options: Low priority reader with specific option");
158 
159  // optionsFilter.push_back("canFly");
160  // returned = readerRegistry->GetReader("test", optionsFilter);
161  // MITK_TEST_CONDITION_REQUIRED(returned == NULL, "Testing correct return of 0 value when no matching reader was
162  // found");
163 
164  // // Onward to test the retrieval of multiple readers
165 
166  // std::vector< mitk::IFileReader* > returnedList;
167  // returnedList = readerRegistry->GetReaders("test", optionsFilter);
168  // MITK_TEST_CONDITION_REQUIRED(returnedList.empty(), "Testing correct return of zero readers when no matching reader
169  // was found, asking for all compatibles");
170 
171  // optionsFilter.clear();
172  // optionsFilter.push_back("canFly");
173  // returnedList = readerRegistry->GetReaders("test", optionsFilter);
174  // MITK_TEST_CONDITION_REQUIRED(returnedList.size() == 2, "Testing correct return of two readers when two matching
175  // reader was found, asking for all compatibles");
176  // MITK_TEST_CONDITION_REQUIRED(dynamic_cast<DummyReader2*>(returnedList.front()), "Testing correct priorization of
177  // returned Readers with options 1/2");
178 
179  // optionsFilter.clear();
180  // optionsFilter.push_back("isAwesome");
181  // returnedList = readerRegistry->GetReaders("test", optionsFilter);
182  // MITK_TEST_CONDITION_REQUIRED(returnedList.size() == 1, "Testing correct return of one readers when one matching
183  // reader was found, asking for all compatibles");
184  // MITK_TEST_CONDITION_REQUIRED(dynamic_cast<DummyReader2*>(returnedList.front()), "Testing correctness of result
185  // from former query");
186 
187  // And now to verify a working read chain for a mps file:
188  // mitk::PointSetReader::Pointer psr = mitk::PointSetReader::New();
189  // std::vector<mitk::BaseData::Pointer> basedata;
190  // basedata = mitk::FileReaderRegistry::Read("F://Build//MITK-Data//pointSet.mps");
191  // MITK_TEST_CONDITION_REQUIRED(basedata.size() > 0, "Testing correct read of PointSet");
192 
193  // Testing templated call to ReaderRegistry
194  // mitk::PointSet::Pointer pointset = mitk::FileReaderRegistry::Read< mitk::PointSet
195  // >("F://Build//MITK-Data//pointSet.mps");
196  // MITK_TEST_CONDITION_REQUIRED(pointset.IsNotNull(), "Testing templated call of Read()");
197 
198  // And now for something completely different... (Debug)
199  // mitk::LegacyFileReaderService::Pointer lfr = mitk::LegacyFileReaderService::New(".nrrd", "Nearly Raw Raster Data");
200  // returned = mitk::FileReaderRegistry::GetReader(".nrrd");
201  // MITK_TEST_CONDITION_REQUIRED(lfr == returned, "Testing correct retrieval of specific FileReader with Options: Low
202  // priority reader with specific option");
203 
204  // std::vector<mitk::BaseData::Pointer> image =
205  // mitk::FileReaderRegistry::Read("F://Build//MITK-Data//Pic2DplusT.nrrd");
206  // MITK_TEST_CONDITION_REQUIRED(image.size() > 0, "Testing whether image was returned or not");
207  // mitk::Image::Pointer image2 = dynamic_cast<mitk::Image*> (image.front().GetPointer());
208  // MITK_TEST_CONDITION_REQUIRED(image2.IsNotNull(), "Testing if BaseData is an image");
209 
210  // Delete this here because it will call the PrototypeServiceFactory::Unget() method
211  // of the dummy readers.
212  // delete readerRegistry;
213 
214  // always end with this!
215  MITK_TEST_END();
216 }
DataCollection - Class to facilitate loading/accessing structured data.
section GeneralTestsDeprecatedOldTestingStyle Deprecated macros All tests with MITK_TEST_BEGIN()
virtual std::vector< itk::SmartPointer< BaseData > > Read() override=0
Reads a path or stream and creates a list of BaseData objects.
int mitkFileReaderRegistryTest(int, char *[])
void SetMimeType(const CustomMimeType &mimeType)
The CustomMimeType class represents a custom mime-type which may be registered as a service object...
us::ServiceRegistration< IFileReader > RegisterService(us::ModuleContext *context=us::GetModuleContext())
void SetRanking(int ranking)
Set the service ranking for this file reader.
Base class for creating mitk::BaseData objects from files or streams.
and MITK_TEST_END()
static mitk::PlanarFigure::Pointer Clone(mitk::PlanarFigure::Pointer original)