IceManagerTests.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
12 /*
13  * $Log$
14  *
15  */
16 
17 #ifndef IceManager_cpp
18 #define IceManager_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>
25 #include <doil/ice/IceManager.h>
27 #include <doil/ImplBase.h>
28 #include <iostream>
29 #include "EchoSample.h"
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 
59 class EchoServant
60  : public virtual Demo::EchoSample,
61  public virtual doil::Ice::IceServantBase
62 {
63 public:
65  : doil::Ice::IceServantBase(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  virtual void echo(const std::string& msg, const Ice::Current&)
75  {
76  std::cout << "servant" << name() << "::echo() was called" << std::endl;
77  m_impl->echo(msg);
78  }
79 private:
80  EchoImpl* m_impl;
81 };
82 
83 
84 
89 namespace IceManager
90 {
92  : public CppUnit::TestFixture
93  {
94  CPPUNIT_TEST_SUITE(IceManagerTests);
95  CPPUNIT_TEST(test_init);
96  CPPUNIT_TEST(test_instance);
97  CPPUNIT_TEST(test_name);
98  CPPUNIT_TEST(test_case0);
99  CPPUNIT_TEST_SUITE_END();
100 
101  private:
102 
103  public:
104 
109  {
110  }
111 
116  {
117  }
118 
122  virtual void setUp()
123  {
124  }
125 
129  virtual void tearDown()
130  {
131  }
132 
133  /* test case */
134  void test_init()
135  {
138 
139  for (int i(0); i < 1000; ++i)
140  {
143  CPPUNIT_ASSERT_MESSAGE("IceManager's pointer should always be same",
144  mgr == tmp);
145  }
146  }
147 
149  {
152  for (int i(0); i < 1000; ++i)
153  {
156  CPPUNIT_ASSERT_MESSAGE("IceManager's pointer should always be same",
157  mgr == tmp);
158  }
159 
160  }
161 
162  void test_name()
163  {
164  for (int i(100); i < 1000; ++i)
165  {
166  std::string name(doil::Ice::IceManager::instance().name());
167  CPPUNIT_ASSERT_MESSAGE("IceManager's name should be corba",
168  name == "ice");
169  }
170  }
171 
172 
173  /* test case */
174  void test_case0()
175  {
177 
178  // name()
179  std::cout << mgr.name() << std::endl;
180 
181  // activateObject
182  EchoImpl* eimpl = new EchoImpl();
184  ret = mgr.activateObject(eimpl);
185  if (ret == doil::OK) std::cout << "register: OK" << std::endl;
186  else if (ret == doil::NOT_FOUND) std::cout << "register: NOT_FOUND" << std::endl;
187  else if (ret == doil::UNKNOWN) std::cout << "register: UNKNOWN" << std::endl;
188  else std::cout << "other error" << std::endl;
189 
190 
191  // regsiterFactory
192  mgr.registerFactory(eimpl->id(), doil::New<EchoServant>,
193  doil::Delete<EchoServant>);
194  ret = mgr.activateObject(eimpl);
195 
196  if (ret == doil::OK) std::cout << "register: OK" << std::endl;
197  else if (ret == doil::NOT_FOUND) std::cout << "register: NOT_FOUND" << std::endl;
198  else if (ret == doil::UNKNOWN) std::cout << "register: UNKNOWN" << std::endl;
199  else std::cout << "other error" << std::endl;
200 
201 
202  // toServant
203  doil::ServantBase* svt = mgr.toServant(eimpl);
204  if (svt == NULL)
205  {
206  std::cout << "servant not found" << std::endl;
207  return;
208  }
209  EchoServant* esvt = dynamic_cast<EchoServant*>(svt);
210  if (esvt == NULL)
211  {
212  std::cout << "dynamic_cast failed" << std::endl;
213  return;
214  }
215  // esvt->echo("daradara");
216  // EchoServant* esvt = new EchoServant(eimpl);
217  // mgr.activateObject(eimpl, esvt);
218 
219  // esvt->echo("hogehoge");
220  std::cout << "ID : " << esvt->id() << std::endl;
221  std::cout << "name: " << esvt->name() << std::endl;
222 
223  // getORB/getPOA
224  Ice::CommunicatorPtr orb = mgr.getORB();
225  Ice::ObjectAdapterPtr adapter = mgr.getAdapter();
226 
227  Ice::ObjectPrx base = mgr.getORB()->stringToProxy("EchoSample0:default -p 10000");
228  std::cout << "Proxy base was created" << std::endl;
229  Demo::EchoSamplePrx eobj = Demo::EchoSamplePrx::checkedCast(base);
230  std::cout << "base was casted to EchoSample proxy" << std::endl;
231  if (!eobj)
232  {
233  std::cout << "Invalud Object reference" << std::endl;
234  throw "Invalid Proxy!";
235  }
236  eobj->echo("munyamunya");
237 
238 
239  // toImpl
240  doil::ImplBase* tmpi = mgr.toImpl(esvt);
241  if (tmpi == NULL)
242  {
243  std::cout << "not found impl" << std::endl;
244  return;
245  }
246 
247  EchoImpl* tmpe = dynamic_cast<EchoImpl*>(tmpi);
248  tmpe->echo("gggggg");
249 
250  printf("eimpl 0x%lx\n", (unsigned long int)eimpl);
251  printf("tmpe 0x%lx\n", (unsigned long int)tmpe);
252 
253 
254  EchoImpl* eimpl2 = new EchoImpl();
255  mgr.activateObject(eimpl2);
256 
257  base = mgr.getORB()->stringToProxy("EchoSample1:default -p 10000");
258  eobj = Demo::EchoSamplePrx::checkedCast(base);
259  eobj->echo("hogehogehoge");
260 
261  mgr.shutdown();
262  }
263  };
264 }; // namespace IceManager
265 
266 /*
267  * Register test suite
268  */
270 
271 #ifdef LOCAL_MAIN
272 int main(int argc, char* argv[])
273 {
274  CppUnit::TextUi::TestRunner runner;
275  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
276  CppUnit::Outputter* outputter =
277  new CppUnit::TextOutputter(&runner.result(), std::cout);
278  runner.setOutputter(outputter);
279  bool retcode = runner.run();
280  return !retcode;
281 }
282 #endif // MAIN
283 #endif // IceManager_cpp
int main(int argc, char **argv)
Ice manager for doil.
virtual ImplBase * toImpl(doil::ServantBase *servant)
Getting impl object by servant.
Definition: IceManager.cpp:275
void echo(std::string msg)
::IceInternal::ProxyHandle< ::IceProxy::Demo::EchoSample > EchoSamplePrx
Definition: EchoSample.h:87
virtual const char * name() const
char m_name[16]
ReturnCode_t
Definition: doil.h:53
RTC::ReturnCode_t ret(RTC::Local::ReturnCode_t r)
EchoServant(doil::ImplBase *impl)
static int count
const char * id()
CPPUNIT_TEST_SUITE_REGISTRATION(IceManager::IceManagerTests)
virtual void tearDown()
Test finalization.
doil implementation base class
virtual ReturnCode_t registerFactory(const char *id, doil::ServantNewFunc new_func, doil::ServantDeleteFunc delete_func)
Register servant&#39;s factory.
Definition: IceManager.cpp:115
virtual ~EchoImpl()
Doil Ice servant base class.
virtual void echo(const std::string &msg, const Ice::Current &)
::Ice::CommunicatorPtr getORB()
Getting ORB pointer.
Definition: IceManager.cpp:404
virtual void shutdown()
Shutdown ORB.
Definition: IceManager.cpp:91
const char * name()
Class represents a set of properties.
Definition: Properties.h:101
::Ice::ObjectAdapterPtr getAdapter()
Getting default POA pointer.
Definition: IceManager.cpp:417
virtual ~EchoServant()
virtual ServantBase * toServant(doil::ImplBase *lobj)
Getting servant object by impl object.
Definition: IceManager.cpp:312
std::string sprintf(char const *__restrict fmt,...)
Convert it into a format given with an argumen.
Definition: stringutil.cpp:593
static IceManager * init(coil::Properties prop)
initializer
Definition: IceManager.cpp:42
const char * name()
virtual void setUp()
Test initialization.
const char * id()
virtual const char * name()
Getting ORB&#39;s name.
Definition: IceManager.cpp:78
virtual const char * id() const
Definition: doil.h:55
static IceManager & instance()
getting instance
Definition: IceManager.cpp:64
virtual ReturnCode_t activateObject(doil::ImplBase *impl)
Activate object.
Definition: IceManager.cpp:138


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