InPortCorbaCdrProviderTests.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00012 /*
00013  * $Log: InPortCorbaCdrProviderTests.cpp,v $
00014  *
00015  *
00016  */
00017 
00018 #ifndef InPortCorbaCdrProvider_cpp
00019 #define InPortCorbaCdrProvider_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/RTC.h>
00029 #include <rtm/Typename.h>
00030 #include <rtm/InPortCorbaCdrProvider.h>
00031 #include <rtm/CdrBufferBase.h>
00032 #include <rtm/PortAdmin.h>
00033 #include <rtm/CORBA_SeqUtil.h>
00034 #include <rtm/ConnectorListener.h>
00035 #include <rtm/InPortPushConnector.h>
00036 
00041 namespace InPortCorbaCdrProvider
00042 {
00043 
00044   class DataListener
00045     : public RTC::ConnectorDataListenerT<RTC::TimedLong>
00046   {
00047   public:
00048     DataListener(const char* name) : m_name(name) {}
00049     virtual ~DataListener()
00050     {
00051       //std::cout << "dtor of " << m_name << std::endl;
00052     }
00053 
00054     virtual void operator()(const RTC::ConnectorInfo& info,
00055                             const RTC::TimedLong& data)
00056     {
00057       std::cout << "------------------------------"   << std::endl;
00058       std::cout << "Data Listener: " << m_name       << std::endl;
00059       std::cout << "Profile::name: " << info.name    << std::endl;
00060       std::cout << "------------------------------"   << std::endl;
00061     };
00062     std::string m_name;
00063   };
00064 
00065 
00066   class ConnListener
00067     : public RTC::ConnectorListener
00068   {
00069   public:
00070     ConnListener(const char* name) : m_name(name) {}
00071     virtual ~ConnListener()
00072     {
00073       //std::cout << "dtor of " << m_name << std::endl;
00074     }
00075 
00076     virtual void operator()(const RTC::ConnectorInfo& info)
00077     {
00078       std::cout << "------------------------------"   << std::endl;
00079       std::cout << "Connector Listener: " << m_name       << std::endl;
00080       std::cout << "Profile::name:      " << info.name    << std::endl;
00081       std::cout << "------------------------------"   << std::endl;
00082     };
00083     std::string m_name;
00084   };
00085 
00090   class InPortCorbaCdrProviderMock
00091     : public RTC::InPortCorbaCdrProvider
00092   {
00093   public:
00098       InPortCorbaCdrProviderMock(void)
00099       {
00100       }
00105       virtual ~InPortCorbaCdrProviderMock()
00106       {
00107       }
00112       SDOPackage::NVList get_m_properties()
00113       {
00114           return m_properties;
00115       }
00116   };
00117   class InPortCorbaCdrProviderTests
00118     : public CppUnit::TestFixture
00119   {
00120     CPPUNIT_TEST_SUITE(InPortCorbaCdrProviderTests);
00121 
00122     CPPUNIT_TEST(test_case0);
00123     CPPUNIT_TEST_SUITE_END();
00124         
00125   private:
00126     CORBA::ORB_ptr m_pORB;
00127     PortableServer::POA_ptr m_pPOA;
00128     RTC::ConnectorListeners m_listeners;
00129     RTC::InPortConnector* connector;
00130 
00131   public:
00132         
00136     InPortCorbaCdrProviderTests()
00137     {
00138       int argc(0);
00139       char** argv(NULL);
00140       m_pORB = CORBA::ORB_init(argc, argv);
00141       m_pPOA = PortableServer::POA::_narrow(
00142                     m_pORB->resolve_initial_references("RootPOA"));
00143       m_pPOA->the_POAManager()->activate();
00144     }
00145                 
00149     ~InPortCorbaCdrProviderTests()
00150     {
00151       delete connector;
00152     }
00153                 
00157     virtual void setUp()
00158     {
00159     }
00160                 
00164     virtual void tearDown()
00165     { 
00166     }
00167                 
00172     void test_case0()
00173     {
00174         InPortCorbaCdrProviderMock provider;
00175         CORBA::Long index;
00176 
00177         //ConnectorInfo
00178         coil::Properties prop;
00179         coil::vstring ports;
00180         RTC::ConnectorInfo info("name", "id", ports, prop);
00181 
00182         //ConnectorDataListeners
00183         m_listeners.connectorData_[RTC::ON_BUFFER_WRITE].addListener(
00184                                    new DataListener("ON_BUFFER_WRITE"), true);
00185         m_listeners.connectorData_[RTC::ON_BUFFER_FULL].addListener(
00186                                    new DataListener("ON_BUFFER_FULL"), true);
00187         m_listeners.connectorData_[RTC::ON_BUFFER_WRITE_TIMEOUT].addListener(
00188                                    new DataListener("ON_BUFFER_WRITE_TIMEOUT"), true);
00189         m_listeners.connectorData_[RTC::ON_BUFFER_OVERWRITE].addListener(
00190                                    new DataListener("ON_BUFFER_OVERWRITE"), true);
00191         m_listeners.connectorData_[RTC::ON_BUFFER_READ].addListener(
00192                                    new DataListener("ON_BUFFER_READ"), true);
00193         m_listeners.connectorData_[RTC::ON_SEND].addListener(
00194                                    new DataListener("ON_SEND"), true);
00195         m_listeners.connectorData_[RTC::ON_RECEIVED].addListener(
00196                                    new DataListener("ON_RECEIVED"), true);
00197         m_listeners.connectorData_[RTC::ON_RECEIVER_FULL].addListener(
00198                                    new DataListener("ON_RECEIVER_FULL"), true);
00199         m_listeners.connectorData_[RTC::ON_RECEIVER_TIMEOUT].addListener(
00200                                    new DataListener("ON_RECEIVER_TIMEOUT"), true);
00201         m_listeners.connectorData_[RTC::ON_RECEIVER_ERROR].addListener(
00202                                    new DataListener("ON_RECEIVER_ERROR"), true);
00203 
00204         //ConnectorListeners
00205         m_listeners.connector_[RTC::ON_BUFFER_EMPTY].addListener(
00206                                     new ConnListener("ON_BUFFER_EMPTY"), true);
00207         m_listeners.connector_[RTC::ON_BUFFER_READ_TIMEOUT].addListener(
00208                                     new ConnListener("ON_BUFFER_READ_TIMEOUT"), true);
00209         m_listeners.connector_[RTC::ON_SENDER_EMPTY].addListener(
00210                                     new ConnListener("ON_SENDER_EMPTY"), true);
00211         m_listeners.connector_[RTC::ON_SENDER_TIMEOUT].addListener(
00212                                     new ConnListener("ON_SENDER_TIMEOUT"), true);
00213         m_listeners.connector_[RTC::ON_SENDER_ERROR].addListener(
00214                                     new ConnListener("ON_SENDER_ERROR"), true);
00215         m_listeners.connector_[RTC::ON_CONNECT].addListener(
00216                                     new ConnListener("ON_CONNECT"), true);
00217         m_listeners.connector_[RTC::ON_DISCONNECT].addListener(
00218                                     new ConnListener("ON_DISCONNECT"), true);
00219 
00220         //IOR をプロパティに追加することを確認
00221         index = NVUtil::find_index(provider.get_m_properties(),"dataport.corba_cdr.inport_ior");
00222         CPPUNIT_ASSERT(0<=index);
00223 
00224         //ref をプロパティに追加することを確認
00225         index = NVUtil::find_index(provider.get_m_properties(),"dataport.corba_cdr.inport_ref");
00226         CPPUNIT_ASSERT(0<=index);
00227 
00228         provider.init(prop);
00229         provider.setListener(info, &m_listeners);
00230 
00231         ::OpenRTM::PortStatus ret;
00232         ::OpenRTM::CdrData data;
00233         int testdata[10] = { 0,1,2,3,4,5,6,7,8,9 };
00234         cdrMemoryStream cdr;
00235         RTC::TimedLong td;
00236         CORBA::ULong len;
00237 
00238         td.data = testdata[0];
00239         td >>= cdr;
00240         len = (CORBA::ULong)cdr.bufSize();
00241         data.length(len);
00242         cdr.get_octet_array(&(data[0]), len);
00243         ret = provider.put(data);
00244 
00245         // buffer 未設定によるPORT_ERROR
00246         CPPUNIT_ASSERT_EQUAL(::OpenRTM::PORT_ERROR,ret);
00247 
00248         RTC::CdrBufferBase* buffer;
00249         buffer = RTC::CdrBufferFactory::instance().createObject("ring_buffer");
00250         provider.setBuffer(buffer);
00251 
00252         connector = new RTC::InPortPushConnector(info, &provider, m_listeners, buffer);
00253         if (connector == 0)
00254           {
00255             std::cout << "ERROR: Connector creation failed." << std::endl;
00256           }
00257         provider.setConnector(connector);
00258 
00259         ret = provider.put(data);
00260         CPPUNIT_ASSERT_EQUAL(::OpenRTM::PORT_OK,ret);
00261 
00262         for( int i(1); i<7; ++i )
00263           {
00264             OpenRTM::PortStatus ret;
00265             cdrMemoryStream cdr;
00266             RTC::TimedLong td;
00267             CORBA::ULong len;
00268             td.data = testdata[i];
00269             td >>= cdr;
00270             len = (CORBA::ULong)cdr.bufSize();
00271             data.length(len);
00272             cdr.get_octet_array(&(data[0]), len);
00273             ret = provider.put(data);
00274             CPPUNIT_ASSERT_EQUAL(::OpenRTM::PORT_OK,ret);
00275 
00276           }
00277         cdrMemoryStream cdr2;
00278         td.data = testdata[7];
00279         td >>= cdr2;
00280         len = (CORBA::ULong)cdr2.bufSize();
00281         data.length(len);
00282         cdr2.get_octet_array(&(data[0]), len);
00283         ret = provider.put(data);
00284         CPPUNIT_ASSERT_EQUAL(::OpenRTM::PORT_OK,ret);
00285 
00286         // delete connector;
00287         // ここで delete すると落ちるので、デストラクタで行う。
00288     }
00289     
00290   };
00291 }; // namespace InPortCorbaCdrProvider
00292 
00293 /*
00294  * Register test suite
00295  */
00296 CPPUNIT_TEST_SUITE_REGISTRATION(InPortCorbaCdrProvider::InPortCorbaCdrProviderTests);
00297 
00298 #ifdef LOCAL_MAIN
00299 int main(int argc, char* argv[])
00300 {
00301 
00302   FORMAT format = TEXT_OUT;
00303   int target = 0;
00304   std::string xsl;
00305   std::string ns;
00306   std::string fname;
00307   std::ofstream ofs;
00308 
00309   int i(1);
00310   while (i < argc)
00311     {
00312       std::string arg(argv[i]);
00313       std::string next_arg;
00314       if (i + 1 < argc) next_arg = argv[i + 1];
00315       else              next_arg = "";
00316 
00317       if (arg == "--text") { format = TEXT_OUT; break; }
00318       if (arg == "--xml")
00319         {
00320           if (next_arg == "")
00321             {
00322               fname = argv[0];
00323               fname += ".xml";
00324             }
00325           else
00326             {
00327               fname = next_arg;
00328             }
00329           format = XML_OUT;
00330           ofs.open(fname.c_str());
00331         }
00332       if ( arg == "--compiler"  ) { format = COMPILER_OUT; break; }
00333       if ( arg == "--cerr"      ) { target = 1; break; }
00334       if ( arg == "--xsl"       )
00335         {
00336           if (next_arg == "") xsl = "default.xsl"; 
00337           else                xsl = next_arg;
00338         }
00339       if ( arg == "--namespace" )
00340         {
00341           if (next_arg == "")
00342             {
00343               std::cerr << "no namespace specified" << std::endl;
00344               exit(1); 
00345             }
00346           else
00347             {
00348               xsl = next_arg;
00349             }
00350         }
00351       ++i;
00352     }
00353   CppUnit::TextUi::TestRunner runner;
00354   if ( ns.empty() )
00355     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00356   else
00357     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
00358   CppUnit::Outputter* outputter = 0;
00359   std::ostream* stream = target ? &std::cerr : &std::cout;
00360   switch ( format )
00361     {
00362     case TEXT_OUT :
00363       outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
00364       break;
00365     case XML_OUT :
00366       std::cout << "XML_OUT" << std::endl;
00367       outputter = new CppUnit::XmlOutputter(&runner.result(),
00368                                             ofs, "shift_jis");
00369       static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
00370       break;
00371     case COMPILER_OUT :
00372       outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
00373       break;
00374     }
00375   runner.setOutputter(outputter);
00376   runner.run();
00377   return 0; // runner.run() ? 0 : 1;
00378 }
00379 #endif // MAIN
00380 #endif // InPortCorbaCdrProvider_cpp


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Sun Mar 26 2017 03:37:15