CORBAManagerTests.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00012 /*
00013  * $Log$
00014  *
00015  */
00016 
00017 #ifndef CORBAManager_cpp
00018 #define CORBAManager_cpp
00019 
00020 #include <cppunit/ui/text/TestRunner.h>
00021 #include <cppunit/TextOutputter.h>
00022 #include <cppunit/extensions/TestFactoryRegistry.h>
00023 #include <cppunit/extensions/HelperMacros.h>
00024 #include <cppunit/TestAssert.h>
00025 #include <doil/corba/CORBAManager.h>
00026 #include <doil/corba/CORBAServantBase.h>
00027 #include <doil/ImplBase.h>
00028 #include <iostream>
00029 #include "Echo.hh"
00030 
00031 class EchoImpl
00032   : public doil::ImplBase
00033 {
00034 public:
00035   EchoImpl()
00036   {
00037     sprintf(m_name, "EchoSample%d", count);
00038     ++count;
00039   }
00040   virtual ~EchoImpl()
00041   {
00042     std::cout << "EchoImpl: " << name() << " deleted." << std::endl;
00043   }
00044   const char* id() {return "EchoSample";}
00045   const char* name() {return m_name;}
00046   void incRef(){}
00047   void decRef(){}
00048   void echo(std::string msg)
00049   {
00050     std::cout << name() <<  " -> Message is: " << msg << std::endl;
00051     return;
00052   }
00053   static int count;
00054   char m_name[16];
00055 };
00056 int EchoImpl::count = 0;
00057 
00058 
00059 class EchoServant
00060   : public virtual doil::CORBA::CORBAServantBase,
00061     public virtual POA_EchoSample
00062 {
00063 public:
00064   EchoServant(doil::ImplBase* impl)
00065     : doil::CORBA::CORBAServantBase(impl)
00066   {
00067     m_impl = dynamic_cast<EchoImpl*>(impl);
00068     if (m_impl == NULL) throw std::bad_alloc();
00069   }
00070   virtual ~EchoServant()
00071   {
00072     std::cout << "EchoServant: " << name() << " deleted." << std::endl;
00073   }
00074   void echo(const char* msg)
00075   {
00076     m_impl->echo(msg);
00077   }
00078 private:
00079   EchoImpl* m_impl;
00080 };
00081 
00082 
00083 
00088 namespace CORBAManager
00089 {
00090   class CORBAManagerTests
00091    : public CppUnit::TestFixture
00092   {
00093     CPPUNIT_TEST_SUITE(CORBAManagerTests);
00094     CPPUNIT_TEST(test_init);
00095     CPPUNIT_TEST(test_instance);
00096     CPPUNIT_TEST(test_name);
00097     CPPUNIT_TEST(test_case0);
00098     CPPUNIT_TEST_SUITE_END();
00099   
00100   private:
00101 
00102   public:
00103   
00107     CORBAManagerTests()
00108     {
00109     }
00110     
00114     ~CORBAManagerTests()
00115     {
00116     }
00117   
00121     virtual void setUp()
00122     {
00123     }
00124     
00128     virtual void tearDown()
00129     { 
00130     }
00131   
00132     /* test case */
00133     void test_init()
00134     {
00135       doil::CORBA::CORBAManager* mgr;
00136       mgr = doil::CORBA::CORBAManager::init(coil::Properties());
00137 
00138       for (int i(0); i < 1000; ++i)
00139         {
00140           doil::CORBA::CORBAManager* tmp;
00141           tmp = doil::CORBA::CORBAManager::init(coil::Properties());
00142           CPPUNIT_ASSERT_MESSAGE("CORBAManager's pointer should always be same",
00143                                  mgr == tmp);
00144         }
00145     }
00146 
00147     void test_instance()
00148     {
00149       doil::CORBA::CORBAManager* mgr;
00150       mgr = &(doil::CORBA::CORBAManager::instance());
00151       for (int i(0); i < 1000; ++i)
00152         {
00153           doil::CORBA::CORBAManager* tmp;
00154           tmp = &(doil::CORBA::CORBAManager::instance());
00155           CPPUNIT_ASSERT_MESSAGE("CORBAManager's pointer should always be same",
00156                                  mgr == tmp);
00157         }
00158       
00159     }
00160     void test_name()
00161     {
00162       for (int i(100); i < 1000; ++i)
00163         {
00164           std::string name(doil::CORBA::CORBAManager::instance().name());
00165           CPPUNIT_ASSERT_MESSAGE("CORBAManager's name should be corba",
00166                                  name == "corba");
00167         }
00168     }
00169 
00170     // ごちゃまぜのテスト
00171     void test_case0()
00172     {
00173       doil::CORBA::CORBAManager& mgr(doil::CORBA::CORBAManager::instance());
00174       
00175       // name()
00176       std::cout << mgr.name() << std::endl;
00177 
00178       // activateObject
00179       EchoImpl* eimpl = new EchoImpl();
00180       doil::ReturnCode_t ret;
00181       ret = mgr.activateObject(eimpl);
00182       if (ret == doil::OK) std::cout << "register: OK" << std::endl;
00183       else if (ret == doil::NOT_FOUND) std::cout << "register: NOT_FOUND" << std::endl;
00184       else if (ret == doil::UNKNOWN) std::cout << "register: UNKNOWN" << std::endl;
00185       else std::cout << "other error" << std::endl;
00186 
00187 
00188       // regsiterFactory
00189       mgr.registerFactory(eimpl->id(), doil::New<EchoServant>,
00190                           doil::Delete<EchoServant>);
00191       ret = mgr.activateObject(eimpl);
00192       
00193       if (ret == doil::OK) std::cout << "register: OK" << std::endl;
00194       else if (ret == doil::NOT_FOUND) std::cout << "register: NOT_FOUND" << std::endl;
00195       else if (ret == doil::UNKNOWN) std::cout << "register: UNKNOWN" << std::endl;
00196       else std::cout << "other error" << std::endl;
00197 
00198 
00199       // toServant
00200       doil::ServantBase* svt = mgr.toServant(eimpl);
00201       if (svt == NULL)
00202         {
00203           std::cout << "servant not found" << std::endl;
00204           return;
00205         }
00206       EchoServant* esvt = dynamic_cast<EchoServant*>(svt);
00207       if (esvt == NULL) 
00208         {
00209           std::cout << "dynamic_cast failed" << std::endl;
00210       return;
00211         }
00212       esvt->echo("daradara");
00213       //  EchoServant* esvt = new EchoServant(eimpl);
00214       //  mgr.activateObject(eimpl, esvt);
00215       
00216       //  esvt->echo("hogehoge");
00217       std::cout << "ID  : " << esvt->id() << std::endl;
00218       std::cout << "name: " << esvt->name() << std::endl;
00219       
00220       // getORB/getPOA
00221       CORBA::ORB_ptr orb = mgr.getORB();
00222       PortableServer::POA_ptr poa = mgr.getPOA();
00223       
00224       CORBA::Object_ptr obj = poa->servant_to_reference(esvt);
00225       EchoSample_ptr eptr = EchoSample::_narrow(obj);
00226       eptr->echo("munyamunya");
00227 
00228       // toImpl
00229       doil::ImplBase* tmpi = mgr.toImpl(eptr);
00230       if (tmpi == NULL)
00231         {
00232           std::cout << "not found impl" << std::endl;
00233           return;
00234         }
00235       
00236       EchoImpl* tmpe = dynamic_cast<EchoImpl*>(tmpi);
00237       tmpe->echo("gggggg");
00238       
00239       printf("eimpl 0x%lx\n", (unsigned long int)eimpl);
00240       printf("tmpe  0x%lx\n", (unsigned long int)tmpe);
00241 
00242       std::cout << "objref from eimpl: " << orb->object_to_string(mgr.toReference(eimpl)) << std::endl;
00243       std::cout << "objref from obj  : " << orb->object_to_string(obj) << std::endl;
00244       
00245 
00246       
00247       EchoImpl* eimpl2 = new EchoImpl();
00248       mgr.activateObject(eimpl2);
00249       std::cout << "objref from eimpl2:" << orb->object_to_string(mgr.toReference(eimpl2)) << std::endl;
00250       
00251       std::string ior0, ior1;
00252       ior0 = orb->object_to_string(obj);
00253       ior1 = orb->object_to_string(mgr.toReference(eimpl2));
00254       
00255       if (ior0 == ior1) std::cout << "same!!!!!!!!!!" << std::endl;
00256       else std::cout << "different" << std::endl;
00257       
00258       //  mgr.deactivateObject(eimpl);
00259       for (int j(0); j < 100; ++j)
00260         {
00261           CORBA::Object_var tobj;
00262           if (j % 2 == 0)
00263             {
00264               tobj = orb->string_to_object(ior0.c_str());
00265             }
00266           else
00267             {
00268               tobj = orb->string_to_object(ior1.c_str());
00269             }
00270           EchoSample_var ec = EchoSample::_narrow(tobj);
00271           char msg[100];
00272           sprintf(msg, "hoge%06d", j);
00273           ec->echo(msg);
00274         }
00275       std::cout << "deactivated" << std::endl;
00276       doil::ImplBase* dimpl = mgr.toImpl(svt);
00277       std::cout << "toImpl" << std::endl;
00278       if (dimpl == NULL) 
00279         {
00280           std::cout << "not found" << std::endl;
00281         }
00282       
00283       mgr.shutdown();
00284     }
00285 
00286   };
00287 }; // namespace CORBAManager
00288 
00289 /*
00290  * Register test suite
00291  */
00292 CPPUNIT_TEST_SUITE_REGISTRATION(CORBAManager::CORBAManagerTests);
00293 
00294 #ifdef LOCAL_MAIN
00295 int main(int argc, char* argv[])
00296 {
00297     CppUnit::TextUi::TestRunner runner;
00298     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00299     CppUnit::Outputter* outputter = 
00300       new CppUnit::TextOutputter(&runner.result(), std::cout);
00301     runner.setOutputter(outputter);
00302     bool retcode = runner.run();
00303     return !retcode;
00304 }
00305 #endif // MAIN
00306 #endif // CORBAManager_cpp


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