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


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