00001
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef InPort_cpp
00039 #define InPort_cpp
00040
00041 #include <cppunit/ui/text/TestRunner.h>
00042 #include <cppunit/TextOutputter.h>
00043 #include <cppunit/extensions/TestFactoryRegistry.h>
00044 #include <cppunit/extensions/HelperMacros.h>
00045 #include <cppunit/TestAssert.h>
00046
00047 #include <rtm/idl/BasicDataTypeSkel.h>
00048 #include <rtm/idl/DataPortSkel.h>
00049 #include <rtm/InPortConsumer.h>
00050 #include <rtm/InPortProvider.h>
00051 #include <rtm/InPort.h>
00052 #include <rtm/InPortBase.h>
00053 #include <rtm/CorbaConsumer.h>
00054 #include <rtm/OutPortConsumer.h>
00055 #include <rtm/CdrBufferBase.h>
00056
00057 #include <string>
00058 #include <vector>
00059 #include <coil/Guard.h>
00060 #include <coil/Mutex.h>
00061 #include <rtm/idl/RTCSkel.h>
00062 #include <rtm/CORBA_SeqUtil.h>
00063 #include <rtm/NVUtil.h>
00064 #include <assert.h>
00065 #include <coil/UUID.h>
00066 #include <memory>
00067 #include <rtm/RTC.h>
00068
00069 #include <coil/Logger.h>
00070 #include <rtm/SystemLogger.h>
00071 #include <rtm/Manager.h>
00072 #include <rtm/PortCallback.h>
00073 #include <rtm/InPortPushConnector.h>
00074 #include <rtm/ConnectorBase.h>
00075 #include <rtm/OutPortConnector.h>
00076
00077 #define WTIMEOUT_USEC 1000000
00078 #define USEC_PER_SEC 1000000
00079
00084 namespace InPort
00085 {
00090 template <class DataType>
00091 class InPortMock
00092 : public RTC::InPort<DataType>
00093 {
00094 public:
00099 InPortMock(const char* name, DataType& value,
00100 int bufsize=64,
00101 bool read_block = false, bool write_block = false,
00102 int read_timeout = 0, int write_timeout = 0)
00103 : RTC::InPort<DataType>(name , value)
00104 {
00105 }
00110 virtual ~InPortMock()
00111 {
00112 }
00117 RTC::CdrBufferBase* get_m_thebuffer()
00118 {
00119 return RTC::InPort<DataType>::m_thebuffer;
00120 }
00125 coil::vstring get_m_providerTypes()
00126 {
00127 return RTC::InPort<DataType>::m_providerTypes;
00128 }
00133 coil::vstring get_m_consumerTypes()
00134 {
00135 return RTC::InPort<DataType>::m_consumerTypes;
00136 }
00141 RTC::InPortConnector*
00142 createConnector_public(RTC::ConnectorProfile& cprof,
00143 coil::Properties& prop,
00144 RTC::InPortProvider* provider)
00145 {
00146
00147 return RTC::InPortBase::createConnector(cprof, prop, provider);
00148 }
00153 RTC::InPortBase::ConnectorList get_m_connectors()
00154 {
00155 return RTC::InPort<DataType>::m_connectors;
00156 }
00161 coil::Properties get_m_properties()
00162 {
00163 return RTC::InPort<DataType>::m_properties;
00164 }
00165 };
00171 class Logger
00172 {
00173 public:
00174 void log(const std::string& msg)
00175 {
00176 m_log.push_back(msg);
00177 }
00178
00179 int countLog(const std::string& msg)
00180 {
00181 int count = 0;
00182 for (int i = 0; i < (int) m_log.size(); ++i)
00183 {
00184 if (m_log[i] == msg) ++count;
00185 }
00186 return count;
00187 }
00188
00189 void clearLog(void)
00190 {
00191 m_log.clear();
00192 }
00193 private:
00194 std::vector<std::string> m_log;
00195 };
00201 class InPortCorbaCdrProviderMock
00202 : public RTC::InPortProvider,
00203 public virtual POA_OpenRTM::InPortCdr,
00204 public virtual PortableServer::RefCountServantBase
00205 {
00206
00207 public:
00208 InPortCorbaCdrProviderMock(void)
00209 {
00210 m_logger = NULL;
00211
00212 setInterfaceType("corba_cdr");
00213
00214
00215 m_objref = this->_this();
00216
00217
00218 std::vector<std::string> args(coil::split("-ORBendPoint giop:tcp:2809", " "));
00219
00220 args.insert(args.begin(), "manager");
00221 char** argv = coil::toArgv(args);
00222 int argc(args.size());
00223
00224
00225 CORBA::ORB_ptr orb = CORBA::ORB_init(argc, argv);
00226
00227 CORBA_SeqUtil::
00228 push_back(m_properties,
00229 NVUtil::newNV("dataport.corba_cdr.inport_ior",
00230 orb->object_to_string(m_objref.in())));
00231 CORBA_SeqUtil::
00232 push_back(m_properties,
00233 NVUtil::newNV("dataport.corba_cdr.inport_ref",
00234 m_objref));
00235 }
00236 virtual ~InPortCorbaCdrProviderMock(void)
00237 {
00238 PortableServer::ObjectId_var oid;
00239 oid = _default_POA()->servant_to_id(this);
00240 _default_POA()->deactivate_object(oid);
00241 }
00246 void setBuffer(RTC::BufferBase<cdrMemoryStream>* buffer)
00247 {
00248 if (m_logger != NULL)
00249 {
00250 m_logger->log("InPortCorbaCdrProviderMock::setBuffer");
00251 }
00252 }
00253
00254 void setListener(RTC::ConnectorInfo& info,
00255 RTC::ConnectorListeners* listeners)
00256 {
00257
00258
00259 }
00260 void setConnector(RTC::InPortConnector* connector)
00261 {
00262
00263 }
00268 ::OpenRTM::PortStatus put(const ::OpenRTM::CdrData& data)
00269 throw (CORBA::SystemException)
00270 {
00271 return ::OpenRTM::PORT_OK;
00272 }
00277 void init(coil::Properties& prop)
00278 {
00279 if (m_logger != NULL)
00280 {
00281 m_logger->log("InPortCorbaCdrProviderMock::init");
00282 }
00283 }
00288 RTC::InPortConsumer::ReturnCode put(const cdrMemoryStream& data)
00289 {
00290 return RTC::InPortConsumer::PORT_OK;
00291 }
00296 void publishInterfaceProfile(SDOPackage::NVList& properties)
00297 {
00298 return;
00299 }
00300
00305 bool subscribeInterface(const SDOPackage::NVList& properties)
00306 {
00307 return true;;
00308 }
00309
00314 void unsubscribeInterface(const SDOPackage::NVList& properties)
00315 {
00316 }
00317
00318
00323
00324
00325
00326
00327
00328
00333 void setLogger(Logger* logger)
00334 {
00335 m_logger = logger;
00336 }
00337 private:
00338 Logger* m_logger;
00339 ::OpenRTM::InPortCdr_var m_objref;
00340
00341 };
00342 class OutPortCorbaCdrConsumerMock
00343 : public RTC::OutPortConsumer,
00344 public RTC::CorbaConsumer< ::OpenRTM::OutPortCdr >
00345 {
00346
00347 public:
00348 OutPortCorbaCdrConsumerMock(void)
00349 {
00350 m_logger = NULL;
00351 }
00352 virtual ~OutPortCorbaCdrConsumerMock(void)
00353 {
00354 }
00359 void setBuffer(RTC::BufferBase<cdrMemoryStream>* buffer)
00360 {
00361 if (m_logger != NULL)
00362 {
00363 m_logger->log("OutPortCorbaCdrConsumerMock::setBuffer");
00364 }
00365 }
00366 void setListener(RTC::ConnectorInfo& info,
00367 RTC::ConnectorListeners* listeners)
00368 {
00369
00370
00371 }
00372 void setConnector(RTC::OutPortConnector* connector)
00373 {
00374
00375 }
00376
00381 ::OpenRTM::PortStatus put(const ::OpenRTM::CdrData& data)
00382 throw (CORBA::SystemException)
00383 {
00384 return ::OpenRTM::PORT_OK;
00385 }
00390 void init(coil::Properties& prop)
00391 {
00392 if (m_logger != NULL)
00393 {
00394 m_logger->log("OutPortCorbaCdrConsumerMock::init");
00395 }
00396 }
00401 RTC::OutPortConsumer::ReturnCode put(const cdrMemoryStream& data)
00402 {
00403 return PORT_OK;
00404 }
00409 void publishInterfaceProfile(SDOPackage::NVList& properties)
00410 {
00411 return;
00412 }
00413
00418 bool subscribeInterface(const SDOPackage::NVList& properties)
00419 {
00420
00421 return true;;
00422 }
00423
00428 void unsubscribeInterface(const SDOPackage::NVList& properties)
00429 {
00430 }
00435 virtual ReturnCode get(cdrMemoryStream& data)
00436 {
00437 return PORT_OK;
00438 }
00439
00440
00445 void setLogger(Logger* logger)
00446 {
00447 m_logger = logger;
00448 }
00449 private:
00450 Logger* m_logger;
00451
00452 };
00458 template <class DataType>
00459 class RingBufferMock
00460 : public RTC::BufferBase<DataType>
00461 {
00462 public:
00463 BUFFERSTATUS_ENUM
00464 RingBufferMock(long int length = 8)
00465 {
00466 m_logger = NULL;
00467 logger.log("RingBufferMock::Constructor");
00468 m_read_return_value = BUFFER_OK;
00469 m_readable_return_value = 0;
00470
00471 }
00472 virtual ~RingBufferMock(void)
00473 {
00474 }
00475
00476
00481 void set_read_return_value(::RTC::BufferStatus::Enum value)
00482 {
00483 m_read_return_value = value;
00484 }
00489 virtual void init(const coil::Properties& prop)
00490 {
00491 }
00496 virtual size_t length(void) const
00497 {
00498 return 0;
00499 }
00504 virtual ReturnCode length(size_t n)
00505 {
00506 return ::RTC::BufferStatus::BUFFER_OK;
00507 }
00512 virtual ReturnCode reset()
00513 {
00514 return ::RTC::BufferStatus::BUFFER_OK;
00515 }
00520 virtual DataType* wptr(long int n = 0)
00521 {
00522 return &m_data;
00523 }
00528 virtual ReturnCode advanceWptr(long int n = 1)
00529 {
00530 return ::RTC::BufferStatus::BUFFER_OK;
00531 }
00536 virtual ReturnCode put(const DataType& value)
00537 {
00538 return ::RTC::BufferStatus::BUFFER_OK;
00539 }
00544 virtual ReturnCode write(const DataType& value,
00545 long int sec = -1, long int nsec = -1)
00546 {
00547 return ::RTC::BufferStatus::BUFFER_OK;
00548 }
00553 virtual size_t writable() const
00554 {
00555 return 0;
00556 }
00561 virtual bool full(void) const
00562 {
00563 return true;
00564 }
00569 virtual DataType* rptr(long int n = 0)
00570 {
00571 return &m_data;
00572 }
00577 virtual ReturnCode advanceRptr(long int n = 1)
00578 {
00579 return ::RTC::BufferStatus::BUFFER_OK;
00580 }
00585 virtual ReturnCode get(DataType& value)
00586 {
00587 return ::RTC::BufferStatus::BUFFER_OK;
00588 }
00593 virtual DataType& get()
00594 {
00595 return m_data;
00596 }
00601 virtual ReturnCode read(DataType& value,
00602 long int sec = -1, long int nsec = -1)
00603 {
00604 if (m_logger != NULL)
00605 {
00606 m_logger->log("RingBufferMock::read");
00607 }
00608 logger.log("RingBufferMock::read");
00609 return m_read_return_value;
00610 }
00615 virtual size_t readable() const
00616 {
00617 return m_readable_return_value;
00618 }
00623 void set_readable_return_value(size_t value)
00624 {
00625 m_readable_return_value = value;
00626 }
00631 virtual bool empty(void) const
00632 {
00633 return true;
00634 }
00639 void setLogger(Logger* logger)
00640 {
00641 m_logger = logger;
00642 }
00643
00644 static Logger logger;
00645 private:
00646 DataType m_data;
00647 std::vector<DataType> m_buffer;
00648 Logger* m_logger;
00649 ::RTC::BufferStatus::Enum m_read_return_value;
00650 size_t m_readable_return_value;
00651 };
00652 template <class DataType>
00653 Logger RingBufferMock<DataType>::logger;
00654 typedef RingBufferMock<cdrMemoryStream> CdrRingBufferMock;
00655 };
00656 namespace RTC
00657 {
00661 ::InPort::Logger RTC_logger;
00662 ConnectorBase::ReturnCode InPortPushConnector_read_return_value;
00663 int InPortPushConnector_read;
00673 InPortPushConnector::InPortPushConnector(ConnectorInfo profile,
00674 InPortProvider* provider,
00675 ConnectorListeners& listeners,
00676 CdrBufferBase* buffer)
00677 : InPortConnector(profile, buffer),
00678 m_listeners(listeners)
00679 {
00680
00681 InPortPushConnector_read = 0;
00682 InPortPushConnector_read_return_value = PORT_OK;
00683 if(profile.properties["InPortBaseTests"]=="bad_alloc")
00684 {
00685 throw std::bad_alloc();
00686 }
00687
00688 }
00693 InPortPushConnector::~InPortPushConnector()
00694 {
00695 }
00700 ConnectorBase::ReturnCode InPortPushConnector::disconnect()
00701 {
00702 return PORT_OK;
00703 }
00708 ConnectorBase::ReturnCode
00709 InPortPushConnector::read(cdrMemoryStream& data)
00710 {
00711 RTC_logger.log("InPortPushConnector::read");
00712 RTC::TimedLong td;
00713 td.data = 777;;
00714 td >>= data;
00715 return InPortPushConnector_read_return_value;
00716
00717
00718 }
00723 CdrBufferBase* InPortPushConnector::createBuffer(ConnectorInfo& profile)
00724 {
00725 return new ::InPort::CdrRingBufferMock();
00726 }
00734 Manager* Manager::manager = NULL;
00735 coil::Mutex Manager::mutex;
00740 Manager::Manager()
00741 : m_initProc(NULL),
00742 m_logStreamBuf(), rtclog(&m_logStreamBuf),
00743 m_runner(NULL), m_terminator(NULL)
00744 {
00745 }
00750 Manager& Manager::instance()
00751 {
00752
00753 if (!manager)
00754 {
00755 Guard guard(mutex);
00756 if (!manager)
00757 {
00758 manager = new Manager();
00759 manager->initORB();
00760 }
00761 }
00762 return *manager;
00763 }
00768 CORBA::ORB_ptr Manager::getORB()
00769 {
00770 return m_pORB;
00771 }
00776 bool Manager::initORB()
00777 {
00778
00779 try
00780 {
00781 std::vector<std::string> args(coil::split(NULL, " "));
00782
00783 args.insert(args.begin(), "manager");
00784 char** argv = coil::toArgv(args);
00785 int argc(args.size());
00786
00787
00788 m_pORB = CORBA::ORB_init(argc, argv);
00789
00790 CORBA::Object_var obj = m_pORB->resolve_initial_references("RootPOA");
00791 PortableServer::POA_var rootPOA = PortableServer::POA::_narrow(obj);
00792 CORBA::PolicyList policies;
00793 policies.length(2);
00794 policies[(CORBA::ULong)0] = rootPOA->create_lifespan_policy(PortableServer::PERSISTENT);
00795 policies[(CORBA::ULong)1] = rootPOA->create_id_assignment_policy(PortableServer::USER_ID);
00796 PortableServer::POAManager_var rootManager = rootPOA->the_POAManager();
00797 m_pPOA = rootPOA->create_POA("persistent", rootManager, policies);
00798 if (CORBA::is_nil(m_pPOA))
00799 {
00800 return false;
00801 }
00802
00803 m_pPOAManager = m_pPOA->the_POAManager();
00804 }
00805 catch (...)
00806 {
00807 RTC_ERROR(("Exception: Caught unknown exception in initORB()." ));
00808 return false;
00809 }
00810 return true;
00811 }
00812
00820 LogStreamBuf m_logStreamBuf;
00821 const char* Logger::m_levelString[] =
00822 {
00823 " SILENT: ",
00824 " FATAL: ",
00825 " ERROR: ",
00826 " WARNING: ",
00827 " INFO: ",
00828 " DEBUG: ",
00829 " TRACE: ",
00830 " VERBOSE: ",
00831 " PARANOID: "
00832 };
00833
00834 Logger::Logger(const char* name)
00835 : ::coil::LogStream(&m_logStreamBuf, 0, 8, 0)
00836 {
00837 }
00838
00839 Logger::Logger(LogStreamBuf* streambuf)
00840 : ::coil::LogStream(&m_logStreamBuf, 0, 8, 0)
00841 {
00842 }
00843
00844 Logger::~Logger(void)
00845 {
00846 }
00847
00855 bool Logger::setLevel(const char* level)
00856 {
00857 return true;
00858 }
00859
00867 void Logger::setDateFormat(const char* format)
00868 {
00869 }
00870
00878 void Logger::setName(const char* name)
00879 {
00880 }
00881
00889 void Logger::header(int level)
00890 {
00891 }
00892
00900 std::string Logger::getDate(void)
00901 {
00902 const int maxsize = 256;
00903 char buf[maxsize];
00904
00905 return std::string(buf);
00906 }
00907
00915 int Logger::strToLevel(const char* level)
00916 {
00917 return 0;
00918 }
00919
00920 #if 1
00921
00922
00923
00924
00925
00926
00927
00935 InPortProvider::InPortProvider()
00936 {
00937 }
00938
00946 InPortProvider::~InPortProvider()
00947 {
00948 }
00949
00957 void InPortProvider::publishInterfaceProfile(SDOPackage::NVList& prop)
00958 {
00959 }
00960
00968 bool InPortProvider::publishInterface(SDOPackage::NVList& prop)
00969 {
00970 return true;
00971 }
00972
00973
00974
00975
00983 void InPortProvider::setInterfaceType(const char* interface_type)
00984 {
00985 }
00986
00994 void InPortProvider::setDataFlowType(const char* dataflow_type)
00995 {
00996 }
00997
01005 void InPortProvider::setSubscriptionType(const char* subs_type)
01006 {
01007 }
01008 #endif
01009 };
01010 namespace InPort
01011 {
01016 template <class DataType>
01017 class OnReadMock
01018 : public RTC::OnRead<DataType>
01019 {
01020 public:
01025 OnReadMock(void)
01026 {
01027 m_logger = NULL;
01028 }
01033 virtual void operator()()
01034 {
01035 if (m_logger != NULL)
01036 {
01037 m_logger->log("OnReadMock::operator");
01038 }
01039
01040 }
01041 DataType m_value;
01046 void setLogger(Logger* logger)
01047 {
01048 m_logger = logger;
01049 }
01050 private:
01051 Logger* m_logger;
01052 };
01057 template <class DataType>
01058 struct OnReadConvertMock
01059 : public RTC::OnReadConvert<DataType>
01060 {
01065 OnReadConvertMock(void)
01066 {
01067 m_logger = NULL;
01068 }
01073 virtual DataType operator()(const DataType& value)
01074 {
01075 if (m_logger != NULL)
01076 {
01077 m_logger->log("OnReadConvertMock::operator");
01078 }
01079 return value;
01080 }
01085 void setLogger(Logger* logger)
01086 {
01087 m_logger = logger;
01088 }
01089 private:
01090 Logger* m_logger;
01091 };
01092
01093
01094
01095
01096
01097
01098
01099
01100
01101
01102
01103
01104
01105
01106 template <class DataType>
01107 class OnWriteMock
01108 : public RTC::OnWrite<DataType>
01109 {
01110 public:
01111 virtual void operator()(const DataType& value)
01112 {
01113 m_value = value;
01114 }
01115 DataType m_value;
01116 };
01117
01118
01119
01120
01121
01122
01123
01124
01125
01126
01127
01128
01129
01130
01131
01132
01133
01134
01135
01136
01137
01138
01139
01140
01141
01142
01143
01144
01145
01150 class InPortTests
01151 : public CppUnit::TestFixture
01152 {
01153 CPPUNIT_TEST_SUITE(InPortTests);
01154
01155 CPPUNIT_TEST(test_name);
01156 CPPUNIT_TEST(test_isNew);
01157 CPPUNIT_TEST(test_isEmpty);
01158 CPPUNIT_TEST(test_read);
01159
01160
01161
01162
01163
01164
01165
01166
01167
01168
01169
01170
01171
01172 CPPUNIT_TEST_SUITE_END();
01173
01174 private:
01175 CORBA::ORB_ptr m_pORB;
01176 PortableServer::POA_ptr m_pPOA;
01177 Logger* m_logger;
01178
01179
01180 public:
01181
01185 InPortTests()
01186 {
01187 int argc(0);
01188 char** argv(NULL);
01189 m_pORB = CORBA::ORB_init(argc, argv);
01190 m_pPOA = PortableServer::POA::_narrow(
01191 m_pORB->resolve_initial_references("RootPOA"));
01192 m_pPOA->the_POAManager()->activate();
01193 m_logger = NULL;
01194 }
01195
01199 ~InPortTests()
01200 {
01201 }
01202
01206 virtual void setUp()
01207 {
01208
01209 if( RTC::InPortProviderFactory::instance().hasFactory("corba_cdr") )
01210 {
01211 RTC::InPortProviderFactory::instance().removeFactory("corba_cdr");
01212 }
01213
01214 RTC::InPortProviderFactory::instance().
01215 addFactory("corba_cdr",
01216 ::coil::Creator< ::RTC::InPortProvider,
01217 InPortCorbaCdrProviderMock>,
01218 ::coil::Destructor< ::RTC::InPortProvider,
01219 InPortCorbaCdrProviderMock>);
01220
01221
01222 if( RTC::OutPortConsumerFactory::instance().hasFactory("corba_cdr") )
01223 {
01224 RTC::OutPortConsumerFactory::instance().removeFactory("corba_cdr");
01225 }
01226
01227 RTC::OutPortConsumerFactory::instance().
01228 addFactory("corba_cdr",
01229 ::coil::Creator< ::RTC::OutPortConsumer,
01230 OutPortCorbaCdrConsumerMock>,
01231 ::coil::Destructor< ::RTC::OutPortConsumer,
01232 OutPortCorbaCdrConsumerMock>);
01233
01234
01235 if( RTC::CdrBufferFactory::instance().hasFactory("ring_buffer") )
01236 {
01237 RTC::CdrBufferFactory::instance().removeFactory("ring_buffer");
01238 }
01239
01240 RTC::CdrBufferFactory::instance().
01241 addFactory("ring_buffer",
01242 coil::Creator<RTC::CdrBufferBase, CdrRingBufferMock>,
01243 coil::Destructor<RTC::CdrBufferBase, CdrRingBufferMock>);
01244
01245 }
01246
01250 virtual void tearDown()
01251 {
01252 }
01257 void test_name(void)
01258 {
01259 RTC::TimedLong tl;
01260 RTC::InPort<RTC::TimedLong> inport("test_name0",tl);
01261 CPPUNIT_ASSERT_EQUAL(std::string("test_name0"),
01262 std::string(inport.name()));
01263
01264 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(&inport));
01265 }
01266
01271 void test_isNew(void)
01272 {
01273 RTC::TimedLong tl;
01274 InPortMock<RTC::TimedLong> inport("test_name0",tl);
01275 coil::Properties prop_;
01276 inport.init(prop_);
01277
01278
01279 CPPUNIT_ASSERT_EQUAL(false, inport.isNew());
01280
01281 RTC::ConnectorProfile prof;
01282 coil::Properties prop(inport.properties());
01283 RTC::InPortProvider* provider = new InPortCorbaCdrProviderMock();
01284 RTC::InPortConnector* connector
01285 = inport.createConnector_public(prof, prop, provider);
01286 CPPUNIT_ASSERT(0!= connector);
01287
01288 CPPUNIT_ASSERT_EQUAL(false, inport.isNew());
01289
01290
01291
01292 CdrRingBufferMock* pbuffer
01293 = dynamic_cast<CdrRingBufferMock*>(inport.get_m_thebuffer());
01294
01295 CPPUNIT_ASSERT(0!= pbuffer);
01296
01297 CPPUNIT_ASSERT_EQUAL(false, inport.isNew());
01298 pbuffer->set_readable_return_value((size_t)5);
01299
01300 CPPUNIT_ASSERT_EQUAL(true, inport.isNew());
01301
01302
01303 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(&inport));
01304 delete provider;
01305 }
01310 void test_isEmpty(void)
01311 {
01312 RTC::TimedLong tl;
01313 InPortMock<RTC::TimedLong> inport("test_name0",tl);
01314 coil::Properties prop_;
01315 inport.init(prop_);
01316
01317
01318 CPPUNIT_ASSERT_EQUAL(true, inport.isEmpty());
01319
01320 RTC::ConnectorProfile prof;
01321 coil::Properties prop(inport.properties());
01322 RTC::InPortProvider* provider = new InPortCorbaCdrProviderMock();
01323 RTC::InPortConnector* connector
01324 = inport.createConnector_public(prof, prop, provider);
01325 CPPUNIT_ASSERT(0!= connector);
01326
01327
01328 CPPUNIT_ASSERT_EQUAL(true, inport.isEmpty());
01329
01330
01331
01332 CdrRingBufferMock* pbuffer
01333 = dynamic_cast<CdrRingBufferMock*>(inport.get_m_thebuffer());
01334
01335 CPPUNIT_ASSERT(0!= pbuffer);
01336
01337 CPPUNIT_ASSERT_EQUAL(true, inport.isEmpty());
01338 pbuffer->set_readable_return_value((size_t)5);
01339
01340
01341 CPPUNIT_ASSERT_EQUAL(false, inport.isEmpty());
01342
01343
01344 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(&inport));
01345 delete provider;
01346 }
01351 void test_read(void)
01352 {
01353 RTC::TimedLong tl;
01354 tl.data = 123;
01355 InPortMock<RTC::TimedLong> inport("test_read0",tl);
01356 coil::Properties prop_;
01357 inport.init(prop_);
01358
01359 OnReadMock<RTC::TimedLong> onRead;
01360 Logger logger;
01361 onRead.setLogger(&logger);
01362 inport.setOnRead(&onRead);
01363
01364 bool ret;
01365 CPPUNIT_ASSERT_EQUAL(0,logger.countLog("OnReadMock::operator"));
01366 ret = inport.read();
01367
01368 CPPUNIT_ASSERT_EQUAL(false, ret);
01369 CPPUNIT_ASSERT_EQUAL((CORBA::Long)123, tl.data);
01370 CPPUNIT_ASSERT_EQUAL(1,logger.countLog("OnReadMock::operator"));
01371
01372 RTC::ConnectorProfile prof;
01373 coil::Properties prop(inport.properties());
01374 RTC::InPortProvider* provider = new InPortCorbaCdrProviderMock();
01375 RTC::InPortConnector* connector
01376 = inport.createConnector_public(prof, prop, provider);
01377 CPPUNIT_ASSERT(0!= connector);
01378
01379
01380
01381 OnReadConvertMock<RTC::TimedLong> onReadConvert;
01382 onReadConvert.setLogger(&logger);
01383
01384 RTC::InPortPushConnector_read_return_value
01385 = RTC::ConnectorBase::PORT_OK;
01386 RTC::TimedLong tl_;
01387 inport.operator>>(tl_);
01388 CPPUNIT_ASSERT_EQUAL((CORBA::Long)777, tl_.data);
01389
01390 int logcount;
01391 CPPUNIT_ASSERT_EQUAL(0,logger.countLog("OnReadConvertMock::operator"));
01392 logcount = ::RTC::RTC_logger.countLog("InPortPushConnector::read");
01393 ret = inport.read();
01394
01395 CPPUNIT_ASSERT_EQUAL(true, ret);
01396 CPPUNIT_ASSERT_EQUAL((CORBA::Long)777, tl.data);
01397 CPPUNIT_ASSERT_EQUAL(logcount+1,
01398 ::RTC::RTC_logger.countLog("InPortPushConnector::read"));
01399 CPPUNIT_ASSERT_EQUAL(0,logger.countLog("OnReadConvertMock::operator"));
01400
01401 inport.setOnReadConvert(&onReadConvert);
01402 CPPUNIT_ASSERT_EQUAL(0,logger.countLog("OnReadConvertMock::operator"));
01403 logcount = ::RTC::RTC_logger.countLog("InPortPushConnector::read");
01404 ret = inport.read();
01405
01406 CPPUNIT_ASSERT_EQUAL(true, ret);
01407 CPPUNIT_ASSERT_EQUAL((CORBA::Long)777, tl.data);
01408 CPPUNIT_ASSERT_EQUAL(logcount+1,
01409 ::RTC::RTC_logger.countLog("InPortPushConnector::read"));
01410 CPPUNIT_ASSERT_EQUAL(1,logger.countLog("OnReadConvertMock::operator"));
01411
01412
01413
01414
01415 RTC::InPortPushConnector_read_return_value
01416 = RTC::ConnectorBase::PORT_OK;
01417 inport.setOnRead(&onRead);
01418
01419 logcount = ::RTC::RTC_logger.countLog("InPortPushConnector::read");
01420 ret = inport.read();
01421
01422 CPPUNIT_ASSERT_EQUAL(true, ret);
01423 CPPUNIT_ASSERT_EQUAL((CORBA::Long)777, tl.data);
01424 CPPUNIT_ASSERT_EQUAL(logcount+1,
01425 ::RTC::RTC_logger.countLog("InPortPushConnector::read"));
01426
01427
01428
01429 RTC::InPortPushConnector_read_return_value
01430 = RTC::ConnectorBase::BUFFER_EMPTY;
01431 logcount = ::RTC::RTC_logger.countLog("InPortPushConnector::read");
01432 ret = inport.read();
01433
01434 CPPUNIT_ASSERT_EQUAL(false, ret);
01435 CPPUNIT_ASSERT_EQUAL((CORBA::Long)777, tl.data);
01436 CPPUNIT_ASSERT_EQUAL(logcount+1,
01437 ::RTC::RTC_logger.countLog("InPortPushConnector::read"));
01438
01439
01440
01441
01442 RTC::InPortPushConnector_read_return_value
01443 = RTC::ConnectorBase::BUFFER_TIMEOUT;
01444 logcount = ::RTC::RTC_logger.countLog("InPortPushConnector::read");
01445 ret = inport.read();
01446
01447 CPPUNIT_ASSERT_EQUAL(false, ret);
01448 CPPUNIT_ASSERT_EQUAL((CORBA::Long)777, tl.data);
01449 CPPUNIT_ASSERT_EQUAL(logcount+1,
01450 ::RTC::RTC_logger.countLog("InPortPushConnector::read"));
01451
01452
01453
01454
01455 RTC::InPortPushConnector_read_return_value
01456 = RTC::ConnectorBase::UNKNOWN_ERROR;
01457 logcount = ::RTC::RTC_logger.countLog("InPortPushConnector::read");
01458 ret = inport.read();
01459
01460 CPPUNIT_ASSERT_EQUAL(false, ret);
01461 CPPUNIT_ASSERT_EQUAL((CORBA::Long)777, tl.data);
01462 CPPUNIT_ASSERT_EQUAL(logcount+1,
01463 ::RTC::RTC_logger.countLog("InPortPushConnector::read"));
01464
01465 m_pPOA->deactivate_object(*m_pPOA->servant_to_id(&inport));
01466 delete provider;
01467 }
01472 void test_setOnWrite(void)
01473 {
01474 }
01479 void test_setOnUnderflow()
01480 {
01481 }
01482
01488
01489
01490
01491
01492
01493
01494
01495
01496
01497
01498
01499
01500
01501
01502
01503
01504
01505
01506
01512
01513
01514
01515
01516
01517
01518
01519
01520
01521
01522
01523
01524
01525
01526
01527
01528
01529
01530
01531
01537
01538
01539
01540
01541
01542
01543
01544
01545
01546
01547
01548
01549
01550
01551
01552
01553
01554
01555
01556
01562
01563
01564
01565
01566
01567
01568
01569
01570
01571
01572
01573
01574
01575
01576
01577
01578
01579
01580
01581
01582
01588
01589
01590
01591
01592
01593
01594
01595
01596
01597
01598
01599
01600
01601
01602
01603
01604
01605
01606
01607
01613
01614
01615
01616
01617
01618
01619
01620
01621
01622
01623
01624
01625
01626
01627
01628
01629
01630
01631
01632
01633
01634
01635
01636
01637
01644
01645
01646
01647
01648
01649
01650
01651
01652
01653
01654
01655
01656
01657
01658
01659
01660
01661
01662
01663
01664
01665
01666
01667
01668
01669
01670
01671
01672
01673
01674
01675
01676
01677
01678
01679
01680
01681
01682
01683
01684
01685
01686
01687 };
01688 };
01689
01690
01691
01692
01693 CPPUNIT_TEST_SUITE_REGISTRATION(InPort::InPortTests);
01694
01695 #ifdef LOCAL_MAIN
01696 int main(int argc, char* argv[])
01697 {
01698
01699 FORMAT format = TEXT_OUT;
01700 int target = 0;
01701 std::string xsl;
01702 std::string ns;
01703 std::string fname;
01704 std::ofstream ofs;
01705
01706 int i(1);
01707 while (i < argc)
01708 {
01709 std::string arg(argv[i]);
01710 std::string next_arg;
01711 if (i + 1 < argc) next_arg = argv[i + 1];
01712 else next_arg = "";
01713
01714 if (arg == "--text") { format = TEXT_OUT; break; }
01715 if (arg == "--xml")
01716 {
01717 if (next_arg == "")
01718 {
01719 fname = argv[0];
01720 fname += ".xml";
01721 }
01722 else
01723 {
01724 fname = next_arg;
01725 }
01726 format = XML_OUT;
01727 ofs.open(fname.c_str());
01728 }
01729 if ( arg == "--compiler" ) { format = COMPILER_OUT; break; }
01730 if ( arg == "--cerr" ) { target = 1; break; }
01731 if ( arg == "--xsl" )
01732 {
01733 if (next_arg == "") xsl = "default.xsl";
01734 else xsl = next_arg;
01735 }
01736 if ( arg == "--namespace" )
01737 {
01738 if (next_arg == "")
01739 {
01740 std::cerr << "no namespace specified" << std::endl;
01741 exit(1);
01742 }
01743 else
01744 {
01745 xsl = next_arg;
01746 }
01747 }
01748 ++i;
01749 }
01750 CppUnit::TextUi::TestRunner runner;
01751 if ( ns.empty() )
01752 runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
01753 else
01754 runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
01755 CppUnit::Outputter* outputter = 0;
01756 std::ostream* stream = target ? &std::cerr : &std::cout;
01757 switch ( format )
01758 {
01759 case TEXT_OUT :
01760 outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
01761 break;
01762 case XML_OUT :
01763 std::cout << "XML_OUT" << std::endl;
01764 outputter = new CppUnit::XmlOutputter(&runner.result(),
01765 ofs, "shift_jis");
01766 static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
01767 break;
01768 case COMPILER_OUT :
01769 outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
01770 break;
01771 }
01772 runner.setOutputter(outputter);
01773 runner.run();
01774 return 0;
01775 }
01776 #endif // MAIN
01777 #endif // InPort_cpp