00001
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #ifndef ModuleManager_cpp
00041 #define ModuleManager_cpp
00042
00043 #include <cppunit/ui/text/TestRunner.h>
00044 #include <cppunit/TextOutputter.h>
00045 #include <cppunit/extensions/TestFactoryRegistry.h>
00046 #include <cppunit/extensions/HelperMacros.h>
00047 #include <cppunit/TestAssert.h>
00048 #include <fstream>
00049 #include <rtm/ModuleManager.h>
00050 #include <coil/Properties.h>
00051
00056 namespace ModuleManager
00057 {
00058 class ModuleManagerTests
00059 : public CppUnit::TestFixture
00060 {
00061 CPPUNIT_TEST_SUITE(ModuleManagerTests);
00062 CPPUNIT_TEST(test_load);
00063 CPPUNIT_TEST(test_load_inexistent_on_load_path);
00064 CPPUNIT_TEST(test_unload);
00065 CPPUNIT_TEST(test_unloadAll);
00066 CPPUNIT_TEST(test_symbol);
00067 CPPUNIT_TEST(test_setLoadpath_and_getLoadPath);
00068 CPPUNIT_TEST(test_addLoadpath);
00069
00070 CPPUNIT_TEST(test_getLoadableModules);
00071 CPPUNIT_TEST(test_allowAbsolutePath_and_disallowAbsolutePath);
00072
00073
00074 CPPUNIT_TEST(test_findFile);
00075 CPPUNIT_TEST(test_fileExist);
00076 CPPUNIT_TEST(test_getInitFuncName);
00077 CPPUNIT_TEST_SUITE_END();
00078
00079 private:
00080 RTC::ModuleManager* m_pModMgr;
00081
00082 private:
00083 bool isFound(const std::vector<coil::Properties>& path, const std::string& mod)
00084 {
00085 for (int i(0), len(path.size()); i < len; ++i)
00086 {
00087 if (mod == path[i]["file_path"]) return true;
00088 }
00089 return false;
00090 }
00091
00092 public:
00096 ModuleManagerTests()
00097 {
00098 }
00099
00103 ~ModuleManagerTests()
00104 {
00105 }
00106
00110 virtual void setUp()
00111 {
00112 const char* default_properties[] =
00113 {
00114 "manager.modules.config_ext", "so",
00115 "manager.modules.config_path", "/etc/rtc",
00116 "manager.modules.detect_loadable", "Yes",
00117 "manager.modules.load_path",
00118 "../../.libs, .libs, /usr/lib, /usr/local/lib, /usr/local/lib/rtc",
00119 "manager.modules.init_func_suffix", "Init",
00120 "manager.modules.init_func_prefix", "",
00121 "manager.modules.abs_path_allowed", "Yes",
00122 "manager.modules.download_allowed", "Yes",
00123 "manager.modules.download_dir", "/tmp/rtc",
00124 "manager.modules.download_cleanup", "Yes",
00125 "manager.modules.preload", "",
00126 ""
00127 };
00128
00129 coil::Properties prop(default_properties);
00130 m_pModMgr = new RTC::ModuleManager(prop);
00131
00132
00133 m_pModMgr->load("libRTC.so");
00134
00135
00136 m_pModMgr->load("libm.so");
00137 }
00138
00142 virtual void tearDown()
00143 {
00144 m_pModMgr->unloadAll();
00145 delete m_pModMgr;
00146 }
00147
00148
00158 void test_load()
00159 {
00160 try
00161 {
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187 }
00188 catch (RTC::ModuleManager::Error& e)
00189 {
00190 CPPUNIT_FAIL("Error" + e.reason);
00191 }
00192 catch (RTC::ModuleManager::NotFound& e)
00193 {
00194 CPPUNIT_FAIL("NotFound" + e.name);
00195 }
00196 catch (RTC::ModuleManager::NotAllowedOperation& e)
00197 {
00198 CPPUNIT_FAIL("NotAllowedOperation");
00199 }
00200 catch (RTC::ModuleManager::InvalidArguments& e)
00201 {
00202 CPPUNIT_FAIL("InvalidArguments");
00203 }
00204 catch (RTC::ModuleManager::FileNotFound& e)
00205 {
00206 CPPUNIT_FAIL("FileNotFound" + e.name);
00207 }
00208 catch (...)
00209 {
00210 CPPUNIT_FAIL("Other Exception");
00211 }
00212
00213
00214 bool notThrown;
00215
00216
00217 notThrown = false;
00218 try
00219 {
00220 m_pModMgr->load("libm.a");
00221 notThrown = true;
00222 }
00223 catch (...) {}
00224 if (notThrown) CPPUNIT_FAIL("Exception not thrown.");
00225
00226
00227 notThrown = false;
00228 try
00229 {
00230 m_pModMgr->load("inexist-file");
00231 notThrown = true;
00232 }
00233 catch (...) {}
00234 if (notThrown) CPPUNIT_FAIL("Exception not thrown.");
00235 }
00236
00242 void test_load_inexistent_on_load_path()
00243 {
00244
00245 std::vector<std::string> loadPath = m_pModMgr->getLoadPath();
00246
00247
00248 std::vector<std::string> emptyPath;
00249 m_pModMgr->setLoadpath(emptyPath);
00250
00251
00252
00253 bool notThrown = false;
00254 try
00255 {
00256 m_pModMgr->load("libm.so");
00257 notThrown = true;
00258 }
00259 catch (...) {}
00260 if (notThrown) CPPUNIT_FAIL("Exception not thrown.");
00261
00262
00263 m_pModMgr->setLoadpath(loadPath);
00264
00265
00266 std::string modName = m_pModMgr->load("libm.so");
00267 CPPUNIT_ASSERT(isFound(m_pModMgr->getLoadedModules(), modName));
00268 }
00269
00279 void test_unload()
00280 {
00281
00282
00283 std::string modName1 = m_pModMgr->load("libRTC.so");
00284 CPPUNIT_ASSERT(isFound(m_pModMgr->getLoadedModules(), modName1));
00285
00286 std::string modName2 = m_pModMgr->load("libm.so");
00287 CPPUNIT_ASSERT(isFound(m_pModMgr->getLoadedModules(), modName2));
00288
00289
00290
00291 m_pModMgr->unload(modName1);
00292 CPPUNIT_ASSERT(! isFound(m_pModMgr->getLoadedModules(), modName1));
00293
00294
00295 CPPUNIT_ASSERT(isFound(m_pModMgr->getLoadedModules(), modName2));
00296
00297
00298
00299
00300 try
00301 {
00302 m_pModMgr->unload("libm.so");
00303 CPPUNIT_FAIL("Exception not thrown.");
00304 }
00305 catch (RTC::ModuleManager::NotFound expected) {}
00306
00307
00308 try
00309 {
00310 m_pModMgr->unload("non-loaded-module.so");
00311 CPPUNIT_FAIL("Exception not thrown.");
00312 }
00313 catch (RTC::ModuleManager::NotFound expected) {}
00314
00315
00316 try
00317 {
00318 m_pModMgr->unload(modName1);
00319 CPPUNIT_FAIL("Exception not thrown.");
00320 }
00321 catch (RTC::ModuleManager::NotFound expected) {}
00322 }
00323
00329 void test_unloadAll()
00330 {
00331
00332 std::string modName1 = m_pModMgr->load("libRTC.so");
00333 CPPUNIT_ASSERT(isFound(m_pModMgr->getLoadedModules(), modName1));
00334
00335 std::string modName2 = m_pModMgr->load("libm.so");
00336 CPPUNIT_ASSERT(isFound(m_pModMgr->getLoadedModules(), modName2));
00337
00338
00339 m_pModMgr->unloadAll();
00340 CPPUNIT_ASSERT(! isFound(m_pModMgr->getLoadedModules(), modName1));
00341 CPPUNIT_ASSERT(! isFound(m_pModMgr->getLoadedModules(), modName2));
00342 }
00343
00350 void test_symbol()
00351 {
00352 typedef double (*FUNCTION_COS)(double);
00353 typedef double (*FUNCTION_SIN)(double);
00354
00355
00356 std::string modName = m_pModMgr->load("libm.so");
00357 CPPUNIT_ASSERT(isFound(m_pModMgr->getLoadedModules(), modName));
00358
00359
00360 FUNCTION_COS func_cos = (FUNCTION_COS) m_pModMgr->symbol(modName, "cos");
00361 CPPUNIT_ASSERT(func_cos != NULL);
00362
00363
00364 FUNCTION_SIN func_sin = (FUNCTION_SIN) m_pModMgr->symbol(modName, "sin");
00365 CPPUNIT_ASSERT(func_sin != NULL);
00366
00367
00368 CPPUNIT_ASSERT_EQUAL(1.0, (*func_cos)(0.0));
00369 CPPUNIT_ASSERT_EQUAL(0.0, (*func_sin)(0.0));
00370 }
00371
00378 void test_setLoadpath_and_getLoadPath()
00379 {
00380
00381 std::vector<std::string> loadPath;
00382 loadPath.push_back("/usr");
00383 loadPath.push_back("/usr/lib");
00384
00385
00386 m_pModMgr->setLoadpath(loadPath);
00387 std::vector<std::string> loadPathRet = m_pModMgr->getLoadPath();
00388 CPPUNIT_ASSERT_EQUAL(2, (int) loadPathRet.size());
00389 CPPUNIT_ASSERT_EQUAL(std::string("/usr"), loadPathRet[0]);
00390 CPPUNIT_ASSERT_EQUAL(std::string("/usr/lib"), loadPathRet[1]);
00391
00392 }
00393
00399 void test_addLoadpath()
00400 {
00401 std::vector<std::string> loadPath1;
00402 loadPath1.push_back("/usr");
00403 loadPath1.push_back("/usr/lib");
00404 std::sort(loadPath1.begin(), loadPath1.end());
00405
00406 std::vector<std::string> loadPath2;
00407 loadPath2.push_back("/usr/local/lib");
00408 loadPath2.push_back("/usr/local/lib/rtc");
00409 std::sort(loadPath2.begin(), loadPath2.end());
00410
00411 std::vector<std::string> expectedPath;
00412 expectedPath.push_back("/usr");
00413 expectedPath.push_back("/usr/lib");
00414 expectedPath.push_back("/usr/local/lib");
00415 expectedPath.push_back("/usr/local/lib/rtc");
00416 std::sort(expectedPath.begin(), expectedPath.end());
00417
00418
00419 std::vector<std::string> loadPathRet;
00420 m_pModMgr->setLoadpath(loadPath1);
00421 loadPathRet = m_pModMgr->getLoadPath();
00422 CPPUNIT_ASSERT_EQUAL(2, (int) loadPathRet.size());
00423 for (int i = 0; i < 2; ++i) {
00424 CPPUNIT_ASSERT_EQUAL(loadPath1[i], loadPathRet[i]);
00425 }
00426
00427
00428 m_pModMgr->addLoadpath(loadPath2);
00429 loadPathRet = m_pModMgr->getLoadPath();
00430 CPPUNIT_ASSERT_EQUAL(4, (int) loadPathRet.size());
00431 for (int i = 0; i < 4; ++i) {
00432 CPPUNIT_ASSERT_EQUAL(expectedPath[i], loadPathRet[i]);
00433 }
00434 }
00435
00439 void test_getLoadedModules()
00440 {
00441
00442 }
00443
00447 void test_getLoadableModules()
00448 {
00449 std::vector<coil::Properties> props = m_pModMgr->getLoadableModules();
00450 int len = props.size();
00451 CPPUNIT_ASSERT(len > 0);
00452
00453 bool bret = false;
00454 for(int i(0); i<len; ++i)
00455 {
00456 if(props[i].getProperty("module_file_name") == "DummyModule1.so")
00457 {
00458 bret = true;
00459 break;
00460 }
00461 }
00462 CPPUNIT_ASSERT(bret);
00463 }
00464
00471 void test_allowAbsolutePath_and_disallowAbsolutePath()
00472 {
00473
00474 m_pModMgr->allowAbsolutePath();
00475 std::string modName = m_pModMgr->load("/usr/lib/libm.so");
00476 CPPUNIT_ASSERT(isFound(m_pModMgr->getLoadedModules(), modName));
00477
00478
00479 m_pModMgr->unloadAll();
00480 m_pModMgr->disallowAbsolutePath();
00481 try
00482 {
00483 m_pModMgr->load("/usr/lib/libm.so");
00484 CPPUNIT_FAIL("Exception not thrown.");
00485 }
00486 catch (RTC::ModuleManager::NotAllowedOperation expected) {}
00487 }
00488
00492 void test_allowModuleDownload()
00493 {
00494
00495 }
00496
00500 void test_disallowModuleDownload()
00501 {
00502
00503 }
00504
00511 void test_findFile()
00512 {
00513
00514 std::vector<std::string> path;
00515 path.push_back("/usr/lib");
00516
00517
00518 CPPUNIT_ASSERT_EQUAL(std::string("/usr/lib/libm.so"),
00519 m_pModMgr->findFile("libm.so", path));
00520
00521
00522 CPPUNIT_ASSERT_EQUAL(std::string(""),
00523 m_pModMgr->findFile("inexist-file.so", path));
00524 }
00525
00533 void test_fileExist()
00534 {
00535
00536 CPPUNIT_ASSERT(m_pModMgr->fileExist("/usr/lib/libm.so"));
00537
00538
00539 CPPUNIT_ASSERT(! m_pModMgr->fileExist("/usr/lib/inexistent-file.so"));
00540
00541
00542 CPPUNIT_ASSERT(m_pModMgr->fileExist("/usr//lib/../lib/libm.so"));
00543 }
00544
00550 void test_getInitFuncName()
00551 {
00552
00553 CPPUNIT_ASSERT_EQUAL(std::string("ManipulatorInit"),
00554 m_pModMgr->getInitFuncName("Manipulator"));
00555
00556 CPPUNIT_ASSERT_EQUAL(std::string("PHANToMInit"),
00557 m_pModMgr->getInitFuncName("PHANToM"));
00558 }
00559
00560 };
00561 };
00562
00563
00564
00565
00566 CPPUNIT_TEST_SUITE_REGISTRATION(ModuleManager::ModuleManagerTests);
00567
00568 #ifdef LOCAL_MAIN
00569 int main(int argc, char* argv[])
00570 {
00571
00572 FORMAT format = TEXT_OUT;
00573 int target = 0;
00574 std::string xsl;
00575 std::string ns;
00576 std::string fname;
00577 std::ofstream ofs;
00578
00579 int i(1);
00580 while (i < argc)
00581 {
00582 std::string arg(argv[i]);
00583 std::string next_arg;
00584 if (i + 1 < argc) next_arg = argv[i + 1];
00585 else next_arg = "";
00586
00587 if (arg == "--text") { format = TEXT_OUT; break; }
00588 if (arg == "--xml")
00589 {
00590 if (next_arg == "")
00591 {
00592 fname = argv[0];
00593 fname += ".xml";
00594 }
00595 else
00596 {
00597 fname = next_arg;
00598 }
00599 format = XML_OUT;
00600 ofs.open(fname.c_str());
00601 }
00602 if ( arg == "--compiler" ) { format = COMPILER_OUT; break; }
00603 if ( arg == "--cerr" ) { target = 1; break; }
00604 if ( arg == "--xsl" )
00605 {
00606 if (next_arg == "") xsl = "default.xsl";
00607 else xsl = next_arg;
00608 }
00609 if ( arg == "--namespace" )
00610 {
00611 if (next_arg == "")
00612 {
00613 std::cerr << "no namespace specified" << std::endl;
00614 exit(1);
00615 }
00616 else
00617 {
00618 xsl = next_arg;
00619 }
00620 }
00621 ++i;
00622 }
00623 CppUnit::TextUi::TestRunner runner;
00624 if ( ns.empty() )
00625 runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00626 else
00627 runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
00628 CppUnit::Outputter* outputter = 0;
00629 std::ostream* stream = target ? &std::cerr : &std::cout;
00630 switch ( format )
00631 {
00632 case TEXT_OUT :
00633 outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
00634 break;
00635 case XML_OUT :
00636 std::cout << "XML_OUT" << std::endl;
00637 outputter = new CppUnit::XmlOutputter(&runner.result(),
00638 ofs, "shift_jis");
00639 static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
00640 break;
00641 case COMPILER_OUT :
00642 outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
00643 break;
00644 }
00645 runner.setOutputter(outputter);
00646 runner.run();
00647 return 0;
00648 }
00649 #endif // MAIN
00650 #endif // ModuleManager_cpp