diff --git a/CMakeLists.txt b/CMakeLists.txt index 75e9d506..ef838be3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -137,6 +137,13 @@ ADD_SUBDIRECTORY(backends) #ADD_SUBDIRECTORY(tests) ADD_SUBDIRECTORY(spectrum_manager) +if (CPPUNIT_FOUND) + message("tests : yes") + include_directories(${CPPUNIT_INCLUDE_DIR}) +else() + message("tests : no (install CPPUnit)") +endif() + if(DOXYGEN_FOUND) message("Docs : yes") ADD_SUBDIRECTORY(docs) diff --git a/include/transport/config.h b/include/transport/config.h index 396fbbde..c0ad64e5 100644 --- a/include/transport/config.h +++ b/include/transport/config.h @@ -62,6 +62,10 @@ class Config { /// \param opts extra options which will be recognized by a parser bool load(const std::string &configfile, boost::program_options::options_description &opts); + bool load(std::istream &ifs, boost::program_options::options_description &opts); + + bool load(std::istream &ifs); + /// Loads data from config file. /// This function loads only config variables needed by libtransport. diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 45b23418..9db9993e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -7,6 +7,14 @@ if (PROTOBUF_FOUND) PROTOBUF_GENERATE_CPP(PROTOBUF_SRC PROTOBUF_HDRS "pbnetwork.proto") endif() +if (CPPUNIT_FOUND) + FILE(GLOB SRC_TEST tests/*.cpp) + + ADD_EXECUTABLE(libtransport_test ${SRC_TEST}) + + target_link_libraries(libtransport_test transport ${CPPUNIT_LIBRARIES} ${Boost_LIBRARIES}) +endif() + # SOURCE_GROUP(headers FILES ${HEADERS}) ADD_LIBRARY(transport SHARED ${HEADERS} ${SRC} ${SWIFTEN_SRC} ${PROTOBUF_SRC} ${PROTOBUF_HDRS}) diff --git a/src/config.cpp b/src/config.cpp index 46ef14c2..aa370efb 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -26,11 +26,19 @@ using namespace boost::program_options; namespace Transport { bool Config::load(const std::string &configfile, boost::program_options::options_description &opts) { - m_unregistered.clear(); std::ifstream ifs(configfile.c_str()); if (!ifs.is_open()) return false; + m_file = configfile; + bool ret = load(ifs, opts); + ifs.close(); + + return ret; +} + +bool Config::load(std::istream &ifs, boost::program_options::options_description &opts) { + m_unregistered.clear(); opts.add_options() ("service.jid", value()->default_value(""), "Transport Jabber ID") ("service.server", value()->default_value(""), "Server to connect to") @@ -84,13 +92,16 @@ bool Config::load(const std::string &configfile, boost::program_options::options store(parsed, m_variables); notify(m_variables); - m_file = configfile; - onConfigReloaded(); return true; } +bool Config::load(std::istream &ifs) { + options_description opts("Transport options"); + return load(ifs, opts); +} + bool Config::load(const std::string &configfile) { options_description opts("Transport options"); return load(configfile, opts); diff --git a/src/tests/main.cpp b/src/tests/main.cpp new file mode 100644 index 00000000..dd1c5f27 --- /dev/null +++ b/src/tests/main.cpp @@ -0,0 +1,33 @@ +#include "main.h" +#include +#include +#include +#include +#include +#include + +int main (int argc, char* argv[]) +{ + // informs test-listener about testresults + CPPUNIT_NS :: TestResult testresult; + + // register listener for collecting the test-results + CPPUNIT_NS :: TestResultCollector collectedresults; + testresult.addListener (&collectedresults); + + // register listener for per-test progress output + CPPUNIT_NS :: BriefTestProgressListener progress; + testresult.addListener (&progress); + + // insert test-suite at test-runner by registry + CPPUNIT_NS :: TestRunner testrunner; + testrunner.addTest (CPPUNIT_NS :: TestFactoryRegistry :: getRegistry ().makeTest ()); + testrunner.run (testresult); + + // output results in compiler-format + CPPUNIT_NS :: CompilerOutputter compileroutputter (&collectedresults, std::cerr); + compileroutputter.write (); + + // return 0 if tests were successful + return collectedresults.wasSuccessful () ? 0 : 1; +} diff --git a/src/tests/main.h b/src/tests/main.h new file mode 100644 index 00000000..27f1edcf --- /dev/null +++ b/src/tests/main.h @@ -0,0 +1,6 @@ +#ifndef TESTS_MAIN_H +#define TESTS_MAIN_H +#include +#include + +#endif diff --git a/src/tests/userregistry.cpp b/src/tests/userregistry.cpp new file mode 100644 index 00000000..c932c1bf --- /dev/null +++ b/src/tests/userregistry.cpp @@ -0,0 +1,33 @@ +#include "transport/userregistry.h" +#include "transport/config.h" +#include +#include + +using namespace Transport; + +class UserRegistryTest : public CPPUNIT_NS :: TestFixture { + CPPUNIT_TEST_SUITE (UserRegistryTest); +// CPPUNIT_TEST (storeBuddies); +// CPPUNIT_TEST (storeBuddiesRemove); + CPPUNIT_TEST_SUITE_END (); + + public: + void setUp (void) { + std::istringstream ifs; + cfg = new Config(); + cfg->load(ifs); + userRegistry = new UserRegistry(cfg); + } + + void tearDown (void) { + delete userRegistry; + delete cfg; + } + + private: + UserRegistry *userRegistry; + Config *cfg; + +}; + +CPPUNIT_TEST_SUITE_REGISTRATION (UserRegistryTest);