From e197aca58ef73ac89535611bdf87195f3238ad68 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 24 Sep 2015 02:08:08 +0100 Subject: Register mocked database instances with the plugin manager and wrap up opening connections to them by name --- libdbpp/mockDatabase.cpp | 20 ++++++++++++++++++++ libdbpp/mockDatabase.h | 1 + 2 files changed, 21 insertions(+) diff --git a/libdbpp/mockDatabase.cpp b/libdbpp/mockDatabase.cpp index 3ef8728..b611c70 100644 --- a/libdbpp/mockDatabase.cpp +++ b/libdbpp/mockDatabase.cpp @@ -2,6 +2,18 @@ #include #include #include +#include + +namespace AdHoc { + template <> + PluginOf::~PluginOf() + { + // This implementation doesn't delete .implementation as + // mock databases simply unregister themselves (via destructor) + // when the mock framework tears them down. + } +} +INSTANIATEPLUGINOF(DB::MockDatabase); namespace DB { @@ -10,10 +22,18 @@ unsigned int MockDatabase::mocked = 0; MockDatabase::MockDatabase(const std::string & name) : mockName(name) { + AdHoc::PluginManager::getDefault()->add(AdHoc::PluginPtr(new AdHoc::PluginOf(this, mockName, __FILE__, __LINE__))); } MockDatabase::~MockDatabase() { + AdHoc::PluginManager::getDefault()->remove(mockName); +} + +Connection * +MockDatabase::openConnectionTo(const std::string & mockName) +{ + return AdHoc::PluginManager::getDefault()->get(mockName)->implementation()->openConnection(); } void diff --git a/libdbpp/mockDatabase.h b/libdbpp/mockDatabase.h index a1d935b..3e26201 100644 --- a/libdbpp/mockDatabase.h +++ b/libdbpp/mockDatabase.h @@ -15,6 +15,7 @@ class DLL_PUBLIC MockDatabase { virtual ~MockDatabase(); virtual DB::Connection * openConnection() const = 0; + static Connection * openConnectionTo(const std::string &); protected: virtual void CreateNewDatabase() const = 0; -- cgit v1.2.3