Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkPropertyPersistenceTest.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 "mitkIOMimeTypes.h"
15 #include "mitkStringProperty.h"
16 
17 #include "mitkTestFixture.h"
18 #include "mitkTestingMacros.h"
19 
20 #include <mitkEqual.h>
21 #include <mitkNumericConstants.h>
22 
23 #include <algorithm>
24 
25 class mitkPropertyPersistenceTestSuite : public mitk::TestFixture
26 {
27  CPPUNIT_TEST_SUITE(mitkPropertyPersistenceTestSuite);
28 
29  MITK_TEST(AddInfo);
30  MITK_TEST(GetInfo);
31  MITK_TEST(GetInfo_mime);
32  MITK_TEST(GetInfoByKey);
33  MITK_TEST(HasInfo);
34  MITK_TEST(RemoveAllInfo);
35  MITK_TEST(RemoveInfo);
36  MITK_TEST(RemoveInfo_withMime);
37 
38  CPPUNIT_TEST_SUITE_END();
39 
40 private:
47 
50 
51  std::string prop1;
52  std::string prop2;
53  std::string prop3;
54  std::string prop4;
55  std::string prop5;
56  std::string prop6;
57 
58  std::string propX;
59  std::string keyX;
60  std::string propXTemplate;
61  std::string keyXTemplate;
62  std::string propX2;
63 
65 
66  static bool checkExistance(const mitk::PropertyPersistence::InfoResultType &infos,
68  {
69  auto predicate = [info](const mitk::PropertyPersistenceInfo::ConstPointer &x) { return infosAreEqual(info, x); };
70 
71  auto finding = std::find_if(infos.begin(), infos.end(), predicate);
72 
73  bool result = finding != infos.end();
74  return result;
75  }
76 
77  static bool infosAreEqual(const mitk::PropertyPersistenceInfo *ref, const mitk::PropertyPersistenceInfo *info)
78  {
79  bool result = true;
80 
81  if (!info || !ref)
82  {
83  return false;
84  }
85 
86  result = result && ref->GetName() == info->GetName();
87  result = result && ref->GetKey() == info->GetKey();
88  result = result && ref->GetMimeTypeName() == info->GetMimeTypeName();
89  return result;
90  }
91 
92 public:
93  void setUp() override
94  {
96 
97  prop1 = "prop1";
98  prop2 = "prop1";
99  prop3 = "prop1";
100  prop4 = "prop4";
101  prop5 = "prop5";
102 
103  propX = "prop(\\d*)";
104  keyX = "key(\\d*)";
105  propXTemplate = "prop$1";
106  keyXTemplate = "key.$1";
107 
108  propX2 = "otherprop(\\d*)";
109 
111  info1->SetNameAndKey(prop1, "key1");
112  info2 = mitk::PropertyPersistenceInfo::New(prop2, "mime2");
113  info2->SetNameAndKey(prop2, "key2");
114  info3 = mitk::PropertyPersistenceInfo::New(prop3, "mime3");
115  info3->SetNameAndKey(prop3, "key3");
116  info4 = mitk::PropertyPersistenceInfo::New(prop4, "mime2");
117  info4->SetNameAndKey(prop4, "key2");
118  info5 = mitk::PropertyPersistenceInfo::New(prop5, "mime5");
119  info5->SetNameAndKey(prop5, "key5");
120 
121  infoX = mitk::PropertyPersistenceInfo::New("", "mimeX");
122  infoX->UseRegEx(propX, propXTemplate, keyX, keyXTemplate);
123 
125  infoX2->UseRegEx(propX2, propXTemplate);
126 
127  service->AddInfo(info1, false);
128  service->AddInfo(info2, false);
129  service->AddInfo(info3, false);
130  service->AddInfo(info4, false);
131  service->AddInfo(info5, false);
132  service->AddInfo(infoX, false);
133  service->AddInfo(infoX2, false);
134  }
135 
136  void tearDown() override { delete service; }
137  void AddInfo()
138  {
140  info2_new->SetNameAndKey(prop2, "newKey");
142  info2_otherKey->SetNameAndKey(prop2, "otherKey");
144  info_newPropNKey->SetNameAndKey("newProp", "newKey");
145 
146  CPPUNIT_ASSERT_MESSAGE("Testing addinfo of already existing info (no overwrite) -> no adding",
147  !service->AddInfo(info2_otherKey, false));
148  CPPUNIT_ASSERT_MESSAGE(
149  "Testing addinfo of already existing info (no overwrite) -> no adding -> key should not be changed.",
150  service->GetInfo(prop2, "mime2", false).front()->GetKey() == "key2");
151 
152  CPPUNIT_ASSERT_MESSAGE("Testing addinfo of already existing info (overwrite) -> adding",
153  service->AddInfo(info2_otherKey, true));
154  CPPUNIT_ASSERT_MESSAGE(
155  "Testing addinfo of already existing info (no overwrite) -> adding -> key should be changed.",
156  service->GetInfo(prop2, "mime2", false).front()->GetKey() == "otherKey");
157 
158  CPPUNIT_ASSERT_MESSAGE("Testing addinfo of info (other mime type; no overwrite) -> adding",
159  service->AddInfo(info2_new, false));
160  CPPUNIT_ASSERT_MESSAGE("Testing addinfo of info (other mime type; no overwrite) -> adding -> info exists.",
161  !service->GetInfo(prop2, "otherMime", false).empty());
162 
163  CPPUNIT_ASSERT_MESSAGE("Testing addinfo of info (new prop name; no overwrite) -> adding",
164  service->AddInfo(info_newPropNKey, false));
165  CPPUNIT_ASSERT_MESSAGE("Testing addinfo of info (new prop name; no overwrite) -> adding ->info exists.",
166  !service->GetInfo("newProp", "otherMime", false).empty());
167  }
168 
169  void GetInfo()
170  {
171  mitk::PropertyPersistence::InfoResultType infos = service->GetInfo(prop1, false);
172  CPPUNIT_ASSERT(infos.size() == 3);
173  CPPUNIT_ASSERT_MESSAGE("Check expected element 1.", checkExistance(infos, info1));
174  CPPUNIT_ASSERT_MESSAGE("Check expected element 1.", checkExistance(infos, info2));
175  CPPUNIT_ASSERT_MESSAGE("Check expected element 1.", checkExistance(infos, info3));
176 
177  infos = service->GetInfo(prop4, false);
178  CPPUNIT_ASSERT(infos.size() == 1);
179  CPPUNIT_ASSERT_MESSAGE("Check expected element 1.", checkExistance(infos, info4));
180 
181  infos = service->GetInfo("unkown", false);
182  CPPUNIT_ASSERT_MESSAGE("Check size of result for unkown prop.", infos.empty());
183 
184  infos = service->GetInfo("prop101", false);
185  CPPUNIT_ASSERT(infos.empty());
186 
187  infos = service->GetInfo("prop101", true);
188  CPPUNIT_ASSERT(infos.size() == 1);
189  CPPUNIT_ASSERT_MESSAGE("Check Name of expected element 1.", infos.front()->GetName() == "prop101");
190  CPPUNIT_ASSERT_MESSAGE("Check Key of expected element 1.", infos.front()->GetKey() == "key.101");
191  CPPUNIT_ASSERT_MESSAGE("Check MimeTypeName of expected element 1.", infos.front()->GetMimeTypeName() == "mimeX");
192  }
193 
194  void GetInfoByKey()
195  {
196  mitk::PropertyPersistence::InfoResultType infos = service->GetInfoByKey("key2", false);
197  CPPUNIT_ASSERT(infos.size() == 2);
198  CPPUNIT_ASSERT_MESSAGE("Check expected element 1.", checkExistance(infos, info2));
199  CPPUNIT_ASSERT_MESSAGE("Check expected element 2.", checkExistance(infos, info4));
200 
201  infos = service->GetInfoByKey("key5", false);
202  CPPUNIT_ASSERT(infos.size() == 1);
203  CPPUNIT_ASSERT_MESSAGE("Check expected element 1.", checkExistance(infos, info5));
204 
205  infos = service->GetInfoByKey("unkownkey", false);
206  CPPUNIT_ASSERT_MESSAGE("Check size of result for unkown key.", infos.empty());
207 
208  infos = service->GetInfoByKey("key101", false);
209  CPPUNIT_ASSERT_MESSAGE("Check size of result for key101.", infos.empty());
210 
211  infos = service->GetInfoByKey("key101", true);
212  CPPUNIT_ASSERT(infos.size() == 1);
213  CPPUNIT_ASSERT_MESSAGE("Check Name of expected element 1.", infos.front()->GetName() == "prop101");
214  CPPUNIT_ASSERT_MESSAGE("Check Key of expected element 1.", infos.front()->GetKey() == "key101");
215  CPPUNIT_ASSERT_MESSAGE("Check MimeTypeName of expected element 1.", infos.front()->GetMimeTypeName() == "mimeX");
216  }
217 
218  void GetInfo_mime()
219  {
220  mitk::PropertyPersistence::InfoResultType infos = service->GetInfo(prop1, "mime2", false, false);
221  CPPUNIT_ASSERT_MESSAGE("Check GetInfos (existing element, no wildcard allowed, wildcard exists).",
222  infosAreEqual(info2, infos.front()));
223  infos = service->GetInfo(prop1, "mime2", true, false);
224  CPPUNIT_ASSERT_MESSAGE("Check GetInfos (existing element, wildcard allowed, wildcard exists).",
225  infosAreEqual(info2, infos.front()));
226  infos = service->GetInfo(prop1, "unknownmime", false, false);
227  CPPUNIT_ASSERT_MESSAGE("Check GetInfos (inexisting element, no wildcard allowed, wildcard exists).", infos.empty());
228  infos = service->GetInfo(prop1, "unknownmime", true, false);
229  CPPUNIT_ASSERT_MESSAGE("Check GetInfos (inexisting element, wildcard allowed, wildcard exists).",
230  infosAreEqual(info1, infos.front()));
231 
232  infos = service->GetInfo(prop4, "unknownmime", false, false);
233  CPPUNIT_ASSERT_MESSAGE("Check GetInfos (inexisting element, no wildcard allowed).", infos.empty());
234  infos = service->GetInfo(prop4, "unknownmime", true, false);
235  CPPUNIT_ASSERT_MESSAGE("Check GetInfos (inexisting element, wildcard allowed).", infos.empty());
236 
237  infos = service->GetInfo("prop101", "unknownmime", false, true);
238  CPPUNIT_ASSERT_MESSAGE("Check GetInfos (inexisting mime, no wildcard allowed, regex allowed).", infos.empty());
239 
240  infos = service->GetInfo("prop101", "mimeX", false, true);
241  CPPUNIT_ASSERT_MESSAGE("Check GetInfos (existing mime, no wildcard allowed, regex allowed).", infos.size() == 1);
242 
243  infos = service->GetInfo("otherprop", "unknownmime", false, false);
244  CPPUNIT_ASSERT_MESSAGE("Check GetInfos (inexisting mime, no wildcard allowed, no regex allowed).", infos.empty());
245 
246  infos = service->GetInfo("otherprop", "unknownmime", true, false);
247  CPPUNIT_ASSERT_MESSAGE("Check GetInfos (inexisting mime, wildcard allowed, no regex allowed).", infos.empty());
248 
249  infos = service->GetInfo("otherprop", "unknownmime", false, true);
250  CPPUNIT_ASSERT_MESSAGE("Check GetInfos (inexisting mime, no wildcard allowed, regex allowed).", infos.empty());
251 
252  infos = service->GetInfo("otherprop", "unknownmime", true, true);
253  CPPUNIT_ASSERT_MESSAGE("Check GetInfos (inexisting mime, wildcard allowed, regex allowed).", infos.size() == 1);
254  }
255 
256  void HasInfo()
257  {
258  CPPUNIT_ASSERT_MESSAGE("Check HasInfos (prop1)", service->HasInfo(prop1));
259  CPPUNIT_ASSERT_MESSAGE("Check HasInfos (prop4)", service->HasInfo(prop4));
260  CPPUNIT_ASSERT_MESSAGE("Check HasInfos (unkown prop)", !service->HasInfo("unkownProp"));
261  }
262 
263  void RemoveAllInfo()
264  {
265  CPPUNIT_ASSERT_NO_THROW(service->RemoveAllInfo());
266  CPPUNIT_ASSERT_MESSAGE("Check HasInfos (prop1)", !service->HasInfo(prop1));
267  CPPUNIT_ASSERT_MESSAGE("Check HasInfos (prop4)", !service->HasInfo(prop4));
268  CPPUNIT_ASSERT_MESSAGE("Check HasInfos (prop5)", !service->HasInfo(prop5));
269  }
270 
271  void RemoveInfo()
272  {
273  CPPUNIT_ASSERT_NO_THROW(service->RemoveInfo(prop1));
274  CPPUNIT_ASSERT_MESSAGE("Check HasInfos (prop1)", !service->HasInfo(prop1, false));
275  CPPUNIT_ASSERT_MESSAGE("Check HasInfos (prop4)", service->HasInfo(prop4, false));
276  CPPUNIT_ASSERT_MESSAGE("Check HasInfos (prop5)", service->HasInfo(prop5, false));
277 
278  CPPUNIT_ASSERT_NO_THROW(service->RemoveInfo(prop4));
279  CPPUNIT_ASSERT_MESSAGE("Check HasInfos (prop4)", !service->HasInfo(prop4, false));
280  CPPUNIT_ASSERT_MESSAGE("Check HasInfos (prop5)", service->HasInfo(prop5, false));
281 
282  CPPUNIT_ASSERT_NO_THROW(service->RemoveInfo(prop5));
283  CPPUNIT_ASSERT_MESSAGE("Check HasInfos (prop5)", !service->HasInfo(prop5, false));
284 
285  CPPUNIT_ASSERT_NO_THROW(service->RemoveInfo("unknown_prop"));
286  }
287 
288  void RemoveInfo_withMime()
289  {
290  CPPUNIT_ASSERT_NO_THROW(service->RemoveInfo(prop1, "mime2"));
291  CPPUNIT_ASSERT_MESSAGE("Check RemoveInfos if info was removed", service->GetInfo(prop1, "mime2", false).empty());
292  CPPUNIT_ASSERT_MESSAGE("Check RemoveInfos, if other info of same property name still exists",
293  !service->GetInfo(prop1, "mime3", false).empty());
294  CPPUNIT_ASSERT_MESSAGE("Check RemoveInfos, if other info of other property name but same mime still exists",
295  !service->GetInfo(prop4, "mime2", false).empty());
296 
297  CPPUNIT_ASSERT_NO_THROW(service->RemoveInfo(prop5, "wrongMime"));
298  CPPUNIT_ASSERT_MESSAGE("Check RemoveInfos on prop 5 with wrong mime", service->HasInfo(prop5, false));
299 
300  CPPUNIT_ASSERT_NO_THROW(service->RemoveInfo(prop5, "mime5"));
301  CPPUNIT_ASSERT_MESSAGE("Check RemoveInfos on prop 5", !service->HasInfo(prop5, false));
302 
303  CPPUNIT_ASSERT_NO_THROW(service->RemoveInfo("unkown_prop", "mime2"));
304  CPPUNIT_ASSERT_MESSAGE("Check RemoveInfos, if unkown property name but exting mime was used",
305  service->HasInfo(prop4, false));
306  }
307 };
308 
309 MITK_TEST_SUITE_REGISTRATION(mitkPropertyPersistence)
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
virtual void RemoveInfo(const std::string &propertyName)=0
Remove persistence info instances of a specific property name/regex.
virtual InfoResultType GetInfoByKey(const std::string &persistenceKey, bool allowKeyRegEx=true) const =0
Get the persistence info that will use the specified key.
virtual void RemoveAllInfo()=0
Remove all persistence info.
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
virtual bool HasInfo(const std::string &propertyName, bool allowNameRegEx=true) const =0
Check if a specific base data property has persistence info.
MITKCORE_EXPORT IPropertyPersistence * CreateTestInstancePropertyPersistence()
IPropertyPersistence::InfoResultType InfoResultType
static void info(const char *fmt,...)
Definition: svm.cpp:86
Property persistence info. This class is used to specify the way the persistance of a property of Bas...
const MimeTypeNameType & GetMimeTypeName() const
virtual InfoResultType GetInfo(const std::string &propertyName, bool allowNameRegEx=true) const =0
Get the persistence info for a specific base data property.
Test fixture for parameterized tests.
Interface of property persistence service.
virtual bool AddInfo(const PropertyPersistenceInfo *info, bool overwrite=false)=0
Add persistence info for a specific base data property. If there is already a property info instance ...