ECFactoryTests.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00011 /*
00012  * $Log: ECFactoryTests.cpp,v $
00013  * Revision 1.1  2008/04/15 08:38:41  arafune
00014  * The first commitment.
00015  *
00016  *
00017  */
00018 
00019 #ifndef ECFactory_cpp
00020 #define ECFactory_cpp
00021 
00022 #include <cppunit/ui/text/TestRunner.h>
00023 #include <cppunit/TextOutputter.h>
00024 #include <cppunit/extensions/TestFactoryRegistry.h>
00025 #include <cppunit/extensions/HelperMacros.h>
00026 #include <cppunit/TestAssert.h>
00027 
00028 #include <rtm/ExtTrigExecutionContext.h>
00029 #include <rtm/PeriodicExecutionContext.h>
00030 #include <rtm/ECFactory.h>
00031 
00036 namespace ECFactory
00037 {
00038   class ECFactoryTests
00039     : public CppUnit::TestFixture
00040   {
00041     CPPUNIT_TEST_SUITE(ECFactoryTests);
00042     CPPUNIT_TEST(test_name);
00043     CPPUNIT_TEST(test_create_and_destroy);
00044     CPPUNIT_TEST_SUITE_END();
00045         
00046   private:
00047     CORBA::ORB_ptr m_pORB;
00048     PortableServer::POA_ptr m_pPOA;
00049         
00050   public:
00054     ECFactoryTests()
00055     {
00056       int argc(0);
00057       char** argv(NULL);
00058       m_pORB = CORBA::ORB_init(argc, argv);
00059       m_pPOA = PortableServer::POA::_narrow(
00060                                             m_pORB->resolve_initial_references("RootPOA"));
00061       m_pPOA->the_POAManager()->activate();
00062     }
00063                     
00067     virtual ~ECFactoryTests()
00068     {
00069     }
00070                   
00074     virtual void setUp()
00075     {
00076     }
00077     
00081     virtual void tearDown()
00082     {
00083     }
00084                 
00090     void test_name()
00091     {
00092       std::string name = "name of execution context";
00093                         
00094       std::auto_ptr<RTC::ECFactoryBase> factory(
00095                 new RTC::ECFactoryCXX(
00096                                       name.c_str(),
00097                                       RTC::ECCreate<RTC::PeriodicExecutionContext>,
00098                                       RTC::ECDelete<RTC::PeriodicExecutionContext>));
00099                         
00100       // コンストラクタで指定した名称を、name()メソッドで正しく取得できるか?
00101       CPPUNIT_ASSERT_EQUAL(name, std::string(factory->name()));
00102     }
00103                 
00110     void test_create_and_destroy()
00111     {
00112       std::auto_ptr<RTC::ECFactoryBase> factory(
00113                 new RTC::ECFactoryCXX(
00114                                       "name of execution context",
00115                                       RTC::ECCreate<RTC::PeriodicExecutionContext>,
00116                                       RTC::ECDelete<RTC::PeriodicExecutionContext>));
00117                         
00118       // create()呼出しにより、所定のExcecutionContextのインスタンスが生成されるか?
00119       RTC::ExecutionContextBase* ec = factory->create();
00120       CPPUNIT_ASSERT(dynamic_cast<RTC::PeriodicExecutionContext*>(ec) != 0);
00121                         
00122       // destroy()呼出しにより、所定のExecutionContextインスタンスが削除されるか?
00123       m_pPOA->deactivate_object(*m_pPOA->servant_to_id(ec));
00124       factory->destroy(ec);
00125       CPPUNIT_ASSERT(dynamic_cast<RTC::PeriodicExecutionContext*>(ec) == 0);
00126     }
00127                 
00128   };
00129 }; // namespace ECFactory
00130 
00131 /*
00132  * Register test suite
00133  */
00134 CPPUNIT_TEST_SUITE_REGISTRATION(ECFactory::ECFactoryTests);
00135 
00136 #ifdef LOCAL_MAIN
00137 int main(int argc, char* argv[])
00138 {
00139 
00140   FORMAT format = TEXT_OUT;
00141   int target = 0;
00142   std::string xsl;
00143   std::string ns;
00144   std::string fname;
00145   std::ofstream ofs;
00146 
00147   int i(1);
00148   while (i < argc)
00149     {
00150       std::string arg(argv[i]);
00151       std::string next_arg;
00152       if (i + 1 < argc) next_arg = argv[i + 1];
00153       else              next_arg = "";
00154 
00155       if (arg == "--text") { format = TEXT_OUT; break; }
00156       if (arg == "--xml")
00157         {
00158           if (next_arg == "")
00159             {
00160               fname = argv[0];
00161               fname += ".xml";
00162             }
00163           else
00164             {
00165               fname = next_arg;
00166             }
00167           format = XML_OUT;
00168           ofs.open(fname.c_str());
00169         }
00170       if ( arg == "--compiler"  ) { format = COMPILER_OUT; break; }
00171       if ( arg == "--cerr"      ) { target = 1; break; }
00172       if ( arg == "--xsl"       )
00173         {
00174           if (next_arg == "") xsl = "default.xsl"; 
00175           else                xsl = next_arg;
00176         }
00177       if ( arg == "--namespace" )
00178         {
00179           if (next_arg == "")
00180             {
00181               std::cerr << "no namespace specified" << std::endl;
00182               exit(1); 
00183             }
00184           else
00185             {
00186               xsl = next_arg;
00187             }
00188         }
00189       ++i;
00190     }
00191   CppUnit::TextUi::TestRunner runner;
00192   if ( ns.empty() )
00193     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00194   else
00195     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
00196   CppUnit::Outputter* outputter = 0;
00197   std::ostream* stream = target ? &std::cerr : &std::cout;
00198   switch ( format )
00199     {
00200     case TEXT_OUT :
00201       outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
00202       break;
00203     case XML_OUT :
00204       std::cout << "XML_OUT" << std::endl;
00205       outputter = new CppUnit::XmlOutputter(&runner.result(),
00206                                             ofs, "shift_jis");
00207       static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
00208       break;
00209     case COMPILER_OUT :
00210       outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
00211       break;
00212     }
00213   runner.setOutputter(outputter);
00214   runner.run();
00215   return 0; // runner.run() ? 0 : 1;
00216 }
00217 #endif // MAIN
00218 #endif // ECFactory_cpp


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