From 4be4e730f5dbf0a2e29593d84e5abf64b4a3e5a6 Mon Sep 17 00:00:00 2001
From: Dan Goodliffe <dan@randomdan.homeip.net>
Date: Thu, 17 Sep 2015 20:36:02 +0100
Subject: Test loading and unloading a library containing a plugin

---
 libadhocutil/unittests/Jamfile.jam            | 25 ++++++++++++++++++++
 libadhocutil/unittests/testPlugins.cpp        | 12 +---------
 libadhocutil/unittests/testPluginsRuntime.cpp | 34 +++++++++++++++++++++++++++
 libadhocutil/unittests/utilTestClasses.cpp    |  5 ++++
 libadhocutil/unittests/utilTestClasses.h      | 20 ++++++++++++++++
 5 files changed, 85 insertions(+), 11 deletions(-)
 create mode 100644 libadhocutil/unittests/testPluginsRuntime.cpp
 create mode 100644 libadhocutil/unittests/utilTestClasses.cpp
 create mode 100644 libadhocutil/unittests/utilTestClasses.h

diff --git a/libadhocutil/unittests/Jamfile.jam b/libadhocutil/unittests/Jamfile.jam
index ecee7d1..b4ec60a 100644
--- a/libadhocutil/unittests/Jamfile.jam
+++ b/libadhocutil/unittests/Jamfile.jam
@@ -6,6 +6,7 @@ lib boost_utf : : <name>boost_unit_test_framework ;
 lib boost_filesystem ;
 lib boost_system ;
 lib boost_thread ;
+lib dl ;
 
 run
 	testContext.cpp
@@ -119,16 +120,40 @@ run
 	testCache
 	;
 
+lib utilTestClasses :
+	utilTestClasses.cpp
+	: :
+	<define>BOOST_TEST_DYN_LINK
+	<library>..//adhocutil
+	<library>boost_utf
+	;
+
 run
 	testPlugins.cpp
 	: : :
 	<define>BOOST_TEST_DYN_LINK
 	<library>..//adhocutil
 	<library>boost_utf
+	<library>utilTestClasses
 	:
 	testPlugins
 	;
 
+run
+	testPluginsRuntime.cpp
+	: : :
+	<define>ROOT=\"$(me)\"
+	<define>BOOST_TEST_DYN_LINK
+	<library>..//adhocutil
+	<library>boost_utf
+	<library>boost_filesystem
+	<library>boost_system
+	<library>dl
+	<dependency>utilTestClasses
+	:
+	testPluginsRuntime
+	;
+
 run
 	testFactory.cpp
 	: : :
diff --git a/libadhocutil/unittests/testPlugins.cpp b/libadhocutil/unittests/testPlugins.cpp
index 130d577..ef5ed4b 100644
--- a/libadhocutil/unittests/testPlugins.cpp
+++ b/libadhocutil/unittests/testPlugins.cpp
@@ -3,20 +3,10 @@
 
 #include "plugins.h"
 #include "plugins.impl.h"
+#include "utilTestClasses.h"
 
 using namespace AdHoc;
 
-class BaseThing { };
-
-class ImplOfThing : public BaseThing { };
-class OtherImplOfThing : public BaseThing { };
-
-class OtherBase { };
-
-class OtherImpl : public OtherBase { };
-
-PLUGIN(ImplOfThing, BaseThing);
-
 BOOST_AUTO_TEST_CASE( ready )
 {
 	BOOST_REQUIRE(PluginManager::getDefault());
diff --git a/libadhocutil/unittests/testPluginsRuntime.cpp b/libadhocutil/unittests/testPluginsRuntime.cpp
new file mode 100644
index 0000000..0aa9955
--- /dev/null
+++ b/libadhocutil/unittests/testPluginsRuntime.cpp
@@ -0,0 +1,34 @@
+#define BOOST_TEST_MODULE Plugins
+#include <boost/test/unit_test.hpp>
+
+#include "plugins.h"
+#include "plugins.impl.h"
+#include "utilTestClasses.h"
+#include <definedDirs.h>
+#include <dlfcn.h>
+
+using namespace AdHoc;
+
+auto variant	= selfExe.parent_path().leaf();
+auto toolset	= selfExe.parent_path().parent_path().leaf();
+auto lib = rootDir / "bin" / toolset / variant / "libutilTestClasses.so";
+
+BOOST_AUTO_TEST_CASE( ready )
+{
+	BOOST_REQUIRE(PluginManager::getDefault());
+	BOOST_REQUIRE(boost::filesystem::exists(lib));
+}
+
+BOOST_AUTO_TEST_CASE( clean )
+{
+	BOOST_REQUIRE_EQUAL(0, AdHoc::PluginManager::getDefault()->getAll<BaseThing>().size());
+}
+
+BOOST_AUTO_TEST_CASE( loadAndUnloadlib )
+{
+	void * handle = dlopen(lib.c_str(), RTLD_NOW);
+	BOOST_REQUIRE(handle);
+	BOOST_REQUIRE_EQUAL(1, AdHoc::PluginManager::getDefault()->getAll<BaseThing>().size());
+	dlclose(handle);
+	BOOST_REQUIRE_EQUAL(0, AdHoc::PluginManager::getDefault()->getAll<BaseThing>().size());
+}
diff --git a/libadhocutil/unittests/utilTestClasses.cpp b/libadhocutil/unittests/utilTestClasses.cpp
new file mode 100644
index 0000000..cb31104
--- /dev/null
+++ b/libadhocutil/unittests/utilTestClasses.cpp
@@ -0,0 +1,5 @@
+#include "utilTestClasses.h"
+#include "plugins.impl.h"
+
+PLUGIN(ImplOfThing, BaseThing);
+
diff --git a/libadhocutil/unittests/utilTestClasses.h b/libadhocutil/unittests/utilTestClasses.h
new file mode 100644
index 0000000..f2a2036
--- /dev/null
+++ b/libadhocutil/unittests/utilTestClasses.h
@@ -0,0 +1,20 @@
+#ifndef LIBADHOCUTIL_UNITTESTS_UTILTESTCLASSES_H
+#define LIBADHOCUTIL_UNITTESTS_UTILTESTCLASSES_H
+
+class BaseThing {
+};
+
+class ImplOfThing : public BaseThing {
+};
+
+class OtherImplOfThing : public BaseThing {
+};
+
+class OtherBase {
+};
+
+class OtherImpl : public OtherBase {
+};
+
+#endif
+
-- 
cgit v1.2.3