OutPortCorbaCdrConsumerTests.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00012 /*
00013  * $Log: OutPortCorbaCdrConsumerTests.cpp,v $
00014  *
00015  *
00016  */
00017 
00018 #ifndef OutPortCorbaCdrConsumer_cpp
00019 #define OutPortCorbaCdrConsumer_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/SystemLogger.h>
00032 #include <rtm/OutPortCorbaCdrConsumer.h>
00033 #include <rtm/OutPortCorbaCdrProvider.h>
00034 #include <rtm/CdrBufferBase.h>
00035 #include <rtm/PortAdmin.h>
00036 #include <rtm/CORBA_SeqUtil.h>
00037 #include <rtm/NVUtil.h>
00038 #include <rtm/ConnectorListener.h>
00039 
00040 
00045 namespace OutPortCorbaCdrConsumer
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 
00090 
00095   class OutPortCorbaCdrConsumerMock
00096     : public RTC::OutPortCorbaCdrConsumer
00097   {
00098   public:
00103       OutPortCorbaCdrConsumerMock(void)
00104       {
00105       }
00110       virtual ~OutPortCorbaCdrConsumerMock()
00111       {
00112       }
00117       CORBA::Object_var get_m_objre()
00118       {
00119           return m_objref;
00120       }
00121   };
00122   class OutPortCorbaCdrConsumerTests
00123     : public CppUnit::TestFixture
00124   {
00125     CPPUNIT_TEST_SUITE(OutPortCorbaCdrConsumerTests);
00126 
00127     CPPUNIT_TEST(test_case0);
00128 
00129     CPPUNIT_TEST_SUITE_END();
00130         
00131   private:
00132     CORBA::ORB_ptr m_pORB;
00133     PortableServer::POA_ptr m_pPOA;
00134     RTC::ConnectorListeners m_listeners;
00135 
00136   public:
00137         
00141     OutPortCorbaCdrConsumerTests()
00142     {
00143       int argc(0);
00144       char** argv(NULL);
00145       m_pORB = CORBA::ORB_init(argc, argv);
00146       m_pPOA = PortableServer::POA::_narrow(
00147                     m_pORB->resolve_initial_references("RootPOA"));
00148       m_pPOA->the_POAManager()->activate();
00149     }
00150                 
00154     ~OutPortCorbaCdrConsumerTests()
00155     {
00156     }
00157                 
00161     virtual void setUp()
00162     {
00163     }
00164                 
00168     virtual void tearDown()
00169     { 
00170     }
00171                 
00176     void test_case0()
00177     {
00178         //
00179         //
00180         //
00181         OutPortCorbaCdrConsumerMock consumer;
00182 
00183         //ConnectorInfo
00184         coil::Properties prop;
00185         coil::vstring ports;
00186         RTC::ConnectorInfo info("name", "id", ports, prop);
00187 
00188         //ConnectorDataListeners
00189         m_listeners.connectorData_[RTC::ON_BUFFER_WRITE].addListener(
00190                                    new DataListener("ON_BUFFER_WRITE"), true);
00191         m_listeners.connectorData_[RTC::ON_BUFFER_FULL].addListener(
00192                                    new DataListener("ON_BUFFER_FULL"), true);
00193         m_listeners.connectorData_[RTC::ON_BUFFER_WRITE_TIMEOUT].addListener(
00194                                    new DataListener("ON_BUFFER_WRITE_TIMEOUT"), true);
00195         m_listeners.connectorData_[RTC::ON_BUFFER_OVERWRITE].addListener(
00196                                    new DataListener("ON_BUFFER_OVERWRITE"), true);
00197         m_listeners.connectorData_[RTC::ON_BUFFER_READ].addListener(
00198                                    new DataListener("ON_BUFFER_READ"), true);
00199         m_listeners.connectorData_[RTC::ON_SEND].addListener(
00200                                    new DataListener("ON_SEND"), true);
00201         m_listeners.connectorData_[RTC::ON_RECEIVED].addListener(
00202                                    new DataListener("ON_RECEIVED"), true);
00203         m_listeners.connectorData_[RTC::ON_RECEIVER_FULL].addListener(
00204                                    new DataListener("ON_RECEIVER_FULL"), true);
00205         m_listeners.connectorData_[RTC::ON_RECEIVER_TIMEOUT].addListener(
00206                                    new DataListener("ON_RECEIVER_TIMEOUT"), true);
00207         m_listeners.connectorData_[RTC::ON_RECEIVER_ERROR].addListener(
00208                                    new DataListener("ON_RECEIVER_ERROR"), true);
00209 
00210         //ConnectorListeners
00211         m_listeners.connector_[RTC::ON_BUFFER_EMPTY].addListener(
00212                                     new ConnListener("ON_BUFFER_EMPTY"), true);
00213         m_listeners.connector_[RTC::ON_BUFFER_READ_TIMEOUT].addListener(
00214                                     new ConnListener("ON_BUFFER_READ_TIMEOUT"), true);
00215         m_listeners.connector_[RTC::ON_SENDER_EMPTY].addListener(
00216                                     new ConnListener("ON_SENDER_EMPTY"), true);
00217         m_listeners.connector_[RTC::ON_SENDER_TIMEOUT].addListener(
00218                                     new ConnListener("ON_SENDER_TIMEOUT"), true);
00219         m_listeners.connector_[RTC::ON_SENDER_ERROR].addListener(
00220                                     new ConnListener("ON_SENDER_ERROR"), true);
00221         m_listeners.connector_[RTC::ON_CONNECT].addListener(
00222                                     new ConnListener("ON_CONNECT"), true);
00223         m_listeners.connector_[RTC::ON_DISCONNECT].addListener(
00224                                     new ConnListener("ON_DISCONNECT"), true);
00225 
00226         consumer.setListener(info, &m_listeners);
00227 
00228         consumer.init(prop);
00229 
00230         RTC::ConnectorProfile prof;
00231         bool ret;
00232 
00233         ret = consumer.subscribeInterface(prof.properties);
00234         //ior を設定していないのでfalseとなることを確認する。
00235         CPPUNIT_ASSERT_EQUAL(false, ret);
00236 
00237         RTC::OutPortCorbaCdrProvider provider;
00238 
00239         CORBA_SeqUtil::push_back(prof.properties,
00240                                  NVUtil::newNV("dataport.interface_type",
00241                                                "corba_cdr"));
00242         provider.setListener(info, &m_listeners);
00243         provider.publishInterface(prof.properties);
00244 
00245         ret = consumer.subscribeInterface(prof.properties);
00246         CPPUNIT_ASSERT_EQUAL(true, ret);
00247 
00248         cdrMemoryStream indata;
00249         indata.setByteSwapFlag(true);
00250         ::RTC::DataPortStatus::Enum retcode;
00251 
00252         RTC::CdrBufferBase* buffer;
00253         buffer = RTC::CdrBufferFactory::instance().createObject("ring_buffer");
00254  
00255         consumer.setBuffer(buffer);
00256 
00257         //provider 側の buffer がない状態でコール(unkown error)
00258         retcode = consumer.get(indata);
00259         CPPUNIT_ASSERT_EQUAL((::RTC::DataPortStatus::Enum)13, retcode);
00260         provider.setBuffer(buffer);
00261 
00262         //データなしの状態でコール(empty)
00263         retcode = consumer.get(indata);
00264         CPPUNIT_ASSERT_EQUAL((::RTC::DataPortStatus::Enum)4, retcode);
00265 
00266         int testdata[8] = { 12,34,56,78,90,23,45,99 };
00267         cdrMemoryStream cdr;
00268         RTC::TimedLong td;
00269         for(int ic(0);ic<8;++ic)
00270         {
00271           td.data = testdata[ic];
00272           td >>= cdr;
00273           buffer->write(cdr);
00274         }
00275 
00276         retcode = consumer.get(indata);
00277         CPPUNIT_ASSERT_EQUAL((::RTC::DataPortStatus::Enum)0, retcode);
00278 
00279         CORBA::ULong inlen = indata.bufSize();
00280         CPPUNIT_ASSERT_EQUAL(12,(int)inlen);
00281 
00282         RTC::TimedLong rtd;
00283         rtd <<= indata;
00284         CPPUNIT_ASSERT_EQUAL((long)testdata[0], (long)rtd.data);
00285 
00286         CPPUNIT_ASSERT(!CORBA::is_nil(consumer.get_m_objre()));
00287         consumer.unsubscribeInterface(prof.properties);
00288         CPPUNIT_ASSERT(CORBA::is_nil(consumer.get_m_objre()));
00289 
00290         int index;
00291         index = NVUtil::find_index(prof.properties,
00292                                    "dataport.corba_cdr.outport_ior");
00293 
00294         const char* ior;
00295         if (prof.properties[index].value >>= ior)
00296         {
00297           CORBA::Object_ptr var = m_pORB->string_to_object(ior);
00298           PortableServer::Servant ser = m_pPOA->reference_to_servant(var);
00299           m_pPOA->deactivate_object(*m_pPOA->servant_to_id(ser));
00300         }
00301     }
00302     
00303   };
00304 }; // namespace OutPortBase
00305 
00306 /*
00307  * Register test suite
00308  */
00309 CPPUNIT_TEST_SUITE_REGISTRATION(OutPortCorbaCdrConsumer::OutPortCorbaCdrConsumerTests);
00310 
00311 #ifdef LOCAL_MAIN
00312 int main(int argc, char* argv[])
00313 {
00314 
00315   FORMAT format = TEXT_OUT;
00316   int target = 0;
00317   std::string xsl;
00318   std::string ns;
00319   std::string fname;
00320   std::ofstream ofs;
00321 
00322   int i(1);
00323   while (i < argc)
00324     {
00325       std::string arg(argv[i]);
00326       std::string next_arg;
00327       if (i + 1 < argc) next_arg = argv[i + 1];
00328       else              next_arg = "";
00329 
00330       if (arg == "--text") { format = TEXT_OUT; break; }
00331       if (arg == "--xml")
00332         {
00333           if (next_arg == "")
00334             {
00335               fname = argv[0];
00336               fname += ".xml";
00337             }
00338           else
00339             {
00340               fname = next_arg;
00341             }
00342           format = XML_OUT;
00343           ofs.open(fname.c_str());
00344         }
00345       if ( arg == "--compiler"  ) { format = COMPILER_OUT; break; }
00346       if ( arg == "--cerr"      ) { target = 1; break; }
00347       if ( arg == "--xsl"       )
00348         {
00349           if (next_arg == "") xsl = "default.xsl"; 
00350           else                xsl = next_arg;
00351         }
00352       if ( arg == "--namespace" )
00353         {
00354           if (next_arg == "")
00355             {
00356               std::cerr << "no namespace specified" << std::endl;
00357               exit(1); 
00358             }
00359           else
00360             {
00361               xsl = next_arg;
00362             }
00363         }
00364       ++i;
00365     }
00366   CppUnit::TextUi::TestRunner runner;
00367   if ( ns.empty() )
00368     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00369   else
00370     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
00371   CppUnit::Outputter* outputter = 0;
00372   std::ostream* stream = target ? &std::cerr : &std::cout;
00373   switch ( format )
00374     {
00375     case TEXT_OUT :
00376       outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
00377       break;
00378     case XML_OUT :
00379       std::cout << "XML_OUT" << std::endl;
00380       outputter = new CppUnit::XmlOutputter(&runner.result(),
00381                                             ofs, "shift_jis");
00382       static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
00383       break;
00384     case COMPILER_OUT :
00385       outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
00386       break;
00387     }
00388   runner.setOutputter(outputter);
00389   runner.run();
00390   return 0; // runner.run() ? 0 : 1;
00391 }
00392 #endif // MAIN
00393 #endif // OutPortBase_cpp


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