rtm/tests/Factory/FactoryTests.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
21 /*
22  * $Log: FactoryTests.cpp,v $
23  * Revision 1.2 2008/05/02 12:30:29 arafune
24  * Modified some tests.
25  *
26  * Revision 1.1 2007/12/20 07:50:18 arafune
27  * *** empty log message ***
28  *
29  * Revision 1.2 2007/01/12 14:50:35 n-ando
30  * A trivial fix.
31  *
32  * Revision 1.1 2006/11/27 08:31:38 n-ando
33  * TestSuites are devided into each directory.
34  *
35  *
36  */
37 
38 #ifndef Factory_cpp
39 #define Factory_cpp
40 
41 #include <cppunit/ui/text/TestRunner.h>
42 #include <cppunit/TextOutputter.h>
43 #include <cppunit/extensions/TestFactoryRegistry.h>
44 #include <cppunit/extensions/HelperMacros.h>
45 #include <cppunit/TestAssert.h>
46 
47 #include <rtm/RTObject.h>
48 #include <rtm/Factory.h>
49 #include <rtm/Manager.h>
50 #include <coil/Properties.h>
51 
52 
53 namespace Tests
54 {
55  class Logger
56  {
57  public:
58  void log(const std::string& msg)
59  {
60  m_log.push_back(msg);
61  }
62 
63  int countLog(const std::string& line)
64  {
65  int count = 0;
66  for (int i = 0; i < (int) m_log.size(); ++i)
67  {
68  if (m_log[i] == line) ++count;
69  }
70  return count;
71  }
72 
73  private:
74  std::vector<std::string> m_log;
75  };
76 
78  : public virtual RTC::RTObject_impl
79  {
80  public:
81  RTObjectMock(CORBA::ORB_ptr orb, PortableServer::POA_ptr poa)
82  : RTC::RTObject_impl(orb, poa), m_logger(NULL)
83  {
84  }
85 
86  public: // helper for test
88  {
89  m_logger = logger;
90  }
91 
92  private:
94 
95  private:
96  void log(const std::string& msg)
97  {
98  if (m_logger != NULL) m_logger->log(msg);
99  }
100  };
101 
103  {
104  CORBA::ORB_ptr orb = manager->getORB();
105  PortableServer::POA_ptr poa = manager->getPOA();
106  return new RTObjectMock(orb, poa);
107  }
108 
110  {
111  if (rtc != NULL) rtc->_remove_ref();
112  }
113 
115  : public CppUnit::TestFixture
116  {
117  CPPUNIT_TEST_SUITE(FactoryTests);
118  CPPUNIT_TEST(test_create_and_destroy);
119  CPPUNIT_TEST(test_profile);
120  CPPUNIT_TEST(test_number);
121  CPPUNIT_TEST_SUITE_END();
122 
123  private:
125 
126  public:
131  {
132  }
133 
138  {
139  }
140 
144  virtual void setUp()
145  {
146  m_mgr = RTC::Manager::init(0, NULL);
147  }
148 
152  virtual void tearDown()
153  {
154  // m_mgr->terminate();
155  }
156 
165  {
166  coil::Properties properties;
167  properties.setProperty("name", "NAME");
168 
169  RTC::FactoryCXX factory(
171 
172  // 正常にコンポーネントを生成できるか?
173  RTC::RtcBase* rtc = factory.create(m_mgr);
174  CPPUNIT_ASSERT(rtc != NULL);
175 
176  RTObjectMock* mock = dynamic_cast<RTObjectMock*>(rtc);
177  CPPUNIT_ASSERT(mock != NULL);
178 
179  Logger logger;
180  mock->setLogger(&logger);
181 
182  // 生成されたコンポーネントには、正しくプロパティが設定されているか?
183  coil::Properties propertiesRet = rtc->getProperties();
184  CPPUNIT_ASSERT_EQUAL(std::string("NAME"), propertiesRet.getProperty("name"));
185 
186  // 正常にコンポーネントを破棄できるか?
187  factory.destroy(rtc);
188  }
189 
196  {
197  coil::Properties properties;
198  properties.setProperty("name", "NAME");
199 
200  RTC::FactoryCXX factory(
202 
203  // コンストラクタで指定したプロパティを取得できるか?
204  coil::Properties propertiesRet = factory.profile();
205  CPPUNIT_ASSERT_EQUAL(std::string("NAME"), propertiesRet.getProperty("name"));
206  }
207 
213  void test_number()
214  {
215 
216  coil::Properties properties;
217  properties.setProperty("name", "NAME");
218 
219  RTC::FactoryCXX factory(
221 
222  int MAX_NUM = 1;
223 
224  std::vector<RTC::RtcBase*> rtcList;
225  for (int i = 0; i < MAX_NUM; ++i)
226  {
227  // create()呼出前のインスタンス数は期待どおりか?
228  CPPUNIT_ASSERT_EQUAL(i-1, factory.number());
229 
230  // createする RTC::RtcBase* rtc = factory.create(m_mgr); CPPUNIT_ASSERT(rtc != NULL); // create()呼出後のインスタンス数は期待どおりか? CPPUNIT_ASSERT_EQUAL(i, factory.number()); rtcList.push_back(rtc); } for (int i = 0; i < MAX_NUM; ++i) { // destroy()呼出前のインスタンス数は期待どおりか? CPPUNIT_ASSERT_EQUAL(i, factory.number()); try { // destroyする factory.destroy(rtcList[i]); } catch (...) {} // destroy()呼出後のインスタンス数は期待どおりか? CPPUNIT_ASSERT_EQUAL(i-1, factory.number()); } } }; }; // namespace Factory /* * Register test suite */ CPPUNIT_TEST_SUITE_REGISTRATION(Tests::FactoryTests); #ifdef LOCAL_MAIN int main(int argc, char* argv[]) { FORMAT format = TEXT_OUT; int target = 0; std::string xsl; std::string ns; std::string fname; std::ofstream ofs; int i(1); while (i < argc) { std::string arg(argv[i]); std::string next_arg; if (i + 1 < argc) next_arg = argv[i + 1]; else next_arg = ""; if (arg == "--text") { format = TEXT_OUT; break; } if (arg == "--xml") { if (next_arg == "") { fname = argv[0]; fname += ".xml"; } else { fname = next_arg; } format = XML_OUT; ofs.open(fname.c_str()); } if ( arg == "--compiler" ) { format = COMPILER_OUT; break; } if ( arg == "--cerr" ) { target = 1; break; } if ( arg == "--xsl" ) { if (next_arg == "") xsl = "default.xsl"; else xsl = next_arg; } if ( arg == "--namespace" ) { if (next_arg == "") { std::cerr << "no namespace specified" << std::endl; exit(1); } else { xsl = next_arg; } } ++i; } CppUnit::TextUi::TestRunner runner; if ( ns.empty() ) runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest()); else runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest()); CppUnit::Outputter* outputter = 0; std::ostream* stream = target ? &std::cerr : &std::cout; switch ( format ) { case TEXT_OUT : outputter = new CppUnit::TextOutputter(&runner.result(),*stream); break; case XML_OUT : std::cout << "XML_OUT" << std::endl; outputter = new CppUnit::XmlOutputter(&runner.result(), ofs, "shift_jis"); static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl); break; case COMPILER_OUT : outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream); break; } runner.setOutputter(outputter); runner.run(); return 0; // runner.run() ? 0 : 1; } #endif // MAIN #endif // Factory_cpp
231  RTC::RtcBase* rtc = factory.create(m_mgr);
232  CPPUNIT_ASSERT(rtc != NULL);
233 
234  // create()呼出後のインスタンス数は期待どおりか?
235  CPPUNIT_ASSERT_EQUAL(i, factory.number());
236 
237  rtcList.push_back(rtc);
238  }
239 
240  for (int i = 0; i < MAX_NUM; ++i)
241  {
242  // destroy()呼出前のインスタンス数は期待どおりか?
243  CPPUNIT_ASSERT_EQUAL(i, factory.number());
244 
245  try {
246  // destroyする factory.destroy(rtcList[i]); } catch (...) {} // destroy()呼出後のインスタンス数は期待どおりか? CPPUNIT_ASSERT_EQUAL(i-1, factory.number()); } } }; }; // namespace Factory /* * Register test suite */ CPPUNIT_TEST_SUITE_REGISTRATION(Tests::FactoryTests); #ifdef LOCAL_MAIN int main(int argc, char* argv[]) { FORMAT format = TEXT_OUT; int target = 0; std::string xsl; std::string ns; std::string fname; std::ofstream ofs; int i(1); while (i < argc) { std::string arg(argv[i]); std::string next_arg; if (i + 1 < argc) next_arg = argv[i + 1]; else next_arg = ""; if (arg == "--text") { format = TEXT_OUT; break; } if (arg == "--xml") { if (next_arg == "") { fname = argv[0]; fname += ".xml"; } else { fname = next_arg; } format = XML_OUT; ofs.open(fname.c_str()); } if ( arg == "--compiler" ) { format = COMPILER_OUT; break; } if ( arg == "--cerr" ) { target = 1; break; } if ( arg == "--xsl" ) { if (next_arg == "") xsl = "default.xsl"; else xsl = next_arg; } if ( arg == "--namespace" ) { if (next_arg == "") { std::cerr << "no namespace specified" << std::endl; exit(1); } else { xsl = next_arg; } } ++i; } CppUnit::TextUi::TestRunner runner; if ( ns.empty() ) runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest()); else runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest()); CppUnit::Outputter* outputter = 0; std::ostream* stream = target ? &std::cerr : &std::cout; switch ( format ) { case TEXT_OUT : outputter = new CppUnit::TextOutputter(&runner.result(),*stream); break; case XML_OUT : std::cout << "XML_OUT" << std::endl; outputter = new CppUnit::XmlOutputter(&runner.result(), ofs, "shift_jis"); static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl); break; case COMPILER_OUT : outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream); break; } runner.setOutputter(outputter); runner.run(); return 0; // runner.run() ? 0 : 1; } #endif // MAIN #endif // Factory_cpp
247  factory.destroy(rtcList[i]);
248  }
249  catch (...) {}
250 
251  // destroy()呼出後のインスタンス数は期待どおりか?
252  CPPUNIT_ASSERT_EQUAL(i-1, factory.number());
253  }
254 
255  }
256 
257  };
258 }; // namespace Factory
259 
260 /*
261  * Register test suite
262  */
264 
265 #ifdef LOCAL_MAIN
266 int main(int argc, char* argv[])
267 {
268 
269  FORMAT format = TEXT_OUT;
270  int target = 0;
271  std::string xsl;
272  std::string ns;
273  std::string fname;
274  std::ofstream ofs;
275 
276  int i(1);
277  while (i < argc)
278  {
279  std::string arg(argv[i]);
280  std::string next_arg;
281  if (i + 1 < argc) next_arg = argv[i + 1];
282  else next_arg = "";
283 
284  if (arg == "--text") { format = TEXT_OUT; break; }
285  if (arg == "--xml")
286  {
287  if (next_arg == "")
288  {
289  fname = argv[0];
290  fname += ".xml";
291  }
292  else
293  {
294  fname = next_arg;
295  }
296  format = XML_OUT;
297  ofs.open(fname.c_str());
298  }
299  if ( arg == "--compiler" ) { format = COMPILER_OUT; break; }
300  if ( arg == "--cerr" ) { target = 1; break; }
301  if ( arg == "--xsl" )
302  {
303  if (next_arg == "") xsl = "default.xsl";
304  else xsl = next_arg;
305  }
306  if ( arg == "--namespace" )
307  {
308  if (next_arg == "")
309  {
310  std::cerr << "no namespace specified" << std::endl;
311  exit(1);
312  }
313  else
314  {
315  xsl = next_arg;
316  }
317  }
318  ++i;
319  }
320  CppUnit::TextUi::TestRunner runner;
321  if ( ns.empty() )
322  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
323  else
324  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
325  CppUnit::Outputter* outputter = 0;
326  std::ostream* stream = target ? &std::cerr : &std::cout;
327  switch ( format )
328  {
329  case TEXT_OUT :
330  outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
331  break;
332  case XML_OUT :
333  std::cout << "XML_OUT" << std::endl;
334  outputter = new CppUnit::XmlOutputter(&runner.result(),
335  ofs, "shift_jis");
336  static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
337  break;
338  case COMPILER_OUT :
339  outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
340  break;
341  }
342  runner.setOutputter(outputter);
343  runner.run();
344  return 0; // runner.run() ? 0 : 1;
345 }
346 #endif // MAIN
347 #endif // Factory_cpp
int main(int argc, char **argv)
RT-Component.
virtual coil::Properties & profile()
Get the component profile.
Definition: Factory.cpp:55
PortableServer::POA_ptr getPOA()
Get a pointer to RootPOA held by Manager.
Definition: Manager.cpp:969
void log(const std::string &msg)
virtual int number()
Get the number of current instances.
Definition: Factory.cpp:67
RT-Component class.
Definition: RTObject.h:89
std::vector< std::string > m_log
Manager class.
Definition: Manager.h:80
CORBA::ORB_ptr getORB()
Get the pointer to ORB.
Definition: Manager.cpp:957
coil::Properties & getProperties()
[local interface] Get RTC property
Definition: RTObject.cpp:1529
RTComponent manager class.
void DeleteRTObjectMock(RTC::RtcBase *rtc)
void test_create_and_destroy()
create()メソッドとdestroy()メソッドのテスト
RTObjectMock(CORBA::ORB_ptr orb, PortableServer::POA_ptr poa)
const std::string & getProperty(const std::string &key) const
Search for the property with the specified key in this property.
Definition: Properties.cpp:160
static Manager * init(int argc, char **argv)
Initialize manager.
Definition: Manager.cpp:110
std::string setProperty(const std::string &key, const std::string &value)
Set a value associated with key in the property list.
Definition: Properties.cpp:240
void test_profile()
profile()メソッドのテスト
int countLog(const std::string &line)
CPPUNIT_TEST_SUITE_REGISTRATION(Tests::FactoryTests)
Class represents a set of properties.
Definition: Properties.h:101
void test_number()
number()メソッドのテスト
::OutPortBase::Logger logger
virtual RTObject_impl * create(Manager *mgr)
Create RT-Components.
Definition: Factory.cpp:99
virtual void destroy(RTObject_impl *comp)
Destroy RT-Components.
Definition: Factory.cpp:129
FactoryCXX class.
Definition: rtm/Factory.h:286
RTC::RtcBase * CreateRTObjectMock(RTC::Manager *manager)
virtual void setUp()
Test initialization.
virtual void tearDown()
Test finalization.
void log(const std::string &msg)


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Mon Feb 28 2022 23:00:43