CorbaConsumerTests.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00012 /*
00013  * $Log: CorbaConsumerTests.cpp,v $
00014  * Revision 1.2  2008/02/24 09:05:52  arafune
00015  * Added some new tests.
00016  *
00017  * Revision 1.1  2007/12/20 07:50:17  arafune
00018  * *** empty log message ***
00019  *
00020  * Revision 1.2  2007/01/12 14:48:30  n-ando
00021  * The name of class to be tested was changed from Consumer to CorbaConsumer.
00022  *
00023  * Revision 1.1  2006/11/27 08:30:48  n-ando
00024  * TestSuites are devided into each directory.
00025  *
00026  *
00027  */
00028 
00029 #ifndef CorbaConsumer_cpp
00030 #define CorbaConsumer_cpp
00031 
00032 #include <cppunit/ui/text/TestRunner.h>
00033 #include <cppunit/TextOutputter.h>
00034 #include <cppunit/extensions/TestFactoryRegistry.h>
00035 #include <cppunit/extensions/HelperMacros.h>
00036 #include <cppunit/TestAssert.h>
00037 
00038 #include <rtm/config_rtc.h>
00039 #include "HelloSkel.h"
00040 #include <rtm/CorbaConsumer.h>
00041 
00046 namespace CorbaConsumer
00047 {
00048   class hello_impl
00049     : virtual public POA_hello,
00050       virtual public PortableServer::RefCountServantBase
00051   {
00052   public:
00053     hello_impl() : m_invokedCount(0) {};
00054     virtual ~hello_impl() {};
00055                 
00056     void hello_world()
00057     {
00058       ++m_invokedCount;
00059     }
00060                 
00061   public:
00062     int m_invokedCount;
00063   };
00064         
00065   class CorbaConsumerTests
00066     : public CppUnit::TestFixture
00067   {
00068     CPPUNIT_TEST_SUITE(CorbaConsumerTests);
00069     CPPUNIT_TEST(test_setObject_and__ptr);
00070     CPPUNIT_TEST(test_copy_constructor);
00071     CPPUNIT_TEST(test_pointer_operator);
00072     CPPUNIT_TEST(test_substitute_operator);
00073     CPPUNIT_TEST(test_releaseObject);
00074     CPPUNIT_TEST_SUITE_END();
00075         
00076   private:
00077     CORBA::ORB_ptr m_pORB;
00078     PortableServer::POA_ptr m_pPOA;
00079         
00080   public:
00081         
00085     CorbaConsumerTests()
00086     {
00087       int argc = 0;
00088       char** argv = 0;
00089                         
00090       m_pORB = CORBA::ORB_init(argc, argv);
00091       m_pPOA = PortableServer::POA::_narrow(
00092                                             m_pORB->resolve_initial_references("RootPOA"));
00093       m_pPOA->the_POAManager()->activate();
00094     }
00095                 
00099     ~CorbaConsumerTests()
00100     {
00101     }
00102                 
00106     virtual void setUp()
00107     {
00108     }
00109                 
00113     virtual void tearDown()
00114     { 
00115     }
00116                 
00122     void test_setObject_and__ptr()
00123     {
00124       // Consumerに割り当てるオブジェクトを生成する
00125       hello_impl* helloImpl = new hello_impl();
00126       PortableServer::ObjectId_var objId = m_pPOA->activate_object(helloImpl);
00127                         
00128       // 生成したオブジェクトをConsumerにセットする
00129       RTC::CorbaConsumer<hello> consumer;
00130       consumer.setObject(m_pPOA->id_to_reference(objId));
00131                         
00132       // Consumerに割り当てたオブジェクトのメソッドを正しく呼び出せるか?
00133       CPPUNIT_ASSERT_EQUAL(0, helloImpl->m_invokedCount);
00134       consumer._ptr()->hello_world();
00135       CPPUNIT_ASSERT_EQUAL(1, helloImpl->m_invokedCount);
00136 
00137       m_pPOA->deactivate_object(*m_pPOA->servant_to_id(helloImpl));
00138       delete helloImpl;
00139     }
00140                 
00147     void test_copy_constructor()
00148     {
00149       // Consumerに割り当てるオブジェクトを生成する
00150       hello_impl* helloImpl = new hello_impl();
00151       PortableServer::ObjectId_var objId = m_pPOA->activate_object(helloImpl);
00152                         
00153       // 生成したオブジェクトをConsumerにセットする
00154       RTC::CorbaConsumer<hello> consumer;
00155       consumer.setObject(m_pPOA->id_to_reference(objId));
00156                         
00157       // 作成したConsumerオブジェクトを元に、別の新たなConsumerをコピーコンストラクトする
00158       RTC::CorbaConsumer<hello> consumerNew(consumer);
00159                         
00160       // 新たに作成したConsumerに対して、メソッドを正しく呼び出せるか?
00161       CPPUNIT_ASSERT_EQUAL(0, helloImpl->m_invokedCount);
00162       consumerNew._ptr()->hello_world();
00163       CPPUNIT_ASSERT_EQUAL(1, helloImpl->m_invokedCount);
00164 
00165       m_pPOA->deactivate_object(*m_pPOA->servant_to_id(helloImpl));
00166       delete helloImpl;
00167     }
00168                 
00175     void test_pointer_operator()
00176     {
00177       // Consumerに割り当てるオブジェクトを生成する
00178       hello_impl* helloImpl = new hello_impl();
00179       PortableServer::ObjectId_var objId = m_pPOA->activate_object(helloImpl);
00180                         
00181       // 生成したオブジェクトをConsumerにセットする
00182       RTC::CorbaConsumer<hello> consumer;
00183       consumer.setObject(m_pPOA->id_to_reference(objId));
00184                         
00185       // ポインタ演算子(->)を用いて、Consumerに割り当てたオブジェクトのメソッドを正しく呼び出せるか?
00186       CPPUNIT_ASSERT_EQUAL(0, helloImpl->m_invokedCount);
00187       consumer->hello_world();
00188       CPPUNIT_ASSERT_EQUAL(1, helloImpl->m_invokedCount);
00189 
00190       m_pPOA->deactivate_object(*m_pPOA->servant_to_id(helloImpl));
00191       delete helloImpl;
00192     }
00193                 
00194     void test_substitute_operator()
00195     {
00196       // Consumerに割り当てるオブジェクトを生成する
00197       hello_impl* helloImpl = new hello_impl();
00198       PortableServer::ObjectId_var objId = m_pPOA->activate_object(helloImpl);
00199                         
00200       // 生成したオブジェクトをConsumerにセットする
00201       RTC::CorbaConsumer<hello> consumer;
00202       consumer.setObject(m_pPOA->id_to_reference(objId));
00203                         
00204       // 作成したConsumerオブジェクトを元に、別の新たなConsumerをコピーコンストラクトする
00205       RTC::CorbaConsumer<hello> consumerNew = consumer;
00206                         
00207       // 新たに作成したConsumerに対して、メソッドを正しく呼び出せるか?
00208       CPPUNIT_ASSERT_EQUAL(0, helloImpl->m_invokedCount);
00209       consumerNew._ptr()->hello_world();
00210       CPPUNIT_ASSERT_EQUAL(1, helloImpl->m_invokedCount);
00211 
00212       m_pPOA->deactivate_object(*m_pPOA->servant_to_id(helloImpl));
00213       delete helloImpl;
00214     }
00215                 
00221     void test_releaseObject()
00222     {
00223       // Consumerに割り当てるオブジェクトを生成する
00224       hello_impl* helloImpl = new hello_impl();
00225       PortableServer::ObjectId_var objId = m_pPOA->activate_object(helloImpl);
00226                         
00227       // 生成したオブジェクトをConsumerにセットする
00228       RTC::CorbaConsumer<hello> consumer;
00229       consumer.setObject(m_pPOA->id_to_reference(objId));
00230 
00231       // この時点では、オブジェクト参照は保持されているはず
00232       CPPUNIT_ASSERT(! CORBA::is_nil(consumer._ptr()));
00233                         
00234       // releaseObject()呼出によって、保持されているオブジェクト参照が正しくクリアされるか?
00235       consumer.releaseObject();
00236       CPPUNIT_ASSERT(CORBA::is_nil(consumer._ptr()));
00237 
00238       m_pPOA->deactivate_object(*m_pPOA->servant_to_id(helloImpl));
00239       delete helloImpl;
00240     }
00241                 
00242   };
00243 }; // namespace CorbaConsumer
00244 
00245 /*
00246  * Register test suite
00247  */
00248 CPPUNIT_TEST_SUITE_REGISTRATION(CorbaConsumer::CorbaConsumerTests);
00249 
00250 #ifdef LOCAL_MAIN
00251 int main(int argc, char* argv[])
00252 {
00253 
00254   FORMAT format = TEXT_OUT;
00255   int target = 0;
00256   std::string xsl;
00257   std::string ns;
00258   std::string fname;
00259   std::ofstream ofs;
00260 
00261   int i(1);
00262   while (i < argc)
00263     {
00264       std::string arg(argv[i]);
00265       std::string next_arg;
00266       if (i + 1 < argc) next_arg = argv[i + 1];
00267       else              next_arg = "";
00268 
00269       if (arg == "--text") { format = TEXT_OUT; break; }
00270       if (arg == "--xml")
00271         {
00272           if (next_arg == "")
00273             {
00274               fname = argv[0];
00275               fname += ".xml";
00276             }
00277           else
00278             {
00279               fname = next_arg;
00280             }
00281           format = XML_OUT;
00282           ofs.open(fname.c_str());
00283         }
00284       if ( arg == "--compiler"  ) { format = COMPILER_OUT; break; }
00285       if ( arg == "--cerr"      ) { target = 1; break; }
00286       if ( arg == "--xsl"       )
00287         {
00288           if (next_arg == "") xsl = "default.xsl"; 
00289           else                xsl = next_arg;
00290         }
00291       if ( arg == "--namespace" )
00292         {
00293           if (next_arg == "")
00294             {
00295               std::cerr << "no namespace specified" << std::endl;
00296               exit(1); 
00297             }
00298           else
00299             {
00300               xsl = next_arg;
00301             }
00302         }
00303       ++i;
00304     }
00305   CppUnit::TextUi::TestRunner runner;
00306   if ( ns.empty() )
00307     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00308   else
00309     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
00310   CppUnit::Outputter* outputter = 0;
00311   std::ostream* stream = target ? &std::cerr : &std::cout;
00312   switch ( format )
00313     {
00314     case TEXT_OUT :
00315       outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
00316       break;
00317     case XML_OUT :
00318       std::cout << "XML_OUT" << std::endl;
00319       outputter = new CppUnit::XmlOutputter(&runner.result(),
00320                                             ofs, "shift_jis");
00321       static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
00322       break;
00323     case COMPILER_OUT :
00324       outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
00325       break;
00326     }
00327   runner.setOutputter(outputter);
00328   runner.run();
00329   return 0; // runner.run() ? 0 : 1;
00330 }
00331 #endif // MAIN
00332 #endif // CorbaConsumer_cpp


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Sun Mar 26 2017 03:37:15