OutPortProviderTests.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00021 /*
00022  * $Log: OutPortProviderTests.cpp,v $
00023  * Revision 1.1  2008/02/21 12:51:22  arafune
00024  * The first commitment.
00025  *
00026  */
00027 
00028 #ifndef OutPortProvider_cpp
00029 #define OutPortProvider_cpp
00030 
00031 #include <cppunit/ui/text/TestRunner.h>
00032 #include <cppunit/TextOutputter.h>
00033 #include <cppunit/extensions/TestFactoryRegistry.h>
00034 #include <cppunit/extensions/HelperMacros.h>
00035 #include <cppunit/TestAssert.h>
00036 #include <rtm/OutPortProvider.h>
00037 
00042 /*
00043  *
00044  *
00045  * Mock RTC
00046  *
00047  *
00048  */
00049 namespace RTC
00050 {
00058   LogStreamBuf m_logStreamBuf;
00059   const char* Logger::m_levelString[] =
00060     {
00061       " SILENT: ",
00062       " FATAL: ",
00063       " ERROR: ",
00064       " WARNING: ",
00065       " INFO: ",
00066       " DEBUG: ",
00067       " TRACE: ",
00068       " VERBOSE: ",
00069       " PARANOID: "
00070     };
00071 
00072   Logger::Logger(const char* name)
00073     : ::coil::LogStream(&m_logStreamBuf, 0, 8, 0)
00074   {
00075   }
00076 
00077   Logger::Logger(LogStreamBuf* streambuf)
00078     : ::coil::LogStream(&m_logStreamBuf, 0, 8, 0)
00079   {
00080   }
00081 
00082   Logger::~Logger(void)
00083   {
00084   }
00085 
00088   bool Logger::setLevel(const char* level)
00089   {
00090     return true; 
00091   }
00092 
00095   void Logger::setDateFormat(const char* format)
00096   {
00097   }
00098 
00101   void Logger::setName(const char* name)
00102   {
00103   }
00104 
00107   void Logger::header(int level)
00108   {
00109   }
00110 
00113   std::string Logger::getDate(void)
00114   {
00115     const int maxsize = 256;
00116     char buf[maxsize];
00117 
00118     return std::string(buf);
00119   }
00120 
00123   int Logger::strToLevel(const char* level)
00124   {
00125       return 0;
00126   }
00127 };
00128 /*
00129  *
00130  *
00131  *
00132  *
00133  *
00134  */
00135 namespace OutPortProvider
00136 {
00137   class OutPortProviderMock
00138     : public RTC::OutPortProvider
00139   {
00140   public:
00141     OutPortProviderMock(
00142                         const std::string& portType,
00143                         const std::string& dataType,
00144                         const std::string& interfaceType,
00145                         const std::string& dataFlowType,
00146                         const std::string& subscriptionType)
00147     {
00148       setPortType(portType.c_str());
00149       setDataType(dataType.c_str());
00150       setInterfaceType(interfaceType.c_str());
00151       setDataFlowType(dataFlowType.c_str());
00152       setSubscriptionType(subscriptionType.c_str());
00153                         
00154     }
00155 
00156     void setDummydataInProperties(void)
00157     {
00158       NVUtil::appendStringValue(m_properties, "PROPERTY_NAME1", "PROPERTY_VALUE1");
00159       NVUtil::appendStringValue(m_properties, "PROPERTY_NAME2", "PROPERTY_VALUE2");
00160     }
00161 
00162     void setBuffer(RTC::CdrBufferBase* buffer)
00163     {
00164     }
00165 
00166     void setListener(RTC::ConnectorInfo& info, RTC::ConnectorListeners* listeners)
00167     {
00168     }
00169     void setConnector(RTC::OutPortConnector* connector)
00170     {
00171     }
00172 
00173   };
00174         
00175   int g_argc;
00176   std::vector<std::string> g_argv;
00177 
00178   class OutPortProviderTests
00179     : public CppUnit::TestFixture
00180   {
00181     CPPUNIT_TEST_SUITE(OutPortProviderTests);
00182     CPPUNIT_TEST(test_publishInterfaceProfile);
00183     CPPUNIT_TEST(test_publishInterfaceProfile2);
00184     CPPUNIT_TEST(test_publishInterface);
00185     CPPUNIT_TEST_SUITE_END();
00186                 
00187   public:
00188         
00192     OutPortProviderTests()
00193     {
00194       char* argv[g_argc];
00195       for (int i = 0; i < g_argc; i++) {
00196         argv[i] = (char *)g_argv[i].c_str();
00197       }
00198               
00199       CORBA::ORB_var orb = CORBA::ORB_init(g_argc, argv);
00200     }
00201                 
00205     ~OutPortProviderTests()
00206     {
00207     }
00208                 
00212     virtual void setUp()
00213     {
00214     }
00215                 
00219     virtual void tearDown()
00220     { 
00221     }
00222                 
00231     void test_publishInterfaceProfile()
00232     {
00233       std::auto_ptr<RTC::OutPortProvider> provider(
00234           new OutPortProviderMock("PORT_TYPE", "DATA_TYPE", "INTERFACE_TYPE",
00235                                       "DATA_FLOW_TYPE", "SUBSCRIPTION_TYPE"));
00236 
00237       SDOPackage::NVList prop;
00238       provider->publishInterfaceProfile(prop);
00239 
00240       // dataport.data_typeプロパティは空か?
00241       {
00242         const char* value;
00243         try {
00244             NVUtil::find(prop, "dataport.data_type") >>= value;
00245             CPPUNIT_FAIL("dataport.data_type fialure.");
00246         }
00247         catch(std::string ex) {
00248         }
00249         catch(...) {
00250             CPPUNIT_FAIL("dataport.data_type failure.");
00251         }
00252       }
00253                         
00254       // dataport.interface_typeプロパティを正しく取得できるか?
00255       {
00256         const char* value;
00257         NVUtil::find(prop, "dataport.interface_type") >>= value;
00258         CPPUNIT_ASSERT_EQUAL(std::string("INTERFACE_TYPE"), std::string(value));
00259       }
00260                         
00261       // dataport.dataflow_typeプロパティは空か?
00262       {
00263         const char* value;
00264         try {
00265             NVUtil::find(prop, "dataport.dataflow_type") >>= value;
00266             CPPUNIT_FAIL("dataport.dataflow_type failure.");
00267         }
00268         catch(std::string ex) {
00269         }
00270         catch(...) {
00271             CPPUNIT_FAIL("dataport.dataflow_type failure.");
00272         }
00273       }
00274                         
00275       // dataport.subscription_typeプロパティは空か?
00276       {
00277         const char* value;
00278         try {
00279             NVUtil::find(prop, "dataport.subscription_type") >>= value;
00280             CPPUNIT_FAIL("dataport.subscription_type failure.");
00281         }
00282         catch(std::string ex) {
00283         }
00284         catch(...) {
00285             CPPUNIT_FAIL("dataport.subscription_type failure.");
00286         }
00287       }
00288     }
00289 
00298     void test_publishInterface()
00299     {
00300       std::auto_ptr<OutPortProviderMock> provider(
00301           new OutPortProviderMock("PORT_TYPE", "DATA_TYPE", "INTERFACE_TYPE",
00302                                   "DATA_FLOW_TYPE", "SUBSCRIPTION_TYPE"));
00303 
00304       SDOPackage::NVList prop;
00305       provider->publishInterfaceProfile(prop);
00306                                 
00307       // (1) インタフェースタイプが不一致の場合:
00308       SDOPackage::NVList prop_dummy = prop;
00309       for (CORBA::ULong i(0); i < prop_dummy.length(); ++i)
00310         {
00311           if (std::string(prop_dummy[i].name) == std::string("dataport.interface_type"))
00312             {
00313               // インタフェースタイプが一致しないように、書き換える
00314               prop_dummy[i].value <<= "DUMMY";
00315             }
00316         }
00317 
00318       //m_propertiesへ"PROPERTY_NAME1",”PROPERTY_NAME2”を設定
00319       provider->setDummydataInProperties();
00320                         
00321       provider->publishInterface(prop_dummy);
00322                         
00323       // インタフェース情報が取得されないことを確認する
00324       CPPUNIT_ASSERT_EQUAL(CORBA::Long(-1), NVUtil::find_index(prop_dummy, "PROPERTY_NAME1"));
00325       CPPUNIT_ASSERT_EQUAL(CORBA::Long(-1), NVUtil::find_index(prop_dummy, "PROPERTY_NAME2"));
00326                         
00327       // (2) インタフェースタイプ一致の場合:
00328       provider->publishInterface(prop);
00329                         
00330       // インタフェース情報が取得されることを確認する
00331       CORBA::Long index1 = NVUtil::find_index(prop, "PROPERTY_NAME1");
00332       CORBA::Long index2 = NVUtil::find_index(prop, "PROPERTY_NAME2");
00333       CPPUNIT_ASSERT(CORBA::Long(-1) != index1);
00334       CPPUNIT_ASSERT(CORBA::Long(-1) != index2);
00335                         
00336       const char* value1;
00337       prop[index1].value >>= value1;
00338       CPPUNIT_ASSERT_EQUAL(std::string("PROPERTY_VALUE1"), std::string(value1));
00339 
00340       const char* value2;                       
00341       prop[index2].value >>= value2;
00342       CPPUNIT_ASSERT_EQUAL(std::string("PROPERTY_VALUE2"), std::string(value2));
00343     }
00349     void test_publishInterfaceProfile2()
00350     {
00351       std::auto_ptr<OutPortProviderMock> provider(
00352           new OutPortProviderMock("PORT_TYPE", "DATA_TYPE", "INTERFACE_TYPE",
00353                                   "DATA_FLOW_TYPE", "SUBSCRIPTION_TYPE"));
00354 
00355 
00356       //m_propertiesへ"PROPERTY_NAME1",”PROPERTY_NAME2”を設定
00357       provider->setDummydataInProperties();
00358 
00359       SDOPackage::NVList prop;
00360       provider->publishInterfaceProfile(prop);
00361       // インタフェース情報が取得されることを確認する
00362       CORBA::Long index1 = NVUtil::find_index(prop, "PROPERTY_NAME1");
00363       CORBA::Long index2 = NVUtil::find_index(prop, "PROPERTY_NAME2");
00364       CPPUNIT_ASSERT(CORBA::Long(-1) != index1);
00365       CPPUNIT_ASSERT(CORBA::Long(-1) != index2);
00366                         
00367       const char* value1;
00368       prop[index1].value >>= value1;
00369       CPPUNIT_ASSERT_EQUAL(std::string("PROPERTY_VALUE1"), std::string(value1));
00370 
00371       const char* value2;                       
00372       prop[index2].value >>= value2;
00373       CPPUNIT_ASSERT_EQUAL(std::string("PROPERTY_VALUE2"), std::string(value2));
00374 
00375     }
00376 
00377   };
00378 }; // namespace OutPortProvider
00379 
00380 /*
00381  * Register test suite
00382  */
00383 CPPUNIT_TEST_SUITE_REGISTRATION(OutPortProvider::OutPortProviderTests);
00384 
00385 #ifdef LOCAL_MAIN
00386 int main(int argc, char* argv[])
00387 {
00388 
00389   FORMAT format = TEXT_OUT;
00390   int target = 0;
00391   std::string xsl;
00392   std::string ns;
00393   std::string fname;
00394   std::ofstream ofs;
00395 
00396   int i(1);
00397   while (i < argc)
00398     {
00399       std::string arg(argv[i]);
00400       std::string next_arg;
00401       if (i + 1 < argc) next_arg = argv[i + 1];
00402       else              next_arg = "";
00403 
00404       if (arg == "--text") { format = TEXT_OUT; break; }
00405       if (arg == "--xml")
00406         {
00407           if (next_arg == "")
00408             {
00409               fname = argv[0];
00410               fname += ".xml";
00411             }
00412           else
00413             {
00414               fname = next_arg;
00415             }
00416           format = XML_OUT;
00417           ofs.open(fname.c_str());
00418         }
00419       if ( arg == "--compiler"  ) { format = COMPILER_OUT; break; }
00420       if ( arg == "--cerr"      ) { target = 1; break; }
00421       if ( arg == "--xsl"       )
00422         {
00423           if (next_arg == "") xsl = "default.xsl"; 
00424           else                xsl = next_arg;
00425         }
00426       if ( arg == "--namespace" )
00427         {
00428           if (next_arg == "")
00429             {
00430               std::cerr << "no namespace specified" << std::endl;
00431               exit(1); 
00432             }
00433           else
00434             {
00435               xsl = next_arg;
00436             }
00437         }
00438       ++i;
00439     }
00440   CppUnit::TextUi::TestRunner runner;
00441   if ( ns.empty() )
00442     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00443   else
00444     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
00445   CppUnit::Outputter* outputter = 0;
00446   std::ostream* stream = target ? &std::cerr : &std::cout;
00447   switch ( format )
00448     {
00449     case TEXT_OUT :
00450       outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
00451       break;
00452     case XML_OUT :
00453       std::cout << "XML_OUT" << std::endl;
00454       outputter = new CppUnit::XmlOutputter(&runner.result(),
00455                                             ofs, "shift_jis");
00456       static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
00457       break;
00458     case COMPILER_OUT :
00459       outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
00460       break;
00461     }
00462   runner.setOutputter(outputter);
00463   runner.run();
00464   return 0; // runner.run() ? 0 : 1;
00465 }
00466 #endif // MAIN
00467 #endif // OutPortProvider_cpp


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