OutPortConnectorTests.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00022 #ifndef OutPortConnector_cpp
00023 #define OutPortConnector_cpp
00024 
00025 #include <cppunit/ui/text/TestRunner.h>
00026 #include <cppunit/TextOutputter.h>
00027 #include <cppunit/extensions/TestFactoryRegistry.h>
00028 #include <cppunit/extensions/HelperMacros.h>
00029 #include <cppunit/TestAssert.h>
00030 
00031 #include <coil/Properties.h>
00032 
00033 #include <rtm/idl/BasicDataTypeSkel.h>
00034 #include <rtm/Typename.h>
00035 #include <rtm/OutPortConnector.h>
00036 #include <rtm/CdrBufferBase.h>
00037 #include <rtm/CORBA_SeqUtil.h>
00038 #include <rtm/NVUtil.h>
00039 #include <rtm/ConnectorBase.h>
00040 #include <rtm/DataPortStatus.h>
00041 #include <rtm/InPortBase.h>
00042 #include <rtm/OutPortBase.h>
00043 #include <rtm/PortAdmin.h>
00044 
00050 namespace OutPortConnector
00051 {
00052   class InPortMock
00053     : public RTC::InPortBase
00054   {
00055   public:
00056     InPortMock(const char* name, const char* value) 
00057      : InPortBase(name, value) {}
00061      bool read()
00062      {
00063         return true;
00064      }
00065 
00066   };
00067 
00068   class OutPortMock
00069     : public RTC::OutPortBase
00070   {
00071   public:
00072     OutPortMock(const char* name, const char* value) 
00073      : OutPortBase(name, value) {}
00077      bool write()
00078      {
00079         return true;
00080      }
00081 
00082   };
00083 
00084   class OutPortConnectorMock
00085     : public RTC::OutPortConnector
00086   {
00087   public:
00088       OutPortConnectorMock(RTC::ConnectorInfo& info)
00089         : RTC::OutPortConnector(info)
00090       {
00091       }
00092       virtual ~OutPortConnectorMock()
00093       {
00094       }
00095       ReturnCode disconnect()
00096       {
00097           return ::RTC::DataPortStatus::PORT_OK;
00098       }
00099       void activate()
00100       {
00101       }
00102       void deactivate()
00103       {
00104       }
00105       RTC::CdrBufferBase* getBuffer()
00106       {
00107           return m_buffer;
00108       }
00109       ReturnCode write(const cdrMemoryStream& data)
00110       {
00111           return ::RTC::DataPortStatus::PORT_OK;
00112       }
00113   private:
00114       RTC::CdrBufferBase *m_buffer;
00115   };
00116 
00117   class OutPortConnectorTests
00118     : public CppUnit::TestFixture
00119   {
00120     CPPUNIT_TEST_SUITE(OutPortConnectorTests);
00121 
00122     CPPUNIT_TEST(test_case0);
00123 
00124     CPPUNIT_TEST_SUITE_END();
00125                 
00126   private:
00127     CORBA::ORB_ptr m_pORB;
00128     PortableServer::POA_ptr m_pPOA;
00129 
00130 
00131   public:
00132         
00136     OutPortConnectorTests()
00137     {
00138 
00139         int argc(0);
00140         char** argv(NULL);
00141         m_pORB = CORBA::ORB_init(argc, argv);
00142         m_pPOA = PortableServer::POA::_narrow(
00143                     m_pORB->resolve_initial_references("RootPOA"));
00144         m_pPOA->the_POAManager()->activate();
00145 
00146     }
00147                 
00151     ~OutPortConnectorTests()
00152     {
00153     }
00154     
00158     virtual void setUp()
00159     {
00160     }
00161                 
00165     virtual void tearDown()
00166     {
00167     }
00168                 
00173     void test_case0()
00174     {
00175 
00176         InPortMock inport("OutPortConnectorTest1", toTypename<RTC::TimedFloat>());
00177         RTC::PortService_var port_ref1= inport.get_port_profile()->port_ref;
00178         OutPortMock outport("OutPortConnectorTest2", toTypename<RTC::TimedFloat>());
00179         RTC::PortService_var port_ref2= outport.get_port_profile()->port_ref;
00180 
00181         RTC::PortAdmin portAdmin(m_pORB,m_pPOA);
00182         portAdmin.registerPort(inport); 
00183         portAdmin.registerPort(outport); 
00184 
00185         RTC::ConnectorProfile cprof;
00186         cprof.connector_id = "id";
00187         cprof.name = CORBA::string_dup("OutPortConnectorTest");
00188 
00189         CORBA_SeqUtil::push_back(cprof.properties,
00190                                  NVUtil::newNV("dataport.interface_type",
00191                                                "corba_cdr"));
00192         CORBA_SeqUtil::push_back(cprof.properties,
00193                                  NVUtil::newNV("dataport.dataflow_type",
00194                                                "push"));
00195         CORBA_SeqUtil::push_back(cprof.properties,
00196                                  NVUtil::newNV("dataport.subscription_type",
00197                                                 "new"));
00198 
00199         cprof.ports.length(2);
00200         cprof.ports[0] = port_ref1;
00201         cprof.ports[1] = port_ref2;
00202 
00203         coil::Properties prop;
00204         NVUtil::copyToProperties(prop, cprof.properties);
00205         RTC::ConnectorInfo info(cprof.name, cprof.connector_id, 
00206                                 CORBA_SeqUtil::refToVstring(cprof.ports), prop);
00207 
00208         OutPortConnectorMock connector(info);
00209         CPPUNIT_ASSERT_EQUAL(std::string(cprof.connector_id), std::string(connector.id()));
00210         CPPUNIT_ASSERT_EQUAL(std::string(cprof.name), std::string(connector.name()));
00211         connector.setEndian(false);
00212         CPPUNIT_ASSERT(!connector.isLittleEndian());
00213         connector.setEndian(true);
00214         CPPUNIT_ASSERT(connector.isLittleEndian());
00215 
00216         RTC::ConnectorInfo info2 = connector.profile();
00217         CPPUNIT_ASSERT_EQUAL(info.name,info2.name);
00218         CPPUNIT_ASSERT_EQUAL(info.id,info2.id);
00219         CPPUNIT_ASSERT(info.ports==info2.ports);
00220         CPPUNIT_ASSERT_EQUAL(info.properties.size(),
00221                              info2.properties.size());
00222         CPPUNIT_ASSERT_EQUAL(info.properties["dataport.interface_type"],
00223                              info2.properties["dataport.interface_type"]);
00224         CPPUNIT_ASSERT_EQUAL(info.properties["dataport.dataflow_type"],
00225                              info2.properties["dataport.dataflow_type"]);
00226         CPPUNIT_ASSERT_EQUAL(info.properties["dataport.subscription_type"],
00227                              info2.properties["dataport.subscription_type"]);
00228         portAdmin.deletePort(inport);
00229         portAdmin.deletePort(outport);
00230     }
00231 
00232   };
00233 }; // namespace OutPortConnector
00234 
00235 /*
00236  * Register test suite
00237  */
00238 CPPUNIT_TEST_SUITE_REGISTRATION(OutPortConnector::OutPortConnectorTests);
00239 
00240 #ifdef LOCAL_MAIN
00241 int main(int argc, char* argv[])
00242 {
00243 
00244   FORMAT format = TEXT_OUT;
00245   int target = 0;
00246   std::string xsl;
00247   std::string ns;
00248   std::string fname;
00249   std::ofstream ofs;
00250 
00251   int i(1);
00252   while (i < argc)
00253     {
00254       std::string arg(argv[i]);
00255       std::string next_arg;
00256       if (i + 1 < argc) next_arg = argv[i + 1];
00257       else              next_arg = "";
00258 
00259       if (arg == "--text") { format = TEXT_OUT; break; }
00260       if (arg == "--xml")
00261         {
00262           if (next_arg == "")
00263             {
00264               fname = argv[0];
00265               fname += ".xml";
00266             }
00267           else
00268             {
00269               fname = next_arg;
00270             }
00271           format = XML_OUT;
00272           ofs.open(fname.c_str());
00273         }
00274       if ( arg == "--compiler"  ) { format = COMPILER_OUT; break; }
00275       if ( arg == "--cerr"      ) { target = 1; break; }
00276       if ( arg == "--xsl"       )
00277         {
00278           if (next_arg == "") xsl = "default.xsl"; 
00279           else                xsl = next_arg;
00280         }
00281       if ( arg == "--namespace" )
00282         {
00283           if (next_arg == "")
00284             {
00285               std::cerr << "no namespace specified" << std::endl;
00286               exit(1); 
00287             }
00288           else
00289             {
00290               xsl = next_arg;
00291             }
00292         }
00293       ++i;
00294     }
00295   CppUnit::TextUi::TestRunner runner;
00296   if ( ns.empty() )
00297     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00298   else
00299     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
00300   CppUnit::Outputter* outputter = 0;
00301   std::ostream* stream = target ? &std::cerr : &std::cout;
00302   switch ( format )
00303     {
00304     case TEXT_OUT :
00305       outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
00306       break;
00307     case XML_OUT :
00308       std::cout << "XML_OUT" << std::endl;
00309       outputter = new CppUnit::XmlOutputter(&runner.result(),
00310                                             ofs, "shift_jis");
00311       static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
00312       break;
00313     case COMPILER_OUT :
00314       outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
00315       break;
00316     }
00317   runner.setOutputter(outputter);
00318   runner.run();
00319   return 0; // runner.run() ? 0 : 1;
00320 }
00321 #endif // MAIN
00322 #endif // OutPortConnector_cpp


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