Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <opc/common/exception.h>
00012 #include <opc/common/addons_core/addon.h>
00013 #include <opc/common/addons_core/addon_manager.h>
00014 #include <opc/managers/device_manager/id.h>
00015 #include <opc/managers/property_tree/id.h>
00016
00017 #include <cppunit/extensions/HelperMacros.h>
00018 #include <cppunit/config/SourcePrefix.h>
00019
00020 class AddonsManagerTestCase : public CPPUNIT_NS::TestFixture
00021 {
00022 CPPUNIT_TEST_SUITE(AddonsManagerTestCase);
00023 CPPUNIT_TEST(TestTwoManagers);
00024 CPPUNIT_TEST(TestOneManager);
00025 CPPUNIT_TEST_SUITE_END();
00026
00027 public:
00028 virtual void setUp()
00029 {
00030 Addons = Common::CreateAddonsManager();
00031 }
00032
00033 virtual void tearDown()
00034 {
00035 Addons.reset();
00036 }
00037 protected:
00038 Common::AddonsManager::UniquePtr Addons;
00039
00040
00041 protected:
00042 void TestOneManager();
00043 void TestTwoManagers();
00044 };
00045
00046 CPPUNIT_TEST_SUITE_REGISTRATION( AddonsManagerTestCase );
00047
00048 unsigned InitializedAddonsCount = 0;
00049
00050
00051 class PropertyTreeAddon : public Common::Addon
00052 {
00053 private:
00054 bool Initialized;
00055 bool Stopped;
00056
00057 public:
00058 PropertyTreeAddon()
00059 : Initialized(false)
00060 , Stopped(false)
00061 {
00062 }
00063
00064 virtual void Initialize(Common::AddonsManager&, const Common::AddonParameters&)
00065 {
00066 CPPUNIT_ASSERT(InitializedAddonsCount == 0);
00067 InitializedAddonsCount++;
00068 Initialized = true;
00069 }
00070
00071 virtual void Stop()
00072 {
00073 Stopped = true;
00074 }
00075
00076 bool IsInitialized() const
00077 {
00078 return Initialized;
00079 }
00080
00081 bool IsStopped() const
00082 {
00083 return Stopped;
00084 }
00085 };
00086
00087 class PropertyTreeAddonFactory : public Common::AddonFactory
00088 {
00089 public:
00090 virtual Common::Addon::UniquePtr CreateAddon()
00091 {
00092 return Common::Addon::UniquePtr(new PropertyTreeAddon());
00093 }
00094 };
00095
00096 class DeviceIOManagerAddon : public Common::Addon
00097 {
00098 private:
00099 bool Initialized;
00100 bool Stopped;
00101
00102 public:
00103 DeviceIOManagerAddon()
00104 : Initialized(false)
00105 , Stopped(false)
00106 {
00107 }
00108
00109 virtual void Initialize(Common::AddonsManager&, const Common::AddonParameters&)
00110 {
00111 CPPUNIT_ASSERT(InitializedAddonsCount == 1);
00112 InitializedAddonsCount++;
00113 Initialized = true;
00114 }
00115
00116 virtual void Stop()
00117 {
00118 Stopped = true;
00119 }
00120
00121 bool IsInitialized() const
00122 {
00123 return Initialized;
00124 }
00125
00126 bool IsStopped() const
00127 {
00128 return Stopped;
00129 }
00130 };
00131
00132 class DeviceIOManagerFactory : public Common::AddonFactory
00133 {
00134 public:
00135 virtual Common::Addon::UniquePtr CreateAddon()
00136 {
00137 return Common::Addon::UniquePtr(new DeviceIOManagerAddon());
00138 }
00139 };
00140 void AddonsManagerTestCase::TestOneManager()
00141 {
00142 InitializedAddonsCount = 0;
00143
00144 Common::AddonInformation dmConfig;
00145 dmConfig.Id = DeviceManager::ManagerId;
00146 dmConfig.Factory = Common::AddonFactory::UniquePtr(new DeviceIOManagerFactory);
00147 dmConfig.Dependencies = std::vector<Common::AddonId>(1, PropertyTree::ManagerId);
00148 CPPUNIT_ASSERT_NO_THROW(Addons->Register(dmConfig));
00149
00150 Common::AddonInformation ptConfig;
00151 ptConfig.Id = PropertyTree::ManagerId;
00152 ptConfig.Factory = Common::AddonFactory::UniquePtr(new PropertyTreeAddonFactory);
00153 CPPUNIT_ASSERT_NO_THROW(Addons->Register(ptConfig));
00154
00155 CPPUNIT_ASSERT_NO_THROW(Addons->Start());
00156 CPPUNIT_ASSERT_THROW(Addons->Start(), Common::Error);
00157
00158 std::shared_ptr<PropertyTreeAddon> propertyTree;
00159 CPPUNIT_ASSERT_NO_THROW(propertyTree = Common::GetAddon<PropertyTreeAddon>(*Addons, PropertyTree::ManagerId));
00160 CPPUNIT_ASSERT(propertyTree);
00161 CPPUNIT_ASSERT(propertyTree->IsInitialized());
00162
00163 CPPUNIT_ASSERT_NO_THROW(Addons->Stop());
00164 CPPUNIT_ASSERT(propertyTree->IsStopped());
00165
00166 CPPUNIT_ASSERT_THROW(Addons->Stop(), Common::Error);
00167 }
00168
00169 void AddonsManagerTestCase::TestTwoManagers()
00170 {
00171 InitializedAddonsCount = 0;
00172
00173 Common::AddonInformation dmConfig;
00174 dmConfig.Id = DeviceManager::ManagerId;
00175 dmConfig.Factory = Common::AddonFactory::UniquePtr(new DeviceIOManagerFactory);
00176 dmConfig.Dependencies = std::vector<Common::AddonId>(1, PropertyTree::ManagerId);
00177 CPPUNIT_ASSERT_NO_THROW(Addons->Register(dmConfig));
00178 CPPUNIT_ASSERT_THROW(Addons->Register(dmConfig), Common::Error);
00179 CPPUNIT_ASSERT_THROW(Addons->GetAddon(DeviceManager::ManagerId), Common::Error);
00180
00181 Common::AddonInformation ptConfig;
00182 ptConfig.Id = PropertyTree::ManagerId;
00183 ptConfig.Factory = Common::AddonFactory::UniquePtr(new PropertyTreeAddonFactory);
00184 CPPUNIT_ASSERT_NO_THROW(Addons->Register(ptConfig));
00185 CPPUNIT_ASSERT_THROW(Addons->GetAddon(PropertyTree::ManagerId), Common::Error);
00186
00187 CPPUNIT_ASSERT_NO_THROW(Addons->Start());
00188 CPPUNIT_ASSERT_THROW(Addons->Start(), Common::Error);
00189
00190 std::shared_ptr<PropertyTreeAddon> propertyTree;
00191 CPPUNIT_ASSERT_NO_THROW(propertyTree = Common::GetAddon<PropertyTreeAddon>(*Addons, PropertyTree::ManagerId));
00192 CPPUNIT_ASSERT(propertyTree);
00193 CPPUNIT_ASSERT(propertyTree->IsInitialized());
00194
00195 std::shared_ptr<DeviceIOManagerAddon> deviceManager;
00196 CPPUNIT_ASSERT_NO_THROW(deviceManager = Common::GetAddon<DeviceIOManagerAddon>(*Addons, DeviceManager::ManagerId));
00197 CPPUNIT_ASSERT(deviceManager);
00198 CPPUNIT_ASSERT(deviceManager->IsInitialized());
00199
00200 CPPUNIT_ASSERT_NO_THROW(Addons->Stop());
00201 CPPUNIT_ASSERT(propertyTree->IsStopped());
00202 CPPUNIT_ASSERT(deviceManager->IsStopped());
00203
00204 CPPUNIT_ASSERT_THROW(Addons->Stop(), Common::Error);
00205 }
00206