00001
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef NamingManager_cpp
00020 #define NamingManager_cpp
00021
00022 #include <cppunit/ui/text/TestRunner.h>
00023 #include <cppunit/TextOutputter.h>
00024 #include <cppunit/extensions/TestFactoryRegistry.h>
00025 #include <cppunit/extensions/HelperMacros.h>
00026 #include <cppunit/TestAssert.h>
00027
00028 #include <rtm/RTC.h>
00029 #include <rtm/RTObject.h>
00030 #include <rtm/NamingManager.h>
00031 #include <rtm/Manager.h>
00032
00033 namespace RTC
00034 {
00035 class CorbaObjectManager
00036 {
00037 public:
00038 CorbaObjectManager(::CORBA::ORB_ptr orb, ::PortableServer::POA_ptr poa)
00039 : m_orb(orb), m_poa(poa)
00040 {
00041 }
00042
00043 void activate(::PortableServer::ServantBase* servant)
00044 {
00045 try
00046 {
00047 m_poa->activate_object(servant);
00048 }
00049 catch(const ::PortableServer::POA::ServantAlreadyActive &)
00050 {
00051 }
00052 }
00053
00054 void deactivate(::PortableServer::ServantBase* servant)
00055 {
00056 ::PortableServer::ObjectId_var id;
00057 id = m_poa->servant_to_id(servant);
00058 m_poa->deactivate_object(id);
00059 }
00060 protected:
00061 ::CORBA::ORB_ptr m_orb;
00062 ::PortableServer::POA_ptr m_poa;
00063 };
00064 };
00065
00066
00071 namespace NamingManager
00072 {
00073
00074 class NamingManagerMock : public RTC::NamingManager
00075 {
00076 public:
00077
00078 NamingManagerMock(RTC::Manager* manager)
00079 : RTC::NamingManager(manager) {}
00080 virtual ~NamingManagerMock(void) {}
00081
00082
00083 void registerMgrName(const char* name,
00084 const RTM::ManagerServant* mgr)
00085 {
00086
00087 RTC::NamingManager::registerMgrName(name, mgr);
00088 mgrNames = RTC::NamingManager::m_mgrNames;
00089
00090 }
00091
00092
00093 void unregisterMgrName(const char* name)
00094 {
00095
00096 RTC::NamingManager::unregisterMgrName(name);
00097 mgrNames = RTC::NamingManager::m_mgrNames;
00098
00099 }
00100
00101 std::vector<RTC::NamingManager::Mgr*> mgrNames;
00102 };
00103
00104 class NamingOnCorbaTests
00105 : public CppUnit::TestFixture
00106 {
00107 CPPUNIT_TEST_SUITE(NamingOnCorbaTests);
00108
00109 CPPUNIT_TEST(test_bindObject_and_unbindObject);
00110 CPPUNIT_TEST(test_bindObject_and_unbindObject3);
00111
00112 CPPUNIT_TEST_SUITE_END();
00113
00114 private:
00115 CORBA::ORB_ptr m_pORB;
00116 PortableServer::POA_ptr m_pPOA;
00117
00118 private:
00119 CosNaming::NamingContext_var getRootContext(const std::string& name_server)
00120 {
00121 std::string nsName
00122 = std::string("corbaloc::")
00123 + name_server
00124 + std::string("/NameService");
00125
00126 CORBA::Object_var obj = m_pORB->string_to_object(nsName.c_str());
00127 CosNaming::NamingContext_var rootContext = CosNaming::NamingContext::_narrow(obj);
00128 if (CORBA::is_nil(rootContext))
00129 {
00130 throw std::bad_alloc();
00131 }
00132
00133 return rootContext;
00134 }
00135
00136 public:
00140 NamingOnCorbaTests()
00141 {
00142 }
00143
00147 virtual ~NamingOnCorbaTests()
00148 {
00149 }
00150
00154 virtual void setUp()
00155 {
00156 int argc(0);
00157 char** argv(NULL);
00158
00159 m_pORB = CORBA::ORB_init(argc, argv);
00160 m_pPOA = PortableServer::POA::_narrow(
00161 m_pORB->resolve_initial_references("RootPOA"));
00162 m_pPOA->the_POAManager()->activate();
00163 }
00164
00168 virtual void tearDown()
00169 {
00170 }
00171
00178 void test_bindObject_and_unbindObject()
00179 {
00180
00181 const char* name_server = "localhost:2809";
00182 RTC::NamingOnCorba noc(m_pORB, name_server);
00183
00184
00185 RTC::CorbaObjectManager objMgr(m_pORB, m_pPOA);
00186 RTC::RTObject_impl* rto = new RTC::RTObject_impl(m_pORB, m_pPOA);
00187 objMgr.activate(rto);
00188 CPPUNIT_ASSERT(! CORBA::is_nil(rto->getObjRef()));
00189
00190
00191 noc.bindObject("id.kind", rto);
00192
00193
00194 CosNaming::NamingContext_var nc = getRootContext(name_server);
00195 CosNaming::Name name;
00196 name.length(1);
00197 name[0].id = "id";
00198 name[0].kind = "kind";
00199 CORBA::Object_ptr obj = nc->resolve(name);
00200 CPPUNIT_ASSERT(! CORBA::is_nil(obj));
00201
00202
00203 noc.unbindObject("id.kind");
00204
00205
00206 try
00207 {
00208 nc->resolve(name);
00209 CPPUNIT_FAIL("Exception not thrown.");
00210 }
00211 catch (CosNaming::NamingContext::NotFound expected) {}
00212 objMgr.deactivate(rto);
00213
00214 rto->finalize();
00215 delete rto;
00216 }
00217
00224 void test_bindObject_and_unbindObject3()
00225 {
00226
00227 const char* name_server = "localhost:2809";
00228 RTC::NamingOnCorba noc(m_pORB, name_server);
00229
00230
00231 RTC::CorbaObjectManager objMgr(m_pORB, m_pPOA);
00232 RTM::ManagerServant* mgrs = new RTM::ManagerServant();
00233 objMgr.activate(mgrs);
00234
00235
00236
00237 noc.bindObject("id.kind", mgrs);
00238
00239
00240 CosNaming::NamingContext_var nc = getRootContext(name_server);
00241 CosNaming::Name name;
00242 name.length(1);
00243 name[0].id = "id";
00244 name[0].kind = "kind";
00245 CORBA::Object_ptr obj = nc->resolve(name);
00246
00247
00248
00249 noc.unbindObject("id.kind");
00250
00251
00252 try
00253 {
00254 nc->resolve(name);
00255 CPPUNIT_FAIL("Exception not thrown.");
00256 }
00257 catch (CosNaming::NamingContext::NotFound expected) {}
00258 objMgr.deactivate(mgrs);
00259
00260 delete mgrs;
00261 }
00262
00263 };
00264
00265 class NamingManagerTests
00266 : public CppUnit::TestFixture
00267 {
00268 CPPUNIT_TEST_SUITE(NamingManagerTests);
00269
00270 CPPUNIT_TEST(test_registerMgrName_and_unregisterMgrName);
00271 CPPUNIT_TEST(test_bindObject_and_unbindObject2);
00272 CPPUNIT_TEST(test_bindObject_and_unbindObject);
00273 CPPUNIT_TEST(test_unbindAll);
00274 CPPUNIT_TEST(test_getObjects);
00275 CPPUNIT_TEST(test_update);
00276
00277 CPPUNIT_TEST_SUITE_END();
00278
00279 private:
00280 RTC::Manager* m_mgr;
00281 CORBA::ORB_ptr m_pORB;
00282 PortableServer::POA_ptr m_pPOA;
00283
00284 private:
00285 CosNaming::NamingContext_var getRootContext(const std::string& name_server)
00286 {
00287 std::string nsName
00288 = std::string("corbaloc::")
00289 + name_server
00290 + std::string("/NameService");
00291
00292 CORBA::Object_var obj = m_mgr->getORB()->string_to_object(nsName.c_str());
00293 CosNaming::NamingContext_var rootContext = CosNaming::NamingContext::_narrow(obj);
00294 if (CORBA::is_nil(rootContext))
00295 {
00296 throw std::bad_alloc();
00297 }
00298
00299 return rootContext;
00300 }
00301
00302 bool canResolve(const char* name_server, const char* id, const char* kind)
00303 {
00304 CosNaming::NamingContext_var nc = getRootContext(name_server);
00305
00306 CosNaming::Name name;
00307 name.length(1);
00308 name[0].id = id;
00309 name[0].kind = kind;
00310
00311 CORBA::Object_ptr obj;
00312 try
00313 {
00314 obj = nc->resolve(name);
00315 }
00316 catch (CosNaming::NamingContext::NotFound e)
00317 {
00318 return false;
00319 }
00320
00321 return !CORBA::is_nil(obj);
00322 }
00323
00324 public:
00328 NamingManagerTests()
00329 {
00330 }
00331
00335 virtual ~NamingManagerTests()
00336 {
00337 }
00338
00342 virtual void setUp()
00343 {
00344 m_mgr = RTC::Manager::init(0, NULL);
00345 m_pORB = m_mgr->getORB();
00346 m_pPOA = m_mgr->getPOA();
00347 }
00348
00352 virtual void tearDown()
00353 {
00354 }
00355
00359 void test_registerNameServer()
00360 {
00361
00362 }
00363
00370 void test_bindObject_and_unbindObject()
00371 {
00372
00373 RTC::CorbaObjectManager objMgr(m_pORB, m_pPOA);
00374
00375 RTC::RTObject_impl* rto = new RTC::RTObject_impl(m_pORB, m_pPOA);
00376 objMgr.activate(rto);
00377 CPPUNIT_ASSERT(! CORBA::is_nil(rto->getObjRef()));
00378
00379
00380 RTC::NamingManager nmgr(m_mgr);
00381
00382 const char* name_server = "localhost:2809";
00383 nmgr.registerNameServer("corba", name_server);
00384
00385
00386 nmgr.bindObject("id.kind", rto);
00387 CPPUNIT_ASSERT(canResolve(name_server, "id", "kind"));
00388
00389
00390 nmgr.unbindObject("id.kind");
00391
00392
00393 CPPUNIT_ASSERT(! canResolve(name_server, "id", "kind"));
00394 objMgr.deactivate(rto);
00395
00396 rto->finalize();
00397 delete rto;
00398 }
00399
00405 void test_unbindAll()
00406 {
00407
00408 RTC::CorbaObjectManager objMgr(m_pORB, m_pPOA);
00409
00410 RTC::RTObject_impl* rto1 = new RTC::RTObject_impl(m_pORB, m_pPOA);
00411 objMgr.activate(rto1);
00412 CPPUNIT_ASSERT(! CORBA::is_nil(rto1->getObjRef()));
00413
00414 RTC::RTObject_impl* rto2 = new RTC::RTObject_impl(m_pORB, m_pPOA);
00415 objMgr.activate(rto2);
00416 CPPUNIT_ASSERT(! CORBA::is_nil(rto2->getObjRef()));
00417
00418
00419 RTC::NamingManager nmgr(m_mgr);
00420
00421 const char* name_server = "localhost:2809";
00422 nmgr.registerNameServer("corba", name_server);
00423
00424
00425 nmgr.bindObject("id1.kind1", rto1);
00426 CPPUNIT_ASSERT(canResolve(name_server, "id1", "kind1"));
00427
00428 nmgr.bindObject("id2.kind2", rto2);
00429 CPPUNIT_ASSERT(canResolve(name_server, "id2", "kind2"));
00430
00431
00432 nmgr.unbindAll();
00433
00434
00435 CPPUNIT_ASSERT(! canResolve(name_server, "id1", "kind1"));
00436 CPPUNIT_ASSERT(! canResolve(name_server, "id2", "kind2"));
00437 objMgr.deactivate(rto2);
00438 objMgr.deactivate(rto1);
00439
00440 rto2->finalize();
00441 delete rto2;
00442 rto1->finalize();
00443 delete rto1;
00444 }
00445
00451 void test_getObjects()
00452 {
00453
00454 RTC::CorbaObjectManager objMgr(m_pORB, m_pPOA);
00455
00456 RTC::RTObject_impl* rto1 = new RTC::RTObject_impl(m_pORB, m_pPOA);
00457 objMgr.activate(rto1);
00458 CPPUNIT_ASSERT(! CORBA::is_nil(rto1->getObjRef()));
00459
00460 RTC::RTObject_impl* rto2 = new RTC::RTObject_impl(m_pORB, m_pPOA);
00461 objMgr.activate(rto2);
00462 CPPUNIT_ASSERT(! CORBA::is_nil(rto2->getObjRef()));
00463
00464
00465 RTC::NamingManager nmgr(m_mgr);
00466
00467 const char* name_server = "localhost:2809";
00468 nmgr.registerNameServer("corba", name_server);
00469
00470
00471 nmgr.bindObject("id1.kind1", rto1);
00472 CPPUNIT_ASSERT(canResolve(name_server, "id1", "kind1"));
00473
00474 nmgr.bindObject("id2.kind2", rto2);
00475 CPPUNIT_ASSERT(canResolve(name_server, "id2", "kind2"));
00476
00477
00478 std::vector<RTC::RTObject_impl*> objs = nmgr.getObjects();
00479 CPPUNIT_ASSERT_EQUAL(2, (int) objs.size());
00480
00481
00482 CPPUNIT_ASSERT(objs.end() != std::find(objs.begin(), objs.end(), rto1));
00483 CPPUNIT_ASSERT(objs.end() != std::find(objs.begin(), objs.end(), rto2));
00484 objMgr.deactivate(rto2);
00485 objMgr.deactivate(rto1);
00486
00487 rto2->finalize();
00488 delete rto2;
00489 rto1->finalize();
00490 delete rto1;
00491 }
00492
00499 void test_update()
00500 {
00501
00502 const char* name_server = "localhost:2809";
00503 RTC::NamingManager nmgr(m_mgr);
00504
00505
00506 RTC::CorbaObjectManager objMgr(m_pORB, m_pPOA);
00507
00508 RTC::RTObject_impl* rto = new RTC::RTObject_impl(m_pORB, m_pPOA);
00509 objMgr.activate(rto);
00510 CPPUNIT_ASSERT(! CORBA::is_nil(rto->getObjRef()));
00511
00512
00513
00514 nmgr.bindObject("id.kind", rto);
00515
00516 CPPUNIT_ASSERT(! canResolve(name_server, "id", "kind"));
00517
00518
00519
00520 nmgr.registerNameServer("corba", name_server);
00521
00522 CPPUNIT_ASSERT(! canResolve(name_server, "id", "kind"));
00523
00524
00525
00526 m_mgr->getConfig()["naming.update.rebind"] = "NO";
00527 nmgr.update();
00528 CPPUNIT_ASSERT(! canResolve(name_server, "id", "kind"));
00529
00530 m_mgr->getConfig()["naming.update.rebind"] = "YES";
00531 nmgr.update();
00532 CPPUNIT_ASSERT(canResolve(name_server, "id", "kind"));
00533 objMgr.deactivate(rto);
00534
00535 rto->finalize();
00536 delete rto;
00537 }
00538
00544 void test_registerMgrName_and_unregisterMgrName()
00545 {
00546
00547 RTC::CorbaObjectManager objMgr(m_pORB, m_pPOA);
00548 RTM::ManagerServant* mgrs1 = new RTM::ManagerServant();
00549 RTM::ManagerServant* mgrs2 = new RTM::ManagerServant();
00550 RTM::ManagerServant* mgrs3 = new RTM::ManagerServant();
00551 objMgr.activate(mgrs1);
00552 objMgr.activate(mgrs2);
00553 objMgr.activate(mgrs3);
00554 NamingManagerMock nmm(m_mgr);
00555
00556
00557 nmm.registerMgrName("id.kind", mgrs1);
00558
00559
00560 bool bret(false);
00561 for (int i(0), len(nmm.mgrNames.size()); i < len; ++i)
00562 {
00563 if (nmm.mgrNames[i]->name == "id.kind")
00564 {
00565 if (nmm.mgrNames[i]->mgr == mgrs1)
00566 {
00567 bret = true;
00568 }
00569 }
00570 }
00571 CPPUNIT_ASSERT(bret);
00572
00573 nmm.registerMgrName("id.kind", mgrs2);
00574
00575
00576 bret = false;
00577 for (int i(0), len(nmm.mgrNames.size()); i < len; ++i)
00578 {
00579 if (nmm.mgrNames[i]->name == "id.kind")
00580 {
00581 if (nmm.mgrNames[i]->mgr == mgrs2)
00582 {
00583 bret = true;
00584 }
00585 }
00586 }
00587 CPPUNIT_ASSERT(bret);
00588
00589 nmm.registerMgrName("id2.kind2", mgrs3);
00590
00591
00592 bret = false;
00593 for (int i(0), len(nmm.mgrNames.size()); i < len; ++i)
00594 {
00595 if (nmm.mgrNames[i]->name == "id2.kind2")
00596 {
00597 if (nmm.mgrNames[i]->mgr == mgrs3)
00598 {
00599 bret = true;
00600 }
00601 }
00602 }
00603 CPPUNIT_ASSERT(bret);
00604
00605
00606 nmm.unregisterMgrName("id2.kind2");
00607
00608
00609 bret = false;
00610 for (int i(0), len(nmm.mgrNames.size()); i < len; ++i)
00611 {
00612 if (nmm.mgrNames[i]->name == "id2.kind2")
00613 {
00614 bret = true;
00615 }
00616 }
00617 CPPUNIT_ASSERT(!bret);
00618
00619
00620 bret = false;
00621 for (int i(0), len(nmm.mgrNames.size()); i < len; ++i)
00622 {
00623 if (nmm.mgrNames[i]->mgr == mgrs3)
00624 {
00625 bret = true;
00626 }
00627 }
00628 CPPUNIT_ASSERT(!bret);
00629
00630
00631 nmm.unregisterMgrName("id.kind");
00632
00633
00634 bret = false;
00635 for (int i(0), len(nmm.mgrNames.size()); i < len; ++i)
00636 {
00637 if (nmm.mgrNames[i]->name == "id.kind")
00638 {
00639 bret = true;
00640 }
00641 }
00642 CPPUNIT_ASSERT(!bret);
00643
00644
00645 bret = false;
00646 for (int i(0), len(nmm.mgrNames.size()); i < len; ++i)
00647 {
00648 if ((nmm.mgrNames[i]->mgr == mgrs1) || (nmm.mgrNames[i]->mgr == mgrs2))
00649 {
00650 bret = true;
00651 }
00652 }
00653 CPPUNIT_ASSERT(!bret);
00654 objMgr.deactivate(mgrs3);
00655 objMgr.deactivate(mgrs2);
00656 objMgr.deactivate(mgrs1);
00657
00658 delete mgrs3;
00659 delete mgrs2;
00660 delete mgrs1;
00661 }
00662
00669 void test_bindObject_and_unbindObject2()
00670 {
00671
00672 RTC::CorbaObjectManager objMgr(m_pORB, m_pPOA);
00673
00674 RTM::ManagerServant* mgrs = new RTM::ManagerServant();
00675 objMgr.activate(mgrs);
00676
00677
00678
00679 RTC::NamingManager nmgr(m_mgr);
00680
00681 const char* name_server = "localhost:2809";
00682 nmgr.registerNameServer("corba", name_server);
00683
00684
00685 nmgr.bindObject("id.kind", mgrs);
00686
00687
00688
00689 nmgr.unbindObject("id.kind");
00690
00691
00692 CPPUNIT_ASSERT(! canResolve(name_server, "id", "kind"));
00693 objMgr.deactivate(mgrs);
00694
00695 delete mgrs;
00696 }
00697
00698 };
00699 };
00700
00701
00702
00703
00704 CPPUNIT_TEST_SUITE_REGISTRATION(NamingManager::NamingOnCorbaTests);
00705 CPPUNIT_TEST_SUITE_REGISTRATION(NamingManager::NamingManagerTests);
00706
00707 #ifdef LOCAL_MAIN
00708 int main(int argc, char* argv[])
00709 {
00710
00711 FORMAT format = TEXT_OUT;
00712 int target = 0;
00713 std::string xsl;
00714 std::string ns;
00715 std::string fname;
00716 std::ofstream ofs;
00717
00718 int i(1);
00719 while (i < argc)
00720 {
00721 std::string arg(argv[i]);
00722 std::string next_arg;
00723 if (i + 1 < argc) next_arg = argv[i + 1];
00724 else next_arg = "";
00725
00726 if (arg == "--text") { format = TEXT_OUT; break; }
00727 if (arg == "--xml")
00728 {
00729 if (next_arg == "")
00730 {
00731 fname = argv[0];
00732 fname += ".xml";
00733 }
00734 else
00735 {
00736 fname = next_arg;
00737 }
00738 format = XML_OUT;
00739 ofs.open(fname.c_str());
00740 }
00741 if ( arg == "--compiler" ) { format = COMPILER_OUT; break; }
00742 if ( arg == "--cerr" ) { target = 1; break; }
00743 if ( arg == "--xsl" )
00744 {
00745 if (next_arg == "") xsl = "default.xsl";
00746 else xsl = next_arg;
00747 }
00748 if ( arg == "--namespace" )
00749 {
00750 if (next_arg == "")
00751 {
00752 std::cerr << "no namespace specified" << std::endl;
00753 exit(1);
00754 }
00755 else
00756 {
00757 xsl = next_arg;
00758 }
00759 }
00760 ++i;
00761 }
00762 CppUnit::TextUi::TestRunner runner;
00763 if ( ns.empty() )
00764 runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00765 else
00766 runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
00767 CppUnit::Outputter* outputter = 0;
00768 std::ostream* stream = target ? &std::cerr : &std::cout;
00769 switch ( format )
00770 {
00771 case TEXT_OUT :
00772 outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
00773 break;
00774 case XML_OUT :
00775 std::cout << "XML_OUT" << std::endl;
00776 outputter = new CppUnit::XmlOutputter(&runner.result(),
00777 ofs, "shift_jis");
00778 static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
00779 break;
00780 case COMPILER_OUT :
00781 outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
00782 break;
00783 }
00784 runner.setOutputter(outputter);
00785 runner.run();
00786 return 0;
00787 }
00788 #endif // MAIN
00789 #endif // NamingManager_cpp