summaryrefslogtreecommitdiff
path: root/src/tests
diff options
context:
space:
mode:
Diffstat (limited to 'src/tests')
-rw-r--r--src/tests/testaccount.cpp55
-rw-r--r--src/tests/testcipher.cpp30
-rw-r--r--src/tests/testdataprovider.cpp17
-rw-r--r--src/tests/testrecordset.cpp45
-rw-r--r--src/tests/testsmain.cpp5
-rw-r--r--src/tests/teststorage.cpp509
-rw-r--r--src/tests/teststorage.h65
7 files changed, 448 insertions, 278 deletions
diff --git a/src/tests/testaccount.cpp b/src/tests/testaccount.cpp
index 814aaa29..d30c8a78 100644
--- a/src/tests/testaccount.cpp
+++ b/src/tests/testaccount.cpp
@@ -21,6 +21,7 @@
*/
+#include "../utils/logger.h"
#include "testaccount.h"
@@ -37,18 +38,11 @@ using namespace tmwserv;
void
AccountTest::setUp(void)
{
- //RawStatistics stats;
- //stats.strength = 1;
- //stats.agility = 1;
- //stats.vitality = 1;
- //stats.intelligence = 1;
- //stats.dexterity = 1;
- //stats.luck = 1;
const RawStatistics stats = {1, 1, 1, 1, 1, 1};
- Being* sam = new Being("sam", GENDER_MALE, 0, 0, stats);
- Being* merry = new Being("merry", GENDER_MALE, 0, 0, stats);
- Being* pippin = new Being("pippin", GENDER_MALE, 0, 0, stats);
+ BeingPtr sam(new Being("sam", GENDER_MALE, 0, 0, stats));
+ BeingPtr merry(new Being("merry", GENDER_MALE, 0, 0, stats));
+ BeingPtr pippin(new Being("pippin", GENDER_MALE, 0, 0, stats));
mCharacters.push_back(sam);
mCharacters.push_back(merry);
mCharacters.push_back(pippin);
@@ -66,13 +60,6 @@ AccountTest::setUp(void)
void
AccountTest::tearDown(void)
{
- for (Beings::iterator it = mCharacters.begin();
- it != mCharacters.end();
- ++it)
- {
- delete (*it);
- }
-
delete mAccount;
mAccount = 0;
}
@@ -85,6 +72,8 @@ AccountTest::tearDown(void)
void
AccountTest::testCreate1(void)
{
+ LOG("AccountTest::testCreate1()");
+
const std::string name("frodo");
const std::string password("baggins");
const std::string email("frodo@theshire.com");
@@ -105,6 +94,8 @@ AccountTest::testCreate1(void)
void
AccountTest::testCreate2(void)
{
+ LOG("AccountTest::testCreate2()");
+
const std::string name("frodo");
const std::string password("baggins");
const std::string email("frodo@theshire.com");
@@ -118,7 +109,7 @@ AccountTest::testCreate2(void)
CPPUNIT_ASSERT_EQUAL(mCharacters.size(),
mAccount->getCharacters().size());
- Beings& characters = account.getCharacters();
+ const Beings& characters = account.getCharacters();
for (size_t i = 0; i < mCharacters.size(); ++i) {
CPPUNIT_ASSERT_EQUAL(characters[i]->getName(),
@@ -133,15 +124,11 @@ AccountTest::testCreate2(void)
void
AccountTest::testAddCharacter1(void)
{
- RawStatistics stats;
- stats.strength = 1;
- stats.agility = 1;
- stats.vitality = 1;
- stats.intelligence = 1;
- stats.dexterity = 1;
- stats.luck = 1;
+ LOG("AccountTest::testAddCharacter1()");
+
+ RawStatistics stats = {1, 1, 1, 1, 1, 1};
- Being* bilbo = new Being("bilbo", GENDER_MALE, 0, 0, stats);
+ BeingPtr bilbo(new Being("bilbo", GENDER_MALE, 0, 0, stats));
mAccount->addCharacter(bilbo);
@@ -156,8 +143,6 @@ AccountTest::testAddCharacter1(void)
for (size_t i = 0; i < mCharacters.size(); ++i) {
CPPUNIT_ASSERT_EQUAL(names[i], mCharacters[i]->getName());
}
-
- delete bilbo;
}
@@ -167,7 +152,11 @@ AccountTest::testAddCharacter1(void)
void
AccountTest::testAddCharacter2(void)
{
- mAccount->addCharacter(NULL);
+ LOG("AccountTest::testAddCharacter2()");
+
+ BeingPtr nullBeing(0);
+
+ mAccount->addCharacter(nullBeing);
CPPUNIT_ASSERT_EQUAL((size_t) 3, mAccount->getCharacters().size());
@@ -188,9 +177,11 @@ AccountTest::testAddCharacter2(void)
void
AccountTest::testGetCharacter1(void)
{
+ LOG("AccountTest::testGetCharacter1()");
+
const std::string name("merry");
- Being* merry = mAccount->getCharacter(name);
+ const Being* merry = mAccount->getCharacter(name);
CPPUNIT_ASSERT(merry != 0);
CPPUNIT_ASSERT_EQUAL(name, merry->getName());
@@ -203,7 +194,9 @@ AccountTest::testGetCharacter1(void)
void
AccountTest::testGetCharacter2(void)
{
- Being* nobody = mAccount->getCharacter("johndoe");
+ LOG("AccountTest::testGetCharacter2()");
+
+ const Being* nobody = mAccount->getCharacter("johndoe");
CPPUNIT_ASSERT(nobody == 0);
}
diff --git a/src/tests/testcipher.cpp b/src/tests/testcipher.cpp
index d51a4404..85e34099 100644
--- a/src/tests/testcipher.cpp
+++ b/src/tests/testcipher.cpp
@@ -24,6 +24,7 @@
#include <string>
#include "../utils/cipher.h"
+#include "../utils/logger.h"
#include "testcipher.h"
@@ -60,8 +61,10 @@ CipherTest::tearDown(void)
void
CipherTest::testMd5_1(void)
{
+ LOG("CipherTest::testMd5_1()");
+
const std::string expected("d41d8cd98f00b204e9800998ecf8427e");
- std::string actual(Cipher::instance().md5(""));
+ const std::string actual(Cipher::instance().md5(""));
CPPUNIT_ASSERT_EQUAL(expected, actual);
}
@@ -73,8 +76,10 @@ CipherTest::testMd5_1(void)
void
CipherTest::testMd5_2(void)
{
+ LOG("CipherTest::testMd5_2()");
+
const std::string expected("0cc175b9c0f1b6a831c399e269772661");
- std::string actual(Cipher::instance().md5("a"));
+ const std::string actual(Cipher::instance().md5("a"));
CPPUNIT_ASSERT_EQUAL(expected, actual);
}
@@ -86,8 +91,10 @@ CipherTest::testMd5_2(void)
void
CipherTest::testMd5_3(void)
{
+ LOG("CipherTest::testMd5_3()");
+
const std::string expected("900150983cd24fb0d6963f7d28e17f72");
- std::string actual(Cipher::instance().md5("abc"));
+ const std::string actual(Cipher::instance().md5("abc"));
CPPUNIT_ASSERT_EQUAL(expected, actual);
}
@@ -99,8 +106,10 @@ CipherTest::testMd5_3(void)
void
CipherTest::testMd5_4(void)
{
+ LOG("CipherTest::testMd5_4()");
+
const std::string expected("f96b697d7cb7938d525a2f31aaf161d0");
- std::string actual(Cipher::instance().md5("message digest"));
+ const std::string actual(Cipher::instance().md5("message digest"));
CPPUNIT_ASSERT_EQUAL(expected, actual);
}
@@ -112,8 +121,11 @@ CipherTest::testMd5_4(void)
void
CipherTest::testMd5_5(void)
{
+ LOG("CipherTest::testMd5_6()");
+
const std::string expected("c3fcd3d76192e4007dfb496cca67e13b");
- std::string actual(Cipher::instance().md5("abcdefghijklmnopqrstuvwxyz"));
+ const std::string actual(
+ Cipher::instance().md5("abcdefghijklmnopqrstuvwxyz"));
CPPUNIT_ASSERT_EQUAL(expected, actual);
}
@@ -125,12 +137,14 @@ CipherTest::testMd5_5(void)
void
CipherTest::testMd5_6(void)
{
+ LOG("CipherTest::testMd5_6()");
+
const std::string expected("d174ab98d277d9f5a5611c2c9f419d9f");
std::string s("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
s += "abcdefghijklmnopqrstuvwxyz";
s += "0123456789";
- std::string actual(Cipher::instance().md5(s));
+ const std::string actual(Cipher::instance().md5(s));
CPPUNIT_ASSERT_EQUAL(expected, actual);
}
@@ -142,13 +156,15 @@ CipherTest::testMd5_6(void)
void
CipherTest::testMd5_7(void)
{
+ LOG("CipherTest::testMd5_7()");
+
const std::string expected("57edf4a22be3c955ac49da2e2107b67a");
std::string s;
for (int i = 0; i < 8; ++i) {
s += "1234567890";
}
- std::string actual(Cipher::instance().md5(s));
+ const std::string actual(Cipher::instance().md5(s));
CPPUNIT_ASSERT_EQUAL(expected, actual);
}
diff --git a/src/tests/testdataprovider.cpp b/src/tests/testdataprovider.cpp
index 06a2b0ee..dd5aff4f 100644
--- a/src/tests/testdataprovider.cpp
+++ b/src/tests/testdataprovider.cpp
@@ -30,6 +30,7 @@
#endif
#include "../dal/dataproviderfactory.h"
+#include "../utils/logger.h"
#include "testdataprovider.h"
@@ -97,6 +98,8 @@ DataProviderTest::tearDown(void)
void
DataProviderTest::testConnection1(void)
{
+ LOG("DataProviderTest::testConnection1()");
+
#ifdef SQLITE_SUPPORT
std::string dbFile(mDbName);
dbFile += ".db";
@@ -116,6 +119,8 @@ DataProviderTest::testConnection1(void)
void
DataProviderTest::testCreateTable1(void)
{
+ LOG("DataProviderTest::testCreateTable1()");
+
#ifdef SQLITE_SUPPORT
std::string dbFile(mDbName);
dbFile += ".db";
@@ -141,6 +146,8 @@ DataProviderTest::testCreateTable1(void)
void
DataProviderTest::testCreateTable2(void)
{
+ LOG("DataProviderTest::testCreateTable2()");
+
#ifdef SQLITE_SUPPORT
std::string dbFile(mDbName);
dbFile += ".db";
@@ -164,6 +171,8 @@ DataProviderTest::testCreateTable2(void)
void
DataProviderTest::testInsert1(void)
{
+ LOG("DataProviderTest::testInsert1()");
+
#ifdef SQLITE_SUPPORT
std::string dbFile(mDbName);
dbFile += ".db";
@@ -193,6 +202,8 @@ DataProviderTest::testInsert1(void)
void
DataProviderTest::testInsert2(void)
{
+ LOG("DataProviderTest::testInsert2()");
+
#ifdef SQLITE_SUPPORT
std::string dbFile(mDbName);
dbFile += ".db";
@@ -216,6 +227,8 @@ DataProviderTest::testInsert2(void)
void
DataProviderTest::testFetch1(void)
{
+ LOG("DataProviderTest::testFetch1()");
+
#ifdef SQLITE_SUPPORT
std::string dbFile(mDbName);
dbFile += ".db";
@@ -249,6 +262,8 @@ DataProviderTest::testFetch1(void)
void
DataProviderTest::testDisconnection1(void)
{
+ LOG("DataProviderTest::testDisconnection1()");
+
#ifdef SQLITE_SUPPORT
std::string dbFile(mDbName);
dbFile += ".db";
@@ -271,6 +286,8 @@ DataProviderTest::testDisconnection1(void)
void
DataProviderTest::testDisconnection2(void)
{
+ LOG("DataProviderTest::testDisconnection2()");
+
mDb->disconnect();
CPPUNIT_ASSERT(!mDb->isConnected());
}
diff --git a/src/tests/testrecordset.cpp b/src/tests/testrecordset.cpp
index 26a9e016..df9a7100 100644
--- a/src/tests/testrecordset.cpp
+++ b/src/tests/testrecordset.cpp
@@ -23,6 +23,7 @@
#include <sstream>
+#include "../utils/logger.h"
#include "testrecordset.h"
@@ -73,6 +74,8 @@ RecordSetTest::tearDown(void)
void
RecordSetTest::testRows1(void)
{
+ LOG("RecordSetTest::testRows1()");
+
CPPUNIT_ASSERT_EQUAL((unsigned int) 0, mEmptyRs.rows());
}
@@ -83,6 +86,8 @@ RecordSetTest::testRows1(void)
void
RecordSetTest::testRows2(void)
{
+ LOG("RecordSetTest::testRows2()");
+
CPPUNIT_ASSERT_EQUAL((unsigned int) 2, mNonEmptyRs.rows());
}
@@ -93,6 +98,8 @@ RecordSetTest::testRows2(void)
void
RecordSetTest::testCols1(void)
{
+ LOG("RecordSetTest::testCols1()");
+
CPPUNIT_ASSERT_EQUAL((unsigned int) 0, mEmptyRs.cols());
}
@@ -103,6 +110,8 @@ RecordSetTest::testCols1(void)
void
RecordSetTest::testCols2(void)
{
+ LOG("RecordSetTest::testCols2()");
+
CPPUNIT_ASSERT_EQUAL((unsigned int) 2, mNonEmptyRs.cols());
}
@@ -113,6 +122,8 @@ RecordSetTest::testCols2(void)
void
RecordSetTest::testIsEmpty1(void)
{
+ LOG("RecordSetTest::testIsEmpty1()");
+
CPPUNIT_ASSERT(mEmptyRs.isEmpty());
}
@@ -123,6 +134,8 @@ RecordSetTest::testIsEmpty1(void)
void
RecordSetTest::testIsEmpty2(void)
{
+ LOG("RecordSetTest::testIsEmpty2()");
+
CPPUNIT_ASSERT(!mNonEmptyRs.isEmpty());
}
@@ -133,6 +146,8 @@ RecordSetTest::testIsEmpty2(void)
void
RecordSetTest::testOperator1(void)
{
+ LOG("RecordSetTest::testOperator1()");
+
// this should throw std::invalid_argument.
mEmptyRs(0, 0);
}
@@ -145,7 +160,9 @@ RecordSetTest::testOperator1(void)
void
RecordSetTest::testOperator2(void)
{
- std::string value("mike");
+ LOG("RecordSetTest::testRows1()");
+
+ const std::string value("mike");
CPPUNIT_ASSERT_EQUAL(value, mNonEmptyRs(1, 1));
}
@@ -159,6 +176,8 @@ RecordSetTest::testOperator2(void)
void
RecordSetTest::testOperator3(void)
{
+ LOG("RecordSetTest::testOperator3()");
+
// this should throw std::out_of_range.
mNonEmptyRs(2, 2);
}
@@ -171,7 +190,9 @@ RecordSetTest::testOperator3(void)
void
RecordSetTest::testOperator4(void)
{
- std::string value("1");
+ LOG("RecordSetTest::testOperator4()");
+
+ const std::string value("1");
CPPUNIT_ASSERT_EQUAL(value, mNonEmptyRs(0, "id"));
}
@@ -184,6 +205,8 @@ RecordSetTest::testOperator4(void)
void
RecordSetTest::testOperator5(void)
{
+ LOG("RecordSetTest::testOperator5()");
+
// this should throw std::out_of_range.
mNonEmptyRs(3, "id");
}
@@ -196,6 +219,8 @@ RecordSetTest::testOperator5(void)
void
RecordSetTest::testOperator6(void)
{
+ LOG("RecordSetTest::testOperator6()");
+
// this should throw std::invalid_argument.
mNonEmptyRs(1, "noname");
}
@@ -207,7 +232,9 @@ RecordSetTest::testOperator6(void)
void
RecordSetTest::testOutputStream1(void)
{
- std::string emptyStr;
+ LOG("RecordSetTest::testOutputStream1()");
+
+ const std::string emptyStr;
std::ostringstream os;
os << mEmptyRs;
@@ -222,6 +249,8 @@ RecordSetTest::testOutputStream1(void)
void
RecordSetTest::testOutputStream2(void)
{
+ LOG("RecordSetTest::testOutputStream2()");
+
std::ostringstream os1;
os1 << "|id|name|" << std::endl << std::endl
<< "|1|john|" << std::endl
@@ -240,8 +269,10 @@ RecordSetTest::testOutputStream2(void)
void
RecordSetTest::testAdd1(void)
{
- std::string id("3");
- std::string name("elena");
+ LOG("RecordSetTest::testAdd1()");
+
+ const std::string id("3");
+ const std::string name("elena");
Row r;
r.push_back(id);
@@ -262,6 +293,8 @@ RecordSetTest::testAdd1(void)
void
RecordSetTest::testAdd2(void)
{
+ LOG("RecordSetTest::testAdd2()");
+
Row r;
r.push_back("4");
@@ -277,6 +310,8 @@ RecordSetTest::testAdd2(void)
void
RecordSetTest::testAdd3(void)
{
+ LOG("RecordSetTest::testAdd3()");
+
Row r;
r.push_back("5");
diff --git a/src/tests/testsmain.cpp b/src/tests/testsmain.cpp
index 70cdc185..21237601 100644
--- a/src/tests/testsmain.cpp
+++ b/src/tests/testsmain.cpp
@@ -26,6 +26,8 @@
#include <physfs.h>
+#include "../utils/logger.h"
+
/**
* Notes:
@@ -45,6 +47,9 @@ int main(int argc, char* argv[])
PHYSFS_addToSearchPath(".", 1);
PHYSFS_setWriteDir(".");
+ // initialize the logger.
+ tmwserv::utils::Logger::instance().setTimestamp(false);
+
using namespace CppUnit;
// get the top level suite from the registry.
diff --git a/src/tests/teststorage.cpp b/src/tests/teststorage.cpp
index 7a7cb7fd..977c2940 100644
--- a/src/tests/teststorage.cpp
+++ b/src/tests/teststorage.cpp
@@ -38,6 +38,7 @@
#include "../utils/cipher.h"
#include "../utils/functors.h"
+#include "../utils/logger.h"
#include "../dalstoragesql.h"
#include "../storage.h"
#include "teststorage.h"
@@ -99,6 +100,8 @@ StorageTest::tearDown(void)
void
StorageTest::testGetAccount1(void)
{
+ LOG("StorageTest::testGetAccount1()");
+
Storage& myStorage = Storage::instance(mStorageName);
CPPUNIT_ASSERT(myStorage.isOpen());
@@ -124,6 +127,8 @@ StorageTest::testGetAccount1(void)
void
StorageTest::testGetAccount2(void)
{
+ LOG("StorageTest::testGetAccount2()");
+
Storage& myStorage = Storage::instance(mStorageName);
if (!myStorage.isOpen()) {
@@ -142,6 +147,8 @@ StorageTest::testGetAccount2(void)
void
StorageTest::testAddAccount1(void)
{
+ LOG("StorageTest::testAddAccount1()");
+
Storage& myStorage = Storage::instance(mStorageName);
if (!myStorage.isOpen()) {
@@ -151,58 +158,14 @@ StorageTest::testAddAccount1(void)
// TODO: when addAccount will throw exceptions, test the exceptions
// thrown.
// nothing should happen at the moment.
- myStorage.addAccount(NULL);
+ AccountPtr nullAccount(0);
+ myStorage.addAccount(nullAccount);
myStorage.flush();
-#if defined (MYSQL_SUPPORT) || defined (POSTGRESQL_SUPPORT) || \
- defined (SQLITE_SUPPORT)
-
- using namespace tmwserv::dal;
-
- std::auto_ptr<DataProvider> db(DataProviderFactory::createDataProvider());
-
- try {
-#ifdef SQLITE_SUPPORT
- std::string dbFile(mStorageName);
- dbFile += ".db";
- db->connect(dbFile, mStorageUser, mStorageUserPassword);
-#else
- db->connect(mStorageName, mStorageUser, mStorageUserPassword);
-#endif
-
- std::string sql("select * from ");
- sql += ACCOUNTS_TBL_NAME;
- sql += ";";
- const RecordSet& rs = db->execSql(sql);
-
- CPPUNIT_ASSERT(rs.rows() == 3);
-
- const std::string frodo("frodo");
- const std::string merry("merry");
- const std::string pippin("pippin");
-
- CPPUNIT_ASSERT_EQUAL(frodo, rs(0, "username"));
- CPPUNIT_ASSERT_EQUAL(merry, rs(1, "username"));
- CPPUNIT_ASSERT_EQUAL(pippin, rs(2, "username"));
-
- db->disconnect();
- }
- catch (const DbConnectionFailure& e) {
- CPPUNIT_FAIL(e.what());
- }
- catch (const DbSqlQueryExecFailure& e) {
- CPPUNIT_FAIL(e.what());
- }
- catch (const DbDisconnectionFailure& e) {
- CPPUNIT_FAIL(e.what());
- }
- catch (const std::exception& e) {
- CPPUNIT_FAIL(e.what());
- }
- catch (...) {
- CPPUNIT_FAIL("unexpected exception");
- }
-#endif
+ // check the database.
+ Checks checks;
+ checks.set(CHK_DEFAULT_ACCOUNTS);
+ checkDb(checks);
}
@@ -212,6 +175,8 @@ StorageTest::testAddAccount1(void)
void
StorageTest::testAddAccount2(void)
{
+ LOG("StorageTest::testAddAccount2()");
+
Storage& myStorage = Storage::instance(mStorageName);
if (!myStorage.isOpen()) {
@@ -219,91 +184,29 @@ StorageTest::testAddAccount2(void)
}
// prepare new account.
- RawStatistics stats;
- stats.strength = 1;
- stats.agility = 1;
- stats.vitality = 1;
- stats.intelligence = 1;
- stats.dexterity = 1;
- stats.luck = 1;
+ RawStatistics stats = {1, 1, 1, 1, 1, 1};
const std::string sam1("sam1");
const std::string sam2("sam2");
- Being* b1 = new Being(sam1, GENDER_MALE, 0, 0, stats);
- Being* b2 = new Being(sam2, GENDER_MALE, 0, 0, stats);
+ BeingPtr b1(new Being(sam1, GENDER_MALE, 0, 0, stats));
+ BeingPtr b2(new Being(sam2, GENDER_MALE, 0, 0, stats));
Beings characters;
characters.push_back(b1);
characters.push_back(b2);
const std::string sam("sam");
- Account* acc = new Account(sam, sam, "sam@domain", characters);
+ AccountPtr acc(new Account(sam, sam, "sam@domain", characters));
// TODO: when addAccount will throw exceptions, test the exceptions
// thrown.
myStorage.addAccount(acc);
myStorage.flush();
-#if defined (MYSQL_SUPPORT) || defined (POSTGRESQL_SUPPORT) || \
- defined (SQLITE_SUPPORT)
-
- using namespace tmwserv::dal;
-
- std::auto_ptr<DataProvider> db(DataProviderFactory::createDataProvider());
-
- try {
-#ifdef SQLITE_SUPPORT
- std::string dbFile(mStorageName);
- dbFile += ".db";
- db->connect(dbFile, mStorageUser, mStorageUserPassword);
-#else
- db->connect(mStorageName, mStorageUser, mStorageUserPassword);
-#endif
-
- std::string sql("select * from ");
- sql += ACCOUNTS_TBL_NAME;
- sql += ";";
- const RecordSet& rs = db->execSql(sql);
-
- CPPUNIT_ASSERT(rs.rows() == 4);
-
- const std::string frodo("frodo");
- const std::string merry("merry");
- const std::string pippin("pippin");
-
- CPPUNIT_ASSERT_EQUAL(frodo, rs(0, "username"));
- CPPUNIT_ASSERT_EQUAL(merry, rs(1, "username"));
- CPPUNIT_ASSERT_EQUAL(pippin, rs(2, "username"));
- CPPUNIT_ASSERT_EQUAL(sam, rs(3, "username"));
-
- sql = "select * from ";
- sql += CHARACTERS_TBL_NAME;
- sql += " where user_id = 4;";
-
- db->execSql(sql);
-
- CPPUNIT_ASSERT(rs.rows() == 2);
-
- CPPUNIT_ASSERT_EQUAL(sam1, rs(0, "name"));
- CPPUNIT_ASSERT_EQUAL(sam2, rs(1, "name"));
-
- db->disconnect();
- }
- catch (const DbConnectionFailure& e) {
- CPPUNIT_FAIL(e.what());
- }
- catch (const DbSqlQueryExecFailure& e) {
- CPPUNIT_FAIL(e.what());
- }
- catch (const DbDisconnectionFailure& e) {
- CPPUNIT_FAIL(e.what());
- }
- catch (const std::exception& e) {
- CPPUNIT_FAIL(e.what());
- }
- catch (...) {
- CPPUNIT_FAIL("unexpected exception");
- }
-#endif
+ // check the database.
+ Checks checks;
+ checks.set(CHK_NEW_ADDED_ACCOUNT);
+ checks.set(CHK_CHARACTERS_4);
+ checkDb(checks);
}
@@ -313,6 +216,8 @@ StorageTest::testAddAccount2(void)
void
StorageTest::testUpdAccount1(void)
{
+ LOG("StorageTest::testUpdAccount1()");
+
Storage& myStorage = Storage::instance(mStorageName);
CPPUNIT_ASSERT(myStorage.isOpen());
@@ -322,18 +227,12 @@ StorageTest::testUpdAccount1(void)
Account* account = myStorage.getAccount(name);
// create new characters.
- RawStatistics stats;
- stats.strength = 1;
- stats.agility = 1;
- stats.vitality = 1;
- stats.intelligence = 1;
- stats.dexterity = 1;
- stats.luck = 1;
+ RawStatistics stats = {1, 1, 1, 1, 1, 1};
const std::string sam1("sam1");
const std::string sam2("sam2");
- Being* b1 = new Being(sam1, GENDER_MALE, 0, 0, stats);
- Being* b2 = new Being(sam2, GENDER_MALE, 0, 0, stats);
+ BeingPtr b1(new Being(sam1, GENDER_MALE, 0, 0, stats));
+ BeingPtr b2(new Being(sam2, GENDER_MALE, 0, 0, stats));
// add the characters to the account.
account->addCharacter(b1);
@@ -342,66 +241,11 @@ StorageTest::testUpdAccount1(void)
// update the database.
myStorage.flush();
-#if defined (MYSQL_SUPPORT) || defined (POSTGRESQL_SUPPORT) || \
- defined (SQLITE_SUPPORT)
-
- using namespace tmwserv::dal;
-
- std::auto_ptr<DataProvider> db(DataProviderFactory::createDataProvider());
-
- try {
-#ifdef SQLITE_SUPPORT
- std::string dbFile(mStorageName);
- dbFile += ".db";
- db->connect(dbFile, mStorageUser, mStorageUserPassword);
-#else
- db->connect(mStorageName, mStorageUser, mStorageUserPassword);
-#endif
-
- std::string sql("select * from ");
- sql += ACCOUNTS_TBL_NAME;
- sql += ";";
- const RecordSet& rs = db->execSql(sql);
-
- CPPUNIT_ASSERT(rs.rows() == 3);
-
- const std::string frodo("frodo");
- const std::string merry("merry");
- const std::string pippin("pippin");
-
- CPPUNIT_ASSERT_EQUAL(frodo, rs(0, "username"));
- CPPUNIT_ASSERT_EQUAL(merry, rs(1, "username"));
- CPPUNIT_ASSERT_EQUAL(pippin, rs(2, "username"));
-
- sql = "select * from ";
- sql += CHARACTERS_TBL_NAME;
- sql += " where user_id = 1;";
-
- db->execSql(sql);
-
- CPPUNIT_ASSERT(rs.rows() == 2);
-
- CPPUNIT_ASSERT_EQUAL(sam1, rs(0, "name"));
- CPPUNIT_ASSERT_EQUAL(sam2, rs(1, "name"));
-
- db->disconnect();
- }
- catch (const DbConnectionFailure& e) {
- CPPUNIT_FAIL(e.what());
- }
- catch (const DbSqlQueryExecFailure& e) {
- CPPUNIT_FAIL(e.what());
- }
- catch (const DbDisconnectionFailure& e) {
- CPPUNIT_FAIL(e.what());
- }
- catch (const std::exception& e) {
- CPPUNIT_FAIL(e.what());
- }
- catch (...) {
- CPPUNIT_FAIL("unexpected exception");
- }
-#endif
+ // check the database.
+ Checks checks;
+ checks.set(CHK_DEFAULT_ACCOUNTS);
+ checks.set(CHK_CHARACTERS_1);
+ checkDb(checks);
}
@@ -412,6 +256,8 @@ StorageTest::testUpdAccount1(void)
void
StorageTest::testUpdAccount2(void)
{
+ LOG("StorageTest::testUpdAccount2()");
+
Storage& myStorage = Storage::instance(mStorageName);
CPPUNIT_ASSERT(myStorage.isOpen());
@@ -421,18 +267,12 @@ StorageTest::testUpdAccount2(void)
Account* account = myStorage.getAccount(name);
// create new characters.
- RawStatistics stats;
- stats.strength = 1;
- stats.agility = 1;
- stats.vitality = 1;
- stats.intelligence = 1;
- stats.dexterity = 1;
- stats.luck = 1;
+ RawStatistics stats = {1, 1, 1, 1, 1, 1};
const std::string sam1("sam1");
const std::string sam2("sam2");
- Being* b1 = new Being(sam1, GENDER_MALE, 0, 0, stats);
- Being* b2 = new Being(sam2, GENDER_MALE, 0, 0, stats);
+ BeingPtr b1(new Being(sam1, GENDER_MALE, 0, 0, stats));
+ BeingPtr b2(new Being(sam2, GENDER_MALE, 0, 0, stats));
// add the characters to the account.
account->addCharacter(b1);
@@ -528,59 +368,151 @@ StorageTest::testUpdAccount2(void)
void
StorageTest::testDelAccount1(void)
{
+ LOG("StorageTest::testDelAccount1()");
+
Storage& myStorage = Storage::instance(mStorageName);
CPPUNIT_ASSERT(myStorage.isOpen());
myStorage.delAccount("frodo");
-#if defined (MYSQL_SUPPORT) || defined (POSTGRESQL_SUPPORT) || \
- defined (SQLITE_SUPPORT)
+ // check the database.
+ Checks checks;
+ checks.set(CHK_1ST_ACCOUNT_DELETED);
+ checkDb(checks);
+}
- using namespace tmwserv::dal;
- std::auto_ptr<DataProvider> db(DataProviderFactory::createDataProvider());
+/**
+ * Test deleting an account that was added to the storage but not
+ * yet persisted.
+ */
+void
+StorageTest::testDelAccount2(void)
+{
+ LOG("StorageTest::testDelAccount2()");
- try {
-#ifdef SQLITE_SUPPORT
- std::string dbFile(mStorageName);
- dbFile += ".db";
- db->connect(dbFile, mStorageUser, mStorageUserPassword);
-#else
- db->connect(mStorageName, mStorageUser, mStorageUserPassword);
-#endif
+ Storage& myStorage = Storage::instance(mStorageName);
- std::string sql("select * from ");
- sql += ACCOUNTS_TBL_NAME;
- sql += ";";
- const RecordSet& rs = db->execSql(sql);
+ if (!myStorage.isOpen()) {
+ CPPUNIT_FAIL("the storage is not opened.");
+ }
- CPPUNIT_ASSERT(rs.rows() == 2);
+ // prepare new account.
+ RawStatistics stats = {1, 1, 1, 1, 1, 1};
- const std::string merry("merry");
- const std::string pippin("pippin");
+ const std::string sam1("sam1");
+ const std::string sam2("sam2");
+ BeingPtr b1(new Being(sam1, GENDER_MALE, 0, 0, stats));
+ BeingPtr b2(new Being(sam2, GENDER_MALE, 0, 0, stats));
+ Beings characters;
+ characters.push_back(b1);
+ characters.push_back(b2);
- CPPUNIT_ASSERT_EQUAL(merry, rs(0, "username"));
- CPPUNIT_ASSERT_EQUAL(pippin, rs(1, "username"));
+ const std::string sam("sam");
+ AccountPtr acc(new Account(sam, sam, "sam@domain", characters));
- db->disconnect();
- }
- catch (const DbConnectionFailure& e) {
- CPPUNIT_FAIL(e.what());
- }
- catch (const DbSqlQueryExecFailure& e) {
- CPPUNIT_FAIL(e.what());
- }
- catch (const DbDisconnectionFailure& e) {
- CPPUNIT_FAIL(e.what());
+ myStorage.addAccount(acc);
+ myStorage.delAccount(sam);
+
+ // nothing should be added to the database.
+ myStorage.flush();
+
+ // check the database.
+ Checks checks;
+ checks.set(CHK_DEFAULT_ACCOUNTS);
+ checkDb(checks);
+}
+
+
+/**
+ * Test deleting an account that exists in the database and loaded
+ * in memory.
+ */
+void
+StorageTest::testDelAccount3(void)
+{
+ LOG("StorageTest::testDelAccount3()");
+
+ Storage& myStorage = Storage::instance(mStorageName);
+
+ if (!myStorage.isOpen()) {
+ CPPUNIT_FAIL("the storage is not opened.");
}
- catch (const std::exception& e) {
- CPPUNIT_FAIL(e.what());
+
+ // get an existing account.
+ const std::string name("frodo");
+ Account* account = myStorage.getAccount(name);
+
+ CPPUNIT_ASSERT_EQUAL(name, account->getName());
+
+ myStorage.delAccount(name);
+ myStorage.flush();
+
+ // check the database.
+ Checks checks;
+ checks.set(CHK_1ST_ACCOUNT_DELETED);
+ checkDb(checks);
+}
+
+
+/**
+ * Test deleting an account that does not exist.
+ */
+void
+StorageTest::testDelAccount4(void)
+{
+ LOG("StorageTest::testDelAccount4()");
+
+ Storage& myStorage = Storage::instance(mStorageName);
+
+ if (!myStorage.isOpen()) {
+ CPPUNIT_FAIL("the storage is not opened.");
}
- catch (...) {
- CPPUNIT_FAIL("unexpected exception");
+
+ // nothing should happen nor modified in the database.
+ myStorage.delAccount("xxx");
+ myStorage.flush();
+
+ // check the database.
+ Checks checks;
+ checks.set(CHK_DEFAULT_ACCOUNTS);
+ checkDb(checks);
+}
+
+
+/**
+ * Test deleting twice an account that exists in the database and
+ * loaded in memory.
+ */
+void
+StorageTest::testDelAccount5(void)
+{
+ LOG("StorageTest::testDelAccount5()");
+
+ Storage& myStorage = Storage::instance(mStorageName);
+
+ if (!myStorage.isOpen()) {
+ CPPUNIT_FAIL("the storage is not opened.");
}
-#endif
+
+ // get an existing account.
+ const std::string name("frodo");
+ Account* account = myStorage.getAccount(name);
+
+ CPPUNIT_ASSERT_EQUAL(name, account->getName());
+
+ myStorage.delAccount(name);
+ myStorage.flush();
+
+ // delete it again.
+ myStorage.delAccount(name);
+ myStorage.flush();
+
+ // check the database.
+ Checks checks;
+ checks.set(CHK_1ST_ACCOUNT_DELETED);
+ checkDb(checks);
}
@@ -757,3 +689,110 @@ StorageTest::insertAccount(std::auto_ptr<DataProvider>& db,
db->execSql(sql.str());
}
+
+
+/**
+ * Check the state of the database.
+ */
+void
+StorageTest::checkDb(const Checks& what)
+{
+#if defined (MYSQL_SUPPORT) || defined (POSTGRESQL_SUPPORT) || \
+ defined (SQLITE_SUPPORT)
+
+ using namespace tmwserv::dal;
+
+ std::auto_ptr<DataProvider> db(DataProviderFactory::createDataProvider());
+
+ try {
+#ifdef SQLITE_SUPPORT
+ std::string dbFile(mStorageName);
+ dbFile += ".db";
+ db->connect(dbFile, mStorageUser, mStorageUserPassword);
+#else
+ db->connect(mStorageName, mStorageUser, mStorageUserPassword);
+#endif
+
+ std::string sql("select * from ");
+ sql += ACCOUNTS_TBL_NAME;
+ sql += ";";
+ const RecordSet& rs = db->execSql(sql);
+
+ if (what[CHK_DEFAULT_ACCOUNTS]) {
+ CPPUNIT_ASSERT(rs.rows() == 3);
+
+ const std::string frodo("frodo");
+ const std::string merry("merry");
+ const std::string pippin("pippin");
+
+ CPPUNIT_ASSERT_EQUAL(frodo, rs(0, "username"));
+ CPPUNIT_ASSERT_EQUAL(merry, rs(1, "username"));
+ CPPUNIT_ASSERT_EQUAL(pippin, rs(2, "username"));
+ }
+
+ if (what[CHK_1ST_ACCOUNT_DELETED]) {
+ CPPUNIT_ASSERT(rs.rows() == 2);
+
+ const std::string merry("merry");
+ const std::string pippin("pippin");
+
+ CPPUNIT_ASSERT_EQUAL(merry, rs(0, "username"));
+ CPPUNIT_ASSERT_EQUAL(pippin, rs(1, "username"));
+ }
+
+ if (what[CHK_NEW_ADDED_ACCOUNT]) {
+ CPPUNIT_ASSERT(rs.rows() == 4);
+
+ const std::string frodo("frodo");
+ const std::string merry("merry");
+ const std::string pippin("pippin");
+ const std::string sam("sam");
+
+ CPPUNIT_ASSERT_EQUAL(frodo, rs(0, "username"));
+ CPPUNIT_ASSERT_EQUAL(merry, rs(1, "username"));
+ CPPUNIT_ASSERT_EQUAL(pippin, rs(2, "username"));
+ CPPUNIT_ASSERT_EQUAL(sam, rs(3, "username"));
+ }
+
+ if (what[CHK_CHARACTERS_1]) {
+ sql = "select * from ";
+ sql += CHARACTERS_TBL_NAME;
+ sql += " where user_id = 1;";
+ }
+ else if (what[CHK_CHARACTERS_4]) {
+ sql = "select * from ";
+ sql += CHARACTERS_TBL_NAME;
+ sql += " where user_id = 4;";
+ }
+
+ if (what[CHK_CHARACTERS_1] || what[CHK_CHARACTERS_4]) {
+ db->execSql(sql);
+
+ CPPUNIT_ASSERT(rs.rows() == 2);
+
+ const std::string sam1("sam1");
+ const std::string sam2("sam2");
+
+ CPPUNIT_ASSERT_EQUAL(sam1, rs(0, "name"));
+ CPPUNIT_ASSERT_EQUAL(sam2, rs(1, "name"));
+ }
+
+ db->disconnect();
+ }
+ catch (const DbConnectionFailure& e) {
+ CPPUNIT_FAIL(e.what());
+ }
+ catch (const DbSqlQueryExecFailure& e) {
+ CPPUNIT_FAIL(e.what());
+ }
+ catch (const DbDisconnectionFailure& e) {
+ CPPUNIT_FAIL(e.what());
+ }
+ catch (const std::exception& e) {
+ CPPUNIT_FAIL(e.what());
+ }
+ catch (...) {
+ CPPUNIT_FAIL("unexpected exception");
+ }
+#endif
+}
diff --git a/src/tests/teststorage.h b/src/tests/teststorage.h
index da1eae8e..8eef60cc 100644
--- a/src/tests/teststorage.h
+++ b/src/tests/teststorage.h
@@ -25,6 +25,7 @@
#define _TMWSERV_TEST_STORAGE_H_
+#include <bitset>
#include <vector>
#include <cppunit/extensions/HelperMacros.h>
@@ -59,6 +60,10 @@ class StorageTest: public CppUnit::TestFixture
CPPUNIT_TEST(testUpdAccount1);
CPPUNIT_TEST(testUpdAccount2);
CPPUNIT_TEST(testDelAccount1);
+ CPPUNIT_TEST(testDelAccount2);
+ CPPUNIT_TEST(testDelAccount3);
+ CPPUNIT_TEST(testDelAccount4);
+ CPPUNIT_TEST(testDelAccount5);
CPPUNIT_TEST_SUITE_END();
@@ -129,6 +134,37 @@ class StorageTest: public CppUnit::TestFixture
testDelAccount1(void);
+ /**
+ * Test deleting an account that was added to the storage but not
+ * yet persisted.
+ */
+ void
+ testDelAccount2(void);
+
+
+ /**
+ * Test deleting an account that exists in the database and loaded
+ * in memory.
+ */
+ void
+ testDelAccount3(void);
+
+
+ /**
+ * Test deleting an account that does not exist.
+ */
+ void
+ testDelAccount4(void);
+
+
+ /**
+ * Test deleting twice an account that exists in the database and
+ * loaded in memory.
+ */
+ void
+ testDelAccount5(void);
+
+
private:
/**
* Initialize the storage.
@@ -166,6 +202,35 @@ class StorageTest: public CppUnit::TestFixture
const std::string& name);
+ /**
+ * Enumeration type for the bits.
+ * Each bit represents a particular check.
+ */
+ enum CheckValues {
+ CHK_DEFAULT_ACCOUNTS,
+ CHK_1ST_ACCOUNT_DELETED,
+ CHK_NEW_ADDED_ACCOUNT,
+ CHK_CHARACTERS_1,
+ CHK_CHARACTERS_4,
+ NUM_CHECKS
+ };
+
+
+ /**
+ * Type definition for the checks.
+ */
+ typedef std::bitset<NUM_CHECKS> Checks;
+
+
+ /**
+ * Check the state of the database.
+ *
+ * @param what bitmask that contains information about what to check.
+ */
+ void
+ checkDb(const Checks& what);
+
+
private:
static std::string mStorageName; /**< name of the storage */
static std::string mStorageUser; /**< storage user */