ExecutionContextProxyTests.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00012 /*
00013  * $Log$
00014  *
00015  */
00016 
00017 #ifndef ExecutionContextProxy_cpp
00018 #define ExecutionContextProxy_cpp
00019 
00020 #include <cppunit/ui/text/TestRunner.h>
00021 #include <cppunit/TextOutputter.h>
00022 #include <cppunit/extensions/TestFactoryRegistry.h>
00023 #include <cppunit/extensions/HelperMacros.h>
00024 #include <cppunit/TestAssert.h>
00025 #include <string>
00026 #include <iostream>
00027 #include <sstream>
00028 #include <idl/RTCSkel.h>
00029 #include <ExecutionContextProxy.h>
00030 #include <ILightweightRTObject.h>
00035 namespace ExecutionContextProxy
00036 {
00042   class Logger
00043   {
00044   public:
00045     void log(const std::string& msg)
00046     {
00047       m_log.push_back(msg);
00048     }
00049 
00050     int countLog(const std::string& msg)
00051     {
00052       int count = 0;
00053       for (int i = 0; i < (int) m_log.size(); ++i)
00054         {
00055           if (m_log[i] == msg) ++count;
00056         }
00057      return count;
00058     }
00059                 
00060   private:
00061     std::vector<std::string> m_log;
00062   };
00063 
00069   class LightweightRTObjectServantMock
00070    : public virtual ::POA_RTC::LightweightRTObject,
00071      public virtual ::doil::CORBA::CORBAServantBase
00072   {
00073   public:
00074     LightweightRTObjectServantMock(doil::ImplBase* impl)
00075      : ::doil::CORBA::CORBAServantBase(impl) 
00076     {
00077     }
00078     virtual ~LightweightRTObjectServantMock()
00079     {
00080     }
00081     virtual ::RTC::ReturnCode_t initialize()
00082     {
00083     }
00084     virtual ::RTC::ReturnCode_t finalize()
00085     {
00086     }
00087     virtual ::CORBA::Boolean is_alive(::RTC::ExecutionContext_ptr exec_context)
00088     {
00089     }
00090     virtual ::RTC::ReturnCode_t exit()
00091     {
00092     }
00093     virtual ::RTC::ExecutionContextHandle_t attach_context(::RTC::ExecutionContext_ptr exec_context)
00094     {
00095     }
00096     virtual ::RTC::ReturnCode_t detach_context(::RTC::ExecutionContextHandle_t exec_handle)
00097     {
00098     }
00099     virtual ::RTC::ExecutionContext_ptr get_context(::RTC::ExecutionContextHandle_t exec_handle)
00100     {
00101     }
00102     virtual ::RTC::ExecutionContextList* get_owned_contexts()
00103     {
00104     }
00105     virtual ::RTC::ExecutionContextList* get_participating_contexts()
00106     {
00107     }
00108     virtual ::RTC::ExecutionContextHandle_t get_context_handle(::RTC::ExecutionContext_ptr cxt)
00109     {
00110     }
00111     virtual ::RTC::ReturnCode_t on_initialize()
00112     {
00113     }
00114     virtual ::RTC::ReturnCode_t on_finalize()
00115     {
00116     }
00117     virtual ::RTC::ReturnCode_t on_startup(::RTC::ExecutionContextHandle_t exec_handle)
00118     {
00119     }
00120     virtual ::RTC::ReturnCode_t on_shutdown(::RTC::ExecutionContextHandle_t exec_handle)
00121     {
00122     }
00123     virtual ::RTC::ReturnCode_t on_activated(::RTC::ExecutionContextHandle_t exec_handle)
00124     {
00125     }
00126     virtual ::RTC::ReturnCode_t on_deactivated(::RTC::ExecutionContextHandle_t exec_handle)
00127     {
00128     }
00129     virtual ::RTC::ReturnCode_t on_aborting(::RTC::ExecutionContextHandle_t exec_handle)
00130     {
00131     }
00132     virtual ::RTC::ReturnCode_t on_error(::RTC::ExecutionContextHandle_t exec_handle)
00133     {
00134     }
00135     virtual ::RTC::ReturnCode_t on_reset(::RTC::ExecutionContextHandle_t exec_handle)
00136     {
00137     }
00138 
00139 
00140 
00141 
00142   private:
00143     ::SDOPackage::Local::ISDOSystemElement* m_impl;
00144   };
00150   class ILightweightRTObjectMock
00151     : public virtual ::RTC::Local::ILightweightRTObject
00152   {
00153   public:
00154     ILightweightRTObjectMock()
00155       : m_refcount(0)
00156     {}
00157     virtual ~ILightweightRTObjectMock(){}
00158     const char* id() {return "ILightweightRTObjectMock";}
00159     const char* name() {return "ILightweightRTObjectMock";}
00160     void incRef()
00161     {
00162       ++m_refcount;
00163     }
00164     void decRef()
00165     {
00166       --m_refcount;
00167       if (m_refcount == 0)
00168         delete this;
00169     }
00170     virtual ::RTC::Local::ReturnCode_t initialize()
00171       throw ()
00172     {
00173     }
00174     virtual ::RTC::Local::ReturnCode_t finalize()
00175       throw ()
00176     {
00177     }
00178     virtual bool is_alive(const ::RTC::Local::IExecutionContext* exec_context)
00179       throw ()
00180     {
00181     }
00182 
00183     virtual ::RTC::Local::ReturnCode_t exit()
00184       throw ()
00185     {
00186     }
00187 
00188     virtual ::RTC::Local::ExecutionContextHandle_t attach_context(const ::RTC::Local::IExecutionContext* exec_context)
00189       throw ()
00190     {
00191     }
00192 
00193     virtual ::RTC::Local::ReturnCode_t detach_context(::RTC::Local::ExecutionContextHandle_t exec_handle)
00194       throw ()
00195     {
00196     }
00197 
00198     virtual ::RTC::Local::IExecutionContext* get_context(::RTC::Local::ExecutionContextHandle_t exec_handle)
00199       throw ()
00200     {
00201     }
00202 
00203     virtual ::RTC::Local::ExecutionContextList get_owned_contexts()
00204       throw ()
00205     {
00206     }
00207 
00208     virtual ::RTC::Local::ExecutionContextList get_participating_contexts()
00209       throw ()
00210     {
00211     }
00212 
00213     virtual ::RTC::Local::ExecutionContextHandle_t get_context_handle(const ::RTC::Local::IExecutionContext* cxt)
00214       throw ()
00215     {
00216     }
00217     virtual ::RTC::Local::ReturnCode_t on_initialize()
00218       throw ()
00219     {
00220     }
00221 
00222     virtual ::RTC::Local::ReturnCode_t on_finalize()
00223       throw ()
00224     {
00225     }
00226 
00227     virtual ::RTC::Local::ReturnCode_t on_startup(::RTC::Local::ExecutionContextHandle_t exec_handle)
00228       throw ()
00229     {
00230     }
00231 
00232     virtual ::RTC::Local::ReturnCode_t on_shutdown(::RTC::Local::ExecutionContextHandle_t exec_handle)
00233       throw ()
00234     {
00235     }
00236 
00237     virtual ::RTC::Local::ReturnCode_t on_activated(::RTC::Local::ExecutionContextHandle_t exec_handle)
00238       throw ()
00239     {
00240     }
00241 
00242     virtual ::RTC::Local::ReturnCode_t on_deactivated(::RTC::Local::ExecutionContextHandle_t exec_handle)
00243       throw ()
00244     {
00245     }
00246 
00247     virtual ::RTC::Local::ReturnCode_t on_aborting(::RTC::Local::ExecutionContextHandle_t exec_handle)
00248       throw ()
00249     {
00250     }
00251 
00252     virtual ::RTC::Local::ReturnCode_t on_error(::RTC::Local::ExecutionContextHandle_t exec_handle)
00253       throw ()
00254     {
00255     }
00256 
00257     virtual ::RTC::Local::ReturnCode_t on_reset(::RTC::Local::ExecutionContextHandle_t exec_handle)
00258       throw ()
00259     {
00260     }
00261   private:
00262     std::string m_name;
00263     int m_refcount;
00264   };
00270   class ExecutionContextRtmMock
00271     : public virtual ::POA_RTC::ExecutionContext  
00272   {
00273   protected:
00274       std::vector<std::string> m_log;
00275   public :
00276       ExecutionContextRtmMock(){}
00277       virtual ~ExecutionContextRtmMock(){}
00278 
00282       void setLogger(Logger* logger)
00283       {
00284         m_logger = logger;
00285       }
00289   ::CORBA::Boolean is_running()
00290   {
00291     return false;
00292   }
00293   
00294 
00298   ::RTC::ReturnCode_t start()
00299   {
00300     return RTC::RTC_OK;
00301   }
00302   
00303 
00307   ::RTC::ReturnCode_t stop()
00308   {
00309     return RTC::RTC_OK;
00310   }
00311   
00312   
00316     ::CORBA::Double get_rate()
00317     {
00318         if (m_logger != NULL)
00319         {
00320             m_logger->log("get_rate");
00321         }
00322         return (CORBA::Double)3.14159;
00323     }
00324   
00325   
00329     ::RTC::ReturnCode_t set_rate(::CORBA::Double rate)
00330     {
00331         if (m_logger != NULL)
00332         {
00333             m_logger->log("set_rate");
00334             ::std::ostringstream os;
00335             os<<rate; 
00336             m_logger->log(os.str());
00337         }
00338 
00339         return RTC::BAD_PARAMETER;
00340     }
00341   
00342 
00346   ::RTC::ReturnCode_t activate_component(::RTC::LightweightRTObject_ptr comp)
00347   {
00348     return RTC::RTC_OK;
00349   }
00350   
00351 
00355   ::RTC::ReturnCode_t deactivate_component(::RTC::LightweightRTObject_ptr comp)
00356   {
00357     return RTC::RTC_OK;
00358   }
00359   
00360 
00364   ::RTC::ReturnCode_t reset_component(::RTC::LightweightRTObject_ptr comp)
00365   {
00366     return RTC::RTC_OK;
00367   }
00368   
00369   
00373   ::RTC::LifeCycleState get_component_state(::RTC::LightweightRTObject_ptr comp)
00374   {
00375     return RTC::INACTIVE_STATE;
00376   }
00377   
00378   
00382   ::RTC::ExecutionKind get_kind()
00383   {
00384 //    return m_profile.kind;
00385   }
00389     ::RTC::ReturnCode_t add_component(::RTC::LightweightRTObject_ptr comp)
00390     {
00391         if (m_logger != NULL)
00392         {
00393             m_logger->log("add_component");
00394 //            ::std::ostringstream os;
00395 //            os<<rate; 
00396 //            m_logger->log(os.str());
00397         }
00398         return RTC::BAD_PARAMETER;
00399     }
00400   
00401   
00405   ::RTC::ReturnCode_t remove_component(::RTC::LightweightRTObject_ptr comp)
00406   {
00407     CORBA::ULong index;
00408 //    index = CORBA_SeqUtil::find(m_profile.participants,
00409 //              find_objref<RTObject_ptr>(RTC::RTObject::_narrow(comp)));
00410                                 
00411     if (index < 0) return RTC::BAD_PARAMETER;
00412 //    CORBA_SeqUtil::erase(m_profile.participants, index);
00413     return RTC::RTC_OK;
00414   }
00415   
00416   private:
00417     Logger* m_logger;
00418 
00419   };
00420 
00426   class ExecutionContextProxyTests
00427    : public CppUnit::TestFixture
00428   {
00429     CPPUNIT_TEST_SUITE(ExecutionContextProxyTests);
00430 //    CPPUNIT_TEST(test_case0);
00431     CPPUNIT_TEST(test_get_rate);
00432     CPPUNIT_TEST(test_set_rate);
00433     CPPUNIT_TEST(test_add_component);
00434     CPPUNIT_TEST_SUITE_END();
00435   
00436   private:
00437     CORBA::ORB_ptr m_pORB;
00438     PortableServer::POA_ptr m_pPOA;
00439   
00440   public:
00441   
00445     ExecutionContextProxyTests()
00446     {
00447       int argc = 0;
00448       char** argv = NULL;
00449       m_pORB = CORBA::ORB_init(argc, argv);
00450       m_pPOA = PortableServer::POA::_narrow(
00451                        m_pORB->resolve_initial_references("RootPOA"));
00452       m_pPOA->the_POAManager()->activate();
00453     }
00454     
00458     ~ExecutionContextProxyTests()
00459     {
00460     }
00461   
00465     virtual void setUp()
00466     {
00467     }
00468     
00472     virtual void tearDown()
00473     { 
00474     }
00475   
00481     void test_get_rate()
00482     {
00483 
00484       ExecutionContextRtmMock* obj = new ExecutionContextRtmMock();
00485       ::CORBA::Object_ptr ref = obj->_this();
00486       if(::CORBA::is_nil(ref))
00487       {
00488          std::cout<<"ref is nil.Abort test."<<std::endl;
00489          return;
00490       }
00491       ::RTC::CORBA::ExecutionContextProxy* ap 
00492                  = new ::RTC::CORBA::ExecutionContextProxy(ref);
00493 
00494       Logger logger;
00495       obj->setLogger(&logger);
00496 
00497       double ret;
00498       CPPUNIT_ASSERT_EQUAL(0, logger.countLog("get_rate"));
00499       ret = ap->get_rate(); 
00500       CPPUNIT_ASSERT_EQUAL(1, logger.countLog("get_rate"));
00501       CPPUNIT_ASSERT_EQUAL(3.14159, ret);
00502 
00503       delete ap;
00504       CORBA::release(ref);
00505 
00506     }
00507   
00513     void test_set_rate()
00514     {
00515 
00516       ExecutionContextRtmMock* obj = new ExecutionContextRtmMock();
00517       ::CORBA::Object_ptr ref = obj->_this();
00518       if(::CORBA::is_nil(ref))
00519       {
00520          std::cout<<"ref is nil.Abort test."<<std::endl;
00521          return;
00522       }
00523       ::RTC::CORBA::ExecutionContextProxy* ap 
00524                  = new ::RTC::CORBA::ExecutionContextProxy(ref);
00525 
00526       Logger logger;
00527       obj->setLogger(&logger);
00528 
00529       double dbuf = 2.71828;
00530       ::RTC::Local::ReturnCode_t ret;
00531       CPPUNIT_ASSERT_EQUAL(0, logger.countLog("2.71828"));
00532       CPPUNIT_ASSERT_EQUAL(0, logger.countLog("set_rate"));
00533       ret = ap->set_rate(dbuf); 
00534       CPPUNIT_ASSERT_EQUAL(1, logger.countLog("set_rate"));
00535       CPPUNIT_ASSERT_EQUAL(::RTC::Local::BAD_PARAMETER, ret);
00536       CPPUNIT_ASSERT_EQUAL(1, logger.countLog("2.71828"));
00537 
00538       delete ap;
00539       CORBA::release(ref);
00540 
00541     }
00547     void test_add_component()
00548     {
00549       doil::CORBA::CORBAManager& 
00550                             mgr(doil::CORBA::CORBAManager::instance());
00551       std::cout <<"Manager Name==>"<< mgr.name() << std::endl;
00552 
00553 
00554       ExecutionContextRtmMock* obj = new ExecutionContextRtmMock();
00555       ::CORBA::Object_ptr ref = obj->_this();
00556       if(::CORBA::is_nil(ref))
00557       {
00558          std::cout<<"ref is nil.Abort test."<<std::endl;
00559          return;
00560       }
00561       ::RTC::CORBA::ExecutionContextProxy* ap 
00562                  = new ::RTC::CORBA::ExecutionContextProxy(ref);
00563 
00564       Logger logger;
00565       obj->setLogger(&logger);
00566 
00567       ::RTC::Local::ReturnCode_t ret;
00568       ILightweightRTObjectMock* comp = new ILightweightRTObjectMock();
00569       mgr.registerFactory(comp->id(), 
00570                           doil::New<LightweightRTObjectServantMock>,
00571                           doil::Delete<LightweightRTObjectServantMock>);
00572       mgr.activateObject(comp);
00573       CPPUNIT_ASSERT_EQUAL(0, logger.countLog("add_component"));
00574       ret = ap->add_component(comp); 
00575       CPPUNIT_ASSERT_EQUAL(1, logger.countLog("add_component"));
00576       CPPUNIT_ASSERT_EQUAL(::RTC::Local::BAD_PARAMETER, ret);
00577 
00578       delete ap;
00579       CORBA::release(ref);
00580 
00581     }
00582     /* test case */
00583     void test_case0()
00584     {
00585     }
00586   };
00587 }; // namespace ExecutionContextProxy
00588 
00589 /*
00590  * Register test suite
00591  */
00592 CPPUNIT_TEST_SUITE_REGISTRATION(ExecutionContextProxy::ExecutionContextProxyTests);
00593 
00594 #ifdef LOCAL_MAIN
00595 int main(int argc, char* argv[])
00596 {
00597     CppUnit::TextUi::TestRunner runner;
00598     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00599     CppUnit::Outputter* outputter = 
00600       new CppUnit::TextOutputter(&runner.result(), std::cout);
00601     runner.setOutputter(outputter);
00602     bool retcode = runner.run();
00603     return !retcode;
00604 }
00605 #endif // MAIN
00606 #endif // ExecutionContextProxy_cpp


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