Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkPropertyExtensionsTest.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 // Testing
14 #include "mitkTestFixture.h"
15 #include "mitkTestingMacros.h"
16 // std includes
17 #include <string>
18 #include <algorithm>
19 // MITK includes
20 #include <mitkCoreServices.h>
22 #include <mitkPropertyExtension.h>
23 // VTK includes
24 #include <vtkDebugLeaks.h>
25 
26 class TestPropertyExtension : public mitk::PropertyExtension
27 {
28 public:
29  mitkClassMacro(TestPropertyExtension, mitk::PropertyExtension);
30  mitkNewMacro1Param(Self, const std::string &);
31 
32  std::string GetName() const { return m_Name; }
33 private:
34  explicit TestPropertyExtension(const std::string &name) : m_Name(name) {}
35  ~TestPropertyExtension() override {}
36  std::string m_Name;
37 };
38 
39 class mitkPropertyExtensionsTestSuite : public mitk::TestFixture
40 {
41  CPPUNIT_TEST_SUITE(mitkPropertyExtensionsTestSuite);
42  MITK_TEST(GetPropertyExtensionService_Success);
43  MITK_TEST(GetPropertyExtension_Success);
44  MITK_TEST(GetOverwrittenExtension_Success);
45  MITK_TEST(GetPropertyExtensionRestricted_Success);
46  CPPUNIT_TEST_SUITE_END();
47 
48 private:
49  mitk::IPropertyExtensions *m_PropertyExtensions;
50  TestPropertyExtension::Pointer m_Extension1;
51 
52 public:
53  void setUp()
54  {
55  m_PropertyExtensions = mitk::CoreServices::GetPropertyExtensions();
56  m_PropertyExtensions->AddExtension("propertyName1", TestPropertyExtension::New("extension1a").GetPointer());
57  m_PropertyExtensions->AddExtension("propertyName1", TestPropertyExtension::New("extension1b").GetPointer());
58  }
59 
60  void tearDown()
61  {
62  m_PropertyExtensions = nullptr;
63  }
64 
65  void GetPropertyExtensionService_Success()
66  {
67  CPPUNIT_ASSERT_MESSAGE("Get property extensions service", m_PropertyExtensions != nullptr);
68  }
69 
70  void GetPropertyExtension_Success()
71  {
73  dynamic_cast<TestPropertyExtension *>(m_PropertyExtensions->GetExtension("propertyName1").GetPointer());
74 
75  CPPUNIT_ASSERT_MESSAGE("Get extension of \"propertyName1\"",
76  extension1.IsNotNull() && extension1->GetName() == "extension1a");
77  }
78 
79  void GetOverwrittenExtension_Success()
80  {
81  m_PropertyExtensions->AddExtension("propertyName1", TestPropertyExtension::New("extension1b").GetPointer(), "", true);
82  m_Extension1 = dynamic_cast<TestPropertyExtension *>(m_PropertyExtensions->GetExtension("propertyName1").GetPointer());
83  CPPUNIT_ASSERT_MESSAGE("Get overwritten extension of \"propertyName1\"",
84  m_Extension1.IsNotNull() && m_Extension1->GetName() == "extension1b");
85  }
86 
87  void GetPropertyExtensionRestricted_Success()
88  {
89  m_PropertyExtensions->AddExtension("propertyName1", TestPropertyExtension::New("extension1c").GetPointer(), "className");
91  dynamic_cast<TestPropertyExtension *>(m_PropertyExtensions->GetExtension("propertyName1", "className").GetPointer());
92  m_Extension1 = dynamic_cast<TestPropertyExtension *>(m_PropertyExtensions->GetExtension("propertyName1").GetPointer());
93 
94  CPPUNIT_ASSERT_MESSAGE("Get extension of \"propertyName1\" restricted to \"className\"",
95  m_Extension1.IsNotNull() && m_Extension1->GetName() == "extension1b" && extension2.IsNotNull() &&
96  extension2->GetName() == "extension1c");
97  }
98  };
99  MITK_TEST_SUITE_REGISTRATION(mitkPropertyExtensions)
100 
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
#define mitkNewMacro1Param(classname, type)
Definition: mitkCommon.h:72
static IPropertyExtensions * GetPropertyExtensions(us::ModuleContext *context=us::GetModuleContext())
Get an IPropertyExtensions instance.
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
virtual PropertyExtension::Pointer GetExtension(const std::string &propertyName, const std::string &className="")=0
Get the extension of a specific property.
Interface of property extensions service.
#define mitkClassMacro(className, SuperClassName)
Definition: mitkCommon.h:40
Test fixture for parameterized tests.
Base class for all property extensions.
static std::string GetName(std::string fileName, std::string suffix)
virtual bool AddExtension(const std::string &propertyName, PropertyExtension::Pointer extension, const std::string &className="", bool overwrite=false)=0
Add an extension to a specific property.