FactoryTests.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00021 /*
00022  * $Log: FactoryTests.cpp,v $
00023  * Revision 1.2  2008/05/02 12:30:29  arafune
00024  * Modified some tests.
00025  *
00026  * Revision 1.1  2007/12/20 07:50:18  arafune
00027  * *** empty log message ***
00028  *
00029  * Revision 1.2  2007/01/12 14:50:35  n-ando
00030  * A trivial fix.
00031  *
00032  * Revision 1.1  2006/11/27 08:31:38  n-ando
00033  * TestSuites are devided into each directory.
00034  *
00035  *
00036  */
00037 
00038 #ifndef Factory_cpp
00039 #define Factory_cpp
00040 
00041 #include <cppunit/ui/text/TestRunner.h>
00042 #include <cppunit/TextOutputter.h>
00043 #include <cppunit/extensions/TestFactoryRegistry.h>
00044 #include <cppunit/extensions/HelperMacros.h>
00045 #include <cppunit/TestAssert.h>
00046 
00047 #include <rtm/RTObject.h>
00048 #include <rtm/Factory.h>
00049 #include <rtm/Manager.h>
00050 #include <coil/Properties.h>
00051 
00052 
00053 namespace Tests
00054 {
00055   class Logger
00056   {
00057   public:
00058     void log(const std::string& msg)
00059     {
00060       m_log.push_back(msg);
00061     }
00062                 
00063     int countLog(const std::string& line)
00064     {
00065       int count = 0;
00066       for (int i = 0; i < (int) m_log.size(); ++i)
00067         {
00068           if (m_log[i] == line) ++count;
00069         }
00070       return count;
00071     }
00072                 
00073   private:
00074     std::vector<std::string> m_log;
00075   };
00076         
00077   class RTObjectMock
00078     : public virtual RTC::RTObject_impl
00079   {
00080   public:
00081     RTObjectMock(CORBA::ORB_ptr orb, PortableServer::POA_ptr poa)
00082       : RTC::RTObject_impl(orb, poa), m_logger(NULL)
00083     {
00084     }
00085                 
00086   public: // helper for test
00087     void setLogger(Logger* logger)
00088     {
00089       m_logger = logger;
00090     }
00091                 
00092   private:
00093     Logger* m_logger;
00094         
00095   private:
00096     void log(const std::string& msg)
00097     {
00098       if (m_logger != NULL) m_logger->log(msg);
00099     }
00100   };
00101 
00102   RTC::RtcBase* CreateRTObjectMock(RTC::Manager* manager)
00103   {
00104     CORBA::ORB_ptr orb = manager->getORB();
00105     PortableServer::POA_ptr poa = manager->getPOA();
00106     return new RTObjectMock(orb, poa);
00107   }
00108 
00109   void DeleteRTObjectMock(RTC::RtcBase* rtc)
00110   {
00111     if (rtc != NULL) rtc->_remove_ref();
00112   }
00113         
00114   class FactoryTests
00115     : public CppUnit::TestFixture
00116   {
00117     CPPUNIT_TEST_SUITE(FactoryTests);
00118     CPPUNIT_TEST(test_create_and_destroy);
00119     CPPUNIT_TEST(test_profile);
00120     CPPUNIT_TEST(test_number);
00121     CPPUNIT_TEST_SUITE_END();
00122 
00123   private:
00124     RTC::Manager* m_mgr;
00125         
00126   public:
00130     FactoryTests()
00131     {
00132     }
00133                 
00137     ~FactoryTests()
00138     {
00139     }
00140                 
00144     virtual void setUp()
00145     {
00146       m_mgr = RTC::Manager::init(0, NULL);
00147     }
00148                 
00152     virtual void tearDown()
00153     {
00154       // m_mgr->terminate();
00155     }
00156                 
00164     void test_create_and_destroy()
00165     {
00166       coil::Properties properties;
00167       properties.setProperty("name", "NAME");
00168                         
00169       RTC::FactoryCXX factory(
00170                               properties, CreateRTObjectMock, DeleteRTObjectMock);
00171                         
00172       // 正常にコンポーネントを生成できるか?
00173       RTC::RtcBase* rtc = factory.create(m_mgr);
00174       CPPUNIT_ASSERT(rtc != NULL);
00175                         
00176       RTObjectMock* mock = dynamic_cast<RTObjectMock*>(rtc);
00177       CPPUNIT_ASSERT(mock != NULL);
00178                         
00179       Logger logger;
00180       mock->setLogger(&logger);
00181                         
00182       // 生成されたコンポーネントには、正しくプロパティが設定されているか?
00183       coil::Properties propertiesRet = rtc->getProperties();
00184       CPPUNIT_ASSERT_EQUAL(std::string("NAME"), propertiesRet.getProperty("name"));
00185                         
00186       // 正常にコンポーネントを破棄できるか?
00187       factory.destroy(rtc);
00188     }
00189                 
00195     void test_profile()
00196     {
00197       coil::Properties properties;
00198       properties.setProperty("name", "NAME");
00199                         
00200       RTC::FactoryCXX factory(
00201                               properties, CreateRTObjectMock, DeleteRTObjectMock);
00202                         
00203       // コンストラクタで指定したプロパティを取得できるか?
00204       coil::Properties propertiesRet = factory.profile();
00205       CPPUNIT_ASSERT_EQUAL(std::string("NAME"), propertiesRet.getProperty("name"));
00206     }
00207                 
00213     void test_number()
00214     {
00215                         
00216       coil::Properties properties;
00217       properties.setProperty("name", "NAME");
00218                         
00219       RTC::FactoryCXX factory(
00220                               properties, CreateRTObjectMock, DeleteRTObjectMock);
00221                         
00222       int MAX_NUM = 1;
00223                         
00224       std::vector<RTC::RtcBase*> rtcList;
00225       for (int i = 0; i < MAX_NUM; ++i)
00226         {
00227           // create()呼出前のインスタンス数は期待どおりか?
00228           CPPUNIT_ASSERT_EQUAL(i-1, factory.number());
00229                                 
00230           // createする
00231           RTC::RtcBase* rtc = factory.create(m_mgr);
00232           CPPUNIT_ASSERT(rtc != NULL);
00233                                 
00234           // create()呼出後のインスタンス数は期待どおりか?
00235           CPPUNIT_ASSERT_EQUAL(i, factory.number());
00236                                 
00237           rtcList.push_back(rtc);
00238         }
00239                         
00240       for (int i = 0; i < MAX_NUM; ++i)
00241         {
00242           // destroy()呼出前のインスタンス数は期待どおりか?
00243           CPPUNIT_ASSERT_EQUAL(i, factory.number());
00244                                 
00245           try {
00246             // destroyする
00247             factory.destroy(rtcList[i]);
00248           }
00249           catch (...) {}
00250                                 
00251           // destroy()呼出後のインスタンス数は期待どおりか?
00252           CPPUNIT_ASSERT_EQUAL(i-1, factory.number());
00253         }
00254 
00255     }
00256                 
00257   };
00258 }; // namespace Factory
00259 
00260 /*
00261  * Register test suite
00262  */
00263 CPPUNIT_TEST_SUITE_REGISTRATION(Tests::FactoryTests);
00264 
00265 #ifdef LOCAL_MAIN
00266 int main(int argc, char* argv[])
00267 {
00268 
00269   FORMAT format = TEXT_OUT;
00270   int target = 0;
00271   std::string xsl;
00272   std::string ns;
00273   std::string fname;
00274   std::ofstream ofs;
00275 
00276   int i(1);
00277   while (i < argc)
00278     {
00279       std::string arg(argv[i]);
00280       std::string next_arg;
00281       if (i + 1 < argc) next_arg = argv[i + 1];
00282       else              next_arg = "";
00283 
00284       if (arg == "--text") { format = TEXT_OUT; break; }
00285       if (arg == "--xml")
00286         {
00287           if (next_arg == "")
00288             {
00289               fname = argv[0];
00290               fname += ".xml";
00291             }
00292           else
00293             {
00294               fname = next_arg;
00295             }
00296           format = XML_OUT;
00297           ofs.open(fname.c_str());
00298         }
00299       if ( arg == "--compiler"  ) { format = COMPILER_OUT; break; }
00300       if ( arg == "--cerr"      ) { target = 1; break; }
00301       if ( arg == "--xsl"       )
00302         {
00303           if (next_arg == "") xsl = "default.xsl"; 
00304           else                xsl = next_arg;
00305         }
00306       if ( arg == "--namespace" )
00307         {
00308           if (next_arg == "")
00309             {
00310               std::cerr << "no namespace specified" << std::endl;
00311               exit(1); 
00312             }
00313           else
00314             {
00315               xsl = next_arg;
00316             }
00317         }
00318       ++i;
00319     }
00320   CppUnit::TextUi::TestRunner runner;
00321   if ( ns.empty() )
00322     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00323   else
00324     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
00325   CppUnit::Outputter* outputter = 0;
00326   std::ostream* stream = target ? &std::cerr : &std::cout;
00327   switch ( format )
00328     {
00329     case TEXT_OUT :
00330       outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
00331       break;
00332     case XML_OUT :
00333       std::cout << "XML_OUT" << std::endl;
00334       outputter = new CppUnit::XmlOutputter(&runner.result(),
00335                                             ofs, "shift_jis");
00336       static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
00337       break;
00338     case COMPILER_OUT :
00339       outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
00340       break;
00341     }
00342   runner.setOutputter(outputter);
00343   runner.run();
00344   return 0; // runner.run() ? 0 : 1;
00345 }
00346 #endif // MAIN
00347 #endif // Factory_cpp


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