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