Go to the documentation of this file.00001
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef ExtTrigExecutionContext_cpp
00020 #define ExtTrigExecutionContext_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/idl/RTCSkel.h>
00029 #include <rtm/RTObject.h>
00030 #include <rtm/ExtTrigExecutionContext.h>
00031 #include <rtm/CORBA_SeqUtil.h>
00032
00037 namespace ExtTrigExecutionContext
00038 {
00039 class LightweightRTObjectMock
00040 : public virtual POA_RTC::LightweightRTObject,
00041 public virtual PortableServer::RefCountServantBase
00042 {
00043 protected:
00044 typedef std::map<RTC::UniqueId, RTC::ExecutionContext_ptr> ExecContexts;
00045 CORBA::Long m_nextUniqueId;
00046 ExecContexts m_execContexts;
00047 std::vector<std::string> m_log;
00048 bool m_alive;
00049 bool m_error;
00050
00051 public:
00052 LightweightRTObjectMock()
00053 : m_alive(true), m_error(false)
00054 {
00055 }
00056
00057
00058 virtual RTC::UniqueId attach_context(RTC::ExecutionContext_ptr exec_context)
00059 {
00060 m_log.push_back("attach_executioncontext");
00061 m_execContexts.insert(
00062 std::pair<RTC::UniqueId, RTC::ExecutionContext_ptr>(m_nextUniqueId++, exec_context));
00063 return m_nextUniqueId;
00064 }
00065 virtual RTC::ReturnCode_t detach_context(RTC::UniqueId ec_id)
00066 {
00067 m_log.push_back("detach_executioncontext");
00068 m_execContexts.erase(ec_id);
00069 return RTC::RTC_OK;
00070 }
00071 virtual RTC::ReturnCode_t on_initialize()
00072 {
00073 m_log.push_back("on_initialize");
00074 return RTC::RTC_OK;
00075 }
00076 virtual RTC::ReturnCode_t on_finalize()
00077 {
00078 m_log.push_back("on_finalize");
00079 return RTC::RTC_OK;
00080 }
00081 virtual RTC::ReturnCode_t on_startup(RTC::UniqueId ec_id)
00082 {
00083 m_log.push_back("on_startup");
00084 return RTC::RTC_OK;
00085 }
00086 virtual RTC::ReturnCode_t on_shutdown(RTC::UniqueId ec_id)
00087 {
00088 m_log.push_back("on_shutdown");
00089 return RTC::RTC_OK;
00090 }
00091 virtual RTC::ReturnCode_t on_activated(RTC::UniqueId ec_id)
00092 {
00093 m_log.push_back("on_activated");
00094 return returnCode(RTC::RTC_OK);
00095 }
00096 virtual RTC::ReturnCode_t on_deactivated(RTC::UniqueId ec_id)
00097 {
00098 m_log.push_back("on_deactivated");
00099 return RTC::RTC_OK;
00100 }
00101 virtual RTC::ReturnCode_t on_aborting(RTC::UniqueId ec_id)
00102 {
00103 m_log.push_back("on_aborting");
00104 return RTC::RTC_OK;
00105 }
00106 virtual RTC::ReturnCode_t on_error(RTC::UniqueId ec_id)
00107 {
00108 m_log.push_back("on_error");
00109 return RTC::RTC_OK;
00110 }
00111 virtual RTC::ReturnCode_t on_reset(RTC::UniqueId ec_id)
00112 {
00113 m_log.push_back("on_reset");
00114 return RTC::RTC_OK;
00115 }
00116
00117
00118 virtual RTC::ReturnCode_t initialize()
00119 {
00120 m_log.push_back("initialize");
00121 return RTC::RTC_OK;
00122 }
00123 virtual RTC::ReturnCode_t finalize()
00124 {
00125 m_log.push_back("finalize");
00126 return RTC::RTC_OK;
00127 }
00128 virtual RTC::ReturnCode_t exit()
00129 {
00130 m_log.push_back("exit");
00131 return RTC::RTC_OK;
00132 }
00133 virtual CORBA::Boolean is_alive(RTC::ExecutionContext_ptr exec_context)
00134 {
00135 m_log.push_back("is_alive");
00136 return CORBA::Boolean(m_alive);
00137 }
00138 virtual RTC::ExecutionContextList* get_owned_contexts()
00139 {
00140 m_log.push_back("get_contexts");
00141 return 0;
00142 }
00143 virtual RTC::ExecutionContextList* get_participating_contexts()
00144 {
00145 m_log.push_back("get_context");
00146 return 0;
00147 }
00148 virtual RTC::_objref_ExecutionContext* get_context(RTC::ExecutionContextHandle_t)
00149 {
00150 return 0;
00151 }
00152 virtual RTC::ExecutionContextHandle_t get_context_handle(RTC::_objref_ExecutionContext*)
00153 {
00154 return 0;
00155 }
00156
00157 public:
00158 int countLog(std::string line)
00159 {
00160 int count = 0;
00161 for (int i = 0; i < (int) m_log.size(); ++i)
00162 {
00163 if (m_log[i] == line) ++count;
00164 }
00165 return count;
00166 }
00167
00168 void setAlive(bool alive)
00169 {
00170 m_alive = alive;
00171 }
00172
00173 void setError(bool error)
00174 {
00175 m_error = error;
00176 }
00177
00178 private:
00179 RTC::ReturnCode_t returnCode(RTC::ReturnCode_t rc)
00180 {
00181 return m_error ? RTC::RTC_ERROR : rc;
00182 }
00183 };
00184
00185 class DataFlowComponentMock
00186
00187 : public virtual POA_OpenRTM::DataFlowComponent,
00188 public virtual LightweightRTObjectMock
00189 {
00190 public:
00191
00192 virtual SDOPackage::OrganizationList* get_owned_organizations()
00193 throw (SDOPackage::NotAvailable)
00194 {
00195 m_log.push_back("get_owned_organizations");
00196 return 0;
00197 }
00198 virtual char* get_sdo_id()
00199 throw (SDOPackage::NotAvailable, SDOPackage::InternalError)
00200 {
00201 m_log.push_back("get_sdo_id");
00202 return 0;
00203 }
00204 virtual char* get_sdo_type()
00205 throw (SDOPackage::NotAvailable, SDOPackage::InternalError)
00206 {
00207 m_log.push_back("get_sdo_type");
00208 return 0;
00209 }
00210 virtual SDOPackage::DeviceProfile* get_device_profile()
00211 throw (SDOPackage::NotAvailable, SDOPackage::InternalError)
00212 {
00213 m_log.push_back("get_device_profile");
00214 return 0;
00215 }
00216 virtual SDOPackage::ServiceProfileList* get_service_profiles()
00217 throw (SDOPackage::InvalidParameter, SDOPackage::NotAvailable, SDOPackage::InternalError)
00218 {
00219 m_log.push_back("get_service_profiles");
00220 return 0;
00221 }
00222 virtual SDOPackage::ServiceProfile* get_service_profile(const char* id)
00223 throw (SDOPackage::InvalidParameter, SDOPackage::NotAvailable, SDOPackage::InternalError)
00224 {
00225 m_log.push_back("get_service_profile");
00226 return 0;
00227 }
00228 virtual SDOPackage::SDOService_ptr get_sdo_service(const char* id)
00229 throw (SDOPackage::InvalidParameter, SDOPackage::NotAvailable, SDOPackage::InternalError)
00230 {
00231 m_log.push_back("get_sdo_service");
00232 return SDOPackage::SDOService::_nil();
00233 }
00234 virtual SDOPackage::Configuration_ptr get_configuration()
00235 throw (SDOPackage::InterfaceNotImplemented, SDOPackage::NotAvailable, SDOPackage::InternalError)
00236 {
00237 m_log.push_back("get_configuration");
00238 return SDOPackage::Configuration::_nil();
00239 }
00240 virtual SDOPackage::Monitoring_ptr get_monitoring()
00241 throw (SDOPackage::InterfaceNotImplemented, SDOPackage::NotAvailable, SDOPackage::InternalError)
00242 {
00243 m_log.push_back("get_monitoring");
00244 return SDOPackage::Monitoring::_nil();
00245 }
00246 virtual SDOPackage::OrganizationList* get_organizations()
00247 throw (SDOPackage::NotAvailable, SDOPackage::InternalError)
00248 {
00249 m_log.push_back("get_organizations");
00250 return 0;
00251 }
00252 virtual SDOPackage::NVList* get_status_list()
00253 throw (SDOPackage::NotAvailable, SDOPackage::InternalError)
00254 {
00255 m_log.push_back("get_status_list");
00256 return 0;
00257 }
00258 virtual CORBA::Any* get_status(const char* name)
00259 throw (SDOPackage::InvalidParameter, SDOPackage::NotAvailable, SDOPackage::InternalError)
00260 {
00261 m_log.push_back("get_status");
00262 return 0;
00263 }
00264
00265
00266 virtual RTC::ComponentProfile* get_component_profile()
00267 {
00268 m_log.push_back("get_component_profile");
00269
00270 RTC::ComponentProfile_var prof(new RTC::ComponentProfile());
00271 return prof._retn();
00272 }
00273 virtual RTC::PortServiceList* get_ports()
00274 {
00275 m_log.push_back("get_ports");
00276
00277 RTC::PortServiceList_var ports(new RTC::PortServiceList());
00278 ports->length(0);
00279 return ports._retn();
00280 }
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292 virtual RTC::ReturnCode_t on_execute(RTC::UniqueId ec_id)
00293 {
00294 m_log.push_back("on_execute");
00295 return RTC::RTC_OK;
00296 }
00297 virtual RTC::ReturnCode_t on_state_update(RTC::UniqueId ec_id)
00298 {
00299 m_log.push_back("on_state_update");
00300 return RTC::RTC_OK;
00301 }
00302 virtual RTC::ReturnCode_t on_rate_changed(RTC::UniqueId ec_id)
00303 {
00304 m_log.push_back("on_rate_changed");
00305 return RTC::RTC_OK;
00306 }
00307 };
00308
00309 class ExtTrigExecutionContextTests
00310 : public CppUnit::TestFixture
00311 {
00312 CPPUNIT_TEST_SUITE(ExtTrigExecutionContextTests);
00313 CPPUNIT_TEST(test_tick);
00314 CPPUNIT_TEST_SUITE_END();
00315
00316 private:
00317 CORBA::ORB_ptr m_pORB;
00318 PortableServer::POA_ptr m_pPOA;
00319
00320 public:
00324 ExtTrigExecutionContextTests()
00325 {
00326 int argc(0);
00327 char** argv(NULL);
00328 m_pORB = CORBA::ORB_init(argc, argv);
00329 m_pPOA = PortableServer::POA::_narrow(
00330 m_pORB->resolve_initial_references("RootPOA"));
00331 m_pPOA->the_POAManager()->activate();
00332 }
00333
00337 virtual ~ExtTrigExecutionContextTests()
00338 {
00339 }
00340
00344 virtual void setUp()
00345 {
00346 }
00347
00351 virtual void tearDown()
00352 {
00353 }
00354
00355 void test_tick()
00356 {
00357
00358 POA_RTC::LightweightRTObject* rto
00359 = new DataFlowComponentMock();
00360 DataFlowComponentMock* mock
00361 = dynamic_cast<DataFlowComponentMock*>(rto);
00362
00363
00364 RTC::ExtTrigExecutionContext* ec
00365 = new RTC::ExtTrigExecutionContext();
00366
00367 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->start());
00368 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->add_component(rto->_this()));
00369 CPPUNIT_ASSERT_EQUAL(RTC::RTC_OK, ec->activate_component(rto->_this()));
00370
00371
00372 ec->tick();
00373 for (int tickCalledCount = 0; tickCalledCount < 10; tickCalledCount++)
00374 {
00375 usleep(1000);
00376 CPPUNIT_ASSERT_EQUAL(tickCalledCount, mock->countLog("on_execute"));
00377 usleep(1000);
00378 ec->tick();
00379 }
00380
00381 ec->stop();
00382 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(ec));
00383 delete ec;
00384 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(rto));
00385 delete rto;
00386 }
00387
00388 };
00389 };
00390
00391
00392
00393
00394 CPPUNIT_TEST_SUITE_REGISTRATION(ExtTrigExecutionContext::ExtTrigExecutionContextTests);
00395
00396 #ifdef LOCAL_MAIN
00397 int main(int argc, char* argv[])
00398 {
00399
00400 FORMAT format = TEXT_OUT;
00401 int target = 0;
00402 std::string xsl;
00403 std::string ns;
00404 std::string fname;
00405 std::ofstream ofs;
00406
00407 int i(1);
00408 while (i < argc)
00409 {
00410 std::string arg(argv[i]);
00411 std::string next_arg;
00412 if (i + 1 < argc) next_arg = argv[i + 1];
00413 else next_arg = "";
00414
00415 if (arg == "--text") { format = TEXT_OUT; break; }
00416 if (arg == "--xml")
00417 {
00418 if (next_arg == "")
00419 {
00420 fname = argv[0];
00421 fname += ".xml";
00422 }
00423 else
00424 {
00425 fname = next_arg;
00426 }
00427 format = XML_OUT;
00428 ofs.open(fname.c_str());
00429 }
00430 if ( arg == "--compiler" ) { format = COMPILER_OUT; break; }
00431 if ( arg == "--cerr" ) { target = 1; break; }
00432 if ( arg == "--xsl" )
00433 {
00434 if (next_arg == "") xsl = "default.xsl";
00435 else xsl = next_arg;
00436 }
00437 if ( arg == "--namespace" )
00438 {
00439 if (next_arg == "")
00440 {
00441 std::cerr << "no namespace specified" << std::endl;
00442 exit(1);
00443 }
00444 else
00445 {
00446 xsl = next_arg;
00447 }
00448 }
00449 ++i;
00450 }
00451 CppUnit::TextUi::TestRunner runner;
00452 if ( ns.empty() )
00453 runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00454 else
00455 runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
00456 CppUnit::Outputter* outputter = 0;
00457 std::ostream* stream = target ? &std::cerr : &std::cout;
00458 switch ( format )
00459 {
00460 case TEXT_OUT :
00461 outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
00462 break;
00463 case XML_OUT :
00464 std::cout << "XML_OUT" << std::endl;
00465 outputter = new CppUnit::XmlOutputter(&runner.result(),
00466 ofs, "shift_jis");
00467 static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
00468 break;
00469 case COMPILER_OUT :
00470 outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
00471 break;
00472 }
00473 runner.setOutputter(outputter);
00474 runner.run();
00475 return 0;
00476 }
00477 #endif // MAIN
00478 #endif // ExtTrigExecutionContext_cpp