CORBAManagerTests.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
12 /*
13  * $Log$
14  *
15  */
16 
17 #ifndef CORBAManager_cpp
18 #define CORBAManager_cpp
19 
20 #include <cppunit/ui/text/TestRunner.h>
21 #include <cppunit/TextOutputter.h>
22 #include <cppunit/extensions/TestFactoryRegistry.h>
23 #include <cppunit/extensions/HelperMacros.h>
24 #include <cppunit/TestAssert.h>
27 #include <doil/ImplBase.h>
28 #include <iostream>
29 #include "Echo.hh"
30 
31 class EchoImpl
32  : public doil::ImplBase
33 {
34 public:
36  {
37  sprintf(m_name, "EchoSample%d", count);
38  ++count;
39  }
40  virtual ~EchoImpl()
41  {
42  std::cout << "EchoImpl: " << name() << " deleted." << std::endl;
43  }
44  const char* id() {return "EchoSample";}
45  const char* name() {return m_name;}
46  void incRef(){}
47  void decRef(){}
48  void echo(std::string msg)
49  {
50  std::cout << name() << " -> Message is: " << msg << std::endl;
51  return;
52  }
53  static int count;
54  char m_name[16];
55 };
56 int EchoImpl::count = 0;
57 
58 
60  : public virtual doil::CORBA::CORBAServantBase,
61  public virtual POA_EchoSample
62 {
63 public:
65  : doil::CORBA::CORBAServantBase(impl)
66  {
67  m_impl = dynamic_cast<EchoImpl*>(impl);
68  if (m_impl == NULL) throw std::bad_alloc();
69  }
70  virtual ~EchoServant()
71  {
72  std::cout << "EchoServant: " << name() << " deleted." << std::endl;
73  }
74  void echo(const char* msg)
75  {
76  m_impl->echo(msg);
77  }
78 private:
80 };
81 
82 
83 
88 namespace CORBAManager
89 {
91  : public CppUnit::TestFixture
92  {
93  CPPUNIT_TEST_SUITE(CORBAManagerTests);
94  CPPUNIT_TEST(test_init);
95  CPPUNIT_TEST(test_instance);
96  CPPUNIT_TEST(test_name);
97  CPPUNIT_TEST(test_case0);
98  CPPUNIT_TEST_SUITE_END();
99 
100  private:
101 
102  public:
103 
108  {
109  }
110 
115  {
116  }
117 
121  virtual void setUp()
122  {
123  }
124 
128  virtual void tearDown()
129  {
130  }
131 
132  /* test case */
133  void test_init()
134  {
137 
138  for (int i(0); i < 1000; ++i)
139  {
142  CPPUNIT_ASSERT_MESSAGE("CORBAManager's pointer should always be same",
143  mgr == tmp);
144  }
145  }
146 
148  {
151  for (int i(0); i < 1000; ++i)
152  {
155  CPPUNIT_ASSERT_MESSAGE("CORBAManager's pointer should always be same",
156  mgr == tmp);
157  }
158 
159  }
160  void test_name()
161  {
162  for (int i(100); i < 1000; ++i)
163  {
165  CPPUNIT_ASSERT_MESSAGE("CORBAManager's name should be corba",
166  name == "corba");
167  }
168  }
169 
170  // ごちゃまぜのテスト void test_case0() { doil::CORBA::CORBAManager& mgr(doil::CORBA::CORBAManager::instance()); // name() std::cout << mgr.name() << std::endl; // activateObject EchoImpl* eimpl = new EchoImpl(); doil::ReturnCode_t ret; ret = mgr.activateObject(eimpl); if (ret == doil::OK) std::cout << "register: OK" << std::endl; else if (ret == doil::NOT_FOUND) std::cout << "register: NOT_FOUND" << std::endl; else if (ret == doil::UNKNOWN) std::cout << "register: UNKNOWN" << std::endl; else std::cout << "other error" << std::endl; // regsiterFactory mgr.registerFactory(eimpl->id(), doil::New<EchoServant>, doil::Delete<EchoServant>); ret = mgr.activateObject(eimpl); if (ret == doil::OK) std::cout << "register: OK" << std::endl; else if (ret == doil::NOT_FOUND) std::cout << "register: NOT_FOUND" << std::endl; else if (ret == doil::UNKNOWN) std::cout << "register: UNKNOWN" << std::endl; else std::cout << "other error" << std::endl; // toServant doil::ServantBase* svt = mgr.toServant(eimpl); if (svt == NULL) { std::cout << "servant not found" << std::endl; return; } EchoServant* esvt = dynamic_cast<EchoServant*>(svt); if (esvt == NULL) { std::cout << "dynamic_cast failed" << std::endl; return; } esvt->echo("daradara"); // EchoServant* esvt = new EchoServant(eimpl); // mgr.activateObject(eimpl, esvt); // esvt->echo("hogehoge"); std::cout << "ID : " << esvt->id() << std::endl; std::cout << "name: " << esvt->name() << std::endl; // getORB/getPOA CORBA::ORB_ptr orb = mgr.getORB(); PortableServer::POA_ptr poa = mgr.getPOA(); CORBA::Object_ptr obj = poa->servant_to_reference(esvt); EchoSample_ptr eptr = EchoSample::_narrow(obj); eptr->echo("munyamunya"); // toImpl doil::ImplBase* tmpi = mgr.toImpl(eptr); if (tmpi == NULL) { std::cout << "not found impl" << std::endl; return; } EchoImpl* tmpe = dynamic_cast<EchoImpl*>(tmpi); tmpe->echo("gggggg"); printf("eimpl 0x%lx\n", (unsigned long int)eimpl); printf("tmpe 0x%lx\n", (unsigned long int)tmpe); std::cout << "objref from eimpl: " << orb->object_to_string(mgr.toReference(eimpl)) << std::endl; std::cout << "objref from obj : " << orb->object_to_string(obj) << std::endl; EchoImpl* eimpl2 = new EchoImpl(); mgr.activateObject(eimpl2); std::cout << "objref from eimpl2:" << orb->object_to_string(mgr.toReference(eimpl2)) << std::endl; std::string ior0, ior1; ior0 = orb->object_to_string(obj); ior1 = orb->object_to_string(mgr.toReference(eimpl2)); if (ior0 == ior1) std::cout << "same!!!!!!!!!!" << std::endl; else std::cout << "different" << std::endl; // mgr.deactivateObject(eimpl); for (int j(0); j < 100; ++j) { CORBA::Object_var tobj; if (j % 2 == 0) { tobj = orb->string_to_object(ior0.c_str()); } else { tobj = orb->string_to_object(ior1.c_str()); } EchoSample_var ec = EchoSample::_narrow(tobj); char msg[100]; sprintf(msg, "hoge%06d", j); ec->echo(msg); } std::cout << "deactivated" << std::endl; doil::ImplBase* dimpl = mgr.toImpl(svt); std::cout << "toImpl" << std::endl; if (dimpl == NULL) { std::cout << "not found" << std::endl; } mgr.shutdown(); } }; }; // namespace CORBAManager /* * Register test suite */ CPPUNIT_TEST_SUITE_REGISTRATION(CORBAManager::CORBAManagerTests); #ifdef LOCAL_MAIN int main(int argc, char* argv[]) { CppUnit::TextUi::TestRunner runner; runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest()); CppUnit::Outputter* outputter = new CppUnit::TextOutputter(&runner.result(), std::cout); runner.setOutputter(outputter); bool retcode = runner.run(); return !retcode; } #endif // MAIN #endif // CORBAManager_cpp
171  void test_case0()
172  {
174 
175  // name()
176  std::cout << mgr.name() << std::endl;
177 
178  // activateObject
179  EchoImpl* eimpl = new EchoImpl();
181  ret = mgr.activateObject(eimpl);
182  if (ret == doil::OK) std::cout << "register: OK" << std::endl;
183  else if (ret == doil::NOT_FOUND) std::cout << "register: NOT_FOUND" << std::endl;
184  else if (ret == doil::UNKNOWN) std::cout << "register: UNKNOWN" << std::endl;
185  else std::cout << "other error" << std::endl;
186 
187 
188  // regsiterFactory
189  mgr.registerFactory(eimpl->id(), doil::New<EchoServant>,
190  doil::Delete<EchoServant>);
191  ret = mgr.activateObject(eimpl);
192 
193  if (ret == doil::OK) std::cout << "register: OK" << std::endl;
194  else if (ret == doil::NOT_FOUND) std::cout << "register: NOT_FOUND" << std::endl;
195  else if (ret == doil::UNKNOWN) std::cout << "register: UNKNOWN" << std::endl;
196  else std::cout << "other error" << std::endl;
197 
198 
199  // toServant
200  doil::ServantBase* svt = mgr.toServant(eimpl);
201  if (svt == NULL)
202  {
203  std::cout << "servant not found" << std::endl;
204  return;
205  }
206  EchoServant* esvt = dynamic_cast<EchoServant*>(svt);
207  if (esvt == NULL)
208  {
209  std::cout << "dynamic_cast failed" << std::endl;
210  return;
211  }
212  esvt->echo("daradara");
213  // EchoServant* esvt = new EchoServant(eimpl);
214  // mgr.activateObject(eimpl, esvt);
215 
216  // esvt->echo("hogehoge");
217  std::cout << "ID : " << esvt->id() << std::endl;
218  std::cout << "name: " << esvt->name() << std::endl;
219 
220  // getORB/getPOA
221  CORBA::ORB_ptr orb = mgr.getORB();
222  PortableServer::POA_ptr poa = mgr.getPOA();
223 
224  CORBA::Object_ptr obj = poa->servant_to_reference(esvt);
225  EchoSample_ptr eptr = EchoSample::_narrow(obj);
226  eptr->echo("munyamunya");
227 
228  // toImpl
229  doil::ImplBase* tmpi = mgr.toImpl(eptr);
230  if (tmpi == NULL)
231  {
232  std::cout << "not found impl" << std::endl;
233  return;
234  }
235 
236  EchoImpl* tmpe = dynamic_cast<EchoImpl*>(tmpi);
237  tmpe->echo("gggggg");
238 
239  printf("eimpl 0x%lx\n", (unsigned long int)eimpl);
240  printf("tmpe 0x%lx\n", (unsigned long int)tmpe);
241 
242  std::cout << "objref from eimpl: " << orb->object_to_string(mgr.toReference(eimpl)) << std::endl;
243  std::cout << "objref from obj : " << orb->object_to_string(obj) << std::endl;
244 
245 
246 
247  EchoImpl* eimpl2 = new EchoImpl();
248  mgr.activateObject(eimpl2);
249  std::cout << "objref from eimpl2:" << orb->object_to_string(mgr.toReference(eimpl2)) << std::endl;
250 
251  std::string ior0, ior1;
252  ior0 = orb->object_to_string(obj);
253  ior1 = orb->object_to_string(mgr.toReference(eimpl2));
254 
255  if (ior0 == ior1) std::cout << "same!!!!!!!!!!" << std::endl;
256  else std::cout << "different" << std::endl;
257 
258  // mgr.deactivateObject(eimpl);
259  for (int j(0); j < 100; ++j)
260  {
261  CORBA::Object_var tobj;
262  if (j % 2 == 0)
263  {
264  tobj = orb->string_to_object(ior0.c_str());
265  }
266  else
267  {
268  tobj = orb->string_to_object(ior1.c_str());
269  }
270  EchoSample_var ec = EchoSample::_narrow(tobj);
271  char msg[100];
272  sprintf(msg, "hoge%06d", j);
273  ec->echo(msg);
274  }
275  std::cout << "deactivated" << std::endl;
276  doil::ImplBase* dimpl = mgr.toImpl(svt);
277  std::cout << "toImpl" << std::endl;
278  if (dimpl == NULL)
279  {
280  std::cout << "not found" << std::endl;
281  }
282 
283  mgr.shutdown();
284  }
285 
286  };
287 }; // namespace CORBAManager
288 
289 /*
290  * Register test suite
291  */
293 
294 #ifdef LOCAL_MAIN
295 int main(int argc, char* argv[])
296 {
297  CppUnit::TextUi::TestRunner runner;
298  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
299  CppUnit::Outputter* outputter =
300  new CppUnit::TextOutputter(&runner.result(), std::cout);
301  runner.setOutputter(outputter);
302  bool retcode = runner.run();
303  return !retcode;
304 }
305 #endif // MAIN
306 #endif // CORBAManager_cpp
int main(int argc, char **argv)
virtual const char * name()
Getting ORB&#39;s name.
void echo(std::string msg)
::PortableServer::POA_ptr getPOA()
Getting default POA pointer.
virtual ServantBase * toServant(doil::ImplBase *lobj)
Getting servant object by impl object.
virtual const char * name() const
virtual ReturnCode_t registerFactory(const char *id, doil::ServantNewFunc new_func, doil::ServantDeleteFunc delete_func)
Register servant&#39;s factory.
::CORBA::ORB_ptr getORB()
Getting ORB pointer.
char m_name[16]
ReturnCode_t
Definition: doil.h:53
virtual ImplBase * toImpl(doil::ServantBase *servant)
Getting impl object by servant.
static CORBAManager & instance()
getting instance
RTC::ReturnCode_t ret(RTC::Local::ReturnCode_t r)
EchoServant(doil::ImplBase *impl)
static int count
static CORBAManager * init(coil::Properties prop)
initializer
::CORBA::Object_ptr toReference(doil::ImplBase *impl)
Converting Impl object to object reference.
doil implementation base class
virtual ~EchoImpl()
CORBA manager for doil.
def j(str, encoding="cp932")
Definition: RunAtFirst.py:198
virtual void setUp()
Test initialization.
EchoImpl * m_impl
Class represents a set of properties.
Definition: Properties.h:101
virtual ~EchoServant()
std::string sprintf(char const *__restrict fmt,...)
Convert it into a format given with an argumen.
Definition: stringutil.cpp:593
const char * name()
const char * id()
CPPUNIT_TEST_SUITE_REGISTRATION(CORBAManager::CORBAManagerTests)
virtual void shutdown()
Shutdown ORB.
virtual const char * id() const
virtual void tearDown()
Test finalization.
Definition: doil.h:55
void echo(const char *msg)
virtual ReturnCode_t activateObject(doil::ImplBase *impl)
Activate object.


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Thu Jun 6 2019 19:25:58