IceManagerTests.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00012 /*
00013  * $Log$
00014  *
00015  */
00016 
00017 #ifndef IceManager_cpp
00018 #define IceManager_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/ice/IceManager.h>
00026 #include <doil/ice/IceServantBase.h>
00027 #include <doil/ImplBase.h>
00028 #include <iostream>
00029 #include "EchoSample.h"
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 Demo::EchoSample,
00061     public virtual doil::Ice::IceServantBase
00062 {
00063 public:
00064   EchoServant(doil::ImplBase* impl)
00065     : doil::Ice::IceServantBase(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   virtual void echo(const std::string& msg, const Ice::Current&)
00075   {
00076     std::cout << "servant" << name() << "::echo()  was called" << std::endl;
00077     m_impl->echo(msg);
00078   }
00079 private:
00080   EchoImpl* m_impl;
00081 };
00082 
00083 
00084 
00089 namespace IceManager
00090 {
00091   class IceManagerTests
00092    : public CppUnit::TestFixture
00093   {
00094     CPPUNIT_TEST_SUITE(IceManagerTests);
00095     CPPUNIT_TEST(test_init);
00096     CPPUNIT_TEST(test_instance);
00097     CPPUNIT_TEST(test_name);
00098     CPPUNIT_TEST(test_case0);
00099     CPPUNIT_TEST_SUITE_END();
00100   
00101   private:
00102   
00103   public:
00104   
00108     IceManagerTests()
00109     {
00110     }
00111     
00115     ~IceManagerTests()
00116     {
00117     }
00118   
00122     virtual void setUp()
00123     {
00124     }
00125     
00129     virtual void tearDown()
00130     { 
00131     }
00132   
00133     /* test case */
00134     void test_init()
00135     {
00136       doil::Ice::IceManager* mgr;
00137       mgr = doil::Ice::IceManager::init(coil::Properties());
00138 
00139       for (int i(0); i < 1000; ++i)
00140         {
00141           doil::Ice::IceManager* tmp;
00142           tmp = doil::Ice::IceManager::init(coil::Properties());
00143           CPPUNIT_ASSERT_MESSAGE("IceManager's pointer should always be same",
00144                                  mgr == tmp);
00145         }
00146     }
00147 
00148     void test_instance()
00149     {
00150       doil::Ice::IceManager* mgr;
00151       mgr = &(doil::Ice::IceManager::instance());
00152       for (int i(0); i < 1000; ++i)
00153         {
00154           doil::Ice::IceManager* tmp;
00155           tmp = &(doil::Ice::IceManager::instance());
00156           CPPUNIT_ASSERT_MESSAGE("IceManager's pointer should always be same",
00157                                  mgr == tmp);
00158         }
00159       
00160     }
00161 
00162     void test_name()
00163     {
00164       for (int i(100); i < 1000; ++i)
00165         {
00166           std::string name(doil::Ice::IceManager::instance().name());
00167           CPPUNIT_ASSERT_MESSAGE("IceManager's name should be corba",
00168                                  name == "ice");
00169         }
00170     }
00171 
00172 
00173     /* test case */
00174     void test_case0()
00175     {
00176       doil::Ice::IceManager& mgr(doil::Ice::IceManager::instance());
00177       
00178       // name()
00179       std::cout << mgr.name() << std::endl;
00180 
00181       // activateObject
00182       EchoImpl* eimpl = new EchoImpl();
00183       doil::ReturnCode_t ret;
00184       ret = mgr.activateObject(eimpl);
00185       if (ret == doil::OK) std::cout << "register: OK" << std::endl;
00186       else if (ret == doil::NOT_FOUND) std::cout << "register: NOT_FOUND" << std::endl;
00187       else if (ret == doil::UNKNOWN) std::cout << "register: UNKNOWN" << std::endl;
00188       else std::cout << "other error" << std::endl;
00189 
00190 
00191       // regsiterFactory
00192       mgr.registerFactory(eimpl->id(), doil::New<EchoServant>,
00193                           doil::Delete<EchoServant>);
00194       ret = mgr.activateObject(eimpl);
00195       
00196       if (ret == doil::OK) std::cout << "register: OK" << std::endl;
00197       else if (ret == doil::NOT_FOUND) std::cout << "register: NOT_FOUND" << std::endl;
00198       else if (ret == doil::UNKNOWN) std::cout << "register: UNKNOWN" << std::endl;
00199       else std::cout << "other error" << std::endl;
00200 
00201 
00202       // toServant
00203       doil::ServantBase* svt = mgr.toServant(eimpl);
00204       if (svt == NULL)
00205         {
00206           std::cout << "servant not found" << std::endl;
00207           return;
00208         }
00209       EchoServant* esvt = dynamic_cast<EchoServant*>(svt);
00210       if (esvt == NULL) 
00211         {
00212           std::cout << "dynamic_cast failed" << std::endl;
00213       return;
00214         }
00215       //      esvt->echo("daradara");
00216       //  EchoServant* esvt = new EchoServant(eimpl);
00217       //  mgr.activateObject(eimpl, esvt);
00218       
00219       //  esvt->echo("hogehoge");
00220       std::cout << "ID  : " << esvt->id() << std::endl;
00221       std::cout << "name: " << esvt->name() << std::endl;
00222       
00223       // getORB/getPOA
00224       Ice::CommunicatorPtr orb = mgr.getORB();
00225       Ice::ObjectAdapterPtr adapter = mgr.getAdapter();
00226       
00227       Ice::ObjectPrx base = mgr.getORB()->stringToProxy("EchoSample0:default -p 10000");
00228       std::cout << "Proxy base was created" << std::endl;
00229       Demo::EchoSamplePrx eobj = Demo::EchoSamplePrx::checkedCast(base);
00230       std::cout << "base was casted to EchoSample proxy" << std::endl;
00231       if (!eobj)
00232         {
00233           std::cout << "Invalud Object reference" << std::endl;
00234           throw "Invalid Proxy!";
00235         }
00236       eobj->echo("munyamunya");
00237 
00238 
00239       // toImpl
00240       doil::ImplBase* tmpi = mgr.toImpl(esvt);
00241       if (tmpi == NULL)
00242         {
00243           std::cout << "not found impl" << std::endl;
00244           return;
00245         }
00246       
00247       EchoImpl* tmpe = dynamic_cast<EchoImpl*>(tmpi);
00248       tmpe->echo("gggggg");
00249       
00250       printf("eimpl 0x%lx\n", (unsigned long int)eimpl);
00251       printf("tmpe  0x%lx\n", (unsigned long int)tmpe);
00252 
00253       
00254       EchoImpl* eimpl2 = new EchoImpl();
00255       mgr.activateObject(eimpl2);
00256 
00257       base = mgr.getORB()->stringToProxy("EchoSample1:default -p 10000");
00258       eobj = Demo::EchoSamplePrx::checkedCast(base);
00259       eobj->echo("hogehogehoge");
00260       
00261       mgr.shutdown();
00262     }
00263   };
00264 }; // namespace IceManager
00265 
00266 /*
00267  * Register test suite
00268  */
00269 CPPUNIT_TEST_SUITE_REGISTRATION(IceManager::IceManagerTests);
00270 
00271 #ifdef LOCAL_MAIN
00272 int main(int argc, char* argv[])
00273 {
00274     CppUnit::TextUi::TestRunner runner;
00275     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00276     CppUnit::Outputter* outputter = 
00277       new CppUnit::TextOutputter(&runner.result(), std::cout);
00278     runner.setOutputter(outputter);
00279     bool retcode = runner.run();
00280     return !retcode;
00281 }
00282 #endif // MAIN
00283 #endif // IceManager_cpp


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