Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkPreferenceListReaderOptionsFunctorTest.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 
14 #include "mitkTestFixture.h"
15 #include "mitkTestingMacros.h"
16 #include <mitkAbstractFileReader.h>
17 #include <mitkCustomMimeType.h>
18 #include <mitkIOMimeTypes.h>
19 #include <usModuleContext.h>
20 
21 #include <limits>
22 
23 namespace mitk
24 {
25  class TestFileReaderService : public mitk::AbstractFileReader
26  {
27  public:
28  TestFileReaderService(const std::string &description)
29  : AbstractFileReader(CustomMimeType("TestMimeType"), description)
30  {
31  m_ServiceRegistration = RegisterService();
32  };
33 
34  ~TestFileReaderService() override
35  {
36  };
37 
39 
40  std::vector<itk::SmartPointer<BaseData>> Read() override
41  {
42  std::vector<itk::SmartPointer<BaseData>> result;
43  return result;
44  };
45 
46  ConfidenceLevel GetConfidenceLevel() const override
47  {
48  return Supported;
49  };
50 
51  private:
52  TestFileReaderService * Clone() const override
53  {
54  return new TestFileReaderService(*this);
55  };
56 
57  us::ServiceRegistration<IFileWriter> m_ServiceRegistration;
58  };
59 
60 } // namespace mitk
61 
62 
63 class mitkPreferenceListReaderOptionsFunctorTestSuite : public mitk::TestFixture
64 {
65  CPPUNIT_TEST_SUITE(mitkPreferenceListReaderOptionsFunctorTestSuite);
66 
67  MITK_TEST(UsePreferenceList);
68  MITK_TEST(UseBlackList);
69  MITK_TEST(UseNoList);
70  MITK_TEST(UseBlackAndPreferenceList);
71  MITK_TEST(UseOverlappingBlackAndPreferenceList);
72  MITK_TEST(UsePreferenceListWithInexistantReaders);
73  MITK_TEST(UseAllBlackedList);
74 
75 
76  CPPUNIT_TEST_SUITE_END();
77 
78 private:
79  std::string m_ImagePath;
83 
84  mitk::TestFileReaderService* m_NormalService;
85  mitk::TestFileReaderService* m_PrefService;
86  mitk::TestFileReaderService* m_BlackService;
87  mitk::CustomMimeType* m_TestMimeType;
88 
89 public:
90  void setUp() override
91  {
92  m_ImagePath = GetTestDataFilePath("BallBinary30x30x30.nrrd");
93 
94  preference = { "Prefered Test Service" };
95  black = { "Unwanted Test Service" };
96  emptyList = {};
97 
98  m_TestMimeType = new mitk::CustomMimeType("TestMimeType");
99  m_TestMimeType->AddExtension("nrrd");
101  m_TestMimeType->SetComment("Test mime type");
102 
103  us::ModuleContext *context = us::GetModuleContext();
104  us::ServiceProperties props;
106  context->RegisterService(m_TestMimeType, props);
107 
108  m_NormalService = new mitk::TestFileReaderService("Normal Test Service");
109  m_PrefService = new mitk::TestFileReaderService("Prefered Test Service");
110  m_BlackService = new mitk::TestFileReaderService("Unwanted Test Service");
111  }
112 
113  void tearDown() override
114  {
115  delete m_PrefService;
116  delete m_BlackService;
117  delete m_NormalService;
118  delete m_TestMimeType;
119  }
120 
121  void UsePreferenceList()
122  {
123  mitk::IOUtil::LoadInfo info(m_ImagePath);
124 
126  CPPUNIT_ASSERT(true == functor(info));
127  auto description = info.m_ReaderSelector.GetSelected().GetDescription();
128  CPPUNIT_ASSERT_EQUAL(std::string("Prefered Test Service"), description);
129  }
130 
131  void UseNoList()
132  {
133  mitk::IOUtil::LoadInfo info(m_ImagePath);
134 
136  CPPUNIT_ASSERT(true == functor(info));
137  auto description = info.m_ReaderSelector.GetSelected().GetDescription();
138  CPPUNIT_ASSERT_EQUAL(std::string("Normal Test Service"), description);
139  }
140 
141  void UseBlackList()
142  {
143  mitk::IOUtil::LoadInfo info(m_ImagePath);
144 
146  CPPUNIT_ASSERT(true == functor(info));
147  auto description = info.m_ReaderSelector.GetSelected().GetDescription();
148  CPPUNIT_ASSERT(description != "Unwanted Test Service");
149  }
150 
151  void UseBlackAndPreferenceList()
152  {
153  mitk::IOUtil::LoadInfo info(m_ImagePath);
154 
156  CPPUNIT_ASSERT(true == functor(info));
157  auto description = info.m_ReaderSelector.GetSelected().GetDescription();
158  CPPUNIT_ASSERT_EQUAL(std::string("Prefered Test Service"), description);
159  }
160 
161  void UseOverlappingBlackAndPreferenceList()
162  {
163  mitk::IOUtil::LoadInfo info(m_ImagePath);
164 
165  black.push_back("Prefered Test Service");
166  black.push_back("Normal Test Service");
167 
169  CPPUNIT_ASSERT(true == functor(info));
170  auto description = info.m_ReaderSelector.GetSelected().GetDescription();
171  CPPUNIT_ASSERT_EQUAL(std::string("ITK NrrdImageIO"), description);
172  }
173 
174  void UsePreferenceListWithInexistantReaders()
175  {
176  mitk::IOUtil::LoadInfo info(m_ImagePath);
177  preference.push_back("InexistantReader");
178 
180  CPPUNIT_ASSERT(true == functor(info));
181  auto description = info.m_ReaderSelector.GetSelected().GetDescription();
182  CPPUNIT_ASSERT_EQUAL(std::string("Prefered Test Service"), description);
183  }
184 
185  void UseAllBlackedList()
186  {
187  mitk::IOUtil::LoadInfo info(m_ImagePath);
188 
189  for (auto reader : info.m_ReaderSelector.Get())
190  {
191  black.push_back(reader.GetDescription());
192  }
193 
195  CPPUNIT_ASSERT_THROW(functor(info), mitk::Exception);
196  }
197 };
198 
199 MITK_TEST_SUITE_REGISTRATION(mitkPreferenceListReaderOptionsFunctor)
Option callback functor with a preference list/ black list option selection strategy.
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
US_Core_EXPORT const std::string & SERVICE_RANKING()
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
DataCollection - Class to facilitate loading/accessing structured data.
std::vector< itk::SmartPointer< BaseData > > Read() override=0
Reads a path or stream and creates a list of BaseData objects.
void SetComment(const std::string &comment)
static void info(const char *fmt,...)
Definition: svm.cpp:86
The CustomMimeType class represents a custom mime-type which may be registered as a service object...
static std::string CATEGORY_IMAGES()
us::ServiceRegistration< IFileReader > RegisterService(us::ModuleContext *context=us::GetModuleContext())
ConfidenceLevel
A confidence level describing the confidence of the reader or writer in handling the given data...
Definition: mitkIFileIO.h:45
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.
DataStorage::SetOfObjects::Pointer Read(mitk::DataStorage &ds) override
Reads the specified file or input stream, loading its contents into the provided DataStorage.
void AddExtension(const std::string &extension)
US_UNORDERED_MAP_TYPE< std::string, Any > ServiceProperties
void SetCategory(const std::string &category)
Base class for creating mitk::BaseData objects from files or streams.
static mitk::PlanarFigure::Pointer Clone(mitk::PlanarFigure::Pointer original)
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.