PortAdminTests.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00021 /*
00022  * $Log: PortAdminTests.cpp,v $
00023  * Revision 1.2  2008/04/21 04:01:01  arafune
00024  * Modified some tests.
00025  *
00026  * Revision 1.1  2007/12/20 07:50:16  arafune
00027  * *** empty log message ***
00028  *
00029  * Revision 1.3  2007/04/13 15:05:03  n-ando
00030  * Now RTC::OK becomes RTC::RTC_OK in RTC.idl.
00031  *
00032  * Revision 1.2  2007/01/12 14:44:36  n-ando
00033  * Some fixes for distribution control.
00034  *
00035  * Revision 1.1  2006/11/27 08:34:18  n-ando
00036  * TestSuites are devided into each directory.
00037  *
00038  * Revision 1.2  2006/11/14 02:21:09  kurihara
00039  *
00040  * test_deletePortByName() and test_finalizePorts() were added.
00041  *
00042  * Revision 1.1  2006/11/13 04:18:45  kurihara
00043  *
00044  * test program for PortAdmin class.
00045  *
00046  */
00047 
00048 #ifndef PortAdmin_cpp
00049 #define PortAdmin_cpp
00050 
00051 #include <cppunit/ui/text/TestRunner.h>
00052 #include <cppunit/TextOutputter.h>
00053 #include <cppunit/extensions/TestFactoryRegistry.h>
00054 #include <cppunit/extensions/HelperMacros.h>
00055 #include <cppunit/TestAssert.h>
00056 
00057 #include <vector>
00058 #include <string>
00059 
00060 #include <rtm/PortAdmin.h>
00061 
00066 namespace PortAdmin
00067 {
00068   int g_argc;
00069   std::vector<std::string> g_argv;
00070         
00076   class Logger
00077   {
00078   public:
00079     void log(const std::string& msg)
00080     {
00081       m_log.push_back(msg);
00082     }
00083 
00084     int countLog(const std::string& msg)
00085     {
00086       int count = 0;
00087       for (int i = 0; i < (int) m_log.size(); ++i)
00088         {
00089           if (m_log[i] == msg) ++count;
00090         }
00091       return count;
00092     }
00093                 
00094     void clearLog(void)
00095     {
00096         m_log.clear();
00097     }
00098   private:
00099     std::vector<std::string> m_log;
00100   };
00106   class PortMock
00107     : public RTC::PortBase
00108   {
00109   protected:
00110     virtual RTC::ReturnCode_t publishInterfaces(RTC::ConnectorProfile&)
00111     {
00112       return RTC::RTC_OK;
00113     }
00114     virtual RTC::ReturnCode_t subscribeInterfaces(const RTC::ConnectorProfile&)
00115     {
00116       return RTC::RTC_OK;
00117     }
00118     virtual void unsubscribeInterfaces(const RTC::ConnectorProfile&)
00119     {
00120     }
00121     virtual void activateInterfaces()
00122     {
00123           if (m_logger != NULL)
00124           {
00125               m_logger->log("PortMock::activateInterfaces");
00126           }
00127     }
00128     virtual void deactivateInterfaces()
00129     {
00130           if (m_logger != NULL)
00131           {
00132               m_logger->log("PortMock::deactivateInterfaces");
00133           }
00134     }
00135   public:
00136       PortMock()
00137       {
00138           m_logger = NULL;
00139       }
00144       void setLogger(Logger* logger)
00145       {
00146           m_logger = logger;
00147       }
00148   private:
00149     Logger* m_logger;
00150 
00151   };
00152 
00153   class PortAdminTests
00154     : public CppUnit::TestFixture
00155   {
00156     CPPUNIT_TEST_SUITE(PortAdminTests);
00157     CPPUNIT_TEST(test_getPortList);
00158     CPPUNIT_TEST(test_getPortRef);
00159     CPPUNIT_TEST(test_getPort);
00160     CPPUNIT_TEST(test_addPort);
00161     CPPUNIT_TEST(test_registerPort);
00162     CPPUNIT_TEST(test_removePort);
00163     CPPUNIT_TEST(test_deletePortByName);
00164     CPPUNIT_TEST(test_finalizePorts);
00165     CPPUNIT_TEST(test_activatePorts);
00166     CPPUNIT_TEST(test_deactivatePorts);
00167     CPPUNIT_TEST_SUITE_END();
00168         
00169   private:
00170     CORBA::ORB_ptr          m_orb;
00171     PortableServer::POA_ptr m_poa;
00172         
00173   public:
00174     PortAdminTests()
00175     {
00176       char* argv[g_argc];
00177       for (int i = 0; i < g_argc; i++) {
00178         argv[i] = (char *)g_argv[i].c_str();
00179       }
00180                         
00181       m_orb = CORBA::ORB_init(g_argc, argv);
00182       CORBA::Object_var  obj = m_orb->resolve_initial_references("RootPOA");
00183       m_poa = PortableServer::POA::_narrow(obj);
00184       PortableServer::POAManager_var pman = m_poa->the_POAManager();
00185       pman->activate();
00186     }
00187                 
00188     ~PortAdminTests()
00189     {
00190     }
00191                 
00192     virtual void setUp()
00193     {
00194       sleep(1);
00195     }
00196                 
00197     virtual void tearDown()
00198     { 
00199     }
00200                 
00206     void test_getPortList()
00207     {
00208       RTC::PortAdmin portAdmin(m_orb, m_poa);
00209                         
00210       // Portを登録しておく
00211       PortMock* port0 = new PortMock();
00212       port0->setName("port0");
00213       CPPUNIT_ASSERT_EQUAL(true, portAdmin.addPort(*port0));
00214 
00215       PortMock* port1 = new PortMock();
00216       port1->setName("port1");
00217       CPPUNIT_ASSERT_EQUAL(true, portAdmin.addPort(*port1));
00218                         
00219       // getPortList()で登録されている全Portを取得する
00220       RTC::PortServiceList* portList = portAdmin.getPortServiceList();
00221                         
00222       // 取得されたPortが、あらかじめ登録したものと一致するか?
00223       RTC::PortProfile* portProf0 = (*portList)[0]->get_port_profile();
00224       CPPUNIT_ASSERT(portProf0 != NULL);
00225       CPPUNIT_ASSERT_EQUAL(std::string("port0"), std::string(portProf0->name));
00226                         
00227       RTC::PortProfile* portProf1 = (*portList)[1]->get_port_profile();
00228       CPPUNIT_ASSERT(portProf1 != NULL);
00229       CPPUNIT_ASSERT_EQUAL(std::string("port1"), std::string(portProf1->name));
00230       portAdmin.removePort(*port1);
00231       portAdmin.removePort(*port0);
00232       delete port1;
00233       delete port0;
00234     }
00235                 
00242     void test_getPortRef()
00243     {
00244       RTC::PortAdmin portAdmin(m_orb, m_poa);
00245                         
00246       // Portを登録しておく
00247       PortMock* port0 = new PortMock();
00248       port0->setName("port0");
00249       CPPUNIT_ASSERT_EQUAL(true, portAdmin.addPort(*port0));
00250 
00251       PortMock* port1 = new PortMock();
00252       port1->setName("port1");
00253       CPPUNIT_ASSERT_EQUAL(true, portAdmin.addPort(*port1));
00254                         
00255       // 登録されているPortの参照を正しく取得できるか?
00256       RTC::PortService_var portRef0 = portAdmin.getPortRef("port0");
00257       CPPUNIT_ASSERT(! CORBA::is_nil(portRef0));
00258       RTC::PortProfile* portProf0 = portRef0->get_port_profile();
00259       CPPUNIT_ASSERT_EQUAL(std::string("port0"), std::string(portProf0->name));
00260 
00261       RTC::PortService_var portRef1 = portAdmin.getPortRef("port1");
00262       CPPUNIT_ASSERT(! CORBA::is_nil(portRef1));
00263       RTC::PortProfile* portProf1 = portRef1->get_port_profile();
00264       CPPUNIT_ASSERT_EQUAL(std::string("port1"), std::string(portProf1->name));
00265                         
00266       // 登録されていないPortの名称を指定した場合、意図どおりnil参照が得られるか?
00267       CPPUNIT_ASSERT(CORBA::is_nil(portAdmin.getPortRef("inexist")));
00268       delete port1;
00269       delete port0;
00270     }
00271                 
00277     void test_getPort()
00278     {
00279       RTC::PortAdmin portAdmin(m_orb, m_poa);
00280                         
00281       // Portを登録しておく
00282       PortMock* port0 = new PortMock();
00283       port0->setName("port0");
00284       CPPUNIT_ASSERT_EQUAL(true, portAdmin.addPort(*port0));
00285 
00286       PortMock* port1 = new PortMock();
00287       port1->setName("port1");
00288       CPPUNIT_ASSERT_EQUAL(true, portAdmin.addPort(*port1));
00289 
00290       // ポート名称を指定して、登録されているPortオブジェクトを正しく取得できるか?
00291       RTC::PortBase* portRet0 = portAdmin.getPort("port0");
00292       RTC::PortProfile* portProf0 = portRet0->get_port_profile();
00293       CPPUNIT_ASSERT_EQUAL(std::string("port0"), std::string(portProf0->name));
00294 
00295       RTC::PortBase* portRet1 = portAdmin.getPort("port1");
00296       RTC::PortProfile* portProf1 = portRet1->get_port_profile();
00297       CPPUNIT_ASSERT_EQUAL(std::string("port1"), std::string(portProf1->name));
00298       delete port1;
00299       delete port0;
00300     }
00301                 
00305     void test_addPort()
00306     {
00307       RTC::PortAdmin portAdmin(m_orb, m_poa);
00308                         
00309       // Portを登録しておく
00310       PortMock* port0 = new PortMock();
00311       port0->setName("port0");
00312       // test for addPort(PortBase&)
00313       CPPUNIT_ASSERT_EQUAL(true, portAdmin.addPort(*port0));
00314       CPPUNIT_ASSERT_EQUAL(false, portAdmin.addPort(*port0));
00315 
00316       PortMock* port1 = new PortMock();
00317       port1->setName("port1");
00318       // test for addPort(PortService_ptr)
00319       CPPUNIT_ASSERT_EQUAL(true, portAdmin.addPort(port1->getPortRef()));
00320       CPPUNIT_ASSERT_EQUAL(false, portAdmin.addPort(port1->getPortRef()));
00321       delete port1;
00322       delete port0;
00323     }
00324 
00328     void test_registerPort()
00329     {
00330       RTC::PortAdmin portAdmin(m_orb, m_poa);
00331                         
00332       // Portを登録しておく
00333       PortMock* port0 = new PortMock();
00334       port0->setName("port0");
00335       // test for registerPort(PortBase&)
00336       portAdmin.registerPort(*port0);
00337 
00338       PortMock* port1 = new PortMock();
00339       port1->setName("port1");
00340       // test for registerPort(PortService_ptr)
00341       portAdmin.registerPort(port1->getPortRef());
00342 
00343       // 登録されているうち、1つのPortを削除する
00344       portAdmin.deletePort(*port0);
00345 
00346       // getPortList()にて、登録されている全Portを取得する
00347       RTC::PortServiceList* portList = portAdmin.getPortServiceList();
00348                         
00349       // 削除したPortが、取得したPortList内に含まれていないことを確認する
00350       CPPUNIT_ASSERT_EQUAL(CORBA::ULong(1), portList->length());
00351       RTC::PortProfile* portProf1 = (*portList)[0]->get_port_profile();
00352       CPPUNIT_ASSERT_EQUAL(std::string("port1"), std::string(portProf1->name));
00353                         
00354       // 削除したPortのProfileのリファレンスがnilになっているか?
00355       const RTC::PortProfile& portProf0 = port0->getProfile();
00356       CPPUNIT_ASSERT(CORBA::is_nil(portProf0.port_ref));
00357       delete port1;
00358       delete port0;
00359     }
00360 
00361                 
00368     void test_removePort()
00369     {
00370       RTC::PortAdmin portAdmin(m_orb, m_poa);
00371                         
00372       // Portを登録しておく
00373       PortMock* port0 = new PortMock();
00374       port0->setName("port0");
00375       portAdmin.addPort(*port0);
00376 
00377       PortMock* port1 = new PortMock();
00378       port1->setName("port1");
00379       portAdmin.addPort(*port1);
00380 
00381       // 登録されているうち、1つのPortを削除する
00382       CPPUNIT_ASSERT_EQUAL(true, portAdmin.removePort(*port0));
00383       CPPUNIT_ASSERT_EQUAL(false, portAdmin.removePort(*port0));
00384                         
00385       // getPortList()にて、登録されている全Portを取得する
00386       RTC::PortServiceList* portList = portAdmin.getPortServiceList();
00387                         
00388       // 削除したPortが、取得したPortList内に含まれていないことを確認する
00389       CPPUNIT_ASSERT_EQUAL(CORBA::ULong(1), portList->length());
00390       RTC::PortProfile* portProf1 = (*portList)[0]->get_port_profile();
00391       CPPUNIT_ASSERT_EQUAL(std::string("port1"), std::string(portProf1->name));
00392                         
00393       // 削除したPortのProfileのリファレンスがnilになっているか?
00394       const RTC::PortProfile& portProf0 = port0->getProfile();
00395       CPPUNIT_ASSERT(CORBA::is_nil(portProf0.port_ref));
00396       delete port1;
00397       delete port0;
00398     }
00399                 
00406     void test_deletePortByName()
00407     {
00408       RTC::PortAdmin portAdmin(m_orb, m_poa);
00409                         
00410       // Portを登録しておく
00411       PortMock* port0 = new PortMock();
00412       port0->setName("port0");
00413       portAdmin.addPort(*port0);
00414 
00415       PortMock* port1 = new PortMock();
00416       port1->setName("port1");
00417       portAdmin.addPort(*port1);
00418 
00419       // 登録されているうち、1つのPortを削除する
00420       portAdmin.deletePortByName("port0");
00421                         
00422       // getPortList()にて、登録されている全Portを取得する
00423       RTC::PortServiceList* portList = portAdmin.getPortServiceList();
00424                         
00425       // 削除したPortが、取得したPortList内に含まれていないことを確認する
00426       CPPUNIT_ASSERT_EQUAL(CORBA::ULong(1), portList->length());
00427       RTC::PortProfile* portProf1 = (*portList)[0]->get_port_profile();
00428       CPPUNIT_ASSERT_EQUAL(std::string("port1"), std::string(portProf1->name));
00429                         
00430       // 削除したPortのProfileのリファレンスがnilになっているか?
00431       const RTC::PortProfile& portProf0 = port0->getProfile();
00432       CPPUNIT_ASSERT(CORBA::is_nil(portProf0.port_ref));
00433       delete port1;
00434       delete port0;
00435     }
00436                 
00443     void test_finalizePorts()
00444     {
00445       RTC::PortAdmin portAdmin(m_orb, m_poa);
00446                         
00447       // Portを登録しておく
00448       PortMock* port0 = new PortMock();
00449       port0->setName("port0");
00450       portAdmin.addPort(*port0);
00451 
00452       PortMock* port1 = new PortMock();
00453       port1->setName("port1");
00454       portAdmin.addPort(*port1);
00455 
00456       // finalizePorts()を呼出す
00457       portAdmin.finalizePorts();
00458                         
00459       // getPortList()にて、登録されている全Portを取得する
00460       RTC::PortServiceList* portList = portAdmin.getPortServiceList();
00461                         
00462       // 取得したPortListが空であることを確認する
00463       CPPUNIT_ASSERT_EQUAL(CORBA::ULong(0), portList->length());
00464                         
00465       // すべてのPortのProfileのリファレンスがnilになっているか?
00466       const RTC::PortProfile& portProf0 = port0->getProfile();
00467       CPPUNIT_ASSERT(CORBA::is_nil(portProf0.port_ref));
00468       const RTC::PortProfile& portProf1 = port1->getProfile();
00469       CPPUNIT_ASSERT(CORBA::is_nil(portProf1.port_ref));
00470       delete port1;
00471       delete port0;
00472     }
00478     void test_activatePorts(void)
00479     {
00480       RTC::PortAdmin portAdmin(m_orb, m_poa);
00481 
00482       Logger logger;
00483                         
00484       // Portを登録しておく
00485       PortMock* port0 = new PortMock();
00486       port0->setName("port0");
00487       port0->setLogger(&logger);
00488       portAdmin.addPort(*port0);
00489 
00490       PortMock* port1 = new PortMock();
00491       port1->setName("port1");
00492       port1->setLogger(&logger);
00493       portAdmin.addPort(*port1);
00494 
00495       CPPUNIT_ASSERT_EQUAL(0,logger.countLog("PortMock::activateInterfaces"));
00496       portAdmin.activatePorts();
00497       CPPUNIT_ASSERT_EQUAL(2,logger.countLog("PortMock::activateInterfaces"));
00498 
00499       CPPUNIT_ASSERT_EQUAL(0,logger.countLog("PortMock::deactivateInterfaces"));
00500       portAdmin.deactivatePorts();
00501       CPPUNIT_ASSERT_EQUAL(2,logger.countLog("PortMock::deactivateInterfaces"));
00502 
00503       m_poa->deactivate_object(*m_poa->servant_to_id(port0));
00504       m_poa->deactivate_object(*m_poa->servant_to_id(port1));
00505       delete port1;
00506       delete port0;
00507     }
00513     void test_deactivatePorts(void)
00514     {
00515     }
00516                 
00517   };
00518 }; // namespace PortAdmin
00519 
00520 /*
00521  * Register test suite
00522  */
00523 CPPUNIT_TEST_SUITE_REGISTRATION(PortAdmin::PortAdminTests);
00524 
00525 #ifdef LOCAL_MAIN
00526 int main(int argc, char* argv[])
00527 {
00528 
00529   FORMAT format = TEXT_OUT;
00530   int target = 0;
00531   std::string xsl;
00532   std::string ns;
00533   std::string fname;
00534   std::ofstream ofs;
00535 
00536   int i(1);
00537   while (i < argc)
00538     {
00539       std::string arg(argv[i]);
00540       std::string next_arg;
00541       if (i + 1 < argc) next_arg = argv[i + 1];
00542       else              next_arg = "";
00543 
00544       if (arg == "--text") { format = TEXT_OUT; break; }
00545       if (arg == "--xml")
00546         {
00547           if (next_arg == "")
00548             {
00549               fname = argv[0];
00550               fname += ".xml";
00551             }
00552           else
00553             {
00554               fname = next_arg;
00555             }
00556           format = XML_OUT;
00557           ofs.open(fname.c_str());
00558         }
00559       if ( arg == "--compiler"  ) { format = COMPILER_OUT; break; }
00560       if ( arg == "--cerr"      ) { target = 1; break; }
00561       if ( arg == "--xsl"       )
00562         {
00563           if (next_arg == "") xsl = "default.xsl"; 
00564           else                xsl = next_arg;
00565         }
00566       if ( arg == "--namespace" )
00567         {
00568           if (next_arg == "")
00569             {
00570               std::cerr << "no namespace specified" << std::endl;
00571               exit(1); 
00572             }
00573           else
00574             {
00575               xsl = next_arg;
00576             }
00577         }
00578       ++i;
00579     }
00580   CppUnit::TextUi::TestRunner runner;
00581   if ( ns.empty() )
00582     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00583   else
00584     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
00585   CppUnit::Outputter* outputter = 0;
00586   std::ostream* stream = target ? &std::cerr : &std::cout;
00587   switch ( format )
00588     {
00589     case TEXT_OUT :
00590       outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
00591       break;
00592     case XML_OUT :
00593       std::cout << "XML_OUT" << std::endl;
00594       outputter = new CppUnit::XmlOutputter(&runner.result(),
00595                                             ofs, "shift_jis");
00596       static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
00597       break;
00598     case COMPILER_OUT :
00599       outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
00600       break;
00601     }
00602   runner.setOutputter(outputter);
00603   runner.run();
00604   return 0; // runner.run() ? 0 : 1;
00605 }
00606 #endif // MAIN
00607 #endif // PortAdmin_cpp


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Sun Mar 26 2017 03:37:16