[Orxonox-commit 6355] r11012 - in code/trunk: src/libraries/core src/libraries/core/singleton test test/core_plugin

landauf at orxonox.net landauf at orxonox.net
Sat Jan 2 11:15:18 CET 2016


Author: landauf
Date: 2016-01-02 11:15:18 +0100 (Sat, 02 Jan 2016)
New Revision: 11012

Added:
   code/trunk/test/core_plugin/
   code/trunk/test/core_plugin/CMakeLists.txt
   code/trunk/test/core_plugin/PluginTest.cc
   code/trunk/test/core_plugin/Testclass.cc
   code/trunk/test/core_plugin/Testclass.h
   code/trunk/test/core_plugin/Testcommandlineargument.cc
   code/trunk/test/core_plugin/Testconsolecommand.cc
   code/trunk/test/core_plugin/Testsingleton.cc
   code/trunk/test/core_plugin/Testsingleton.h
Modified:
   code/trunk/src/libraries/core/Core.h
   code/trunk/src/libraries/core/singleton/ScopeManager.h
   code/trunk/test/CMakeLists.txt
Log:
added unittest for plugin loading and unloading

Modified: code/trunk/src/libraries/core/Core.h
===================================================================
--- code/trunk/src/libraries/core/Core.h	2016-01-02 10:04:55 UTC (rev 11011)
+++ code/trunk/src/libraries/core/Core.h	2016-01-02 10:15:18 UTC (rev 11012)
@@ -44,6 +44,7 @@
 #include "CorePrereqs.h"
 
 #include <string>
+#include <list>
 #include "util/DestructionHelper.h"
 #include "util/Singleton.h"
 #include "CoreConfig.h"

Modified: code/trunk/src/libraries/core/singleton/ScopeManager.h
===================================================================
--- code/trunk/src/libraries/core/singleton/ScopeManager.h	2016-01-02 10:04:55 UTC (rev 11011)
+++ code/trunk/src/libraries/core/singleton/ScopeManager.h	2016-01-02 10:15:18 UTC (rev 11012)
@@ -70,6 +70,10 @@
             /** Unregisters a listener for the given scope. If the scope is still active, the listener is deactivated before removal. */
             void removeListener(ScopeListener* listener, ScopeID::Value scope);
 
+            /** Returns all registered listeners for a given scope. */
+            const std::set<ScopeListener*>& getListeners(ScopeID::Value scope)
+                { return this->listeners_[scope]; }
+
         private:
             void activateListenersForScope(ScopeID::Value scope);
             void deactivateListenersForScope(ScopeID::Value scope);

Modified: code/trunk/test/CMakeLists.txt
===================================================================
--- code/trunk/test/CMakeLists.txt	2016-01-02 10:04:55 UTC (rev 11011)
+++ code/trunk/test/CMakeLists.txt	2016-01-02 10:15:18 UTC (rev 11012)
@@ -66,3 +66,4 @@
 
 ADD_SUBDIRECTORY(util)
 ADD_SUBDIRECTORY(core)
+ADD_SUBDIRECTORY(core_plugin)

Added: code/trunk/test/core_plugin/CMakeLists.txt
===================================================================
--- code/trunk/test/core_plugin/CMakeLists.txt	                        (rev 0)
+++ code/trunk/test/core_plugin/CMakeLists.txt	2016-01-02 10:15:18 UTC (rev 11012)
@@ -0,0 +1,33 @@
+# Test-executable that loads and unloads the plugin at runtime an tests if it correctly (un-)registers static instances
+ORXONOX_ADD_EXECUTABLE(
+    core_plugin_test
+    EXCLUDE_FROM_ALL
+    NO_INSTALL
+  LINK_LIBRARIES
+    core
+    gmock_orxonox
+  SOURCE_FILES
+    ${GMOCK_MAIN}
+    PluginTest.cc
+)
+
+# A plugin that statically registers some instances
+ORXONOX_ADD_LIBRARY(
+  testplugin
+    PLUGIN
+    EXCLUDE_FROM_ALL
+    NO_INSTALL
+  LINK_LIBRARIES
+    core
+  SOURCE_FILES
+    Testclass.cc
+    Testcommandlineargument.cc
+    Testconsolecommand.cc
+    Testsingleton.cc
+)
+
+# let the test-executable depend on the plugin to ensure that the plugin always gets built before the test runs
+ADD_DEPENDENCIES(core_plugin_test testplugin)
+
+ADD_DEPENDENCIES(all_tests core_plugin_test)
+ADD_TEST(core_plugin_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/core_plugin_test --gtest_output=xml)


Property changes on: code/trunk/test/core_plugin/CMakeLists.txt
___________________________________________________________________
Added: svn:eol-style
   + native

Added: code/trunk/test/core_plugin/PluginTest.cc
===================================================================
--- code/trunk/test/core_plugin/PluginTest.cc	                        (rev 0)
+++ code/trunk/test/core_plugin/PluginTest.cc	2016-01-02 10:15:18 UTC (rev 11012)
@@ -0,0 +1,272 @@
+#include <gtest/gtest.h>
+#include <vector>
+
+#include "util/Exception.h"
+#include "core/Core.h"
+#include "core/CoreIncludes.h"
+#include "core/module/PluginReference.h"
+#include "core/commandline/CommandLineParser.h"
+#include "core/command/ConsoleCommand.h"
+#include "core/command/ConsoleCommandManager.h"
+
+#include "Testclass.h"
+#include "Testsingleton.h"
+
+namespace orxonox
+{
+    namespace
+    {
+        // Fixture
+        class PluginTest : public ::testing::Test
+        {
+            public:
+                static void SetUpTestCase()
+                {
+                    new Core("--noIOConsole");
+                }
+
+                static void TearDownTestCase()
+                {
+                    delete &Core::getInstance();
+                }
+        };
+    }
+
+    PluginReference* loadPlugin()
+    {
+        return new PluginReference("testplugin");
+    }
+
+    TEST_F(PluginTest, CanLoadPlugin)
+    {
+        PluginReference* plugin = loadPlugin();
+        delete plugin;
+    }
+
+
+    ///////////////////////////////////////////
+    /////////////// Identifier ////////////////
+    ///////////////////////////////////////////
+
+    Identifier* getIdentifier()
+    {
+        return ClassByString("Testclass");
+    }
+
+    TEST_F(PluginTest, LoadsIdentifier)
+    {
+        EXPECT_TRUE(getIdentifier() == NULL);
+        PluginReference* plugin = loadPlugin();
+        EXPECT_TRUE(getIdentifier() != NULL);
+        delete plugin;
+    }
+
+    TEST_F(PluginTest, UnloadsIdentifier)
+    {
+        PluginReference* plugin = loadPlugin();
+        EXPECT_TRUE(getIdentifier() != NULL);
+        delete plugin;
+        EXPECT_TRUE(getIdentifier() == NULL);
+    }
+
+    TEST_F(PluginTest, ReloadsIdentifier)
+    {
+        PluginReference* plugin = loadPlugin();
+        EXPECT_TRUE(getIdentifier() != NULL);
+        delete plugin;
+        EXPECT_TRUE(getIdentifier() == NULL);
+        plugin = loadPlugin();
+        EXPECT_TRUE(getIdentifier() != NULL);
+        delete plugin;
+    }
+
+    TEST_F(PluginTest, CanCreateObjectFromIdentifier)
+    {
+        PluginReference* plugin = loadPlugin();
+
+        Identifier* identifier = getIdentifier();
+        ASSERT_TRUE(identifier != NULL);
+
+        Identifiable* object = identifier->fabricate(NULL);
+        ASSERT_TRUE(object != NULL);
+
+        Testclass* testclass = orxonox_cast<Testclass*>(object);
+        ASSERT_TRUE(testclass != NULL);
+
+        EXPECT_EQ(666, testclass->getValue());
+
+        delete plugin;
+    }
+
+
+    ///////////////////////////////////////////
+    //////////////// Singleton ////////////////
+    ///////////////////////////////////////////
+
+    // Cannot directly use Testsingleton::getInstance() because we don't link the test to the plugin.
+    // Also cannot directly use ObjectList<Testsingleton> because the Identifier is not known before the plugin is loaded.
+    Testsingleton* getSingleton()
+    {
+        std::vector<Testsingleton*> singletons;
+
+        for (ObjectList<Listable>::iterator it = ObjectList<Listable>::begin(); it; ++it)
+        {
+            Testsingleton* singleton = dynamic_cast<Testsingleton*>(*it);
+            if (singleton)
+                singletons.push_back(singleton);
+        }
+
+        switch (singletons.size())
+        {
+            case 0:
+                return NULL;
+            case 1:
+                return singletons[0];
+            default:
+                throw std::exception(); // unexpected number of singletons found
+        }
+    }
+
+    TEST_F(PluginTest, LoadsSingleton)
+    {
+        EXPECT_TRUE(getSingleton() == NULL);
+        PluginReference* plugin = loadPlugin();
+        EXPECT_TRUE(getSingleton() != NULL);
+        delete plugin;
+    }
+
+    TEST_F(PluginTest, UnloadsSingleton)
+    {
+        PluginReference* plugin = loadPlugin();
+        EXPECT_TRUE(getSingleton() != NULL);
+        delete plugin;
+        EXPECT_TRUE(getSingleton() == NULL);
+    }
+
+    TEST_F(PluginTest, ReloadsSingleton)
+    {
+        PluginReference* plugin = loadPlugin();
+        EXPECT_TRUE(getSingleton() != NULL);
+        delete plugin;
+        EXPECT_TRUE(getSingleton() == NULL);
+        plugin = loadPlugin();
+        EXPECT_TRUE(getSingleton() != NULL);
+        delete plugin;
+    }
+
+    TEST_F(PluginTest, CanCallFunctionOnSingleton)
+    {
+        PluginReference* plugin = loadPlugin();
+
+        Testsingleton* singleton = getSingleton();
+        ASSERT_TRUE(singleton != NULL);
+
+        EXPECT_EQ(999, singleton->getValue());
+
+        delete plugin;
+    }
+
+
+    ///////////////////////////////////////////
+    ////////// Command Line Argument //////////
+    ///////////////////////////////////////////
+
+    bool hasCommandLineArgument()
+    {
+        try
+        {
+            CommandLineParser::getValue("testvalue");
+            return true;
+        }
+        catch (const ArgumentException&)
+        {
+            return false;
+        }
+    }
+
+    TEST_F(PluginTest, LoadsCommandLineArgument)
+    {
+        EXPECT_FALSE(hasCommandLineArgument());
+        PluginReference* plugin = loadPlugin();
+        EXPECT_TRUE(hasCommandLineArgument());
+        delete plugin;
+    }
+
+    TEST_F(PluginTest, UnloadsCommandLineArgument)
+    {
+        PluginReference* plugin = loadPlugin();
+        EXPECT_TRUE(hasCommandLineArgument());
+        delete plugin;
+        EXPECT_FALSE(hasCommandLineArgument());
+    }
+
+    TEST_F(PluginTest, ReloadsCommandLineArgument)
+    {
+        PluginReference* plugin = loadPlugin();
+        EXPECT_TRUE(hasCommandLineArgument());
+        delete plugin;
+        EXPECT_FALSE(hasCommandLineArgument());
+        plugin = loadPlugin();
+        EXPECT_TRUE(hasCommandLineArgument());
+        delete plugin;
+    }
+
+    TEST_F(PluginTest, CommandLineArgumentHasCorrectValue)
+    {
+        PluginReference* plugin = loadPlugin();
+
+        ASSERT_TRUE(hasCommandLineArgument());
+        EXPECT_EQ(333, CommandLineParser::getValue("testvalue").get<int>());
+
+        delete plugin;
+    }
+
+
+    ///////////////////////////////////////////
+    ///////////// Console Command /////////////
+    ///////////////////////////////////////////
+
+    ConsoleCommand* getConsoleCommand()
+    {
+        return ConsoleCommandManager::getInstance().getCommand("testcommand");
+    }
+
+    TEST_F(PluginTest, LoadsConsoleCommand)
+    {
+        EXPECT_TRUE(getConsoleCommand() == NULL);
+        PluginReference* plugin = loadPlugin();
+        EXPECT_TRUE(getConsoleCommand() != NULL);
+        delete plugin;
+    }
+
+    TEST_F(PluginTest, UnloadsConsoleCommand)
+    {
+        PluginReference* plugin = loadPlugin();
+        EXPECT_TRUE(getConsoleCommand() != NULL);
+        delete plugin;
+        EXPECT_TRUE(getConsoleCommand() == NULL);
+    }
+
+    TEST_F(PluginTest, ReloadsConsoleCommand)
+    {
+        PluginReference* plugin = loadPlugin();
+        EXPECT_TRUE(getConsoleCommand() != NULL);
+        delete plugin;
+        EXPECT_TRUE(getConsoleCommand() == NULL);
+        plugin = loadPlugin();
+        EXPECT_TRUE(getConsoleCommand() != NULL);
+        delete plugin;
+    }
+
+    TEST_F(PluginTest, CanCallConsoleCommand)
+    {
+        PluginReference* plugin = loadPlugin();
+
+        ConsoleCommand* command = getConsoleCommand();
+        ASSERT_TRUE(command != NULL);
+
+        EXPECT_EQ(999, (*command->getExecutor())(333, 666).get<int>());
+
+        delete plugin;
+    }
+}


Property changes on: code/trunk/test/core_plugin/PluginTest.cc
___________________________________________________________________
Added: svn:eol-style
   + native

Added: code/trunk/test/core_plugin/Testclass.cc
===================================================================
--- code/trunk/test/core_plugin/Testclass.cc	                        (rev 0)
+++ code/trunk/test/core_plugin/Testclass.cc	2016-01-02 10:15:18 UTC (rev 11012)
@@ -0,0 +1,20 @@
+#include "Testclass.h"
+#include "core/CoreIncludes.h"
+#include "core/config/ConfigValueIncludes.h"
+
+namespace orxonox
+{
+    RegisterClassNoArgs(Testclass);
+
+    Testclass::Testclass()
+    {
+        RegisterObject(Testclass);
+
+        this->setConfigValues();
+    }
+
+    void Testclass::setConfigValues()
+    {
+        SetConfigValue(value_, 666);
+    }
+}


Property changes on: code/trunk/test/core_plugin/Testclass.cc
___________________________________________________________________
Added: svn:eol-style
   + native

Added: code/trunk/test/core_plugin/Testclass.h
===================================================================
--- code/trunk/test/core_plugin/Testclass.h	                        (rev 0)
+++ code/trunk/test/core_plugin/Testclass.h	2016-01-02 10:15:18 UTC (rev 11012)
@@ -0,0 +1,22 @@
+#ifndef _Testclass_H__
+#define _Testclass_H__
+
+#include "core/class/OrxonoxClass.h"
+
+namespace orxonox
+{
+    class Testclass : public OrxonoxClass
+    {
+        public:
+            Testclass();
+            void setConfigValues();
+
+            int getValue() const
+                { return this->value_; }
+
+        private:
+            int value_;
+    };
+}
+
+#endif /* _Testclass_H__ */


Property changes on: code/trunk/test/core_plugin/Testclass.h
___________________________________________________________________
Added: svn:eol-style
   + native

Added: code/trunk/test/core_plugin/Testcommandlineargument.cc
===================================================================
--- code/trunk/test/core_plugin/Testcommandlineargument.cc	                        (rev 0)
+++ code/trunk/test/core_plugin/Testcommandlineargument.cc	2016-01-02 10:15:18 UTC (rev 11012)
@@ -0,0 +1,6 @@
+#include "core/commandline/CommandLineIncludes.h"
+
+namespace orxonox
+{
+    SetCommandLineArgument(testvalue, 333);
+}


Property changes on: code/trunk/test/core_plugin/Testcommandlineargument.cc
___________________________________________________________________
Added: svn:eol-style
   + native

Added: code/trunk/test/core_plugin/Testconsolecommand.cc
===================================================================
--- code/trunk/test/core_plugin/Testconsolecommand.cc	                        (rev 0)
+++ code/trunk/test/core_plugin/Testconsolecommand.cc	2016-01-02 10:15:18 UTC (rev 11012)
@@ -0,0 +1,11 @@
+#include "core/command/ConsoleCommandIncludes.h"
+
+namespace orxonox
+{
+    int testcommand(int a, int b)
+    {
+        return a + b;
+    }
+
+    SetConsoleCommand("testcommand", &testcommand);
+}


Property changes on: code/trunk/test/core_plugin/Testconsolecommand.cc
___________________________________________________________________
Added: svn:eol-style
   + native

Added: code/trunk/test/core_plugin/Testsingleton.cc
===================================================================
--- code/trunk/test/core_plugin/Testsingleton.cc	                        (rev 0)
+++ code/trunk/test/core_plugin/Testsingleton.cc	2016-01-02 10:15:18 UTC (rev 11012)
@@ -0,0 +1,17 @@
+#include "Testsingleton.h"
+#include "core/CoreIncludes.h"
+#include "core/singleton/ScopedSingletonIncludes.h"
+
+namespace orxonox
+{
+    ManageScopedSingleton(Testsingleton, ScopeID::ROOT, false);
+
+    RegisterAbstractClass(Testsingleton).inheritsFrom<Listable>();
+
+    Testsingleton::Testsingleton()
+    {
+        RegisterObject(Testsingleton);
+
+        this->value_ = 999;
+    }
+}


Property changes on: code/trunk/test/core_plugin/Testsingleton.cc
___________________________________________________________________
Added: svn:eol-style
   + native

Added: code/trunk/test/core_plugin/Testsingleton.h
===================================================================
--- code/trunk/test/core_plugin/Testsingleton.h	                        (rev 0)
+++ code/trunk/test/core_plugin/Testsingleton.h	2016-01-02 10:15:18 UTC (rev 11012)
@@ -0,0 +1,26 @@
+#ifndef _Testsingleton_H__
+#define _Testsingleton_H__
+
+#include "util/Singleton.h"
+#include "core/object/Listable.h"
+
+namespace orxonox
+{
+    class Testsingleton : public Singleton<Testsingleton>, public Listable
+    {
+        friend class Singleton<Testsingleton>;
+
+        public:
+            Testsingleton();
+
+            int getValue() const
+                { return this->value_; }
+
+        private:
+            int value_;
+
+            static Testsingleton* singletonPtr_s;
+    };
+}
+
+#endif /* _Testsingleton_H__ */


Property changes on: code/trunk/test/core_plugin/Testsingleton.h
___________________________________________________________________
Added: svn:eol-style
   + native




More information about the Orxonox-commit mailing list