Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkPropertyKeyPathTest.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 "mitkPropertyKeyPath.h"
14 
15 #include "mitkTestFixture.h"
16 #include "mitkTestingMacros.h"
17 
18 #include <regex>
19 
20 class mitkPropertyKeyPathTestSuite : public mitk::TestFixture
21 {
22  CPPUNIT_TEST_SUITE(mitkPropertyKeyPathTestSuite);
23 
24  MITK_TEST(AccessFunctions);
31  MITK_TEST(ExecutePropertyRegEx);
32  MITK_TEST(Comparison);
33 
34  CPPUNIT_TEST_SUITE_END();
35 
36 private:
37  mitk::PropertyKeyPath simplePath;
38  mitk::PropertyKeyPath simplePath2;
39  mitk::PropertyKeyPath deepPath;
40  mitk::PropertyKeyPath deepPath_withAnyElement;
41  mitk::PropertyKeyPath deepPath_withAnySelection;
42  mitk::PropertyKeyPath deepPath_withSelection;
43  mitk::PropertyKeyPath verydeepPath;
44 
45  mitk::PropertyKeyPath emptyPath;
46 
47 public:
48  void setUp() override
49  {
50  simplePath.AddElement("simple");
51 
52  simplePath2.AddElement("AA-11");
53 
54  deepPath.AddElement("a").AddElement("b2").AddElement("c3");
55 
56  deepPath_withAnyElement.AddElement("a");
57  deepPath_withAnyElement.AddAnyElement();
58  deepPath_withAnyElement.AddElement("c3");
59 
60  deepPath_withAnySelection.AddElement("a");
61  deepPath_withAnySelection.AddAnySelection("b");
62  deepPath_withAnySelection.AddElement("c");
63 
64  deepPath_withSelection.AddElement("a");
65  deepPath_withSelection.AddSelection("b", 6);
66  deepPath_withSelection.AddElement("c");
67 
68  verydeepPath.AddAnySelection("a");
69  verydeepPath.AddAnyElement();
70  verydeepPath.AddElement("c");
71  verydeepPath.AddSelection("d", 4);
72  verydeepPath.AddElement("e");
73  }
74 
75  void tearDown() override {}
76 
77  void AccessFunctions()
78  {
79  const auto constEmptyPath = emptyPath;
80  const auto constVerydeepPath = verydeepPath;
81 
82  CPPUNIT_ASSERT_THROW(emptyPath.GetFirstNode(), mitk::InvalidPathNodeException);
83  CPPUNIT_ASSERT_THROW(emptyPath.GetLastNode(), mitk::InvalidPathNodeException);
84  CPPUNIT_ASSERT_THROW(emptyPath.GetNode(0), mitk::InvalidPathNodeException);
85  CPPUNIT_ASSERT_THROW(constEmptyPath.GetFirstNode(), mitk::InvalidPathNodeException);
86  CPPUNIT_ASSERT_THROW(constEmptyPath.GetLastNode(), mitk::InvalidPathNodeException);
87  CPPUNIT_ASSERT_THROW(constEmptyPath.GetNode(0), mitk::InvalidPathNodeException);
88 
89  CPPUNIT_ASSERT_EQUAL_MESSAGE(
90  "Testing GetFirstNode with 'a.[*].*.c.d.[4].e'", std::string("a"), verydeepPath.GetFirstNode().name);
91  CPPUNIT_ASSERT_EQUAL_MESSAGE(
92  "Testing const GetFirstNode with 'a.[*].*.c.d.[4].e'", std::string("a"), constVerydeepPath.GetFirstNode().name);
93  CPPUNIT_ASSERT_EQUAL_MESSAGE(
94  "Testing GetLastNode with 'a.[*].*.c.d.[4].e'", std::string("e"), verydeepPath.GetLastNode().name);
95  CPPUNIT_ASSERT_EQUAL_MESSAGE(
96  "Testing GetLastNode with 'a.[*].*.c.d.[4].e'", std::string("e"), constVerydeepPath.GetLastNode().name);
97  CPPUNIT_ASSERT_EQUAL_MESSAGE(
98  "Testing GetNode(3) with 'a.[*].*.c.d.[4].e'", std::string("d"), verydeepPath.GetNode(3).name);
99  CPPUNIT_ASSERT_EQUAL_MESSAGE(
100  "Testing GetNode(3) with 'a.[*].*.c.d.[4].e'", std::string("d"), constVerydeepPath.GetNode(3).name);
101 
102  CPPUNIT_ASSERT(5 == constVerydeepPath.GetSize());
103  CPPUNIT_ASSERT(0 == emptyPath.GetSize());
104  }
105 
107  {
108  std::string result = mitk::PropertyKeyPathToPropertyRegEx(simplePath);
109  CPPUNIT_ASSERT_EQUAL_MESSAGE(
110  "Testing PropertyKeyPathToPropertyRegEx() with 'simple'", std::string("simple"), result);
111  result = mitk::PropertyKeyPathToPropertyRegEx(deepPath);
112  CPPUNIT_ASSERT_EQUAL_MESSAGE(
113  "Testing PropertyKeyPathToPropertyRegEx() with 'a.b2.c3'", std::string("a\\.b2\\.c3"), result);
114  result = mitk::PropertyKeyPathToPropertyRegEx(deepPath_withAnyElement);
115  CPPUNIT_ASSERT_EQUAL_MESSAGE(
116  "Testing PropertyKeyPathToPropertyRegEx() with 'a.*.c3'", std::string("a\\.([a-zA-Z0-9- ]+)\\.c3"), result);
117  result = mitk::PropertyKeyPathToPropertyRegEx(deepPath_withAnySelection);
118  CPPUNIT_ASSERT_EQUAL_MESSAGE(
119  "Testing PropertyKeyPathToPropertyRegEx() with 'a.b.[*].c'", std::string("a\\.b\\.\\[(\\d*)\\]\\.c"), result);
120  result = mitk::PropertyKeyPathToPropertyRegEx(deepPath_withSelection);
121  CPPUNIT_ASSERT_EQUAL_MESSAGE(
122  "Testing PropertyKeyPathToPropertyRegEx() with 'a.b.[6].c'", std::string("a\\.b\\.\\[6\\]\\.c"), result);
123  result = mitk::PropertyKeyPathToPropertyRegEx(verydeepPath);
124  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing PropertyKeyPathToPropertyRegEx() with 'a.[*].*.c.d.[4].e'",
125  std::string("a\\.\\[(\\d*)\\]\\.([a-zA-Z0-9- ]+)\\.c\\.d\\.\\[4\\]\\.e"),
126  result);
127  }
128 
130  {
131  std::string result = mitk::PropertyKeyPathToPersistenceKeyRegEx(simplePath);
132  CPPUNIT_ASSERT_EQUAL_MESSAGE(
133  "Testing PropertyKeyPathToPersistenceKeyRegEx() with 'simple'", std::string("simple"), result);
135  CPPUNIT_ASSERT_EQUAL_MESSAGE(
136  "Testing PropertyKeyPathToPersistenceKeyRegEx() with 'a.b2.c3'", std::string("a_b2_c3"), result);
137  result = mitk::PropertyKeyPathToPersistenceKeyRegEx(deepPath_withAnyElement);
138  CPPUNIT_ASSERT_EQUAL_MESSAGE(
139  "Testing PropertyKeyPathToPersistenceKeyRegEx() with 'a.*.c3'", std::string("a_([a-zA-Z0-9- ]+)_c3"), result);
140  result = mitk::PropertyKeyPathToPersistenceKeyRegEx(deepPath_withAnySelection);
141  CPPUNIT_ASSERT_EQUAL_MESSAGE(
142  "Testing PropertyKeyPathToPersistenceKeyRegEx() with 'a.b.[*].c'", std::string("a_b_\\[(\\d*)\\]_c"), result);
143  result = mitk::PropertyKeyPathToPersistenceKeyRegEx(deepPath_withSelection);
144  CPPUNIT_ASSERT_EQUAL_MESSAGE(
145  "Testing PropertyKeyPathToPersistenceKeyRegEx() with 'a.b.[6].c'", std::string("a_b_\\[6\\]_c"), result);
146  result = mitk::PropertyKeyPathToPersistenceKeyRegEx(verydeepPath);
147  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing PropertyKeyPathToPersistenceKeyRegEx() with 'a.[*].*.c.d.[4].e'",
148  std::string("a_\\[(\\d*)\\]_([a-zA-Z0-9- ]+)_c_d_\\[4\\]_e"),
149  result);
150  }
151 
153  {
154  std::string result = mitk::PropertyKeyPathToPersistenceKeyTemplate(simplePath);
155  CPPUNIT_ASSERT_EQUAL_MESSAGE(
156  "Testing PropertyKeyPathToPersistenceKeyTemplate() with 'simple'", std::string("simple"), result);
158  CPPUNIT_ASSERT_EQUAL_MESSAGE(
159  "Testing PropertyKeyPathToPersistenceKeyTemplate() with 'a.b2.c3'", std::string("a_b2_c3"), result);
160  result = mitk::PropertyKeyPathToPersistenceKeyTemplate(deepPath_withAnyElement);
161  CPPUNIT_ASSERT_EQUAL_MESSAGE(
162  "Testing PropertyKeyPathToPersistenceKeyTemplate() with 'a.*.c3'", std::string("a_$1_c3"), result);
163  result = mitk::PropertyKeyPathToPersistenceKeyTemplate(deepPath_withAnySelection);
164  CPPUNIT_ASSERT_EQUAL_MESSAGE(
165  "Testing PropertyKeyPathToPersistenceKeyTemplate() with 'a.b.[*].c'", std::string("a_b_[$1]_c"), result);
166  result = mitk::PropertyKeyPathToPersistenceKeyTemplate(deepPath_withSelection);
167  CPPUNIT_ASSERT_EQUAL_MESSAGE(
168  "Testing PropertyKeyPathToPersistenceKeyTemplate() with 'a.b.[6].c'", std::string("a_b_[6]_c"), result);
169  result = mitk::PropertyKeyPathToPersistenceKeyTemplate(verydeepPath);
170  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing PropertyKeyPathToPersistenceKeyTemplate() with 'a.[*].*.c.d.[4].e'",
171  std::string("a_[$1]_$2_c_d_[4]_e"),
172  result);
173  }
174 
176  {
177  std::string result = mitk::PropertyKeyPathToPersistenceNameTemplate(simplePath);
178  CPPUNIT_ASSERT_EQUAL_MESSAGE(
179  "Testing PropertyKeyPathToPersistenceNameTemplate() with 'simple'", std::string("simple"), result);
181  CPPUNIT_ASSERT_EQUAL_MESSAGE(
182  "Testing PropertyKeyPathToPersistenceNameTemplate() with 'a.b2.c3'", std::string("a.b2.c3"), result);
183  result = mitk::PropertyKeyPathToPersistenceNameTemplate(deepPath_withAnyElement);
184  CPPUNIT_ASSERT_EQUAL_MESSAGE(
185  "Testing PropertyKeyPathToPersistenceNameTemplate() with 'a.*.c3'", std::string("a.$1.c3"), result);
186  result = mitk::PropertyKeyPathToPersistenceNameTemplate(deepPath_withAnySelection);
187  CPPUNIT_ASSERT_EQUAL_MESSAGE(
188  "Testing PropertyKeyPathToPersistenceNameTemplate() with 'a.b.[*].c'", std::string("a.b.[$1].c"), result);
189  result = mitk::PropertyKeyPathToPersistenceNameTemplate(deepPath_withSelection);
190  CPPUNIT_ASSERT_EQUAL_MESSAGE(
191  "Testing PropertyKeyPathToPersistenceNameTemplate() with 'a.b.[6].c'", std::string("a.b.[6].c"), result);
192  result = mitk::PropertyKeyPathToPersistenceNameTemplate(verydeepPath);
193  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing PropertyKeyPathToPersistenceNameTemplate() with 'a.[*].*.c.d.[4].e'",
194  std::string("a.[$1].$2.c.d.[4].e"),
195  result);
196  }
197 
199  {
201  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing PropertyNameToPropertyKeyPath() with 'simple'", simplePath, result);
202  result = mitk::PropertyNameToPropertyKeyPath("a.b2.c3");
203  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing PropertyNameToPropertyKeyPath() with 'a.b2.c3'", deepPath, result);
204  result = mitk::PropertyNameToPropertyKeyPath("a.*.c3");
205  CPPUNIT_ASSERT_EQUAL_MESSAGE(
206  "Testing PropertyNameToPropertyKeyPath() with 'a.*.c3'", deepPath_withAnyElement, result);
207  result = mitk::PropertyNameToPropertyKeyPath("a.b.[*].c");
208  CPPUNIT_ASSERT_EQUAL_MESSAGE(
209  "Testing PropertyNameToPropertyKeyPath() with 'a.b.[*].c'", deepPath_withAnySelection, result);
210  result = mitk::PropertyNameToPropertyKeyPath("a.b.[6].c");
211  CPPUNIT_ASSERT_EQUAL_MESSAGE(
212  "Testing PropertyNameToPropertyKeyPath() with 'a.b.[6].c'", deepPath_withSelection, result);
213  result = mitk::PropertyNameToPropertyKeyPath("a.[*].*.c.d.[4].e");
214  CPPUNIT_ASSERT_EQUAL_MESSAGE(
215  "Testing PropertyNameToPropertyKeyPath() with 'a.[*].*.c.d.[4].e'", verydeepPath, result);
216 
217  result = mitk::PropertyNameToPropertyKeyPath("AA-11");
218  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing PropertyNameToPropertyKeyPath() with 'AA-11'", simplePath2, result);
219 
220  result = mitk::PropertyNameToPropertyKeyPath("$$$IlligalNameChar.sub");
221  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing PropertyNameToPropertyKeyPath() with wrong path", emptyPath, result);
222  result = mitk::PropertyNameToPropertyKeyPath("emptyNode..sub");
223  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing PropertyNameToPropertyKeyPath() with wrong path", emptyPath, result);
224  result = mitk::PropertyNameToPropertyKeyPath("wrongIndex.[d]");
225  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing PropertyNameToPropertyKeyPath() with wrong path", emptyPath, result);
226  }
227 
229  {
230  std::string result = mitk::PropertyKeyPathToPropertyName(simplePath);
231  CPPUNIT_ASSERT_EQUAL_MESSAGE(
232  "Testing PropertyKeyPathToPropertyName() with 'simple'", result, std::string("simple"));
233  result = mitk::PropertyKeyPathToPropertyName(deepPath);
234  CPPUNIT_ASSERT_EQUAL_MESSAGE(
235  "Testing PropertyKeyPathToPropertyName() with 'a.b2.c3'", result, std::string("a.b2.c3"));
236  result = mitk::PropertyKeyPathToPropertyName(deepPath_withAnyElement);
237  CPPUNIT_ASSERT_EQUAL_MESSAGE(
238  "Testing PropertyKeyPathToPropertyName() with 'a.*.c3'", result, std::string("a.*.c3"));
239  result = mitk::PropertyKeyPathToPropertyName(deepPath_withAnySelection);
240  CPPUNIT_ASSERT_EQUAL_MESSAGE(
241  "Testing PropertyKeyPathToPropertyName() with 'a.b.[*].c'", result, std::string("a.b.[*].c"));
242  result = mitk::PropertyKeyPathToPropertyName(deepPath_withSelection);
243  CPPUNIT_ASSERT_EQUAL_MESSAGE(
244  "Testing PropertyKeyPathToPropertyName() with 'a.b.[6].c'", result, std::string("a.b.[6].c"));
245  result = mitk::PropertyKeyPathToPropertyName(verydeepPath);
246  CPPUNIT_ASSERT_EQUAL_MESSAGE(
247  "Testing PropertyKeyPathToPropertyName() with 'a.[*].*.c.d.[4].e'", result, std::string("a.[*].*.c.d.[4].e"));
248  }
249 
250  void Comparison()
251  {
252  mitk::PropertyKeyPath deepPath_noSelection =
254 
255  CPPUNIT_ASSERT(deepPath_noSelection < deepPath);
256  CPPUNIT_ASSERT(deepPath_noSelection < deepPath_withSelection);
257  CPPUNIT_ASSERT(deepPath_withSelection < deepPath);
258  CPPUNIT_ASSERT(deepPath_withSelection < deepPath_withAnySelection);
259  CPPUNIT_ASSERT(deepPath_withAnyElement < deepPath_noSelection);
260 
261  CPPUNIT_ASSERT(!(deepPath_noSelection < deepPath_noSelection));
262  CPPUNIT_ASSERT(!(deepPath_noSelection > deepPath_noSelection));
263  CPPUNIT_ASSERT(deepPath_noSelection <= deepPath_noSelection);
264  CPPUNIT_ASSERT(deepPath_noSelection >= deepPath_noSelection);
265  }
266 
267  void ExecutePropertyRegEx()
268  {
269  std::regex regEx(mitk::PropertyKeyPathToPropertyRegEx(simplePath));
270  std::string result = mitk::PropertyKeyPathToPropertyName(simplePath);
271  CPPUNIT_ASSERT(std::regex_match(result, regEx));
272  regEx = std::regex(mitk::PropertyKeyPathToPropertyRegEx(deepPath));
273  result = mitk::PropertyKeyPathToPropertyName(deepPath);
274  CPPUNIT_ASSERT(std::regex_match(result, regEx));
275 
276  regEx = std::regex(mitk::PropertyKeyPathToPropertyRegEx(deepPath_withAnyElement));
277  result = mitk::PropertyKeyPathToPropertyName(deepPath_withAnyElement);
278  auto position = result.find("*");
279  if (std::string::npos != position)
280  {
281  result.replace(position, 1, "ConcreteNode1");
282  CPPUNIT_ASSERT(std::regex_match(result, regEx));
283  }
284 
285  regEx = std::regex(mitk::PropertyKeyPathToPropertyRegEx(deepPath_withAnySelection));
286  result = mitk::PropertyKeyPathToPropertyName(deepPath_withAnySelection);
287  position = result.find("[*]");
288  if (std::string::npos != position)
289  {
290  result.replace(position, 3, "[10]");
291  CPPUNIT_ASSERT(std::regex_match(result, regEx));
292  }
293 
294  regEx = std::regex(mitk::PropertyKeyPathToPropertyRegEx(deepPath_withSelection));
295  result = mitk::PropertyKeyPathToPropertyName(deepPath_withSelection);
296  CPPUNIT_ASSERT(std::regex_match(result, regEx));
297 
298  regEx = std::regex(mitk::PropertyKeyPathToPropertyRegEx(verydeepPath));
299  result = mitk::PropertyKeyPathToPropertyName(verydeepPath);
300  position = result.find("[*]");
301  if (std::string::npos != position)
302  {
303  result.replace(position, 3, "[1]");
304  position = result.find("*");
305  if (std::string::npos != position)
306  {
307  result.replace(position, 1, "ConcreteNode2");
308  CPPUNIT_ASSERT(std::regex_match(result, regEx));
309  }
310  }
311  }
312 };
313 
314 MITK_TEST_SUITE_REGISTRATION(mitkPropertyKeyPath)
MITKCORE_EXPORT std::string PropertyKeyPathToPropertyName(const PropertyKeyPath &tagPath)
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
PathIndexType GetSize() const
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
PropertyKeyPath & AddElement(const ElementNameType &name)
const NodeInfo & GetNode(const PathIndexType &index) const
PropertyKeyPath & AddSelection(const ElementNameType &name, ItemSelectionIndex index)
Test fixture for parameterized tests.
MITKCORE_EXPORT PropertyKeyPath PropertyNameToPropertyKeyPath(const std::string &propertyName)
MITKCORE_EXPORT std::string PropertyKeyPathToPersistenceNameTemplate(const PropertyKeyPath &tagPath)
MITKCORE_EXPORT std::string PropertyKeyPathToPersistenceKeyTemplate(const PropertyKeyPath &tagPath)
PropertyKeyPath & AddAnyElement()
Class that can be used to specify nested or wild carded property keys. E.g. for the use in context of...
MITKCORE_EXPORT std::string PropertyKeyPathToPropertyRegEx(const PropertyKeyPath &tagPath)
MITKCORE_EXPORT std::string PropertyKeyPathToPersistenceKeyRegEx(const PropertyKeyPath &tagPath)
PropertyKeyPath & AddAnySelection(const ElementNameType &name)