Medical Imaging Interaction Toolkit  2018.4.99-12ad79a3
Medical Imaging Interaction Toolkit
mitkPropertyPersistenceInfoTest.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 #include "mitkTestFixture.h"
17 #include "mitkTestingMacros.h"
18 
19 #include <limits>
20 
21 ::std::string testSerializeFunction(const mitk::BaseProperty * /*prop*/)
22 {
23  return "testSerialize";
24 }
25 
27 {
29  return result.GetPointer();
30 }
31 
32 class mitkPropertyPersistenceInfoTestSuite : public mitk::TestFixture
33 {
34  CPPUNIT_TEST_SUITE(mitkPropertyPersistenceInfoTestSuite);
35 
36  MITK_TEST(CheckDefaultInfo);
37  MITK_TEST(SetName);
38  MITK_TEST(SetNameAndKey);
39  MITK_TEST(SetMimeTypeName);
40  MITK_TEST(UseRegEx);
41  MITK_TEST(UseRegEx2);
42  MITK_TEST(UnRegExByName);
43  MITK_TEST(UnRegExByKey);
44  MITK_TEST(SetDeserializationFunction);
45  MITK_TEST(SetSerializationFunction);
48 
49  CPPUNIT_TEST_SUITE_END();
50 
51 private:
53  std::string refSerialization;
54  mitk::StringProperty::Pointer refProp_testFunction;
55  std::string refSerialization_testFunction;
58 
59  std::string nameRegEx;
60  std::string keyRegEx;
61  std::string nameTemplate;
62  std::string keyTemplate;
63 
64 public:
65  void setUp() override
66  {
67  refSerialization = "my_shiny_test_value";
68  refProp = mitk::StringProperty::New("my_shiny_test_value");
70  refSerialization_testFunction = "testSerialize";
71  refProp_testFunction = mitk::StringProperty::New("testSerialize");
72 
73  nameRegEx = "name(\\d*)";
74  nameTemplate = "$1_name";
75  keyRegEx = "key(\\d*)";
76  keyTemplate = "newkey_[$1]";
77 
79  regexInfo->UseRegEx(nameRegEx, nameTemplate, keyRegEx, keyTemplate);
80  }
81 
82  void tearDown() override {}
83  void CheckDefaultInfo()
84  {
85  CPPUNIT_ASSERT_MESSAGE(
86  "Testing PropertyPersistenceInfo::ANY_MIMETYPE_NAME()",
88  CPPUNIT_ASSERT_MESSAGE("Testing PropertyPersistenceInfo::GetName()", info->GetName() == "");
89  CPPUNIT_ASSERT_MESSAGE("Testing PropertyPersistenceInfo::GetKey()", info->GetKey() == "");
90  CPPUNIT_ASSERT_MESSAGE("Testing PropertyPersistenceInfo::GetMimeTypeName()",
91  info->GetMimeTypeName() == mitk::PropertyPersistenceInfo::ANY_MIMETYPE_NAME());
92  CPPUNIT_ASSERT_MESSAGE("Testing PropertyPersistenceInfo::IsRegEx()", !info->IsRegEx());
93 
94  CPPUNIT_ASSERT_MESSAGE("Testing PropertyPersistenceInfo::IsRegEx()", regexInfo->IsRegEx());
95 
96  mitk::BaseProperty::Pointer prop = info->GetDeserializationFunction()(refSerialization);
97  CPPUNIT_ASSERT_MESSAGE("Testing PropertyPersistenceInfo::GetDeserializationFunction() producing a StringProperty",
98  dynamic_cast<mitk::StringProperty *>(prop.GetPointer()));
99  CPPUNIT_ASSERT_MESSAGE(
100  "Testing PropertyPersistenceInfo::GetDeserializationFunction() producing a StringProperty with correct value",
101  prop->GetValueAsString() == refSerialization);
102 
103  std::string value = info->GetSerializationFunction()(refProp);
104  CPPUNIT_ASSERT_MESSAGE("Testing PropertyPersistenceInfo::GetSerializationFunction()", value == refSerialization);
105  }
106 
107  void SetName()
108  {
109  info->SetName("MyName");
110  CPPUNIT_ASSERT_MESSAGE("Testing PropertyPersistenceInfo::SetKey() changed name", info->GetName() == "MyName");
111  CPPUNIT_ASSERT_MESSAGE("Testing PropertyPersistenceInfo::SetKey() changed key", info->GetKey() == "MyName");
112 
113  // test if setter resets to non regex
114  regexInfo->SetName("MyName");
115  CPPUNIT_ASSERT_MESSAGE("Testing regex reset when using SetName()", !regexInfo->IsRegEx());
116  }
117 
118  void SetNameAndKey()
119  {
120  info->SetNameAndKey("MyName", "MyKey");
121  CPPUNIT_ASSERT_MESSAGE("Testing PropertyPersistenceInfo::SetNameAndKey() changed name",
122  info->GetName() == "MyName");
123  CPPUNIT_ASSERT_MESSAGE("Testing PropertyPersistenceInfo::SetNameAndKey() changed key", info->GetKey() == "MyKey");
124 
125  // test if setter resets to non regex
126  regexInfo->SetNameAndKey("MyName", "MyKey");
127  CPPUNIT_ASSERT_MESSAGE("Testing regex reset when using SetNameAndKey()", !regexInfo->IsRegEx());
128  }
129 
130  void SetMimeTypeName()
131  {
132  info->SetMimeTypeName("newMime");
133  CPPUNIT_ASSERT_MESSAGE("Testing PropertyPersistenceInfo::SetMimeTypeName()", info->GetMimeTypeName() == "newMime");
134  }
135 
136  void UseRegEx()
137  {
138  info->UseRegEx(nameRegEx, nameTemplate);
139  CPPUNIT_ASSERT_MESSAGE("Testing PropertyPersistenceInfo::IsRegEx()", info->IsRegEx());
140  CPPUNIT_ASSERT_MESSAGE("Testing PropertyPersistenceInfo::UseRegEx() changed name", info->GetName() == nameRegEx);
141  CPPUNIT_ASSERT_MESSAGE("Testing PropertyPersistenceInfo::UseRegEx() changed key", info->GetKey() == nameRegEx);
142  CPPUNIT_ASSERT_MESSAGE("Testing PropertyPersistenceInfo::UseRegEx() changed name template",
143  info->GetNameTemplate() == nameTemplate);
144  CPPUNIT_ASSERT_MESSAGE("Testing PropertyPersistenceInfo::UseRegEx() changed key template",
145  info->GetKeyTemplate() == nameTemplate);
146  }
147 
148  void UseRegEx2()
149  {
150  info->UseRegEx(nameRegEx, nameTemplate, keyRegEx, keyTemplate);
151  CPPUNIT_ASSERT_MESSAGE("Testing PropertyPersistenceInfo::IsRegEx()", info->IsRegEx());
152  CPPUNIT_ASSERT_MESSAGE("Testing PropertyPersistenceInfo::UseRegEx() changed name", info->GetName() == nameRegEx);
153  CPPUNIT_ASSERT_MESSAGE("Testing PropertyPersistenceInfo::UseRegEx() changed key", info->GetKey() == keyRegEx);
154  CPPUNIT_ASSERT_MESSAGE("Testing PropertyPersistenceInfo::UseRegEx() changed name template",
155  info->GetNameTemplate() == nameTemplate);
156  CPPUNIT_ASSERT_MESSAGE("Testing PropertyPersistenceInfo::UseRegEx() changed key template",
157  info->GetKeyTemplate() == keyTemplate);
158  }
159 
160  void UnRegExByName()
161  {
162  mitk::PropertyPersistenceInfo::Pointer newInfo = regexInfo->UnRegExByName("name42");
163  CPPUNIT_ASSERT_MESSAGE("Testing IsRegEx() of \"unreg\"ed regexInfo", !newInfo->IsRegEx());
164  CPPUNIT_ASSERT_MESSAGE("Testing GetName() of \"unreg\"ed regexInfo", newInfo->GetName() == "name42");
165  CPPUNIT_ASSERT_MESSAGE("Testing GetKey() of \"unreg\"ed regexInfo", newInfo->GetKey() == "newkey_[42]");
166 
167  newInfo = info->UnRegExByName("name42");
168  CPPUNIT_ASSERT_MESSAGE("Testing IsRegEx() of \"unreg\"ed info", !newInfo->IsRegEx());
169  CPPUNIT_ASSERT_MESSAGE("Testing GetName() of \"unreg\"ed info", newInfo->GetName() == info->GetName());
170  CPPUNIT_ASSERT_MESSAGE("Testing GetKey() of \"unreg\"ed info", newInfo->GetKey() == info->GetKey());
171  }
172 
173  void UnRegExByKey()
174  {
175  mitk::PropertyPersistenceInfo::Pointer newInfo = regexInfo->UnRegExByKey("key42");
176  CPPUNIT_ASSERT_MESSAGE("Testing IsRegEx() of \"unreg\"ed regexInfo", !newInfo->IsRegEx());
177  CPPUNIT_ASSERT_MESSAGE("Testing GetName() of \"unreg\"ed regexInfo", newInfo->GetName() == "42_name");
178  CPPUNIT_ASSERT_MESSAGE("Testing GetKey() of \"unreg\"ed regexInfo", newInfo->GetKey() == "key42");
179 
180  newInfo = info->UnRegExByKey("key42");
181  CPPUNIT_ASSERT_MESSAGE("Testing IsRegEx() of \"unreg\"ed info", !newInfo->IsRegEx());
182  CPPUNIT_ASSERT_MESSAGE("Testing GetName() of \"unreg\"ed info", newInfo->GetName() == info->GetName());
183  CPPUNIT_ASSERT_MESSAGE("Testing GetKey() of \"unreg\"ed info", newInfo->GetKey() == info->GetKey());
184  }
185 
186  void SetDeserializationFunction()
187  {
188  info->SetDeserializationFunction(testDeserializeFunction);
189  mitk::BaseProperty::Pointer prop = info->GetDeserializationFunction()(refSerialization);
190  CPPUNIT_ASSERT_MESSAGE(
191  "Testing PropertyPersistenceInfo::SetDeserializationFunction() producing a StringProperty with correct value",
192  prop->GetValueAsString() == refSerialization_testFunction);
193  }
194 
195  void SetSerializationFunction()
196  {
197  info->SetSerializationFunction(testSerializeFunction);
198  std::string value = info->GetSerializationFunction()(refProp);
199  CPPUNIT_ASSERT_MESSAGE("Testing PropertyPersistenceInfo::SetSerializationFunction()",
200  value == refSerialization_testFunction);
201  }
202 
204  {
206  CPPUNIT_ASSERT_MESSAGE("Testing serializeByGetValueAsString()", value == refSerialization);
207  }
208 
210  {
213  CPPUNIT_ASSERT_MESSAGE("Testing deserializeToStringProperty() producing a StringProperty",
214  dynamic_cast<mitk::StringProperty *>(prop.GetPointer()));
215  CPPUNIT_ASSERT_MESSAGE("Testing deserializeToStringProperty() producing a StringProperty with correct value",
216  prop->GetValueAsString() == refSerialization);
217  }
218 };
219 
220 MITK_TEST_SUITE_REGISTRATION(mitkPropertyPersistenceInfo)
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
MITKCORE_EXPORT::std::string serializeByGetValueAsString(const mitk::BaseProperty *prop)
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
MITKCORE_EXPORT mitk::BaseProperty::Pointer deserializeToStringProperty(const std::string &value)
static void info(const char *fmt,...)
Definition: svm.cpp:86
Abstract base class for properties.
static std::string DEFAULT_BASE_NAME()
mitk::BaseProperty::Pointer testDeserializeFunction(const std::string &)
Test fixture for parameterized tests.
::std::string testSerializeFunction(const mitk::BaseProperty *)
static Pointer New()