summaryrefslogtreecommitdiff
path: root/src/tests/teststorage.cpp
blob: 7a7cb7fdb50fcfe65bf8e9cc41d326afcbfd54ce (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
/*
 *  The Mana World Server
 *  Copyright 2004 The Mana World Development Team
 *
 *  This file is part of The Mana World.
 *
 *  The Mana World  is free software; you can redistribute  it and/or modify it
 *  under the terms of the GNU General  Public License as published by the Free
 *  Software Foundation; either version 2 of the License, or any later version.
 *
 *  The Mana  World is  distributed in  the hope  that it  will be  useful, but
 *  WITHOUT ANY WARRANTY; without even  the implied warranty of MERCHANTABILITY
 *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 *  more details.
 *
 *  You should  have received a  copy of the  GNU General Public  License along
 *  with The Mana  World; if not, write to the  Free Software Foundation, Inc.,
 *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 *  $Id$
 */


#if !defined (MYSQL_SUPPORT) && !defined (SQLITE_SUPPORT) && \
    !defined (POSTGRESQL_SUPPORT)

    // if we are in this block it means that we are not using a database
    // to persist the data from the storage.
    // at the moment, Storage assume that the data are persisted in a database
    // so let's raise a preprocessing error.
#error "no database backend defined"
#endif


#include <sstream>

#include <physfs.h>

#include "../utils/cipher.h"
#include "../utils/functors.h"
#include "../dalstoragesql.h"
#include "../storage.h"
#include "teststorage.h"


// register the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION(StorageTest);


// initialization of static attributes.
std::string StorageTest::mStorageName("tmwteststorage");
std::string StorageTest::mStorageUser("guest");
std::string StorageTest::mStorageUserPassword("guest");


using namespace tmwserv;


/**
 * Set up fixtures.
 */
void
StorageTest::setUp(void)
{
    // reinitialize the storage before each test.
    initStorage();

    // create a storage.
    Storage& myStorage = Storage::instance(mStorageName);
    myStorage.setUser(mStorageUser);
    myStorage.setPassword(mStorageUserPassword);

    // open the storage.
    myStorage.open();
}


/**
 * Tear down fixtures.
 */
void
StorageTest::tearDown(void)
{
    // close the storage.
    Storage& myStorage = Storage::instance(mStorageName);
    myStorage.close();

    // delete the storage.
    Storage::destroy();

    // clean the storage after each test.
    cleanStorage();
}


/**
 * Fetch an existing account from the database.
 */
void
StorageTest::testGetAccount1(void)
{
    Storage& myStorage = Storage::instance(mStorageName);

    CPPUNIT_ASSERT(myStorage.isOpen());

    const std::string name("frodo");
    Account* account = myStorage.getAccount(name);

    using namespace tmwserv::utils;

    const std::string password(Cipher::instance().md5(name));
    const std::string email("frodo@domain");

    CPPUNIT_ASSERT(account != 0);
    CPPUNIT_ASSERT_EQUAL(name, account->getName());
    CPPUNIT_ASSERT_EQUAL(password, account->getPassword());
    CPPUNIT_ASSERT_EQUAL(email, account->getEmail());
}


/**
 * Fetch an unexisting account from the database.
 */
void
StorageTest::testGetAccount2(void)
{
    Storage& myStorage = Storage::instance(mStorageName);

    if (!myStorage.isOpen()) {
        CPPUNIT_FAIL("the storage is not opened.");
    }

    Account* account = myStorage.getAccount("xxx");

    CPPUNIT_ASSERT(account == 0);
}


/**
 * Test passing a null pointer to addAcccount().
 */
void
StorageTest::testAddAccount1(void)
{
    Storage& myStorage = Storage::instance(mStorageName);

    if (!myStorage.isOpen()) {
        CPPUNIT_FAIL("the storage is not opened.");
    }

    // TODO: when addAccount will throw exceptions, test the exceptions
    // thrown.
    // nothing should happen at the moment.
    myStorage.addAccount(NULL);
    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
}


/**
 * Test adding a new account.
 */
void
StorageTest::testAddAccount2(void)
{
    Storage& myStorage = Storage::instance(mStorageName);

    if (!myStorage.isOpen()) {
        CPPUNIT_FAIL("the storage is not opened.");
    }

    // prepare new account.
    RawStatistics stats;
    stats.strength = 1;
    stats.agility = 1;
    stats.vitality = 1;
    stats.intelligence = 1;
    stats.dexterity = 1;
    stats.luck = 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);
    Beings characters;
    characters.push_back(b1);
    characters.push_back(b2);

    const std::string sam("sam");
    Account* 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
}


/**
 * Test updating an existing account with new characters.
 */
void
StorageTest::testUpdAccount1(void)
{
    Storage& myStorage = Storage::instance(mStorageName);

    CPPUNIT_ASSERT(myStorage.isOpen());

    // get an existing account.
    const std::string name("frodo");
    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;

    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);

    // add the characters to the account.
    account->addCharacter(b1);
    account->addCharacter(b2);

    // 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
}


/**
 * Test updating an existing account with a new password and new
 * character stats.
 */
void
StorageTest::testUpdAccount2(void)
{
    Storage& myStorage = Storage::instance(mStorageName);

    CPPUNIT_ASSERT(myStorage.isOpen());

    // get an existing account.
    const std::string name("frodo");
    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;

    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);

    // add the characters to the account.
    account->addCharacter(b1);
    account->addCharacter(b2);

    // update the database.
    myStorage.flush();

    // change the account password.
    using tmwserv::utils::Cipher;
    const std::string newPassword(Cipher::instance().md5("myprecious"));
    account->setPassword(newPassword);

    // change the strength of the first character.
    b1->setStrength(10);

    // 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"));

        CPPUNIT_ASSERT_EQUAL(newPassword, rs(0, "password"));

        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"));

        string_to<unsigned short> toUshort;
        CPPUNIT_ASSERT_EQUAL((unsigned short) 10, toUshort(rs(0, "str")));

        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
}


/**
 * Test deleting an account that exists in the database but not
 * loaded yet in memory.
 */
void
StorageTest::testDelAccount1(void)
{
    Storage& myStorage = Storage::instance(mStorageName);

    CPPUNIT_ASSERT(myStorage.isOpen());

    myStorage.delAccount("frodo");

#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() == 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"));

        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
}


/**
 * Initialize the storage.
 */
void
StorageTest::initStorage(void)
{
#if defined (MYSQL_SUPPORT) || defined (POSTGRESQL_SUPPORT) || \
    defined (SQLITE_SUPPORT)

    // we are using a database to persist the data from the storage.

    using namespace tmwserv::dal;

    // insert initial data using the data provider directly.
    // we must avoid using the APIs from Storage here as it's the purpose
    // of these tests.
    std::auto_ptr<DataProvider> db(DataProviderFactory::createDataProvider());

    try {
#ifdef SQLITE_SUPPORT
        std::string dbFile(mStorageName);
        dbFile += ".db";

        // ensure that the file does not exist before the tests begin.
        if (PHYSFS_exists(dbFile.c_str())) {
            if (PHYSFS_delete(dbFile.c_str()) == 0) {
                CPPUNIT_FAIL(PHYSFS_getLastError());
            }
        }

        db->connect(dbFile, mStorageUser, mStorageUserPassword);
#else
        db->connect(mStorageName, mStorageUser, mStorageUserPassword);
#endif

        // drop the tables.
        dropTable(db, MAPS_TBL_NAME);
        dropTable(db, ACCOUNTS_TBL_NAME);
        dropTable(db, CHARACTERS_TBL_NAME);
        dropTable(db, ITEMS_TBL_NAME);
        dropTable(db, WORLD_ITEMS_TBL_NAME);
        dropTable(db, INVENTORIES_TBL_NAME);

        // recreate the tables.
        db->execSql(SQL_MAPS_TABLE);
        db->execSql(SQL_ACCOUNTS_TABLE);
        db->execSql(SQL_CHARACTERS_TABLE);
        db->execSql(SQL_ITEMS_TABLE);
        db->execSql(SQL_WORLD_ITEMS_TABLE);
        db->execSql(SQL_INVENTORIES_TABLE);

        // populate the tables.
        insertAccount(db, "frodo");
        insertAccount(db, "merry");
        insertAccount(db, "pippin");

        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
}


/**
 * Clean the storage.
 */
void
StorageTest::cleanStorage(void)
{
#if defined (MYSQL_SUPPORT) || defined (POSTGRESQL_SUPPORT) || \
    defined (SQLITE_SUPPORT)

    // we are using a database to persist the data from the storage.

    using namespace tmwserv::dal;

    std::auto_ptr<DataProvider> db(DataProviderFactory::createDataProvider());

    try {
#ifdef SQLITE_SUPPORT
        std::string dbFile(mStorageName);
        dbFile += ".db";

        // ensure that the file does not exist before the tests begin.
        if (PHYSFS_exists(dbFile.c_str())) {
            if (PHYSFS_delete(dbFile.c_str()) == 0) {
                CPPUNIT_FAIL(PHYSFS_getLastError());
            }
        }
#else
        db->connect(mStorageName, mStorageUser, mStorageUserPassword);

        // drop the tables.
        dropTable(db, MAPS_TBL_NAME);
        dropTable(db, ACCOUNTS_TBL_NAME);
        dropTable(db, CHARACTERS_TBL_NAME);
        dropTable(db, ITEMS_TBL_NAME);
        dropTable(db, WORLD_ITEMS_TBL_NAME);
        dropTable(db, INVENTORIES_TBL_NAME);

        db->disconnect();
#endif
    }
    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
}


/**
 * Drop a table.
 */
void
StorageTest::dropTable(std::auto_ptr<DataProvider>& db,
                       const std::string& name)
{
    try {
        std::string sql("drop table ");
        sql += name;
        sql += ";";
        db->execSql(sql);
    }
    catch (const DbSqlQueryExecFailure& e) {
        // ignore, the table may not exist.
    }
}


/**
 * Insert a new account.
 */
void
StorageTest::insertAccount(std::auto_ptr<DataProvider>& db,
                           const std::string& name)
{
    std::ostringstream sql;

    // the password will be identical to the name.

    sql << "insert into " << ACCOUNTS_TBL_NAME << " values "
        << "(null, '" << name << "', '"
        << tmwserv::utils::Cipher::instance().md5(name) << "', '"
        << name << "@domain', 0, 0);";

    db->execSql(sql.str());
}