Skip to content

Commit

Permalink
[Helper] PluginManager: testing loading a plugin with a dependency on…
Browse files Browse the repository at this point in the history
… an other plugin (#4464)

* add test on a second plugin with a dependency on an other plugin

* fix runsofa test
  • Loading branch information
fredroy authored Feb 7, 2024
1 parent 847bb41 commit 118eead
Show file tree
Hide file tree
Showing 17 changed files with 213 additions and 66 deletions.
3 changes: 2 additions & 1 deletion Sofa/framework/Helper/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ set(SOURCE_FILES
system/thread/CircularQueue_test.cpp
)

add_subdirectory(system/TestPlugin)
add_subdirectory(system/TestPluginA)
add_subdirectory(system/TestPluginB)

add_executable(${PROJECT_NAME} ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} Sofa.Testing Sofa.Helper)
Expand Down
76 changes: 51 additions & 25 deletions Sofa/framework/Helper/test/system/PluginManager_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ using sofa::testing::BaseTest;
using sofa::helper::system::PluginManager;
using sofa::helper::system::FileSystem;

static std::string pluginName = "TestPlugin";
static std::string pluginAName = "TestPluginA";
static std::string pluginBName = "TestPluginB";

#ifdef NDEBUG
static std::string pluginFileName = "TestPlugin";
static std::string pluginAFileName = "TestPluginA";
static std::string pluginBFileName = "TestPluginB";
#else
static std::string pluginFileName = "TestPlugin_d";
static std::string pluginAFileName = "TestPluginA_d";
static std::string pluginBFileName = "TestPluginB_d";
#endif //N_DEBUG

static std::string nonpluginName = "RandomNameForAPluginButHopeItDoesNotExist";
Expand All @@ -63,7 +66,7 @@ struct PluginManager_test: public BaseTest
// Set pluginDir by searching pluginFileName in the PluginRepository
for ( std::string path : sofa::helper::system::PluginRepository.getPaths() )
{
if ( FileSystem::exists(path + separator + prefix + pluginFileName + dotExt) )
if ( FileSystem::exists(path + separator + prefix + pluginAFileName + dotExt) )
{
pluginDir = path;
break;
Expand All @@ -72,7 +75,7 @@ struct PluginManager_test: public BaseTest

ASSERT_FALSE( pluginDir.empty() );

std::cout << "PluginManager_test.loadTestPluginByPath: "
std::cout << "PluginManager_test.loadTestPluginAByPath: "
<< "pluginDir = " << pluginDir
<< std::endl;
}
Expand Down Expand Up @@ -102,14 +105,14 @@ struct PluginManager_test: public BaseTest
};


TEST_F(PluginManager_test, loadTestPluginByPath)
TEST_F(PluginManager_test, loadTestPluginAByPath)
{
PluginManager&pm = PluginManager::getInstance();

const std::string pluginPath = pluginDir + separator + prefix + pluginFileName + dotExt;
const std::string pluginPath = pluginDir + separator + prefix + pluginAFileName + dotExt;
const std::string nonpluginPath = pluginDir + separator + prefix + nonpluginName + dotExt;

std::cout << "PluginManager_test.loadTestPluginByPath: "
std::cout << "PluginManager_test.loadTestPluginAByPath: "
<< "pluginPath = " << pluginPath
<< std::endl;

Expand All @@ -118,11 +121,11 @@ TEST_F(PluginManager_test, loadTestPluginByPath)
{
EXPECT_MSG_NOEMIT(Warning, Error);

std::cout << "PluginManager_test.loadTestPluginByPath: "
std::cout << "PluginManager_test.loadTestPluginAByPath: "
<< "pm.getPluginMap().size() = " << pm.getPluginMap().size()
<< std::endl;
ASSERT_EQ(pm.loadPluginByPath(pluginPath), PluginManager::PluginLoadStatus::SUCCESS);
ASSERT_GT(pm.findPlugin(pluginName).size(), 0u);
ASSERT_GT(pm.findPlugin(pluginAName).size(), 0u);
}

/// Check that non existing plugin are currectly handled and returns an
Expand All @@ -131,18 +134,18 @@ TEST_F(PluginManager_test, loadTestPluginByPath)
EXPECT_MSG_NOEMIT(Warning);
EXPECT_MSG_EMIT(Error);

std::cout << "PluginManager_test.loadTestPluginByPath: "
std::cout << "PluginManager_test.loadTestPluginAByPath: "
<< "pm.getPluginMap().size() = " << pm.getPluginMap().size()
<< std::endl;
ASSERT_EQ(pm.loadPluginByPath(nonpluginPath), PluginManager::PluginLoadStatus::PLUGIN_FILE_NOT_FOUND);
ASSERT_EQ(pm.findPlugin(nonpluginName).size(), 0u);
std::cout << "PluginManager_test.loadTestPluginByPath: "
std::cout << "PluginManager_test.loadTestPluginAByPath: "
<< "pm.getPluginMap().size() = " << pm.getPluginMap().size()
<< std::endl;
}
}

TEST_F(PluginManager_test, loadTestPluginByName )
TEST_F(PluginManager_test, loadTestPluginAByName )
{
PluginManager&pm = PluginManager::getInstance();

Expand All @@ -151,8 +154,8 @@ TEST_F(PluginManager_test, loadTestPluginByName )
{
EXPECT_MSG_NOEMIT(Warning, Error);

ASSERT_EQ(pm.loadPluginByName(pluginName), PluginManager::PluginLoadStatus::SUCCESS );
const std::string pluginPath = pm.findPlugin(pluginName);
ASSERT_EQ(pm.loadPluginByName(pluginAName), PluginManager::PluginLoadStatus::SUCCESS );
const std::string pluginPath = pm.findPlugin(pluginAName);
ASSERT_GT(pluginPath.size(), 0u);
}

Expand All @@ -171,8 +174,8 @@ TEST_F(PluginManager_test, pluginEntries)
{
PluginManager&pm = PluginManager::getInstance();

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

EXPECT_TRUE(p.initExternalModule.func != nullptr);
Expand All @@ -188,14 +191,14 @@ TEST_F(PluginManager_test, pluginEntriesValues)
{
PluginManager&pm = PluginManager::getInstance();

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

std::string testModuleName = "TestPlugin";
std::string testModuleName = "TestPluginA";
std::string testModuleVersion = "0.7";
std::string testModuleLicense = "LicenseTest";
std::string testModuleDescription = "Description of the Test Plugin";
std::string testModuleDescription = "Description of the Test Plugin A";
std::string testModuleComponentList = "ComponentA, ComponentB";

ASSERT_EQ(0, std::string(p.getModuleName()).compare(testModuleName));
Expand All @@ -220,8 +223,8 @@ TEST_F(PluginManager_test, testIniFile)
EXPECT_MSG_NOEMIT(Deprecated);

PluginManager&pm = PluginManager::getInstance();
pm.loadPluginByName(pluginName);
const std::string pluginPath = pm.findPlugin(pluginName);
pm.loadPluginByName(pluginAName);
const std::string pluginPath = pm.findPlugin(pluginAName);

const std::string pathIniFile = "PluginManager_test.ini";
pm.writeToIniFile(pathIniFile);
Expand All @@ -237,7 +240,7 @@ TEST_F(PluginManager_test, testIniFile)
ASSERT_TRUE(FileSystem::exists(pathIniFile));

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

}

Expand All @@ -252,10 +255,33 @@ TEST_F(PluginManager_test, testDeprecatedIniFileWoVersion)
const std::string pathIniFile = "PluginManager_test_deprecated_wo_version.ini";
std::ofstream outstream(pathIniFile.c_str());

outstream << pluginName << std::endl;
outstream << pluginAName << std::endl;
outstream.close();
ASSERT_TRUE(FileSystem::exists(pathIniFile));
pm.readFromIniFile(pathIniFile);

createdFilesToDelete.push_back(pathIniFile);
}

TEST_F(PluginManager_test, testPluginAAsDependencyOfPluginB)
{
std::string testModuleName = "TestPluginB";
PluginManager&pm = PluginManager::getInstance();

ASSERT_EQ(pm.getPluginMap().size(), 0u);

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

EXPECT_TRUE(p.initExternalModule.func != nullptr);
EXPECT_TRUE(p.getModuleName.func != nullptr);
ASSERT_EQ(0, std::string(p.getModuleName()).compare(testModuleName));

// TestPluginB does not implement the other get*() functions
EXPECT_FALSE(p.getModuleVersion.func != nullptr);
EXPECT_FALSE(p.getModuleLicense.func != nullptr);
EXPECT_FALSE(p.getModuleDescription.func != nullptr);
EXPECT_FALSE(p.getModuleComponentList.func != nullptr);

}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
cmake_minimum_required(VERSION 3.12)
project(TestPlugin VERSION 0.7)
project(TestPluginA VERSION 0.7)


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

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

add_library(${PROJECT_NAME} SHARED ${HEADER_FILES} ${SOURCE_FILES} ${README_FILES})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
* *
* Contact information: [email protected] *
******************************************************************************/
#ifndef TESTPLUGIN_COMPONENT_A_H
#define TESTPLUGIN_COMPONENT_A_H
#ifndef TESTPLUGINA_COMPONENT_A_H
#define TESTPLUGINA_COMPONENT_A_H

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

namespace sofa
Expand All @@ -31,7 +31,7 @@ namespace sofa
namespace test
{

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

public:
Expand All @@ -47,4 +47,4 @@ class SOFA_TESTPLUGIN_API ComponentA : public sofa::core::objectmodel::BaseObjec
} // namespace sofa


#endif // TESTPLUGIN_COMPONENT_A_H
#endif // TESTPLUGINA_COMPONENT_A_H
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* *
* Contact information: [email protected] *
******************************************************************************/
#define TESTPLUGIN_COMPONENT_B_CPP
#define TESTPLUGINA_COMPONENT_B_CPP

#include "ComponentB.h"

Expand Down Expand Up @@ -50,9 +50,9 @@ int ComponentBClass = sofa::core::RegisterObject("Component B")

;

template class SOFA_TESTPLUGIN_API ComponentB<double>;
template class SOFA_TESTPLUGIN_API ComponentB<sofa::defaulttype::Vec2Types>;
template class SOFA_TESTPLUGIN_API ComponentB<sofa::defaulttype::Rigid3Types>;
template class SOFA_TESTPLUGINA_API ComponentB<double>;
template class SOFA_TESTPLUGINA_API ComponentB<sofa::defaulttype::Vec2Types>;
template class SOFA_TESTPLUGINA_API ComponentB<sofa::defaulttype::Rigid3Types>;



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
* *
* Contact information: [email protected] *
******************************************************************************/
#ifndef TESTPLUGIN_COMPONENT_B_H
#define TESTPLUGIN_COMPONENT_B_H
#ifndef TESTPLUGINA_COMPONENT_B_H
#define TESTPLUGINA_COMPONENT_B_H

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

Expand All @@ -33,7 +33,7 @@ namespace test
{

template<class T>
class SOFA_TESTPLUGIN_API ComponentB : public sofa::core::objectmodel::BaseObject
class SOFA_TESTPLUGINA_API ComponentB : public sofa::core::objectmodel::BaseObject
{

public:
Expand All @@ -45,16 +45,16 @@ class SOFA_TESTPLUGIN_API ComponentB : public sofa::core::objectmodel::BaseObjec

};

#if !defined(TESTPLUGIN_COMPONENT_B_CPP)
extern template class SOFA_TESTPLUGIN_API ComponentB<double>;
extern template class SOFA_TESTPLUGIN_API ComponentB<defaulttype::Vec2Types>;
extern template class SOFA_TESTPLUGIN_API ComponentB<defaulttype::Rigid3Types>;
#if !defined(TESTPLUGINA_COMPONENT_B_CPP)
extern template class SOFA_TESTPLUGINA_API ComponentB<double>;
extern template class SOFA_TESTPLUGINA_API ComponentB<defaulttype::Vec2Types>;
extern template class SOFA_TESTPLUGINA_API ComponentB<defaulttype::Rigid3Types>;

#endif // !defined(TESTPLUGIN_COMPONENT_B_CPP)
#endif // !defined(TESTPLUGINA_COMPONENT_B_CPP)

} // namespace test

} // namespace sofa


#endif // TESTPLUGIN_COMPONENT_B_H
#endif // TESTPLUGINA_COMPONENT_B_H
39 changes: 39 additions & 0 deletions Sofa/framework/Helper/test/system/TestPluginA/TestPluginA.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 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: [email protected] *
******************************************************************************/
#ifndef TESTPLUGINA_CONFIG_H
#define TESTPLUGINA_CONFIG_H

#include <sofa/helper/config.h>

#ifdef SOFA_BUILD_TESTPLUGINA
# define SOFA_TARGET TestPluginA
# define SOFA_TESTPLUGINA_API SOFA_EXPORT_DYNAMIC_LIBRARY
#else
# define SOFA_TESTPLUGINA_API SOFA_IMPORT_DYNAMIC_LIBRARY
#endif

namespace testplugina
{
SOFA_TESTPLUGINA_API void init();
}

#endif // TESTPLUGINA_CONFIG_H
Loading

0 comments on commit 118eead

Please sign in to comment.