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