From de0fef925e3d0fd2e82b30261fc196f97bb31db4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?C=C3=A9dric=20Aguerre?= Date: Wed, 26 Aug 2015 17:45:47 +0200 Subject: [PATCH] UserService + tests --- projects/GDE_API_CPP/api/CMakeLists.txt | 3 + .../api/cmake_files/FindCppUnit.cmake | 68 +++++++++++ projects/GDE_API_CPP/api/src/CMakeLists.txt | 1 + projects/GDE_API_CPP/api/src/GDESession.cpp | 40 ++++++- projects/GDE_API_CPP/api/src/GDESession.hpp | 11 +- projects/GDE_API_CPP/api/src/Group.hpp | 33 ++++++ projects/GDE_API_CPP/api/src/UserService.cpp | 99 +++++++++++++++- projects/GDE_API_CPP/api/src/UserService.hpp | 24 ++-- projects/GDE_API_CPP/api/tests/CMakeLists.txt | 17 +++ .../GDE_API_CPP/api/tests/TestUtilities.hpp | 11 ++ projects/GDE_API_CPP/api/tests/UserTest.cpp | 108 ++++++++++++++++++ projects/GDE_API_CPP/api/tests/UserTest.hpp | 28 +++++ .../GDE_API_CPP/api/tests/simple_test.cpp | 24 +++- .../GDE_API_CPP/api/tests/test_runner.cpp | 77 +++++++++++++ 14 files changed, 524 insertions(+), 20 deletions(-) create mode 100644 projects/GDE_API_CPP/api/cmake_files/FindCppUnit.cmake create mode 100644 projects/GDE_API_CPP/api/src/Group.hpp create mode 100644 projects/GDE_API_CPP/api/tests/TestUtilities.hpp create mode 100644 projects/GDE_API_CPP/api/tests/UserTest.cpp create mode 100644 projects/GDE_API_CPP/api/tests/UserTest.hpp create mode 100644 projects/GDE_API_CPP/api/tests/test_runner.cpp diff --git a/projects/GDE_API_CPP/api/CMakeLists.txt b/projects/GDE_API_CPP/api/CMakeLists.txt index 525cf76..79798a4 100644 --- a/projects/GDE_API_CPP/api/CMakeLists.txt +++ b/projects/GDE_API_CPP/api/CMakeLists.txt @@ -8,5 +8,8 @@ LIST(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake_files") FIND_PACKAGE(POCO REQUIRED) INCLUDE_DIRECTORIES(${Poco_INCLUDE_DIRS}) +FIND_PACKAGE(CppUnit REQUIRED) +enable_testing() + ADD_SUBDIRECTORY(src) ADD_SUBDIRECTORY(tests) diff --git a/projects/GDE_API_CPP/api/cmake_files/FindCppUnit.cmake b/projects/GDE_API_CPP/api/cmake_files/FindCppUnit.cmake new file mode 100644 index 0000000..8d900fe --- /dev/null +++ b/projects/GDE_API_CPP/api/cmake_files/FindCppUnit.cmake @@ -0,0 +1,68 @@ +# - Find CppUnit +# Sets the following variables: +# CPPUNIT_INCLUDE_DIRS - path to the CppUnit include directory +# CPPUNIT_LIBRARIES - path to the CppUnit libraries to be linked against +# CPPUNIT_DEFINITIONS - specific CppUnit definitions to be added +# +# The header cppunit/extensions/HelperMacros.h is looked for. +# The following libraries are searched +# cppunit_dll, or cppunitd_dll (Windows) +# cppunit (Linux) +# + +# Headers +SET(CPPUNIT_ROOT_DIR $ENV{CPPUNIT_ROOT_DIR} CACHE PATH "Path to the CPPUNIT.") +IF(CPPUNIT_ROOT_DIR) + LIST(APPEND CMAKE_INCLUDE_PATH "${CPPUNIT_ROOT_DIR}/include") + LIST(APPEND CMAKE_PROGRAM_PATH "${CPPUNIT_ROOT_DIR}/bin") +ENDIF(CPPUNIT_ROOT_DIR) + +SET(CPPUNIT_INCLUDE_TO_FIND cppunit/extensions/HelperMacros.h) +FIND_PATH(CPPUNIT_INCLUDE_DIRS ${CPPUNIT_INCLUDE_TO_FIND}) + +# Libraries +IF(WIN32) + IF(CMAKE_BUILD_TYPE STREQUAL Debug) + FIND_LIBRARY(CPPUNIT_LIBRARIES cppunitd_dll) + ELSE(CMAKE_BUILD_TYPE STREQUAL Debug) + FIND_LIBRARY(CPPUNIT_LIBRARIES cppunit_dll) + ENDIF(CMAKE_BUILD_TYPE STREQUAL Debug) +ELSE(WIN32) + FIND_PROGRAM(CPPUNIT_CONFIG_BIN cppunit-config) + IF(NOT CPPUNIT_CONFIG_BIN) + MESSAGE(FATAL_ERROR "Error in CPPUNIT detection ! cppunit-config executable not found !") + ENDIF(NOT CPPUNIT_CONFIG_BIN) + EXECUTE_PROCESS(COMMAND ${CPPUNIT_CONFIG_BIN} --libs OUTPUT_VARIABLE CPPUNIT_LDFLAGS) + STRING(STRIP ${CPPUNIT_LDFLAGS} CPPUNIT_LDFLAGS) + STRING(REPLACE " " ";" LDFLAGS_LIST ${CPPUNIT_LDFLAGS}) + FOREACH(LDFLAG ${LDFLAGS_LIST}) + STRING(REGEX MATCH "^-L.*" LIBDIR "${LDFLAG}") + STRING(REGEX MATCH "^-l.*" LIB "${LDFLAG}") + IF(LIBDIR) + STRING(REGEX REPLACE "^-L" "" LIBDIR ${LIBDIR}) + LIST(APPEND CMAKE_LIBRARY_PATH ${LIBDIR}) + ELSEIF(LIB) + STRING(REGEX REPLACE "^-l" "" LIB ${LIB}) + LIST(APPEND LIBS ${LIB}) + ELSE() + MESSAGE(FATAL_ERROR "Unrecognized token \"${LDFLAG}\" in the output of cppunit-config --libs") + ENDIF() + ENDFOREACH(LDFLAG ${LDFLAGS_LIST}) + FOREACH(LIB ${LIBS}) + FIND_LIBRARY(CPPUNIT_SUBLIB_${LIB} ${LIB}) + IF(NOT CPPUNIT_SUBLIB_${LIB}) + MESSAGE(FATAL_ERROR "Error in CPPUNIT detection! Fail to locate the needed library ${LIB}!") + ENDIF(NOT CPPUNIT_SUBLIB_${LIB}) + LIST(APPEND CPPUNIT_LIBRARIES ${CPPUNIT_SUBLIB_${LIB}}) + ENDFOREACH(LIB ${LIBS}) +# MESSAGE("**** ${CPPUNIT_LIBRARIES}") +ENDIF(WIN32) + +# Global variables +SET(CPPUNIT_DEFINITIONS) +IF(WIN32) + SET(CPPUNIT_DEFINITIONS -DCPPUNIT_DLL) +ENDIF(WIN32) + +INCLUDE(FindPackageHandleStandardArgs) +FIND_PACKAGE_HANDLE_STANDARD_ARGS(CppUnit REQUIRED_VARS CPPUNIT_INCLUDE_DIRS CPPUNIT_LIBRARIES) diff --git a/projects/GDE_API_CPP/api/src/CMakeLists.txt b/projects/GDE_API_CPP/api/src/CMakeLists.txt index b822e73..10d44a4 100644 --- a/projects/GDE_API_CPP/api/src/CMakeLists.txt +++ b/projects/GDE_API_CPP/api/src/CMakeLists.txt @@ -19,6 +19,7 @@ SET(gde_api_cpp_HEADERS CommandTO.hpp Credentials.hpp GDESession.hpp + Group.hpp HttpConnection.hpp JsonFormatter.hpp User.hpp diff --git a/projects/GDE_API_CPP/api/src/GDESession.cpp b/projects/GDE_API_CPP/api/src/GDESession.cpp index 37234e3..81956e8 100644 --- a/projects/GDE_API_CPP/api/src/GDESession.cpp +++ b/projects/GDE_API_CPP/api/src/GDESession.cpp @@ -22,8 +22,44 @@ gde::GDESession::createUser(const std::string& name, const std::string& password return gde::UserService(*this).createUser(name, password); } -void +bool gde::GDESession::deleteUser(const User& user) { - gde::UserService(*this).deleteUser(user); + return gde::UserService(*this).deleteUser(user); +} + +const gde::User +gde::GDESession::findUser(const std::string& name) +{ + return gde::UserService(*this).findUser(name); +} + +const gde::Group +gde::GDESession::createGroup(const std::string& name) +{ + return gde::UserService(*this).createGroup(name); +} + +bool +gde::GDESession::deleteGroup(const gde::Group& group) +{ + return gde::UserService(*this).deleteGroup(group); +} + +const gde::Group +gde::GDESession::findGroup(const std::string& name) +{ + return gde::UserService(*this).findGroup(name); +} + +bool +gde::GDESession::addToGroup(const gde::Group& group, const gde::User& user) +{ + return gde::UserService(*this).addToGroup(group, user); +} + +bool +gde::GDESession::removeFromGroup(const gde::Group& group, const gde::User& user) +{ + return gde::UserService(*this).removeFromGroup(group, user); } diff --git a/projects/GDE_API_CPP/api/src/GDESession.hpp b/projects/GDE_API_CPP/api/src/GDESession.hpp index bdef8ab..e07efea 100644 --- a/projects/GDE_API_CPP/api/src/GDESession.hpp +++ b/projects/GDE_API_CPP/api/src/GDESession.hpp @@ -2,6 +2,7 @@ #define GDE_SESSION_HPP #include "User.hpp" +#include "Group.hpp" #include "Credentials.hpp" #include @@ -29,7 +30,15 @@ namespace gde { std::string getServiceURI(const std::string& serviceName) const; const User createUser(const std::string& name, const std::string& password); - void deleteUser(const User&); + bool deleteUser(const User&); + const User findUser(const std::string& name); + + const Group createGroup(const std::string& name); + bool deleteGroup(const Group&); + const Group findGroup(const std::string& name); + + bool addToGroup(const Group&, const User&); + bool removeFromGroup(const Group&, const User&); private: std::string _serverAddress; diff --git a/projects/GDE_API_CPP/api/src/Group.hpp b/projects/GDE_API_CPP/api/src/Group.hpp new file mode 100644 index 0000000..b47c9b5 --- /dev/null +++ b/projects/GDE_API_CPP/api/src/Group.hpp @@ -0,0 +1,33 @@ +#ifndef GDE_GROUP_HPP +#define GDE_GROUP_HPP + +#include + +namespace gde { + + class Group { + friend class UserService; + friend class GDESession; + + public: + ~Group() {} + + inline int getId() const { return _id; } + inline void setId(int id) { this->_id = id; } + + inline std::string getName() const { return _name; } + inline void setName(const std::string& name) { this->_name = name; } + + private: + Group(int id=-1, const std::string& name="") : _id(id), _name(name) {} + Group(const Group&); // non copyable + Group& operator=(const Group&); // non copyable + + private: + int _id; + std::string _name; + }; + +}; + +#endif diff --git a/projects/GDE_API_CPP/api/src/UserService.cpp b/projects/GDE_API_CPP/api/src/UserService.cpp index ef94e0b..329dfc2 100644 --- a/projects/GDE_API_CPP/api/src/UserService.cpp +++ b/projects/GDE_API_CPP/api/src/UserService.cpp @@ -45,12 +45,109 @@ gde::UserService::createUser(const std::string& name, const std::string& passwor } } -void +bool gde::UserService::deleteUser(const User& user) { CommandTO cto(DELETE_USER); cto.setParameter("id", user.getId()); + + const Credentials& credentials = _session.getCredentials(); + std::string uri = _session.getServiceURI(_servletName); + CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto); + return crto.getCode() == CommandResultTO::OK; +} + +const gde::User +gde::UserService::findUser(const std::string& name) +{ + CommandTO cto(FIND_USER); + cto.setParameter("username", name); + + const Credentials& credentials = _session.getCredentials(); + std::string uri = _session.getServiceURI(_servletName); + CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto); + + // build User object from CommandResultTO data (json format) + { + Poco::JSON::Object::Ptr object = JsonFormatter::parse(crto.getData()); + int id = JsonFormatter::extract(object, "id"); + std::string name = JsonFormatter::extract(object, "name"); + return gde::User(id, name); + } +} + +const gde::Group +gde::UserService::createGroup(const std::string& name) +{ + CommandTO cto(CREATE_GROUP); + cto.setParameter("name", name); + + const Credentials& credentials = _session.getCredentials(); + std::string uri = _session.getServiceURI(_servletName); + CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto); + + // build Group object from CommandResultTO data (json format) + { + Poco::JSON::Object::Ptr object = JsonFormatter::parse(crto.getData()); + int id = JsonFormatter::extract(object, "id"); + std::string name = JsonFormatter::extract(object, "name"); + return gde::Group(id, name); + } +} + +bool +gde::UserService::deleteGroup(const gde::Group& group) +{ + CommandTO cto(DELETE_GROUP); + cto.setParameter("id", group.getId()); + + const Credentials& credentials = _session.getCredentials(); + std::string uri = _session.getServiceURI(_servletName); + CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto); + return crto.getCode() == CommandResultTO::OK; +} + +const gde::Group +gde::UserService::findGroup(const std::string& name) +{ + CommandTO cto(FIND_GROUP); + cto.setParameter("groupname", name); + + const Credentials& credentials = _session.getCredentials(); + std::string uri = _session.getServiceURI(_servletName); + CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto); + + // build Group object from CommandResultTO data (json format) + { + Poco::JSON::Object::Ptr object = JsonFormatter::parse(crto.getData()); + int id = JsonFormatter::extract(object, "id"); + std::string name = JsonFormatter::extract(object, "name"); + return gde::Group(id, name); + } +} + +bool +gde::UserService::addToGroup(const gde::Group& group, const gde::User& user) +{ + CommandTO cto(ADD_TO_GROUP); + cto.setParameter("groupId", group.getId()); + cto.setParameter("userId", user.getId()); + + const Credentials& credentials = _session.getCredentials(); + std::string uri = _session.getServiceURI(_servletName); + CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto); + return crto.getCode() == CommandResultTO::OK; +} + +bool +gde::UserService::removeFromGroup(const gde::Group& group, const gde::User& user) +{ + CommandTO cto(REMOVE_FROM_GROUP); + cto.setParameter("groupId", group.getId()); + cto.setParameter("userId", user.getId()); + const Credentials& credentials = _session.getCredentials(); std::string uri = _session.getServiceURI(_servletName); CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto); + return crto.getCode() == CommandResultTO::OK; } diff --git a/projects/GDE_API_CPP/api/src/UserService.hpp b/projects/GDE_API_CPP/api/src/UserService.hpp index 8fbc7f8..e180a3c 100644 --- a/projects/GDE_API_CPP/api/src/UserService.hpp +++ b/projects/GDE_API_CPP/api/src/UserService.hpp @@ -2,6 +2,7 @@ #define GDE_USER_SERVICE_HPP #include "User.hpp" +#include "Group.hpp" #include "GDESession.hpp" #include @@ -13,19 +14,16 @@ namespace gde { public: const User createUser(const std::string& name, const std::string& password); - void deleteUser(const User&); - /* - void createUser(); - void deleteUser(); - void findUser(); - - void createGroup(); - void deleteGroup(); - void findGroup(); - - void addToGroup(); - void removeFromGroup(); - */ + bool deleteUser(const User&); + const User findUser(const std::string& name); + + const Group createGroup(const std::string& name); + bool deleteGroup(const Group&); + const Group findGroup(const std::string& name); + + bool addToGroup(const Group&, const User&); + bool removeFromGroup(const Group&, const User&); + private: UserService(const GDESession& session) : _session(session) {} ~UserService() {} diff --git a/projects/GDE_API_CPP/api/tests/CMakeLists.txt b/projects/GDE_API_CPP/api/tests/CMakeLists.txt index cd80d75..f463197 100644 --- a/projects/GDE_API_CPP/api/tests/CMakeLists.txt +++ b/projects/GDE_API_CPP/api/tests/CMakeLists.txt @@ -1,10 +1,15 @@ +ADD_DEFINITIONS(${CPPUNIT_DEFINITIONS}) INCLUDE_DIRECTORIES( + ${CMAKE_CURRENT_SOURCE_DIR} + ${CPPUNIT_INCLUDE_DIRS} + ${PTHREADS_INCLUDE_DIRS} ${Poco_INCLUDE_DIRS} ${PROJECT_SOURCE_DIR}/src ) LINK_DIRECTORIES( + ${CPPUNIT_LIBRARIES} ${Poco_LIBRARY_DIRS} # ${CMAKE_INSTALL_DIR/lib} ) @@ -18,3 +23,15 @@ SET(_link_LIBRARIES ADD_EXECUTABLE(simple_test simple_test.cpp) TARGET_LINK_LIBRARIES(simple_test ${_link_LIBRARIES}) INSTALL(TARGETS simple_test EXPORT ${PROJECT_NAME}TargetGroup DESTINATION tests) + + +# Tests + +FILE(GLOB UnitTests_SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*Test.cpp" ) +ADD_EXECUTABLE(UnitTester test_runner.cpp ${UnitTests_SRCS}) +TARGET_LINK_LIBRARIES(UnitTester GdeApiCpp ${CPPUNIT_LIBRARIES} ${PLATFORM_LIBS}) + +FOREACH(test ${UnitTests_SRCS}) + GET_FILENAME_COMPONENT(TestName ${test} NAME_WE) + ADD_TEST(${TestName} UnitTester ${TestName}) +ENDFOREACH(test) diff --git a/projects/GDE_API_CPP/api/tests/TestUtilities.hpp b/projects/GDE_API_CPP/api/tests/TestUtilities.hpp new file mode 100644 index 0000000..fa5b2be --- /dev/null +++ b/projects/GDE_API_CPP/api/tests/TestUtilities.hpp @@ -0,0 +1,11 @@ +#ifndef GDE_TEST_UTILITIES_HPP +#define GDE_TEST_UTILITIES_HPP + +#include + +#include + +std::string serverAddress = "http://localhost:8080/GDE-war"; +gde::Credentials credentials("admin", "edf123"); + +#endif diff --git a/projects/GDE_API_CPP/api/tests/UserTest.cpp b/projects/GDE_API_CPP/api/tests/UserTest.cpp new file mode 100644 index 0000000..ffc1926 --- /dev/null +++ b/projects/GDE_API_CPP/api/tests/UserTest.cpp @@ -0,0 +1,108 @@ +#include + +#include +#include +#include +#include + +void +UserTest::testCreateDeleteUser() +{ + gde::GDESession session(serverAddress, credentials); + + const gde::User& user = session.createUser("toto", "toto_passwd"); + CPPUNIT_ASSERT(user.getName() == "toto"); + CPPUNIT_ASSERT(user.getPassword() == "toto_passwd"); + CPPUNIT_ASSERT(user.getId() > 0); + + CPPUNIT_ASSERT(session.deleteUser(user)); +} + +void +UserTest::testFindUser() +{ + gde::GDESession session(serverAddress, credentials); + + const gde::User& user = session.createUser("toto", "toto_passwd"); + + { + const gde::User& toto = session.findUser("toto"); + CPPUNIT_ASSERT(toto.getName() == "toto"); + CPPUNIT_ASSERT(toto.getId() > 0); + } + + CPPUNIT_ASSERT(session.deleteUser(user)); + + /* + // Will raise an exception: user does not exist anymore + { + const gde::User& toto = session.findUser("toto"); + } + */ +} + +void +UserTest::testCreateDeleteGroup() +{ + gde::GDESession session(serverAddress, credentials); + + const gde::Group& group = session.createGroup("mygroup"); + CPPUNIT_ASSERT(group.getName() == "mygroup"); + CPPUNIT_ASSERT(group.getId() > 0); + + CPPUNIT_ASSERT(session.deleteGroup(group)); +} + +void +UserTest::testFindGroup() +{ + gde::GDESession session(serverAddress, credentials); + + const gde::Group& group = session.createGroup("mygroup"); + + { + const gde::Group& myGroup = session.findGroup("mygroup"); + CPPUNIT_ASSERT(myGroup.getName() == "mygroup"); + CPPUNIT_ASSERT(myGroup.getId() > 0); + } + + CPPUNIT_ASSERT(session.deleteGroup(group)); + + /* + // Will raise an exception: group does not exist anymore + { + const gde::Group& myGroup = session.findGroup("mygroup"); + } + */ +} + +void +UserTest::testAddToGroup() +{ + gde::GDESession session(serverAddress, credentials); + const gde::User& user = session.createUser("toto", "toto_passwd"); + CPPUNIT_ASSERT(user.getId() > 0); + const gde::Group& group = session.createGroup("mygroup"); + CPPUNIT_ASSERT(group.getId() > 0); + + CPPUNIT_ASSERT(session.addToGroup(group, user)); + + CPPUNIT_ASSERT(session.deleteGroup(group)); + CPPUNIT_ASSERT(session.deleteUser(user)); +} + +void +UserTest::testRemoveFromGroup() +{ + gde::GDESession session(serverAddress, credentials); + const gde::User& user = session.createUser("toto", "toto_passwd"); + CPPUNIT_ASSERT(user.getId() > 0); + const gde::Group& group = session.createGroup("mygroup"); + CPPUNIT_ASSERT(group.getId() > 0); + + CPPUNIT_ASSERT(session.addToGroup(group, user)); + CPPUNIT_ASSERT(session.removeFromGroup(group, user)); + + CPPUNIT_ASSERT(session.deleteGroup(group)); + CPPUNIT_ASSERT(session.deleteUser(user)); +} diff --git a/projects/GDE_API_CPP/api/tests/UserTest.hpp b/projects/GDE_API_CPP/api/tests/UserTest.hpp new file mode 100644 index 0000000..8fb99aa --- /dev/null +++ b/projects/GDE_API_CPP/api/tests/UserTest.hpp @@ -0,0 +1,28 @@ +#ifndef GDE_USER_TEST_HPP +#define GDE_USER_TEST_HPP + +#include + +class UserTest : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE(UserTest); + CPPUNIT_TEST(testCreateDeleteUser); + CPPUNIT_TEST(testFindUser); + CPPUNIT_TEST(testCreateDeleteGroup); + CPPUNIT_TEST(testFindGroup); + CPPUNIT_TEST(testAddToGroup); + CPPUNIT_TEST(testRemoveFromGroup); + CPPUNIT_TEST_SUITE_END(); + +public: + void testCreateDeleteUser(); + void testFindUser(); + void testCreateDeleteGroup(); + void testFindGroup(); + void testAddToGroup(); + void testRemoveFromGroup(); +}; + +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(UserTest, "UserTest"); + +#endif diff --git a/projects/GDE_API_CPP/api/tests/simple_test.cpp b/projects/GDE_API_CPP/api/tests/simple_test.cpp index 270b98a..4a0898e 100644 --- a/projects/GDE_API_CPP/api/tests/simple_test.cpp +++ b/projects/GDE_API_CPP/api/tests/simple_test.cpp @@ -4,6 +4,14 @@ #include #include +void +printUserInfo(const gde::User& user) +{ + std::cout << "Name: " << user.getName() << std::endl; + std::cout << "Password: " << user.getPassword() << std::endl; + std::cout << "Id: " << user.getId() << std::endl; +} + int main() { @@ -12,10 +20,20 @@ main() gde::GDESession session(serverAddress, cred); const gde::User& user = session.createUser("toto", "toto_passwd"); + printUserInfo(user); - std::cout << "Name: " << user.getName() << std::endl; - std::cout << "Password: " << user.getPassword() << std::endl; - std::cout << "Id: " << user.getId() << std::endl; + { + const gde::User& toto = session.findUser("toto"); + printUserInfo(toto); + } session.deleteUser(user); + + /* + // Will raise an exception: user does not exist anymore + { + const gde::User& toto = session.findUser("toto"); + printUserInfo(toto); + } + */ } diff --git a/projects/GDE_API_CPP/api/tests/test_runner.cpp b/projects/GDE_API_CPP/api/tests/test_runner.cpp new file mode 100644 index 0000000..ab50748 --- /dev/null +++ b/projects/GDE_API_CPP/api/tests/test_runner.cpp @@ -0,0 +1,77 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#ifndef WIN32 +#include +#endif + +// ============================================================================ +/*! + * Main program source for Unit Tests with cppunit package does not depend + * on actual tests, so we use the same for all partial unit tests. + */ +// ============================================================================ + +int main(int argc, char* argv[]) +{ + if (argc != 2) { + std::cout << "Usage: " << argv[0] << " \n"; + return 1; + } + +#ifndef WIN32 + fpu_control_t cw = _FPU_DEFAULT & ~(_FPU_MASK_IM | _FPU_MASK_ZM | _FPU_MASK_OM); + _FPU_SETCW(cw); +#endif + // --- Create the event manager and test controller + CPPUNIT_NS::TestResult controller; + + // --- Add a listener that colllects test result + CPPUNIT_NS::TestResultCollector result; + controller.addListener( &result ); + + // --- Add a listener that print dots as test run. +#ifdef WIN32 + CPPUNIT_NS::TextTestProgressListener progress; +#else + CPPUNIT_NS::BriefTestProgressListener progress; +#endif + controller.addListener( &progress ); + + // --- Get the top level suite from the registry + + //CPPUNIT_NS::Test *suite = CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest(); + CPPUNIT_NS::Test *suite = CPPUNIT_NS::TestFactoryRegistry::getRegistry(argv[1]).makeTest(); + + // --- Adds the test to the list of test to run + + CPPUNIT_NS::TestRunner runner; + runner.addTest( suite ); + runner.run( controller); + + // --- Print test in a compiler compatible format. + + std::ofstream testFile; + testFile.open("UnitTestsResult", std::ios::out | std::ios::trunc); + //CPPUNIT_NS::CompilerOutputter outputter( &result, std::cerr ); + CPPUNIT_NS::CompilerOutputter outputter( &result, testFile ); + outputter.write(); + + // --- Run the tests. + + bool wasSucessful = result.wasSuccessful(); + testFile.close(); + + // --- Return error code 1 if the one of test failed. + + return wasSucessful ? 0 : 1; +} -- 2.39.2