Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SofaKernel] Add tests on PluginManager #240

Merged
merged 2 commits into from
Apr 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions SofaKernel/framework/framework_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ set(SOURCE_FILES
helper/system/FileMonitor_test.cpp
helper/system/FileRepository_test.cpp
helper/system/FileSystem_test.cpp
helper/system/PluginManager_test.cpp
helper/system/atomic_test.cpp
helper/logging/logging_test.cpp
main.cpp
Expand All @@ -31,6 +32,10 @@ if(SOFA_HAVE_PNG)
list(APPEND SOURCE_FILES helper/io/ImagePNG_test.cpp)
endif()

#Plugin tests
#add_subdirectory(plugin-system) ## TODO: check if deprecated or... ?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it the right time to clean that? :p

add_subdirectory(plugins)

include_directories(${gtest_SOURCE_DIR}/include)

add_definitions("-DFRAMEWORK_TEST_RESOURCES_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}/resources\"")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture, development version *
* (c) 2006-2017 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/

#include <sofa/helper/system/PluginManager.h>
#include <sofa/helper/system/FileRepository.h>
#include <sofa/helper/system/FileSystem.h>
#include <sofa/helper/Utils.h>
#include <gtest/gtest.h>

using sofa::helper::system::PluginManager;

static std::string pluginName = "TestPlugin";;
static std::string nonpluginName = "RandomNameForAPluginButHopeItDoesNotExist";

const std::string dotExt = "." + sofa::helper::system::DynamicLibrary::extension;
#ifdef WIN32
const std::string separator = "\\";
const std::string prefix = "";
#else
const std::string separator = "/";
const std::string prefix = "lib";
#endif // WIN32

struct PluginManager_test: public ::testing::Test
{
std::string pluginDir;

void SetUp()
{
// Add the plugin directory to PluginRepository
#ifdef WIN32
pluginDir = sofa::helper::Utils::getExecutableDirectory();
#else
pluginDir = sofa::helper::Utils::getSofaPathPrefix() + "/lib";
#endif
sofa::helper::system::PluginRepository.addFirstPath(pluginDir);
}
};


TEST_F(PluginManager_test, loadTestPluginByPath)
{
sofa::helper::system::PluginManager&pm = sofa::helper::system::PluginManager::getInstance();

std::string pluginPath = pluginDir + separator + prefix + pluginName + dotExt;
std::string nonpluginPath = pluginDir + separator + prefix + nonpluginName + dotExt;

std::cout << pluginPath << std::endl;

ASSERT_TRUE(pm.loadPluginByPath(pluginPath));
ASSERT_FALSE(pm.loadPluginByPath(nonpluginPath));

ASSERT_GT(pm.findPlugin(pluginName).size(), 0);
ASSERT_EQ(pm.findPlugin(nonpluginName).size(), 0);

//It is better to unload the testPlugin in each test or it will stay loaded for the entire fixture
ASSERT_TRUE(pm.unloadPlugin(pluginPath));
ASSERT_EQ(pm.getPluginMap().size(), 0);

}

TEST_F(PluginManager_test, loadTestPluginByName )
{
sofa::helper::system::PluginManager&pm = sofa::helper::system::PluginManager::getInstance();

ASSERT_TRUE(pm.loadPluginByName(pluginName) );
ASSERT_FALSE(pm.loadPluginByName(nonpluginName));

std::string pluginPath = pm.findPlugin(pluginName);
ASSERT_GT(pluginPath.size(), 0);
ASSERT_EQ(pm.findPlugin(nonpluginName).size(), 0);

//Same
ASSERT_TRUE(pm.unloadPlugin(pluginPath));
ASSERT_EQ(pm.getPluginMap().size(), 0);
}

TEST_F(PluginManager_test, pluginEntries)
{
sofa::helper::system::PluginManager&pm = sofa::helper::system::PluginManager::getInstance();

pm.loadPluginByName(pluginName);
const std::string pluginPath = pm.findPlugin(pluginName);
sofa::helper::system::Plugin& p = pm.getPluginMap()[pluginPath];

EXPECT_TRUE(p.initExternalModule.func != NULL);
EXPECT_TRUE(p.getModuleName.func != NULL);
EXPECT_TRUE(p.getModuleVersion.func != NULL);
EXPECT_TRUE(p.getModuleLicense.func != NULL);
EXPECT_TRUE(p.getModuleDescription.func != NULL);
EXPECT_TRUE(p.getModuleComponentList.func != NULL);

ASSERT_TRUE(pm.unloadPlugin(pluginPath));
ASSERT_EQ(pm.getPluginMap().size(), 0);
}

TEST_F(PluginManager_test, pluginEntriesValues)
{
sofa::helper::system::PluginManager&pm = sofa::helper::system::PluginManager::getInstance();

pm.loadPluginByName(pluginName);
const std::string pluginPath = pm.findPlugin(pluginName);
sofa::helper::system::Plugin& p = pm.getPluginMap()[pluginPath];

std::string testModuleName = "TestPlugin";
std::string testModuleVersion = "0.7";
std::string testModuleLicence = "LicenceTest";
std::string testModuleDescription = "Description of the Test Plugin";
std::string testModuleComponentList = "ComponentA, ComponentB";

ASSERT_EQ(0, std::string(p.getModuleName()).compare(testModuleName));
ASSERT_NE(0, std::string(p.getModuleName()).compare(testModuleName + "azerty"));

ASSERT_EQ(0, std::string(p.getModuleVersion()).compare(testModuleVersion));
ASSERT_NE(0, std::string(p.getModuleVersion()).compare(testModuleVersion + "77777"));

ASSERT_EQ(0, std::string(p.getModuleLicense()).compare(testModuleLicence));
ASSERT_NE(0, std::string(p.getModuleLicense()).compare(testModuleLicence + "GPLBSDProprio"));

ASSERT_EQ(0, std::string(p.getModuleDescription()).compare(testModuleDescription));
ASSERT_NE(0, std::string(p.getModuleDescription()).compare(testModuleDescription + "blablablabalbal"));

ASSERT_EQ(0, std::string(p.getModuleComponentList()).compare(testModuleComponentList));
ASSERT_NE(0, std::string(p.getModuleComponentList()).compare(testModuleComponentList + "ComponentZ"));

ASSERT_TRUE(pm.unloadPlugin(pluginPath));
ASSERT_EQ(pm.getPluginMap().size(), 0);
}

TEST_F(PluginManager_test, testIniFile)
{
sofa::helper::system::PluginManager&pm = sofa::helper::system::PluginManager::getInstance();
pm.loadPluginByName(pluginName);
const std::string pluginPath = pm.findPlugin(pluginName);

const std::string pathIniFile = std::string(FRAMEWORK_TEST_RESOURCES_DIR) + separator + "PluginManager_test.ini";
pm.writeToIniFile(pathIniFile);

//writeToIniFile does not return anything to say if the file was created without error...
ASSERT_TRUE(sofa::helper::system::FileSystem::exists(pathIniFile));

ASSERT_TRUE(pm.unloadPlugin(pluginPath));
ASSERT_EQ(pm.getPluginMap().size(), 0);

ASSERT_TRUE(sofa::helper::system::FileSystem::exists(pathIniFile));

pm.readFromIniFile(pathIniFile);
ASSERT_EQ(pm.findPlugin(pluginName).compare(pluginPath), 0);

ASSERT_TRUE(pm.unloadPlugin(pluginPath));
ASSERT_EQ(pm.getPluginMap().size(), 0);
}
5 changes: 5 additions & 0 deletions SofaKernel/framework/framework_test/plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cmake_minimum_required(VERSION 3.1)

#dont use sofa_add_plugin as it will add an option in CMake
add_subdirectory(TestPlugin TestPlugin)
set_target_properties(TestPlugin PROPERTIES DEBUG_POSTFIX "_d")
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
cmake_minimum_required(VERSION 3.1)
project(TestPlugin)


set(HEADER_FILES
ComponentA.h
ComponentB.h
TestPlugin.h
)

set(SOURCE_FILES
ComponentA.cpp
ComponentB.cpp
initTestPlugin.cpp
)

add_library(${PROJECT_NAME} SHARED ${HEADER_FILES} ${SOURCE_FILES} ${README_FILES})
set_target_properties(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "-DSOFA_BUILD_TESTPLUGIN")
target_link_libraries(${PROJECT_NAME} SofaCore SofaDefaultType)
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/..")

install(TARGETS ${PROJECT_NAME}
COMPONENT TestPlugin_libraries
EXPORT TestPluginTargets
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib)
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture, development version *
* (c) 2006-2017 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#include "ComponentA.h"

#include <sofa/core/ObjectFactory.h>


namespace sofa
{

namespace test
{

ComponentA::ComponentA()
{
}


ComponentA::~ComponentA()
{
}


SOFA_DECL_CLASS(ComponentA)

int ComponentAClass = core::RegisterObject("Component A").add< ComponentA >();


} // namespace test

} // namespace sofa
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture, development version *
* (c) 2006-2017 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#ifndef TESTPLUGIN_COMPONENT_A_H
#define TESTPLUGIN_COMPONENT_A_H

#include <TestPlugin/TestPlugin.h>
#include <sofa/core/objectmodel/BaseObject.h>

namespace sofa
{

namespace test
{

class SOFA_TESTPLUGIN_API ComponentA : public sofa::core::objectmodel::BaseObject
{

public:
SOFA_CLASS(ComponentA, sofa::core::objectmodel::BaseObject);

protected:
ComponentA();
~ComponentA();
};

} // namespace test

} // namespace sofa


#endif // TESTPLUGIN_COMPONENT_A_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture, development version *
* (c) 2006-2017 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#define TESTPLUGIN_COMPONENT_B_CPP

#include "ComponentB.h"

#include <sofa/core/ObjectFactory.h>


namespace sofa
{

namespace test
{

template<class T>
ComponentB<T>::ComponentB()
{
}


template<class T>
ComponentB<T>::~ComponentB()
{
}

SOFA_DECL_CLASS(ComponentB)

int ComponentBClass = sofa::core::RegisterObject("Component B")
.add< ComponentB<float> >()
.add< ComponentB<double> >()
.add< ComponentB<sofa::defaulttype::Vec2dTypes> >()
.add< ComponentB<sofa::defaulttype::Vec2fTypes> >()
.add< ComponentB<sofa::defaulttype::Rigid3dTypes> >()
.add< ComponentB<sofa::defaulttype::Rigid3fTypes> >()
;
template class SOFA_TESTPLUGIN_API ComponentB<float>;
template class SOFA_TESTPLUGIN_API ComponentB<double>;
template class SOFA_TESTPLUGIN_API ComponentB<sofa::defaulttype::Vec2dTypes>;
template class SOFA_TESTPLUGIN_API ComponentB<sofa::defaulttype::Vec2fTypes>;
template class SOFA_TESTPLUGIN_API ComponentB<sofa::defaulttype::Rigid3dTypes>;
template class SOFA_TESTPLUGIN_API ComponentB<sofa::defaulttype::Rigid3fTypes>;



} // namespace test

} // namespace sofa
Loading