From b20611964dce5316fabb1122114951d7040734e1 Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Thu, 15 Dec 2011 20:54:20 +0100 Subject: [PATCH] Unittest for Message --- include/Message.hpp | 6 +- test/test_Message.hpp | 182 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 185 insertions(+), 3 deletions(-) create mode 100644 test/test_Message.hpp diff --git a/include/Message.hpp b/include/Message.hpp index 62f2df9..4a31ec6 100644 --- a/include/Message.hpp +++ b/include/Message.hpp @@ -20,7 +20,7 @@ class Message public: Message( Connection *connection, - void *msgParam = 0 ) + void *msgParam = 0 ) : m_connection(connection) , m_param(msgParam) , m_buffer() @@ -55,8 +55,8 @@ protected: Connection *m_connection; - void *m_param; - std::string m_buffer; + void *m_param; + std::string m_buffer; private: diff --git a/test/test_Message.hpp b/test/test_Message.hpp new file mode 100644 index 0000000..de58972 --- /dev/null +++ b/test/test_Message.hpp @@ -0,0 +1,182 @@ +#include + +#include "Fixture.hpp" + +#include "Message.hpp" +#include "Connection.hpp" + +class TestMessage : public CxxTest::TestSuite +{ +private: + + class SimpleMessage : public Message + { + public: + + SimpleMessage( void *msgParam = 0) + : Message(msgParam) + { + TRACE; + } + + bool buildMessage( const void *msgPart, + const size_t msgLen ) + { + TRACE; + m_buffer = std::string( (const char*) msgPart, msgLen ); + onMessageReady(); + return true; + } + + void onMessageReady() + { + TRACE; + + LOG_BEGIN(Logger::INFO) + LOG_PROP("message", m_buffer) + LOG_END("Got reply from server."); + + *( static_cast(m_param) ) = true; + + if (m_connection != 0) + m_connection->send(m_buffer.c_str(), m_buffer.length()); + } + + Message* clone() + { + TRACE; + return new SimpleMessage(m_param); + } + + std::string getBuffer() + { + TRACE; + return m_buffer; + } + + protected: + + size_t getExpectedLength() + { + TRACE; + return 0; + } + + }; // SimpleMessage + + + class DummyConnection : public Connection + { + public: + + DummyConnection(const std::string host, + const std::string port, + Message *message) + : Connection(host, port) + , m_message(message) + , m_receiveBuffer() + , m_sendBuffer() + { + TRACE; + } + + virtual ~DummyConnection() + { + TRACE; + } + + Connection* clone(const int) { return 0; } + bool bind() { return true; } + bool send(const void* msg, const size_t len) + { + TRACE; + m_sendBuffer = std::string( (char*)msg, len); + return true; + } + + bool receive() + { + TRACE; + m_message->buildMessage(m_receiveBuffer.c_str(), m_receiveBuffer.length()); + return true; + } + + int getSocket() const { return 0; } + + void setReceiveBuffer(const std::string msg) + { + TRACE; + m_receiveBuffer = msg; + } + + std::string getSendBuffer() + { + TRACE; + return m_sendBuffer; + } + + private: + + DummyConnection(const TestMessage::DummyConnection&); + DummyConnection& operator=(const DummyConnection&); + + Message *m_message; + std::string m_receiveBuffer; + std::string m_sendBuffer; + + }; // DummyConnection + + +public: + + void testSimpleMessage() + { + TEST_HEADER; + + bool param(false); + SimpleMessage simpleMessage(¶m); + + std::string msg("hello"); + simpleMessage.buildMessage(msg.c_str(), msg.length()); + TS_ASSERT_EQUALS(simpleMessage.getBuffer(), msg); + TS_ASSERT_EQUALS(param, true); + } + + void testConnectionWithSimpleMessage() + { + TEST_HEADER; + + bool param(false); + SimpleMessage simpleMessage(¶m); + + DummyConnection dummyConnection("localhost", "1234", &simpleMessage); + + std::string msg("hello"); + dummyConnection.setReceiveBuffer(msg); + dummyConnection.receive(); + + TS_ASSERT_EQUALS(simpleMessage.getBuffer(), msg); + TS_ASSERT_EQUALS(param, true); + } + + void testInteraction() + { + TEST_HEADER; + + bool param(false); + SimpleMessage simpleMessage(¶m); + + DummyConnection dummyConnection("localhost", "1234", &simpleMessage); + simpleMessage.setConnection(&dummyConnection); + + std::string msg("hello"); + dummyConnection.setReceiveBuffer(msg); + dummyConnection.receive(); + + TS_ASSERT_EQUALS(simpleMessage.getBuffer(), msg); + TS_ASSERT_EQUALS(param, true); + + TS_ASSERT_EQUALS(dummyConnection.getSendBuffer(), msg); + } + +};