OutPortCorbaCdrProviderTests.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00012 /*
00013  * $Log: OutPortCorbaCdrProviderTests.cpp,v $
00014  *
00015  *
00016  */
00017 
00018 #ifndef OutPortCorbaCdrProvider_cpp
00019 #define OutPortCorbaCdrProvider_cpp
00020 
00021 #include <cppunit/ui/text/TestRunner.h>
00022 #include <cppunit/TextOutputter.h>
00023 #include <cppunit/extensions/TestFactoryRegistry.h>
00024 #include <cppunit/extensions/HelperMacros.h>
00025 #include <cppunit/TestAssert.h>
00026 
00027 #include <rtm/idl/BasicDataTypeSkel.h>
00028 #include <rtm/idl/DataPortSkel.h>
00029 #include <rtm/RTC.h>
00030 #include <rtm/Typename.h>
00031 #include <rtm/OutPortCorbaCdrProvider.h>
00032 #include <rtm/OutPortCorbaCdrConsumer.h>
00033 #include <rtm/CdrBufferBase.h>
00034 #include <rtm/PortAdmin.h>
00035 #include <rtm/CORBA_SeqUtil.h>
00036 #include <rtm/NVUtil.h>
00037 #include <rtm/DataPortStatus.h>
00038 #include <rtm/ConnectorListener.h>
00039 #include <rtm/OutPortPullConnector.h>
00040 
00045 namespace OutPortCorbaCdrProvider
00046 {
00047 
00048   class DataListener
00049     : public RTC::ConnectorDataListenerT<RTC::TimedLong>
00050   {
00051   public:
00052     DataListener(const char* name) : m_name(name) {}
00053     virtual ~DataListener()
00054     {
00055       //std::cout << "dtor of " << m_name << std::endl;
00056     }
00057 
00058     virtual void operator()(const RTC::ConnectorInfo& info,
00059                             const RTC::TimedLong& data)
00060     {
00061       std::cout << "------------------------------"   << std::endl;
00062       std::cout << "Data Listener: " << m_name       << std::endl;
00063       std::cout << "Profile::name: " << info.name    << std::endl;
00064       std::cout << "------------------------------"   << std::endl;
00065     };
00066     std::string m_name;
00067   };
00068 
00069 
00070   class ConnListener
00071     : public RTC::ConnectorListener
00072   {
00073   public:
00074     ConnListener(const char* name) : m_name(name) {}
00075     virtual ~ConnListener()
00076     {
00077       //std::cout << "dtor of " << m_name << std::endl;
00078     }
00079 
00080     virtual void operator()(const RTC::ConnectorInfo& info)
00081     {
00082       std::cout << "------------------------------"   << std::endl;
00083       std::cout << "Connector Listener: " << m_name       << std::endl;
00084       std::cout << "Profile::name:      " << info.name    << std::endl;
00085       std::cout << "------------------------------"   << std::endl;
00086     };
00087     std::string m_name;
00088   };
00089 
00094   class OutPortCorbaCdrProviderMock
00095     : public RTC::OutPortCorbaCdrProvider
00096   {
00097   public:
00102       OutPortCorbaCdrProviderMock(void)
00103       {
00104       }
00109       virtual ~OutPortCorbaCdrProviderMock()
00110       {
00111       }
00116       SDOPackage::NVList get_m_properties()
00117       {
00118           return m_properties;
00119       }
00120   };
00121   class OutPortCorbaCdrProviderTests
00122     : public CppUnit::TestFixture
00123   {
00124     CPPUNIT_TEST_SUITE(OutPortCorbaCdrProviderTests);
00125 
00126     CPPUNIT_TEST(test_case0);
00127     CPPUNIT_TEST_SUITE_END();
00128         
00129   private:
00130     CORBA::ORB_ptr m_pORB;
00131     PortableServer::POA_ptr m_pPOA;
00132 
00133   public:
00134     RTC::ConnectorListeners m_listeners;
00135         
00139     OutPortCorbaCdrProviderTests()
00140     {
00141       int argc(0);
00142       char** argv(NULL);
00143       m_pORB = CORBA::ORB_init(argc, argv);
00144       m_pPOA = PortableServer::POA::_narrow(
00145                     m_pORB->resolve_initial_references("RootPOA"));
00146       m_pPOA->the_POAManager()->activate();
00147     }
00148                 
00152     ~OutPortCorbaCdrProviderTests()
00153     {
00154     }
00155                 
00159     virtual void setUp()
00160     {
00161     }
00162                 
00166     virtual void tearDown()
00167     { 
00168     }
00169                 
00174     void test_case0()
00175     {
00176         //
00177         //
00178         //
00179         OutPortCorbaCdrProviderMock* provider 
00180             = new OutPortCorbaCdrProviderMock();
00181 
00182         //ConnectorInfo
00183         coil::Properties prop;
00184         coil::vstring ports;
00185         RTC::ConnectorInfo info("name", "id", ports, prop);
00186 
00187         //ConnectorDataListeners
00188         m_listeners.connectorData_[RTC::ON_BUFFER_WRITE].addListener(
00189                                    new DataListener("ON_BUFFER_WRITE"), true);
00190         m_listeners.connectorData_[RTC::ON_BUFFER_FULL].addListener(
00191                                    new DataListener("ON_BUFFER_FULL"), true);
00192         m_listeners.connectorData_[RTC::ON_BUFFER_WRITE_TIMEOUT].addListener(
00193                                    new DataListener("ON_BUFFER_WRITE_TIMEOUT"), true);
00194         m_listeners.connectorData_[RTC::ON_BUFFER_OVERWRITE].addListener(
00195                                    new DataListener("ON_BUFFER_OVERWRITE"), true);
00196         m_listeners.connectorData_[RTC::ON_BUFFER_READ].addListener(
00197                                    new DataListener("ON_BUFFER_READ"), true);
00198         m_listeners.connectorData_[RTC::ON_SEND].addListener(
00199                                    new DataListener("ON_SEND"), true);
00200         m_listeners.connectorData_[RTC::ON_RECEIVED].addListener(
00201                                    new DataListener("ON_RECEIVED"), true);
00202         m_listeners.connectorData_[RTC::ON_RECEIVER_FULL].addListener(
00203                                    new DataListener("ON_RECEIVER_FULL"), true);
00204         m_listeners.connectorData_[RTC::ON_RECEIVER_TIMEOUT].addListener(
00205                                    new DataListener("ON_RECEIVER_TIMEOUT"), true);
00206         m_listeners.connectorData_[RTC::ON_RECEIVER_ERROR].addListener(
00207                                    new DataListener("ON_RECEIVER_ERROR"), true);
00208 
00209         //ConnectorListeners
00210         m_listeners.connector_[RTC::ON_BUFFER_EMPTY].addListener(
00211                                     new ConnListener("ON_BUFFER_EMPTY"), true);
00212         m_listeners.connector_[RTC::ON_BUFFER_READ_TIMEOUT].addListener(
00213                                     new ConnListener("ON_BUFFER_READ_TIMEOUT"), true);
00214         m_listeners.connector_[RTC::ON_SENDER_EMPTY].addListener(
00215                                     new ConnListener("ON_SENDER_EMPTY"), true);
00216         m_listeners.connector_[RTC::ON_SENDER_TIMEOUT].addListener(
00217                                     new ConnListener("ON_SENDER_TIMEOUT"), true);
00218         m_listeners.connector_[RTC::ON_SENDER_ERROR].addListener(
00219                                     new ConnListener("ON_SENDER_ERROR"), true);
00220         m_listeners.connector_[RTC::ON_CONNECT].addListener(
00221                                     new ConnListener("ON_CONNECT"), true);
00222         m_listeners.connector_[RTC::ON_DISCONNECT].addListener(
00223                                     new ConnListener("ON_DISCONNECT"), true);
00224 
00225         provider->setListener(info, &m_listeners);
00226 
00227         int index;
00228         //IOR をプロぺティに追加することを確認
00229         index = NVUtil::find_index(provider->get_m_properties(),
00230                                    "dataport.corba_cdr.outport_ior");
00231         CPPUNIT_ASSERT(0<=index);
00232 
00233         //ref をプロぺティに追加することを確認
00234         index = NVUtil::find_index(provider->get_m_properties(),
00235                                    "dataport.corba_cdr.outport_ref");
00236         CPPUNIT_ASSERT(0<=index);
00237 
00238         provider->init(prop);
00239 
00240         RTC::CdrBufferBase* buffer;
00241         buffer = RTC::CdrBufferFactory::instance().createObject("ring_buffer");
00242 
00243         ::OpenRTM::PortStatus retcode;
00244 
00245         ::OpenRTM::CdrData_var cdr_data;
00246 
00247         // buffer がない状態でコール(unkown error)
00248         retcode = provider->get(cdr_data.out());
00249         CPPUNIT_ASSERT_EQUAL((::OpenRTM::PortStatus)5, retcode);
00250         
00251         provider->setBuffer(buffer);
00252 
00253         RTC::OutPortConnector* connector;
00254         connector = new RTC::OutPortPullConnector(info, 
00255                                                   provider, 
00256                                                   m_listeners, 
00257                                                   buffer);
00258         if (connector == 0)
00259           {
00260             std::cout << "ERROR: Connector creation failed." << std::endl;
00261           }
00262         provider->setConnector(connector);
00263 
00264         //データなしの状態でコール(empty)
00265         retcode = provider->get(cdr_data);
00266         CPPUNIT_ASSERT_EQUAL((::OpenRTM::PortStatus)3, retcode);
00267 
00268         int testdata[8] = { 12,34,56,78,90,23,45,99 };
00269         cdrMemoryStream cdr;
00270         RTC::TimedLong td;
00271         for(int ic(0);ic<8;++ic)
00272         {
00273           td.data = testdata[ic];
00274           td >>= cdr;
00275           buffer->write(cdr);
00276 
00277         }
00278 
00279         retcode = provider->get(cdr_data);
00280         CPPUNIT_ASSERT_EQUAL((::OpenRTM::PortStatus)0, retcode);
00281 
00282 
00283         SDOPackage::NVList list = provider->get_m_properties();
00284         index = NVUtil::find_index(list,
00285                                    "dataport.corba_cdr.outport_ior");
00286 
00287         const char* ior;
00288         if (list[index].value >>= ior)
00289         {
00290             CORBA::Object_ptr var = m_pORB->string_to_object(ior);
00291             PortableServer::Servant ser = m_pPOA->reference_to_servant(var);
00292             m_pPOA->deactivate_object(*m_pPOA->servant_to_id(ser));
00293         }
00294 
00295 
00296         delete connector;
00297     }
00298   };
00299 }; // namespace OutPortCorbaCdrProvider
00300 
00301 /*
00302  * Register test suite
00303  */
00304 CPPUNIT_TEST_SUITE_REGISTRATION(OutPortCorbaCdrProvider::OutPortCorbaCdrProviderTests);
00305 
00306 #ifdef LOCAL_MAIN
00307 int main(int argc, char* argv[])
00308 {
00309 
00310   FORMAT format = TEXT_OUT;
00311   int target = 0;
00312   std::string xsl;
00313   std::string ns;
00314   std::string fname;
00315   std::ofstream ofs;
00316 
00317   int i(1);
00318   while (i < argc)
00319     {
00320       std::string arg(argv[i]);
00321       std::string next_arg;
00322       if (i + 1 < argc) next_arg = argv[i + 1];
00323       else              next_arg = "";
00324 
00325       if (arg == "--text") { format = TEXT_OUT; break; }
00326       if (arg == "--xml")
00327         {
00328           if (next_arg == "")
00329             {
00330               fname = argv[0];
00331               fname += ".xml";
00332             }
00333           else
00334             {
00335               fname = next_arg;
00336             }
00337           format = XML_OUT;
00338           ofs.open(fname.c_str());
00339         }
00340       if ( arg == "--compiler"  ) { format = COMPILER_OUT; break; }
00341       if ( arg == "--cerr"      ) { target = 1; break; }
00342       if ( arg == "--xsl"       )
00343         {
00344           if (next_arg == "") xsl = "default.xsl"; 
00345           else                xsl = next_arg;
00346         }
00347       if ( arg == "--namespace" )
00348         {
00349           if (next_arg == "")
00350             {
00351               std::cerr << "no namespace specified" << std::endl;
00352               exit(1); 
00353             }
00354           else
00355             {
00356               xsl = next_arg;
00357             }
00358         }
00359       ++i;
00360     }
00361   CppUnit::TextUi::TestRunner runner;
00362   if ( ns.empty() )
00363     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00364   else
00365     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
00366   CppUnit::Outputter* outputter = 0;
00367   std::ostream* stream = target ? &std::cerr : &std::cout;
00368   switch ( format )
00369     {
00370     case TEXT_OUT :
00371       outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
00372       break;
00373     case XML_OUT :
00374       std::cout << "XML_OUT" << std::endl;
00375       outputter = new CppUnit::XmlOutputter(&runner.result(),
00376                                             ofs, "shift_jis");
00377       static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
00378       break;
00379     case COMPILER_OUT :
00380       outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
00381       break;
00382     }
00383   runner.setOutputter(outputter);
00384   runner.run();
00385   return 0; // runner.run() ? 0 : 1;
00386 }
00387 #endif // MAIN
00388 #endif // OutPortCorbaCdrProvider_cpp


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Sat Jun 8 2019 18:49:05