PortBaseTests.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00021 /*
00022  * $Log: PortBaseTests.cpp,v $
00023  * Revision 1.2  2008/02/08 10:57:23  arafune
00024  * Some tests were added.
00025  *
00026  * Revision 1.1  2007/12/20 07:50:17  arafune
00027  * *** empty log message ***
00028  *
00029  * Revision 1.3  2007/04/13 15:05:10  n-ando
00030  * Now RTC::OK becomes RTC::RTC_OK in RTC.idl.
00031  *
00032  * Revision 1.2  2007/01/12 14:44:43  n-ando
00033  * Some fixes for distribution control.
00034  *
00035  * Revision 1.1  2006/11/27 08:35:12  n-ando
00036  * TestSuites are devided into each directory.
00037  *
00038  * Revision 1.2  2006/11/13 12:30:06  kurihara
00039  *
00040  * document is added.
00041  *
00042  * Revision 1.1  2006/11/08 01:19:07  kurihara
00043  *
00044  * test program for PortBase class.
00045  *
00046  */
00047 
00048 #ifndef PortBase_cpp
00049 #define PortBase_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 #include <rtm/RTC.h>
00060 #include <rtm/PortBase.h>
00061 #include <rtm/RTObject.h>
00062 #include <rtm/PortCallback.h>
00063 
00069 namespace PortBase
00070 {
00071 
00072   class ConnectionCallbackMock : public RTC::ConnectionCallback
00073   {
00074   public:
00075     ConnectionCallbackMock(const char* name) : m_name(name) {}
00076     virtual ~ConnectionCallbackMock()
00077     {
00078       //std::cout << "dtor of " << m_name << std::endl;
00079     }
00080 
00081     virtual void operator()(RTC::ConnectorProfile& profile)
00082     {
00083       std::cout << "---------------------------------------------"   << std::endl;
00084       std::cout << "Connection Callback: " << m_name                 << std::endl;
00085       std::cout << "Profile::name: " << profile.name           << std::endl;
00086       std::cout << "---------------------------------------------"   << std::endl;
00087     };
00088     std::string m_name;
00089   };
00090 
00091 
00092   class PortBaseMock : public RTC::PortBase
00093   {
00094   public:
00095                 
00096     PortBaseMock(const RTC::PortProfile& profile)
00097     {
00098       this->m_profile = profile;
00099       this->m_profile.connector_profiles[0].ports.length(1);
00100       this->m_profile.connector_profiles[0].ports[0] = this->m_objref;
00101       this->m_profile.port_ref = this->m_objref;
00102     }
00103 
00104     const std::string getUUID() const
00105     {
00106       return RTC::PortBase::getUUID();
00107     }
00108                 
00109     virtual RTC::ReturnCode_t notify_connect(RTC::ConnectorProfile& connector_profile)
00110       throw (CORBA::SystemException)
00111     {
00112       _notifyConnectTimes.push_back(getNow());
00113       return PortBase::notify_connect(connector_profile);
00114     }
00115                 
00116     virtual RTC::ReturnCode_t notify_disconnect(const char* connector_id)
00117       throw (CORBA::SystemException)
00118     {
00119       _notifyDisconnectTimes.push_back(getNow());
00120       return PortBase::notify_disconnect(connector_id);
00121     }
00122     void erase_m_profile(void) 
00123     {
00124       CORBA_SeqUtil::erase(this->m_profile.connector_profiles, 0);
00125     }
00126                 
00127   protected:
00128         
00129     virtual RTC::ReturnCode_t publishInterfaces(RTC::ConnectorProfile& connector_profile)
00130     {
00131       _publishIfsTimes.push_back(getNow());
00132       return RTC::RTC_OK;
00133     }
00134                 
00135     virtual RTC::ReturnCode_t subscribeInterfaces(const RTC::ConnectorProfile& connector_profile)
00136     {
00137       _subscribeIfsTimes.push_back(getNow());
00138       return RTC::RTC_OK;
00139     }
00140                 
00141     virtual void unsubscribeInterfaces(const RTC::ConnectorProfile& connector_profile)
00142     {
00143       _unsubscribeIfsTimes.push_back(getNow());
00144     }
00145     virtual void activateInterfaces()
00146     {
00147     }
00148     virtual void deactivateInterfaces()
00149     {
00150     }
00151         
00152   private:
00153         
00154     std::vector<timeval> _notifyConnectTimes;
00155     std::vector<timeval> _notifyDisconnectTimes;
00156     std::vector<timeval> _publishIfsTimes;
00157     std::vector<timeval> _subscribeIfsTimes;
00158     std::vector<timeval> _unsubscribeIfsTimes;
00159         
00160   private:
00161         
00162     timeval getNow() const
00163     {
00164       timeval now;
00165       gettimeofday(&now, 0);
00166       return now;
00167     }
00168                 
00169   public:
00170         
00171     const std::vector<timeval>& getNotifyConnectTimes() const
00172     {
00173       return _notifyConnectTimes;
00174     }
00175                 
00176     const std::vector<timeval>& getNotifyDisconnectTimes() const
00177     {
00178       return _notifyDisconnectTimes;
00179     }
00180                 
00181     const std::vector<timeval>& getPublishIfsTimes() const
00182     {
00183       return _publishIfsTimes;
00184     }
00185                 
00186     const std::vector<timeval>& getSubscribeIfsTimes() const
00187     {
00188       return _subscribeIfsTimes;
00189     }
00190                 
00191     const std::vector<timeval>& getUnsubscribeIfsTimes() const
00192     {
00193       return _unsubscribeIfsTimes;
00194     }
00195   };
00196 
00197   
00198   int g_argc;
00199   std::vector<std::string> g_argv;
00200 
00201   class PortBaseTests
00202     : public CppUnit::TestFixture
00203   {
00204     CPPUNIT_TEST_SUITE(PortBaseTests);
00205                 
00206     CPPUNIT_TEST(test_get_port_profile);
00207     CPPUNIT_TEST(test_getPortProfile);
00208     CPPUNIT_TEST(test_get_connector_profiles);
00209     CPPUNIT_TEST(test_get_connector_profile);
00210     CPPUNIT_TEST(test_connect);
00211     CPPUNIT_TEST(test_notify_connect);
00212     CPPUNIT_TEST(test_disconnect);
00213     CPPUNIT_TEST(test_setName);
00214     CPPUNIT_TEST(test_getProfile);
00215     CPPUNIT_TEST(test_setPortRef);
00216     CPPUNIT_TEST(test_getPortRef);
00217     CPPUNIT_TEST(test_getUUID);
00218     CPPUNIT_TEST(test_disconnect_all);
00219     CPPUNIT_TEST(test_setOwner);
00220                 
00221     CPPUNIT_TEST_SUITE_END();
00222                 
00223   private:
00224         
00225     CORBA::ORB_ptr m_orb;
00226     RTC::PortBase* m_pPortBase;
00227     RTC::PortBase* m_pPortBase_2;
00228     RTC::PortBase* m_pPortBase_3;
00229 
00230   public:
00231     ConnectionCallbackMock* m_on_publish;
00232     ConnectionCallbackMock* m_on_subscribe;
00233     ConnectionCallbackMock* m_on_connected;
00234     ConnectionCallbackMock* m_on_unsubscribe;
00235     ConnectionCallbackMock* m_on_disconnected;
00236     ConnectionCallbackMock* m_on_connection_lost;
00237         
00241     PortBaseTests()
00242     {
00243     }
00244                 
00248     ~PortBaseTests()
00249     {
00250     }
00251                 
00264     virtual void setUp()
00265     {
00266       char* argv[g_argc];
00267       for (int i = 0; i < g_argc; i++) {
00268         argv[i] = (char *)g_argv[i].c_str();
00269       }
00270 
00271       // ORBの初期化
00272       m_orb = CORBA::ORB_init(g_argc, argv);
00273       PortableServer::POA_ptr poa = PortableServer::POA::_narrow(
00274                                 m_orb->resolve_initial_references("RootPOA"));
00275                         
00276       // PortProfile.interfacesの構築準備
00277       RTC::PortInterfaceProfile portIfProfile;
00278       portIfProfile.instance_name = "PortInterfaceProfile-instance_name";
00279       portIfProfile.type_name = "PortInterfaceProfile-type_name";
00280       portIfProfile.polarity = RTC::REQUIRED;
00281 
00282       RTC::PortInterfaceProfileList portIfProfiles;
00283       portIfProfiles.length(1);
00284       portIfProfiles[0] = portIfProfile;
00285 
00286       // PortProfile.connector_profilesの構築準備
00287       SDOPackage::NameValue connProfileProperty;
00288       connProfileProperty.name = "ConnectorProfile-properties0-name";
00289       connProfileProperty.value <<= CORBA::Float(1.1);
00290                         
00291       SDOPackage::NVList connProfileProperties;
00292       connProfileProperties.length(1);
00293       connProfileProperties[0] = connProfileProperty;
00294       RTC::ConnectorProfile connProfile;
00295       connProfile.name = "ConnectorProfile-name";
00296       connProfile.connector_id = "connect_id0";
00297       connProfile.properties = connProfileProperties;
00298 
00299       RTC::ConnectorProfileList connProfiles;
00300       connProfiles.length(1);
00301       connProfiles[0] = connProfile;
00302 
00303       // PortProfile.propertiesの構築準備
00304       SDOPackage::NameValue portProfileProperty;
00305       portProfileProperty.name = "PortProfile-properties0-name";
00306       portProfileProperty.value <<= CORBA::Float(2.2);
00307       SDOPackage::NVList portProfileProperties;
00308       portProfileProperties.length(1);
00309       portProfileProperties[0] = portProfileProperty;
00310 
00311       // PortProfileを構築する
00312       RTC::PortProfile portProfile;
00313       portProfile.name = "inport0";
00314       portProfile.interfaces = portIfProfiles;
00315       portProfile.connector_profiles = connProfiles;
00316       portProfile.properties = portProfileProperties;
00317 
00318       // PortBaseのインスタンスを生成する
00319       m_pPortBase = new PortBaseMock(portProfile);
00320       m_pPortBase_2 = new PortBaseMock(portProfile);
00321       m_pPortBase_3 = new PortBaseMock(portProfile);
00322 
00323       // POAを活性化する                        
00324       PortableServer::POAManager_var poaMgr = poa->the_POAManager();
00325       poaMgr->activate();
00326 
00327       m_on_publish = new ConnectionCallbackMock("OnPublishInterfaces");
00328       m_on_subscribe = new ConnectionCallbackMock("OnSubscribeInterfaces");
00329       m_on_connected = new ConnectionCallbackMock("OnConnected");
00330       m_on_unsubscribe = new ConnectionCallbackMock("OnUnsubscribeInterfaces");
00331       m_on_disconnected = new ConnectionCallbackMock("OnDisconnected");
00332       m_on_connection_lost = new ConnectionCallbackMock("OnConnectionLost");
00333 
00334       m_pPortBase->setOnPublishInterfaces(m_on_publish);
00335       m_pPortBase->setOnSubscribeInterfaces(m_on_subscribe);
00336       m_pPortBase->setOnConnected(m_on_connected);
00337       m_pPortBase->setOnUnsubscribeInterfaces(m_on_unsubscribe);
00338       m_pPortBase->setOnDisconnected(m_on_disconnected);
00339       m_pPortBase->setOnConnectionLost(m_on_connection_lost);
00340 
00341     }
00342                 
00346     virtual void tearDown()
00347     {
00348 /*
00349       delete m_on_connection_lost;
00350       delete m_on_disconnected;
00351       delete m_on_unsubscribe;
00352       delete m_on_connected;
00353       delete m_on_subscribe;
00354       delete m_on_publish;
00355       delete m_pPortBase_3;
00356       delete m_pPortBase_2;
00357       delete m_pPortBase;
00358 */
00359       //if (m_orb != 0) {
00360       // m_orb->destroy();
00361       // m_orb = 0;
00362       // m_pPortBase = 0;
00363       // }
00364     }
00365                 
00375     void test_get_port_profile()
00376     {
00377       // (1) オブジェクト参照経由で、get_port_profile()に正しくアクセスできるか?
00378       // get_port_profile()はCORBAインタフェースなので、オブジェクト参照経由でアクセスし、
00379       // CORBAインタフェースとして機能していることを確認する
00380       const RTC::PortService_ptr portRef = m_pPortBase->getPortRef();
00381       const RTC::PortProfile* pPortProfile = portRef->get_port_profile();
00382                         
00383       // (2) PortProfile.nameを正しく取得できるか?
00384       CPPUNIT_ASSERT_EQUAL(
00385                            std::string("inport0"),
00386                            std::string(pPortProfile->name));
00387 
00388       // (3) PortProfile.interfaceを正しく取得できるか?
00389       const RTC::PortInterfaceProfile& portIfProfile = pPortProfile->interfaces[0];
00390       // (3-a) PortInterfaceProfile.instance_nameを正しく取得できるか?
00391       CPPUNIT_ASSERT_EQUAL(
00392                            std::string("PortInterfaceProfile-instance_name"),
00393                            std::string(portIfProfile.instance_name));
00394 
00395       // (3-b) PortInterfaceProfile.type_nameを正しく取得できるか?
00396       CPPUNIT_ASSERT_EQUAL(
00397                            std::string("PortInterfaceProfile-type_name"),
00398                            std::string(portIfProfile.type_name));
00399 
00400       // (3-c) PortInterfaceProfile.polarityを正しく取得できるか?
00401       CPPUNIT_ASSERT_EQUAL(RTC::REQUIRED, portIfProfile.polarity);
00402 
00403       // (4) PortProfile.connector_profilesを正しく取得できるか?
00404       const RTC::ConnectorProfile& connProfile = pPortProfile->connector_profiles[0];
00405       // (4-a) ConnectorProfile.nameを正しく取得できるか?
00406       CPPUNIT_ASSERT_EQUAL(
00407                            std::string("ConnectorProfile-name"),
00408                            std::string(connProfile.name));
00409       
00410       // (4-b) ConnectorProfile.connector_idを正しく取得できるか?
00411       CPPUNIT_ASSERT_EQUAL(
00412                            std::string("connect_id0"),
00413                            std::string(connProfile.connector_id));
00414                         
00415       // (4-c) ConnectorProfile.propertiesを正しく取得できるか?
00416       CPPUNIT_ASSERT_EQUAL(
00417                            std::string("ConnectorProfile-properties0-name"),
00418                            std::string(connProfile.properties[0].name));
00419                         
00420       {
00421         CORBA::Float value;
00422         connProfile.properties[0].value >>= value;
00423         CPPUNIT_ASSERT_EQUAL(CORBA::Float(1.1), value);
00424       }
00425                         
00426       // (5) PortProfile.propertiesを正しく取得できるか?
00427       CPPUNIT_ASSERT_EQUAL(
00428                            std::string("PortProfile-properties0-name"),
00429                            std::string(pPortProfile->properties[0].name));
00430 
00431       {
00432         CORBA::Float value;
00433         pPortProfile->properties[0].value >>= value;
00434         CPPUNIT_ASSERT_EQUAL(CORBA::Float(2.2), value);
00435       }
00436     }
00437                 
00446     void test_getPortProfile()
00447     {
00448       const RTC::PortProfile& portProfile = m_pPortBase->getPortProfile();
00449                         
00450       // (1) PortProfile.nameを正しく取得できるか?
00451       CPPUNIT_ASSERT_EQUAL(
00452                            std::string("inport0"),
00453                            std::string(portProfile.name));
00454 
00455       // (2) PortProfile.interfaceを正しく取得できるか?
00456       const RTC::PortInterfaceProfile& portIfProfile = portProfile.interfaces[0];
00457       // (2-a) PortInterfaceProfile.instance_nameを正しく取得できるか?
00458       CPPUNIT_ASSERT_EQUAL(
00459                            std::string("PortInterfaceProfile-instance_name"),
00460                            std::string(portIfProfile.instance_name));
00461 
00462       // (2-b) PortInterfaceProfile.type_nameを正しく取得できるか?
00463       CPPUNIT_ASSERT_EQUAL(
00464                            std::string("PortInterfaceProfile-type_name"),
00465                            std::string(portIfProfile.type_name));
00466 
00467       // (2-c) PortInterfaceProfile.polarityを正しく取得できるか?
00468       CPPUNIT_ASSERT_EQUAL(RTC::REQUIRED, portIfProfile.polarity);
00469 
00470       // (3) PortProfile.connector_profilesを正しく取得できるか?
00471       const RTC::ConnectorProfile& connProfile = portProfile.connector_profiles[0];
00472       // (3-a) ConnectorProfile.nameを正しく取得できるか?
00473       CPPUNIT_ASSERT_EQUAL(
00474                            std::string("ConnectorProfile-name"),
00475                            std::string(connProfile.name));
00476       
00477       // (3-b) ConnectorProfile.connector_idを正しく取得できるか?
00478       CPPUNIT_ASSERT_EQUAL(
00479                            std::string("connect_id0"),
00480                            std::string(connProfile.connector_id));
00481                         
00482       // (3-c) ConnectorProfile.propertiesを正しく取得できるか?
00483       CPPUNIT_ASSERT_EQUAL(
00484                            std::string("ConnectorProfile-properties0-name"),
00485                            std::string(connProfile.properties[0].name));
00486                         
00487       {
00488         CORBA::Float value;
00489         connProfile.properties[0].value >>= value;
00490         CPPUNIT_ASSERT_EQUAL(CORBA::Float(1.1), value);
00491       }
00492                         
00493       // (4) PortProfile.propertiesを正しく取得できるか?
00494       CPPUNIT_ASSERT_EQUAL(
00495                            std::string("PortProfile-properties0-name"),
00496                            std::string(portProfile.properties[0].name));
00497 
00498       {
00499         CORBA::Float value;
00500         portProfile.properties[0].value >>= value;
00501         CPPUNIT_ASSERT_EQUAL(CORBA::Float(2.2), value);
00502       }
00503     }
00504                 
00513     void test_get_connector_profiles()
00514     {
00515       // (1) オブジェクト参照経由で、get_connector_profiles()に正しくアクセスできるか?
00516       // get_connector_profiles()はCORBAインタフェースなので、オブジェクト参照経由でアクセスし、
00517       // CORBAインタフェースとして機能していることを確認する
00518       const RTC::PortService_ptr portRef = m_pPortBase->getPortRef();
00519       const RTC::ConnectorProfileList* pConnProfList = portRef->get_connector_profiles();
00520 
00521       // ConnectorProfileList内のConnectorProfileのチェック
00522       const RTC::ConnectorProfile& connProfile = (*pConnProfList)[0];
00523       // (2) ConnectorProfile.nameを正しく取得できるか?
00524       CPPUNIT_ASSERT_EQUAL(
00525                            std::string("ConnectorProfile-name"),
00526                            std::string(connProfile.name));
00527                         
00528       // (3) ConnectorProfile.connector_idを正しく取得できるか?
00529       CPPUNIT_ASSERT_EQUAL(
00530                            std::string("connect_id0"),
00531                            std::string(connProfile.connector_id));
00532 
00533       // (4) ConnectorProfile.propertiesを正しく取得できるか?
00534       const SDOPackage::NameValue& property = connProfile.properties[0];
00535       // (4-a) nameを正しく取得できるか?
00536       CPPUNIT_ASSERT_EQUAL(
00537                            std::string("ConnectorProfile-properties0-name"),
00538                            std::string(property.name));
00539                         
00540       // (4-b) valueを正しく取得できるか?
00541       {
00542         CORBA::Float value;
00543         property.value >>= value;
00544         CPPUNIT_ASSERT_EQUAL(CORBA::Float(1.1), value);
00545       }
00546     }
00547                 
00556     void test_get_connector_profile()
00557     {
00558       // (1) オブジェクト参照経由で、get_connector_profile()に正しくアクセスできるか?
00559       // get_connector_profile()はCORBAインタフェースなので、オブジェクト参照経由でアクセスし、
00560       // CORBAインタフェースとして機能していることを確認する
00561       const RTC::PortService_ptr portRef = m_pPortBase->getPortRef();
00562       const RTC::ConnectorProfile* pConnProfile = portRef->get_connector_profile("connect_id0");
00563 
00564       // (2) ConnectorProfile.nameを正しく取得できるか?
00565       CPPUNIT_ASSERT_EQUAL(
00566                            std::string("ConnectorProfile-name"),
00567                            std::string(pConnProfile->name));
00568 
00569       // (3) ConnectorProfile.connector_idを正しく取得できるか?
00570       CPPUNIT_ASSERT_EQUAL(
00571                            std::string("connect_id0"),
00572                            std::string(pConnProfile->connector_id));
00573 
00574       // (4) ConnectorProfile.propertiesを正しく取得できるか?
00575       const SDOPackage::NameValue& property = pConnProfile->properties[0];
00576       // (4-a) nameを正しく取得できるか?
00577       CPPUNIT_ASSERT_EQUAL(
00578                            std::string("ConnectorProfile-properties0-name"),
00579                            std::string(property.name));
00580 
00581       // (4-b) valueを正しく取得できるか?
00582       {
00583         CORBA::Float value;
00584         property.value >>= value;
00585         CPPUNIT_ASSERT_EQUAL(CORBA::Float(1.1), value);
00586       }
00587     }
00588                 
00596     void test_connect()
00597     {
00598       // (1) オブジェクト参照経由で、connect()に正しくアクセスできるか?
00599       // connect()はCORBAインタフェースなので、オブジェクト参照経由でアクセスし、
00600       // CORBAインタフェースとして機能していることを確認する
00601       RTC::PortService_ptr portRef = m_pPortBase->getPortRef();
00602                         
00603       // 接続時に必要となるConnectorProfileを構築する
00604       RTC::ConnectorProfile connProfile;
00605       connProfile.name = "ConnectorProfile-name";
00606       connProfile.connector_id = "connect_id1";
00607       connProfile.ports.length(1);
00608       connProfile.ports[0] = portRef;
00609 
00610       // (2) 接続が成功するか?
00611       CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, portRef->connect(connProfile));
00612       
00613       // (3) 接続時にnotify_connect()が意図どおりに1回だけ呼び出されたか?
00614       const PortBaseMock* pPortBaseMock = dynamic_cast<const PortBaseMock*>(m_pPortBase);
00615       CPPUNIT_ASSERT(pPortBaseMock != 0);
00616       CPPUNIT_ASSERT_EQUAL(1, (int) pPortBaseMock->getNotifyConnectTimes().size());
00617     }
00618                 
00622     void test_notify_connect()
00623     {
00624       // notify_connect()メソッドは、test_connectにて間接的にテストされているので、ここではテスト不要である
00625     }
00626                 
00634     void test_disconnect()
00635     {
00636       // (1) オブジェクト参照経由で、disconnect()に正しくアクセスできるか?
00637       // disconnect()はCORBAインタフェースなので、オブジェクト参照経由でアクセスし、
00638       // CORBAインタフェースとして機能していることを確認する
00639       RTC::PortService_ptr portRef = m_pPortBase->getPortRef();
00640                         
00641       // 接続時に必要となるConnectorProfileを構築する
00642       RTC::ConnectorProfile connProfile;
00643       connProfile.name = "ConnectorProfile-name";
00644       connProfile.connector_id = "connect_id2";
00645       connProfile.ports.length(1);
00646       connProfile.ports[0] = portRef;
00647 
00648       // まずは接続する
00649       CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, portRef->connect(connProfile));
00650       
00651       // (2) 切断が成功するか?
00652       CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, portRef->disconnect(connProfile.connector_id));
00653       
00654       // (3) 切断時にnotify_disconnect()が、意図どおり1回だけ呼び出されているか?
00655       const PortBaseMock* pPortBaseMock = dynamic_cast<const PortBaseMock*>(m_pPortBase);
00656       CPPUNIT_ASSERT(pPortBaseMock != 0);
00657       CPPUNIT_ASSERT_EQUAL(1, (int) pPortBaseMock->getNotifyDisconnectTimes().size());
00658     }
00659                 
00660     void test_disconnect_all()
00661     {
00662       RTC::PortService_ptr portRef_1 = m_pPortBase->getPortRef();
00663       RTC::PortService_ptr portRef_2 = m_pPortBase_2->getPortRef();
00664       RTC::PortService_ptr portRef_3 = m_pPortBase_3->getPortRef();
00665 
00666       // ここでは、  setUp() の PortBaseMockのコンストラクタ内で
00667       // テストの為設定されている connect_id0 は不要なため削除する
00668       // connect_id0 は
00669       //  get_port_profile(),getPortProfile(),get_connector_profiles() 
00670       // のテストで使用している
00671       PortBaseMock* pPBMock
00672                  = dynamic_cast<PortBaseMock*>(m_pPortBase);
00673       pPBMock->erase_m_profile();
00674 
00675 
00676       RTC::ConnectorProfile connProfile;
00677       connProfile.name = "ConnectorProfile-name";
00678       connProfile.connector_id = "connect_id3";
00679       connProfile.ports.length(3);
00680       connProfile.ports[0] = portRef_1;
00681       connProfile.ports[1] = portRef_2;
00682       connProfile.ports[2] = portRef_3;
00683       
00684       const PortBaseMock* pPortBaseMock_1 = dynamic_cast<const PortBaseMock*>(m_pPortBase);
00685       CPPUNIT_ASSERT(pPortBaseMock_1 != 0);
00686       CPPUNIT_ASSERT_EQUAL(0, (int) pPortBaseMock_1->getNotifyDisconnectTimes().size());
00687       const PortBaseMock* pPortBaseMock_2 = dynamic_cast<const PortBaseMock*>(m_pPortBase_2);
00688       CPPUNIT_ASSERT(pPortBaseMock_2 != 0);
00689       CPPUNIT_ASSERT_EQUAL(0, (int) pPortBaseMock_2->getNotifyDisconnectTimes().size());
00690       const PortBaseMock* pPortBaseMock_3 = dynamic_cast<const PortBaseMock*>(m_pPortBase_3);
00691       CPPUNIT_ASSERT(pPortBaseMock_3 != 0);
00692       CPPUNIT_ASSERT_EQUAL(0, (int) pPortBaseMock_3->getNotifyDisconnectTimes().size());
00693       CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, portRef_1->connect(connProfile));
00694       CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, portRef_1->disconnect_all());
00695       CPPUNIT_ASSERT_EQUAL(1, (int) pPortBaseMock_1->getNotifyDisconnectTimes().size());
00696       CPPUNIT_ASSERT_EQUAL(1, (int) pPortBaseMock_2->getNotifyDisconnectTimes().size());
00697       CPPUNIT_ASSERT_EQUAL(1, (int) pPortBaseMock_3->getNotifyDisconnectTimes().size());
00698 
00699     }
00700                 
00706     void test_setName()
00707     {
00708       // setName()を用いて、PortProfile.nameを書き換える
00709       m_pPortBase->setName("inport0-changed");
00710                         
00711       // setName()を用いて、PortProfile.nameを書き換える
00712       std::string str(m_pPortBase->getName());
00713       CPPUNIT_ASSERT_EQUAL(std::string("inport0-changed"), str);
00714                         
00715       // setName()により、意図どおりにPortProfile.nameが書き換えられているか?
00716       const RTC::PortProfile& portProfile = m_pPortBase->getPortProfile();
00717       CPPUNIT_ASSERT_EQUAL(std::string("inport0-changed"), std::string(portProfile.name));
00718     }
00719                 
00728     void test_getProfile()
00729     {
00730       const RTC::PortProfile& portProfile = m_pPortBase->getProfile();
00731 
00732       // (1) PortProfile.nameを正しく取得できるか?
00733       CPPUNIT_ASSERT_EQUAL(std::string("inport0"), std::string(portProfile.name));
00734 
00735       // (2) PortProfile.interfacesを正しく取得できるか?
00736       const RTC::PortInterfaceProfile& portIfProfile = portProfile.interfaces[0];
00737       // (2-a) PortInterfaceProfile.instance_nameを正しく取得できるか?
00738       CPPUNIT_ASSERT_EQUAL(
00739                            std::string("PortInterfaceProfile-instance_name"),
00740                            std::string(portIfProfile.instance_name));
00741 
00742       // (2-b) PortInterfaceProfile.type_nameを正しく取得できるか?
00743       CPPUNIT_ASSERT_EQUAL(
00744                            std::string("PortInterfaceProfile-type_name"),
00745                            std::string(portIfProfile.type_name));
00746                         
00747       // (2-c) PortInterfaceProfile.polarityを正しく取得できるか?
00748       CPPUNIT_ASSERT_EQUAL(RTC::REQUIRED, portIfProfile.polarity);
00749 
00750       // (3) PortProfile.connector_profilesを正しく取得できるか?
00751       const RTC::ConnectorProfile& connProfile = portProfile.connector_profiles[0];
00752       // (3-a) ConnectorProfile.nameを正しく取得できるか?
00753       CPPUNIT_ASSERT_EQUAL(
00754                            std::string("ConnectorProfile-name"),
00755                            std::string(connProfile.name));
00756                         
00757       // (3-b) ConnectorProfile.connector_idを正しく取得できるか?
00758       CPPUNIT_ASSERT_EQUAL(
00759                            std::string("connect_id0"),
00760                            std::string(connProfile.connector_id));
00761 
00762       // (3-c) ConnectorPofile.propertiesを正しく取得できるか?
00763       {
00764         const SDOPackage::NameValue& property = connProfile.properties[0];
00765         // (3-c-1) nameを正しく取得できるか?
00766         CPPUNIT_ASSERT_EQUAL(
00767                              std::string("ConnectorProfile-properties0-name"),
00768                              std::string(property.name));
00769                         
00770         // (3-c-2) valueを正しく取得できるか?
00771         {
00772           CORBA::Float value;
00773           property.value >>= value;
00774           CPPUNIT_ASSERT_EQUAL(CORBA::Float(1.1), value);
00775         }
00776       }
00777 
00778       // (4) PortProfile.propertiesを正しく取得できるか?
00779       {
00780         const SDOPackage::NameValue& property = portProfile.properties[0];
00781         // (4-a) nameを正しく取得できるか?
00782         CPPUNIT_ASSERT_EQUAL(
00783                              std::string("PortProfile-properties0-name"),
00784                              std::string(property.name));
00785                                 
00786         // (4-b) valueを正しく取得できるか?
00787         {
00788           CORBA::Float value;
00789           property.value >>= value;
00790           CPPUNIT_ASSERT_EQUAL(CORBA::Float(2.2), value);
00791         }
00792       }
00793     }
00794                 
00800     void test_setPortRef()
00801     {
00802       // 一旦、設定済みのPortBaseオブジェクト参照をリセットしておく
00803       m_pPortBase->_remove_ref();
00804 
00805       // setPortRef()を用いて、PortBaseオブジェクト参照を正しく設定できるか?
00806       // (getPortRef()を用いて、Portインタフェースのオブジェクト参照を取得し、
00807       // 取得した参照が、あらかじめ設定した参照と一致することを確認する)
00808       RTC::PortService_var port = m_pPortBase->_this();
00809       RTC::PortService_ptr portRef = port._retn();
00810       m_pPortBase->setPortRef(portRef);
00811 
00812       CPPUNIT_ASSERT_EQUAL(portRef, m_pPortBase->getPortRef());
00813     }
00814 
00818     void test_getPortRef()
00819     {
00820       // test_setPortRef()によりテストされている
00821     }
00822                 
00828     void test_getUUID()
00829     {
00830       // getUUID()メソッドはprotectedであるため、PortBaseMockにダウンキャストしてからアクセスする
00831       PortBaseMock* pPortBase = dynamic_cast<PortBaseMock*>(m_pPortBase);
00832       CPPUNIT_ASSERT(pPortBase != 0);
00833                         
00834       // UUIDを取得できるか?(空文字列でないかどうかのみでチェック)
00835       std::string uuid = pPortBase->getUUID();
00836       CPPUNIT_ASSERT(uuid.length() > 0);
00837       //std::cout << std::endl << "uuid: " << uuid << std::endl;
00838     }
00843     void test_setOwner()
00844     {
00845         PortableServer::POA_ptr poa = PortableServer::POA::_narrow(
00846                                 m_orb->resolve_initial_references("RootPOA"));
00847         RTC::RTObject_impl* obj = new RTC::RTObject_impl(m_orb,poa);
00848         RTC::RTObject_ptr owner = obj->getObjRef();
00849         RTC::PortProfile  portprofile = m_pPortBase->getProfile();
00850         CPPUNIT_ASSERT(CORBA::is_nil(portprofile.owner));
00851         m_pPortBase->setOwner(owner);
00852         portprofile = m_pPortBase->getProfile();
00853         CPPUNIT_ASSERT(!CORBA::is_nil(portprofile.owner));
00854 
00855         poa->the_POAManager()->deactivate(false, true);
00856         obj->finalize();
00857         delete obj;
00858     }
00859   };
00860 }; // namespace PortBase
00861 
00862 /*
00863  * Register test suite
00864  */
00865 CPPUNIT_TEST_SUITE_REGISTRATION(PortBase::PortBaseTests);
00866 
00867 #ifdef LOCAL_MAIN
00868 int main(int argc, char* argv[])
00869 {
00870 
00871   FORMAT format = TEXT_OUT;
00872   int target = 0;
00873   std::string xsl;
00874   std::string ns;
00875   std::string fname;
00876   std::ofstream ofs;
00877 
00878   int i(1);
00879   while (i < argc)
00880     {
00881       std::string arg(argv[i]);
00882       std::string next_arg;
00883       if (i + 1 < argc) next_arg = argv[i + 1];
00884       else              next_arg = "";
00885 
00886       if (arg == "--text") { format = TEXT_OUT; break; }
00887       if (arg == "--xml")
00888         {
00889           if (next_arg == "")
00890             {
00891               fname = argv[0];
00892               fname += ".xml";
00893             }
00894           else
00895             {
00896               fname = next_arg;
00897             }
00898           format = XML_OUT;
00899           ofs.open(fname.c_str());
00900         }
00901       if ( arg == "--compiler"  ) { format = COMPILER_OUT; break; }
00902       if ( arg == "--cerr"      ) { target = 1; break; }
00903       if ( arg == "--xsl"       )
00904         {
00905           if (next_arg == "") xsl = "default.xsl"; 
00906           else                xsl = next_arg;
00907         }
00908       if ( arg == "--namespace" )
00909         {
00910           if (next_arg == "")
00911             {
00912               std::cerr << "no namespace specified" << std::endl;
00913               exit(1); 
00914             }
00915           else
00916             {
00917               xsl = next_arg;
00918             }
00919         }
00920       ++i;
00921     }
00922   CppUnit::TextUi::TestRunner runner;
00923   if ( ns.empty() )
00924     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00925   else
00926     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
00927   CppUnit::Outputter* outputter = 0;
00928   std::ostream* stream = target ? &std::cerr : &std::cout;
00929   switch ( format )
00930     {
00931     case TEXT_OUT :
00932       outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
00933       break;
00934     case XML_OUT :
00935       std::cout << "XML_OUT" << std::endl;
00936       outputter = new CppUnit::XmlOutputter(&runner.result(),
00937                                             ofs, "shift_jis");
00938       static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
00939       break;
00940     case COMPILER_OUT :
00941       outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
00942       break;
00943     }
00944   runner.setOutputter(outputter);
00945   runner.run();
00946   return 0; // runner.run() ? 0 : 1;
00947 }
00948 #endif // MAIN
00949 #endif // PortBase_cpp


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