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