Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:12:59

0001 // -*- C++ -*-
0002 //
0003 // Package:     PluginManager
0004 // Class  :     pluginmanager_t
0005 //
0006 // Implementation:
0007 //     <Notes on implementation>
0008 //
0009 // Original Author:  Chris Jones
0010 //         Created:  Wed Apr  4 13:38:29 EDT 2007
0011 //
0012 
0013 // system include files
0014 #include <Utilities/Testing/interface/CppUnit_testdriver.icpp>
0015 #include <cppunit/extensions/HelperMacros.h>
0016 #include <iostream>
0017 
0018 // user include files
0019 #include "FWCore/PluginManager/interface/PluginManager.h"
0020 #include "FWCore/PluginManager/interface/PluginFactoryBase.h"
0021 #include "FWCore/PluginManager/interface/standard.h"
0022 #include "FWCore/Utilities/interface/Exception.h"
0023 
0024 #include "FWCore/PluginManager/test/DummyFactory.h"
0025 
0026 class TestPluginManager : public CppUnit::TestFixture {
0027   CPPUNIT_TEST_SUITE(TestPluginManager);
0028   CPPUNIT_TEST(test);
0029   CPPUNIT_TEST_SUITE_END();
0030 
0031 public:
0032   void test();
0033   void setUp() {}
0034   void tearDown() {}
0035 };
0036 
0037 ///registration of the test so that the runner can find it
0038 CPPUNIT_TEST_SUITE_REGISTRATION(TestPluginManager);
0039 
0040 class DummyTestPlugin : public edmplugin::PluginFactoryBase {
0041 public:
0042   DummyTestPlugin(const std::string& iName) : name_(iName) { finishedConstruction(); }
0043   const std::string& category() const { return name_; }
0044   std::vector<edmplugin::PluginInfo> available() const { return std::vector<edmplugin::PluginInfo>(); }
0045   const std::string name_;
0046 };
0047 /*
0048 struct Catcher {
0049   std::string lastSeen_;
0050   
0051   void catchIt(const edmplugin::PluginFactoryBase* iFactory) {
0052     lastSeen_=iFactory->category();
0053   }
0054 };
0055 */
0056 
0057 namespace testedmplugin {
0058   struct DummyThree : public DummyBase {
0059     int value() const { return 3; }
0060   };
0061 }  // namespace testedmplugin
0062 
0063 DEFINE_EDM_PLUGIN(testedmplugin::DummyFactory, testedmplugin::DummyThree, "DummyThree");
0064 
0065 void TestPluginManager::test() {
0066   using namespace edmplugin;
0067   using namespace testedmplugin;
0068   CPPUNIT_ASSERT_THROW(PluginManager::get(), cms::Exception);
0069 
0070   PluginManager::Config config;
0071   CPPUNIT_ASSERT_THROW(PluginManager::configure(config), cms::Exception);
0072 
0073   edmplugin::PluginManager& db = edmplugin::PluginManager::configure(edmplugin::standard::config());
0074 
0075   std::string toLoadCategory("Test Dummy");
0076   std::string toLoadPlugin;
0077   unsigned int nTimesAsked = 0;
0078 
0079   edmplugin::PluginManager::get()->askedToLoadCategoryWithPlugin_.connect(
0080       [&](std::string const& iCategory, std::string const& iPlugin) {
0081         //std::cout <<iCategory<<" "<<iPlugin<<std::endl;
0082         CPPUNIT_ASSERT(toLoadCategory == iCategory);
0083         CPPUNIT_ASSERT(toLoadPlugin == iPlugin);
0084         ++nTimesAsked;
0085       });
0086 
0087   unsigned int nTimesGoingToLoad = 0;
0088   edmplugin::PluginManager::get()->goingToLoad_.connect(
0089       [&nTimesGoingToLoad](const std::filesystem::path&) { ++nTimesGoingToLoad; });
0090 
0091   unsigned int nTimesLoaded = 0;
0092   edmplugin::PluginManager::get()->justLoaded_.connect(
0093       [&nTimesLoaded](const edmplugin::SharedLibrary&) { ++nTimesLoaded; });
0094 
0095   toLoadPlugin = "DummyOne";
0096   std::unique_ptr<DummyBase> ptr(DummyFactory::get()->create("DummyOne"));
0097   CPPUNIT_ASSERT(1 == ptr->value());
0098   CPPUNIT_ASSERT(nTimesAsked == 1);
0099   CPPUNIT_ASSERT(nTimesGoingToLoad == 1);
0100   CPPUNIT_ASSERT(nTimesLoaded == 1);
0101   CPPUNIT_ASSERT(db.loadableFor("Test Dummy", "DummyThree") == "static");
0102   std::unique_ptr<DummyBase> ptr2(DummyFactory::get()->create("DummyThree"));
0103   CPPUNIT_ASSERT(3 == ptr2->value());
0104   CPPUNIT_ASSERT(nTimesAsked == 1);  //no request to load
0105   CPPUNIT_ASSERT(nTimesGoingToLoad == 1);
0106   CPPUNIT_ASSERT(nTimesLoaded == 1);
0107 
0108   toLoadPlugin = "DoesNotExist";
0109   CPPUNIT_ASSERT_THROW(DummyFactory::get()->create("DoesNotExist"), cms::Exception);
0110   CPPUNIT_ASSERT(nTimesAsked == 2);  //request happens even though it failed
0111   CPPUNIT_ASSERT(nTimesGoingToLoad == 1);
0112   CPPUNIT_ASSERT(nTimesLoaded == 1);
0113 }