CorbaPortTests.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00012 /*
00013  * $Log: CorbaPortTests.cpp,v $
00014  * Revision 1.2  2008/04/04 12:06:09  arafune
00015  * Refactored some tests. No new tests were added.
00016  *
00017  * Revision 1.1  2007/12/20 07:50:18  arafune
00018  * *** empty log message ***
00019  *
00020  * Revision 1.1  2007/01/04 00:50:38  n-ando
00021  * *** empty log message ***
00022  *
00023  *
00024  */
00025 
00026 #ifndef CorbaPort_cpp
00027 #define CorbaPort_cpp
00028 
00029 #include <cppunit/ui/text/TestRunner.h>
00030 #include <cppunit/TextOutputter.h>
00031 #include <cppunit/extensions/TestFactoryRegistry.h>
00032 #include <cppunit/extensions/HelperMacros.h>
00033 #include <cppunit/TestAssert.h>
00034 
00035 #include <rtm/CorbaPort.h>
00036 #include <rtm/CorbaConsumer.h>
00037 
00038 #include "MyServiceSkel.h"
00039 
00040 class MyService_impl
00041   : public virtual POA_MyService,
00042     public virtual PortableServer::RefCountServantBase
00043 {
00044 public:
00045   MyService_impl() : m_hello_world_called(false)
00046   {
00047   };
00048 
00049   virtual ~MyService_impl()
00050   {
00051   };
00052         
00053   void setName(const char* name)
00054   {
00055     m_name = name;
00056   }
00057         
00058   char* name()
00059   {
00060     return "MyService";
00061   }
00062         
00063   void hello_world()
00064   {
00065     m_hello_world_called = true;
00066   }
00067         
00068   void print_msg(const char* msg)
00069   {
00070     std::cout << m_name << ": " << msg << std::endl;
00071   }
00072 
00073   bool is_hello_world_called()
00074   {
00075     return m_hello_world_called;
00076   }
00077         
00078 private:
00079   std::string m_name;
00080   bool m_hello_world_called;
00081 };
00082 
00083 
00088 class CorbaPortMock
00089  : public RTC::CorbaPort
00090 {
00091   public:
00095     CorbaPortMock(const char* name)
00096     : RTC::CorbaPort(name)
00097     {
00098     }
00102     virtual ~CorbaPortMock(void)
00103     {
00104     }
00108     void deactivateInterfaces_public()
00109     {
00110         deactivateInterfaces();
00111     }
00115     void activateInterfaces_public()
00116     {
00117         activateInterfaces();
00118     }
00119 };
00124 namespace CorbaPort
00125 {
00126   class CorbaPortTests
00127     : public CppUnit::TestFixture
00128   {
00129     CPPUNIT_TEST_SUITE(CorbaPortTests);
00130     CPPUNIT_TEST(test_get_port_profile);
00131     CPPUNIT_TEST(test_connect);
00132     CPPUNIT_TEST(test_disconnect);
00133     CPPUNIT_TEST(test_registerProvider);
00134     CPPUNIT_TEST(test_activateInterfaces_deactivateInterfaces);
00135     CPPUNIT_TEST_SUITE_END();
00136                 
00137   private:
00138     CORBA::ORB_ptr m_pORB;
00139     PortableServer::POA_ptr m_pPOA;
00140                 
00141   public:
00145     CorbaPortTests()
00146     {
00147     }
00148     
00152     ~CorbaPortTests()
00153     {
00154     }
00155                 
00159     virtual void setUp()
00160     {
00161       int argc(0);
00162       char** argv(NULL);
00163                         
00164       m_pORB = CORBA::ORB_init(argc, argv);
00165       m_pPOA = PortableServer::POA::_narrow(
00166                                             m_pORB->resolve_initial_references("RootPOA"));
00167       m_pPOA->the_POAManager()->activate();
00168     }
00169                 
00173     virtual void tearDown()
00174     { 
00175       try
00176         {
00177           // m_pPOA->destroy(false, false);
00178           // m_pPOA->the_POAManager()->deactivate(false, false);
00179           // m_pORB->destroy(); 
00180         }
00181       catch (...)
00182         {
00183           // do nothing
00184         }
00185     }
00186                 
00195     void test_get_port_profile()
00196     {
00197       // テスト対象となるCorbaPortを構成する
00198       MyService_impl* pMyServiceImpl
00199         = new MyService_impl(); // will be deleted automatically
00200       RTC::CorbaConsumer<MyService>* pMyServiceConsumer
00201         = new RTC::CorbaConsumer<MyService>(); // will be deleted automatically
00202                         
00203       RTC::CorbaPort* port = new RTC::CorbaPort("name of port");
00204       coil::Properties dummy;
00205       port->init(dummy);
00206       port->registerProvider("MyService (provided)", "Generic (provided)", *pMyServiceImpl);
00207       port->registerConsumer("MyService (required)", "Generic (required)", *pMyServiceConsumer);
00208                         
00209       RTC::PortService_var portRef = port->getPortRef();
00210       RTC::PortProfile* profile = portRef->get_port_profile();
00211 
00212       // ポート名称を正しく取得できるか?
00213       CPPUNIT_ASSERT_EQUAL(std::string("unknown.name of port"), std::string(profile->name));
00214                         
00215       // インタフェースプロファイルを取得し、あらかじめ設定しておいた内容と一致することを確認する
00216       RTC::PortInterfaceProfileList& profiles = profile->interfaces;
00217       for (CORBA::ULong i = 0; i < profile->interfaces.length(); ++i)
00218         {
00219           if (std::string(profiles[i].instance_name)
00220               == std::string("MyService (provided)"))
00221             {
00222               // type_nameを正しく取得できるか?
00223               CPPUNIT_ASSERT_EQUAL(std::string("Generic (provided)"),
00224                                    std::string(profiles[i].type_name));
00225                                         
00226               // polarityを正しく取得できるか?
00227               CPPUNIT_ASSERT_EQUAL(RTC::PROVIDED, profiles[i].polarity);
00228             }
00229           else if (std::string(profiles[i].instance_name)
00230                    == std::string("MyService (required)"))
00231             {
00232               // type_nameを正しく取得できるか?
00233               CPPUNIT_ASSERT_EQUAL(std::string("Generic (required)"),
00234                                    std::string(profiles[i].type_name));
00235 
00236               // polarityを正しく取得できるか?
00237               CPPUNIT_ASSERT_EQUAL(RTC::REQUIRED, profiles[i].polarity);
00238             }
00239           else
00240             {
00241               // 予期しないinstance_nameが取得された場合
00242               std::string msg("Unexpected instance_name:");
00243               msg += std::string(profiles[i].instance_name);
00244               CPPUNIT_FAIL(msg);
00245             }
00246         }
00247 
00248       delete port;
00249       delete pMyServiceConsumer;
00250       m_pPOA->deactivate_object(*m_pPOA->servant_to_id(pMyServiceImpl));
00251       delete pMyServiceImpl;
00252     }
00253                 
00260     void test_connect()
00261     {
00262       // ポート0を構成する
00263       MyService_impl* pMyServiceImplA
00264         = new MyService_impl(); // will be deleted automatically
00265       RTC::CorbaConsumer<MyService>* pMyServiceConsumerB
00266         = new RTC::CorbaConsumer<MyService>(); // will be deleted automatically
00267                         
00268       CorbaPortMock* port0 = new CorbaPortMock("name of port0");
00269       port0->registerProvider("MyServiceA", "Generic", *pMyServiceImplA);
00270       port0->registerConsumer("MyServiceB", "Generic", *pMyServiceConsumerB);
00271 
00272       // ポート1を構成する
00273       MyService_impl* pMyServiceImplB
00274         = new MyService_impl(); // will be deleted automatically
00275       RTC::CorbaConsumer<MyService>* pMyServiceConsumerA
00276         = new RTC::CorbaConsumer<MyService>(); // will be deleted automatically
00277                         
00278       CorbaPortMock* port1 = new CorbaPortMock("name of port1");
00279       port1->registerProvider("MyServiceB", "Generic", *pMyServiceImplB);
00280       port1->registerConsumer("MyServiceA", "Generic", *pMyServiceConsumerA);
00281                         
00282       // 接続プロファイルを構成する
00283       RTC::ConnectorProfile connProfile;
00284       connProfile.connector_id = "";
00285       connProfile.name = CORBA::string_dup("name of connector profile");
00286       connProfile.ports.length(2);
00287       connProfile.ports[0] = port0->getPortRef();
00288       connProfile.ports[1] = port1->getPortRef();
00289 
00290       // 接続する
00291       port0->getPortRef()->connect(connProfile);
00292 
00293       port0->activateInterfaces_public();
00294       port1->activateInterfaces_public();
00295 
00296       // ポート1のコンシューマ側からメソッドを呼び出すと、ポート0のプロバイダ側が意図どおり呼び出されるか?
00297       CPPUNIT_ASSERT(! pMyServiceImplA->is_hello_world_called());
00298       (*pMyServiceConsumerA)->hello_world();
00299       CPPUNIT_ASSERT(pMyServiceImplA->is_hello_world_called());
00300 
00301       // ポート0のコンシューマ側からメソッドを呼び出すと、ポート1のプロバイダ側が意図どおり呼び出されるか?
00302       CPPUNIT_ASSERT(! pMyServiceImplB->is_hello_world_called());
00303       (*pMyServiceConsumerB)->hello_world();
00304       CPPUNIT_ASSERT(pMyServiceImplB->is_hello_world_called());
00305 
00306 //      delete port1;
00307       delete port0;
00308       delete pMyServiceConsumerA;
00309       m_pPOA->deactivate_object(*m_pPOA->servant_to_id(pMyServiceImplB));
00310       delete pMyServiceImplB;
00311       delete pMyServiceConsumerB;
00312       m_pPOA->deactivate_object(*m_pPOA->servant_to_id(pMyServiceImplA));
00313       delete pMyServiceImplA;
00314     }
00315     
00321     void test_disconnect()
00322     {
00323       // ポート0を構成する
00324       MyService_impl* pMyServiceImplA
00325         = new MyService_impl(); // will be deleted automatically
00326       RTC::CorbaConsumer<MyService>* pMyServiceConsumerB
00327         = new RTC::CorbaConsumer<MyService>(); // will be deleted automatically
00328                         
00329       CorbaPortMock* port0 = new CorbaPortMock("name of port0-1");
00330       port0->registerProvider("MyServiceA-1", "Generic", *pMyServiceImplA);
00331       port0->registerConsumer("MyServiceB-1", "Generic", *pMyServiceConsumerB);
00332 
00333       // ポート1を構成する
00334       MyService_impl* pMyServiceImplB
00335         = new MyService_impl(); // will be deleted automatically
00336       RTC::CorbaConsumer<MyService>* pMyServiceConsumerA
00337         = new RTC::CorbaConsumer<MyService>(); // will be deleted automatically
00338                         
00339       CorbaPortMock* port1 = new CorbaPortMock("name of port1-1");
00340       port1->registerProvider("MyServiceB-1", "Generic", *pMyServiceImplB);
00341       port1->registerConsumer("MyServiceA-1", "Generic", *pMyServiceConsumerA);
00342                         
00343       // 接続プロファイルを構成する
00344       RTC::ConnectorProfile connProfile;
00345       connProfile.connector_id = "id1";
00346       connProfile.name = CORBA::string_dup("name of connector profile-1");
00347       connProfile.ports.length(2);
00348       connProfile.ports[0] = port0->getPortRef();
00349       connProfile.ports[1] = port1->getPortRef();
00350 
00351       // 接続する
00352       port0->getPortRef()->connect(connProfile);
00353 
00354       port0->activateInterfaces_public();
00355       port1->activateInterfaces_public();
00356 
00357       // ポート1のコンシューマ側からメソッドを呼び出すと、ポート0のプロバイダ側が意図どおり呼び出されるか?
00358       CPPUNIT_ASSERT(! pMyServiceImplA->is_hello_world_called());
00359       (*pMyServiceConsumerA)->hello_world();
00360       CPPUNIT_ASSERT(pMyServiceImplA->is_hello_world_called());
00361                         
00362       // 切断する
00363       port0->getPortRef()->disconnect(connProfile.connector_id);
00364                         
00365       // ポート0のコンシューマ側からメソッドを呼び出しを試みると、意図どおりに失敗するか?
00366       try
00367         {
00368           CPPUNIT_ASSERT(! pMyServiceImplB->is_hello_world_called());
00369           (*pMyServiceConsumerB)->hello_world();
00370                                                         
00371           CPPUNIT_FAIL("Couldn't catch no exceptions. Disconnection failed.");
00372         }
00373       catch(...)
00374         {
00375           // Properly disconnected.
00376         }
00377 
00378 //      delete port1;
00379       delete port0;
00380       delete pMyServiceConsumerA;
00381       m_pPOA->deactivate_object(*m_pPOA->servant_to_id(pMyServiceImplB));
00382       delete pMyServiceImplB;
00383       delete pMyServiceConsumerB;
00384       m_pPOA->deactivate_object(*m_pPOA->servant_to_id(pMyServiceImplA));
00385       delete pMyServiceImplA;
00386     }
00391     void test_registerProvider(void)
00392     {
00393       MyService_impl* pImpl0
00394         = new MyService_impl();
00395 
00396       MyService_impl* pImpl1
00397         = new MyService_impl();
00398                         
00399                         
00400       CorbaPortMock* port0 = new CorbaPortMock("name of port");
00401       bool ret;
00402       ret = port0->registerProvider("registerProvider0", "Generic", *pImpl0);
00403       CPPUNIT_ASSERT_EQUAL(true,ret);
00404 
00405       //既に登録してあるインスタンス名とオブジェクトを登録した場合、falseを返す。
00406       ret = port0->registerProvider("registerProvider0", "Generic", *pImpl0);
00407       CPPUNIT_ASSERT_EQUAL(false,ret);
00408 
00409       //既に登録してあるインスタンス名を登録した場合、falseを返す。
00410       ret = port0->registerProvider("registerProvider0", "Generic", *pImpl1);
00411       CPPUNIT_ASSERT_EQUAL(false,ret);
00412      
00413       //別なインスタンス名で既に登録してあるオブジェクトを登録した場合、trueを返す。
00414       //   falseを返すが登録される。
00415       ret = port0->registerProvider("registerProvider1", "Generic", *pImpl0);
00416       CPPUNIT_ASSERT_EQUAL(true,ret);
00417 
00418       //登録してないインスタンス名とオブジェクトを登録した場合、trueを返す。
00419       ret = port0->registerProvider("registerProvider2", "Generic", *pImpl1);
00420       CPPUNIT_ASSERT_EQUAL(true,ret);
00421 
00422       port0->deactivateInterfaces_public();
00423 
00424       delete port0;
00425       m_pPOA->deactivate_object(*m_pPOA->servant_to_id(pImpl1));
00426       delete pImpl1;
00427       m_pPOA->deactivate_object(*m_pPOA->servant_to_id(pImpl0));
00428       delete pImpl0;
00429     }
00434     void test_activateInterfaces_deactivateInterfaces(void)
00435     {
00436       // ポート0を構成する
00437       MyService_impl* pMyServiceImplA
00438         = new MyService_impl(); // will be deleted automatically
00439       RTC::CorbaConsumer<MyService>* pMyServiceConsumerB
00440         = new RTC::CorbaConsumer<MyService>(); // will be deleted automatically
00441                         
00442       CorbaPortMock* port0 = new CorbaPortMock("name of port0");
00443       port0->registerProvider("MyServiceAA", "Generic", *pMyServiceImplA);
00444       port0->registerConsumer("MyServiceBB", "Generic", *pMyServiceConsumerB);
00445 
00446       // ポート1を構成する
00447       MyService_impl* pMyServiceImplB
00448         = new MyService_impl(); // will be deleted automatically
00449       RTC::CorbaConsumer<MyService>* pMyServiceConsumerA
00450         = new RTC::CorbaConsumer<MyService>(); // will be deleted automatically
00451                         
00452       CorbaPortMock* port1 = new CorbaPortMock("name of port1");
00453       port1->registerProvider("MyServiceBB", "Generic", *pMyServiceImplB);
00454       port1->registerConsumer("MyServiceAA", "Generic", *pMyServiceConsumerA);
00455                         
00456       // 接続プロファイルを構成する
00457       RTC::ConnectorProfile connProfile;
00458       connProfile.connector_id = "";
00459       connProfile.name = CORBA::string_dup("name of connector profile");
00460       connProfile.ports.length(2);
00461       connProfile.ports[0] = port0->getPortRef();
00462       connProfile.ports[1] = port1->getPortRef();
00463 
00464       // 接続する
00465       port0->getPortRef()->connect(connProfile);
00466 
00467       port0->activateInterfaces_public();
00468       port1->activateInterfaces_public();
00469 
00470       // ポート1のコンシューマ側からメソッドを呼び出すと、ポート0のプロバイダ側が意図どおり呼び出されるか?
00471       CPPUNIT_ASSERT(! pMyServiceImplA->is_hello_world_called());
00472       (*pMyServiceConsumerA)->hello_world();
00473       CPPUNIT_ASSERT(pMyServiceImplA->is_hello_world_called());
00474                         
00475       // 切断する
00476       port0->getPortRef()->disconnect(connProfile.connector_id);
00477 
00478       //全てdeactivate obujectする
00479       port0->deactivateInterfaces_public();
00480       port1->deactivateInterfaces_public();
00481 
00482       port0->getPortRef()->connect(connProfile);
00483 
00484       //deactivate object が実行されてたため、エラーとなる。
00485       try
00486       {
00487          (*pMyServiceConsumerA)->hello_world();
00488           CPPUNIT_FAIL("Couldn't catch no exceptions. Disconnection failed.");
00489       }
00490       catch(...)
00491       {
00492       }
00493                         
00494       // 切断する
00495       port0->getPortRef()->disconnect(connProfile.connector_id);
00496 
00497       port0->activateInterfaces_public();
00498       port1->activateInterfaces_public();
00499 
00500       port0->getPortRef()->connect(connProfile);
00501 
00502       //activate object が実行されてたため、正常に動作する 。
00503       (*pMyServiceConsumerA)->hello_world();
00504 
00505       // 切断する
00506       port0->getPortRef()->disconnect(connProfile.connector_id);
00507 
00508 //      delete port1;
00509       delete port0;
00510       delete pMyServiceConsumerA;
00511       m_pPOA->deactivate_object(*m_pPOA->servant_to_id(pMyServiceImplB));
00512       delete pMyServiceImplB;
00513       delete pMyServiceConsumerB;
00514       m_pPOA->deactivate_object(*m_pPOA->servant_to_id(pMyServiceImplA));
00515       delete pMyServiceImplA;
00516     }
00517     
00518   };
00519 }; // namespace CorbaPort
00520 
00521 /*
00522  * Register test suite
00523  */
00524 CPPUNIT_TEST_SUITE_REGISTRATION(CorbaPort::CorbaPortTests);
00525 
00526 #ifdef LOCAL_MAIN
00527 int main(int argc, char* argv[])
00528 {
00529 
00530   FORMAT format = TEXT_OUT;
00531   int target = 0;
00532   std::string xsl;
00533   std::string ns;
00534   std::string fname;
00535   std::ofstream ofs;
00536 
00537   int i(1);
00538   while (i < argc)
00539     {
00540       std::string arg(argv[i]);
00541       std::string next_arg;
00542       if (i + 1 < argc) next_arg = argv[i + 1];
00543       else              next_arg = "";
00544 
00545       if (arg == "--text") { format = TEXT_OUT; break; }
00546       if (arg == "--xml")
00547         {
00548           if (next_arg == "")
00549             {
00550               fname = argv[0];
00551               fname += ".xml";
00552             }
00553           else
00554             {
00555               fname = next_arg;
00556             }
00557           format = XML_OUT;
00558           ofs.open(fname.c_str());
00559         }
00560       if ( arg == "--compiler"  ) { format = COMPILER_OUT; break; }
00561       if ( arg == "--cerr"      ) { target = 1; break; }
00562       if ( arg == "--xsl"       )
00563         {
00564           if (next_arg == "") xsl = "default.xsl"; 
00565           else                xsl = next_arg;
00566         }
00567       if ( arg == "--namespace" )
00568         {
00569           if (next_arg == "")
00570             {
00571               std::cerr << "no namespace specified" << std::endl;
00572               exit(1); 
00573             }
00574           else
00575             {
00576               xsl = next_arg;
00577             }
00578         }
00579       ++i;
00580     }
00581   CppUnit::TextUi::TestRunner runner;
00582   if ( ns.empty() )
00583     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00584   else
00585     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
00586   CppUnit::Outputter* outputter = 0;
00587   std::ostream* stream = target ? &std::cerr : &std::cout;
00588   switch ( format )
00589     {
00590     case TEXT_OUT :
00591       outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
00592       break;
00593     case XML_OUT :
00594       std::cout << "XML_OUT" << std::endl;
00595       outputter = new CppUnit::XmlOutputter(&runner.result(),
00596                                             ofs, "shift_jis");
00597       static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
00598       break;
00599     case COMPILER_OUT :
00600       outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
00601       break;
00602     }
00603   runner.setOutputter(outputter);
00604   runner.run();
00605   return 0; // runner.run() ? 0 : 1;
00606 }
00607 #endif // MAIN
00608 #endif // CorbaPort_cpp


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Thu Aug 27 2015 14:16:37