00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #ifndef INCL_TESTSYNCCLIENT
00037 #define INCL_TESTSYNCCLIENT
00038
00042 #include <string>
00043 #include <vector>
00044 #include <list>
00045 #include "spds/SyncSource.h"
00046 #include "spds/SyncReport.h"
00047
00048 #ifdef ENABLE_INTEGRATION_TESTS
00049
00050 #include <cppunit/TestSuite.h>
00051 #include <cppunit/TestAssert.h>
00052 #include <cppunit/TestFixture.h>
00053 #include "base/globalsdef.h"
00054
00055 BEGIN_NAMESPACE
00056
00066 class CheckSyncReport {
00067 public:
00068 CheckSyncReport(int clAdded = -1, int clUpdated = -1, int clDeleted = -1,
00069 int srAdded = -1, int srUpdated = -1, int srDeleted = -1) :
00070 clientAdded(clAdded),
00071 clientUpdated(clUpdated),
00072 clientDeleted(clDeleted),
00073 serverAdded(srAdded),
00074 serverUpdated(srUpdated),
00075 serverDeleted(srDeleted)
00076 {}
00077
00078 virtual ~CheckSyncReport() {}
00079
00080 const int clientAdded, clientUpdated, clientDeleted,
00081 serverAdded, serverUpdated, serverDeleted;
00082
00090 virtual void check(int res, SyncReport &report) const;
00091 };
00092
00093 class LocalTests;
00094 class SyncTests;
00095
00139 class ClientTest {
00140 public:
00141 ClientTest(int serverSleepSec = 0, const std::string &serverLog= "");
00142 virtual ~ClientTest();
00143
00152 virtual void registerTests();
00153
00154 struct Config;
00155
00166 virtual LocalTests *createLocalTests(const std::string &name, int sourceParam, ClientTest::Config &co);
00167
00176 virtual SyncTests *createSyncTests(const std::string &name, std::vector<int> sourceIndices, bool isClientA = true);
00177
00181 static int dump(ClientTest &client, SyncSource &source, const char *file);
00182
00186 static int import(ClientTest &client, SyncSource &source, const char *file);
00187
00192 static bool compare(ClientTest &client, const char *fileA, const char *fileB);
00193
00208 static void getTestData(const char *type, Config &config);
00209
00217 struct Config {
00221 const char *sourceName;
00222
00226 const char *uri;
00227
00251 typedef SyncSource *(*createsource_t)(ClientTest &client, int source, bool isSourceA);
00252
00258 createsource_t createSourceA;
00259
00276 createsource_t createSourceB;
00277
00285 const char *templateItem;
00286
00291 const char *uniqueProperties;
00292
00296 int numItems;
00297
00302 const char *sizeProperty;
00303
00308 const char *insertItem;
00309
00315 const char *updateItem;
00316
00322 const char *complexUpdateItem;
00323
00328 const char *mergeItem1;
00329
00336 const char *mergeItem2;
00337
00354 const char *parentItem, *childItem;
00355
00361 #ifndef LINKED_ITEMS_RELAXED_SEMANTIC
00362 # define LINKED_ITEMS_RELAXED_SEMANTIC 1
00363 #endif
00364
00376 int (*dump)(ClientTest &client, SyncSource &source, const char *file);
00377
00390 int (*import)(ClientTest &client, SyncSource &source, const char *file);
00391
00399 bool (*compare)(ClientTest &client, const char *fileA, const char *fileB);
00400
00404 const char *testcases;
00405
00410 const char *type;
00411 };
00412
00417 virtual int getNumSources() = 0;
00418
00425 virtual void getSourceConfig(int source, Config &config) = 0;
00426
00436 virtual ClientTest *getClientB() = 0;
00437
00443 virtual bool isB64Enabled() = 0;
00444
00464 virtual int sync(
00465 const int *activeSources,
00466 SyncMode syncMode,
00467 const CheckSyncReport &checkReport,
00468 long maxMsgSize = 0,
00469 long maxObjSize = 0,
00470 bool loSupport = false,
00471 const char *encoding = "") = 0;
00472
00482 virtual void postSync(int res, const std::string &logname);
00483
00484 protected:
00488 int serverSleepSeconds;
00489
00495 std::string serverLogFileName;
00496
00497 private:
00503 void *factory;
00504 };
00505
00510 class CreateSource {
00511 public:
00512 CreateSource(ClientTest::Config::createsource_t createSourceParam, ClientTest &clientParam, int sourceParam, bool isSourceAParam) :
00513 createSource(createSourceParam),
00514 client(clientParam),
00515 source(sourceParam),
00516 isSourceA(isSourceAParam) {}
00517
00518 SyncSource *operator() () {
00519 CPPUNIT_ASSERT(createSource);
00520 return createSource(client, source, isSourceA);
00521 }
00522
00523 const ClientTest::Config::createsource_t createSource;
00524 ClientTest &client;
00525 const int source;
00526 const bool isSourceA;
00527 };
00528
00529
00534 class LocalTests : public CppUnit::TestSuite, public CppUnit::TestFixture {
00535 public:
00537 ClientTest &client;
00538
00540 const int source;
00541
00543 const ClientTest::Config config;
00544
00546 CreateSource createSourceA, createSourceB;
00547
00548 LocalTests(const std::string &name, ClientTest &cl, int sourceParam, ClientTest::Config &co) :
00549 CppUnit::TestSuite(name),
00550 client(cl),
00551 source(sourceParam),
00552 config(co),
00553 createSourceA(co.createSourceA, cl, sourceParam, true),
00554 createSourceB(co.createSourceB, cl, sourceParam, false)
00555 {}
00556
00562 virtual void addTests();
00563
00573 virtual std::string insert(CreateSource createSource, const char *data);
00574
00582 virtual void update(CreateSource createSource, const char *data, bool check = true);
00583
00585 virtual void deleteAll(CreateSource createSource);
00586
00595 virtual void compareDatabases(const char *refFile, SyncSource ©, bool raiseAssert = true);
00596
00607 virtual int insertManyItems(CreateSource createSource, int startIndex = 1, int numItems = 0, int size = -1);
00608
00609
00610
00611
00612 virtual void testOpen();
00613 virtual void testIterateTwice();
00614 virtual void testSimpleInsert();
00615 virtual void testLocalDeleteAll();
00616 virtual void testComplexInsert();
00617 virtual void testLocalUpdate();
00618 virtual void testChanges();
00619 virtual void testImport();
00620 virtual void testImportDelete();
00621 virtual void testManyChanges();
00622 virtual void testLinkedItems();
00623 };
00624
00625 enum itemType {
00626 NEW_ITEMS,
00627 UPDATED_ITEMS,
00628 DELETED_ITEMS,
00629 TOTAL_ITEMS
00630 };
00631
00638 int countItemsOfType(SyncSource *source, itemType type);
00639
00640 typedef std::list<std::string> UIDList;
00644 UIDList listItemsOfType(SyncSource *source, itemType type);
00645
00651 class SyncTests : public CppUnit::TestSuite, public CppUnit::TestFixture {
00652 public:
00654 ClientTest &client;
00655
00656 SyncTests(const std::string &name, ClientTest &cl, std::vector<int> sourceIndices, bool isClientA = true);
00657 ~SyncTests();
00658
00660 virtual void addTests();
00661
00662 protected:
00664 std::vector< std::pair<int, LocalTests *> > sources;
00665 typedef std::vector< std::pair<int, LocalTests *> >::iterator source_it;
00666
00668 int *sourceArray;
00669
00671 SyncTests *accessClientB;
00672
00673 enum DeleteAllMode {
00674 DELETE_ALL_SYNC,
00677 DELETE_ALL_REFRESH
00678 };
00679
00681 virtual void compareDatabases();
00682
00684 virtual void deleteAll(DeleteAllMode mode = DELETE_ALL_SYNC);
00685
00687 virtual void doCopy();
00688
00694 virtual void refreshClient();
00695
00696
00697
00698
00699 virtual void testTwoWaySync() {
00700 sync(SYNC_TWO_WAY);
00701 }
00702
00703
00704 virtual void testSlowSync() {
00705 sync(SYNC_SLOW);
00706 }
00707
00708 virtual void testRefreshFromServerSync() {
00709 sync(SYNC_REFRESH_FROM_SERVER);
00710 }
00711
00712
00713 virtual void testRefreshFromClientSync() {
00714 sync(SYNC_REFRESH_FROM_CLIENT);
00715 }
00716
00717
00718 virtual void testDeleteAllSync() {
00719 deleteAll(DELETE_ALL_SYNC);
00720 }
00721
00722 virtual void testDeleteAllRefresh();
00723 virtual void testRefreshSemantic();
00724 virtual void testRefreshStatus();
00725
00726
00727 void testCopy() {
00728 doCopy();
00729 compareDatabases();
00730 }
00731
00732 virtual void testUpdate();
00733 virtual void testComplexUpdate();
00734 virtual void testDelete();
00735 virtual void testMerge();
00736 virtual void testTwinning();
00737 virtual void testOneWayFromServer();
00738 virtual void testOneWayFromClient();
00739 virtual void testItems();
00740 virtual void testAddUpdate();
00741
00742
00743 void testMaxMsg() {
00744 doVarSizes(true, false, NULL);
00745 }
00746
00747 void testLargeObject() {
00748 doVarSizes(true, true, NULL);
00749 }
00750
00751 void testLargeObjectBin() {
00752 doVarSizes(true, true, "bin");
00753 }
00754
00755 void testLargeObjectEncoded() {
00756 doVarSizes(true, true, "b64");
00757 }
00758
00759 virtual void testManyItems();
00760
00761
00766 virtual void doVarSizes(bool withMaxMsgSize,
00767 bool withLargeObject,
00768 const char *encoding);
00769
00774 virtual void sync(SyncMode syncMode,
00775 const std::string &logprefix = "",
00776 CheckSyncReport checkReport = CheckSyncReport(),
00777 long maxMsgSize = 0,
00778 long maxObjSize = 0,
00779 bool loSupport = false,
00780 const char *encoding = "");
00781 };
00782
00783
00785 #define CLIENT_TEST_EQUAL( _prefix, \
00786 _expected, \
00787 _actual ) \
00788 CPPUNIT_ASSERT_EQUAL_MESSAGE( std::string(_prefix) + ": " + #_expected + " == " + #_actual, \
00789 _expected, \
00790 _actual )
00791
00793 #define SOURCE_ASSERT_NO_FAILURE(_source, _x) \
00794 { \
00795 CPPUNIT_ASSERT_NO_THROW(_x); \
00796 CPPUNIT_ASSERT((_source) && (!(_source)->getReport() || (_source)->getReport()->getState() != SOURCE_ERROR)); \
00797 }
00798
00800 #define SOURCE_ASSERT(_source, _x) \
00801 { \
00802 CPPUNIT_ASSERT(_x); \
00803 CPPUNIT_ASSERT((_source) && (!(_source)->getReport() || (_source)->getReport()->getState() != SOURCE_ERROR)); \
00804 }
00805
00807 #define SOURCE_ASSERT_EQUAL(_source, _value, _x) \
00808 { \
00809 CPPUNIT_ASSERT_EQUAL(_value, _x); \
00810 CPPUNIT_ASSERT((_source) && (!(_source)->getReport() || (_source)->getReport()->getState() != SOURCE_ERROR)); \
00811 }
00812
00814 #define SOURCE_ASSERT_MESSAGE(_message, _source, _x) \
00815 { \
00816 CPPUNIT_ASSERT_MESSAGE((_message), (_x)); \
00817 CPPUNIT_ASSERT((_source) && (!(_source)->getReport() || (_source)->getReport()->getState() != SOURCE_ERROR)); \
00818 }
00819
00820
00828 #define ADD_TEST(_class, _function) \
00829 addTest(new CppUnit::TestCaller<_class>(getName() + "::" #_function, &_class::_function, *this))
00830
00831
00832 #endif // ENABLE_INTEGRATION_TESTS
00833
00834
00835 END_NAMESPACE
00836
00839 #endif // INCL_TESTSYNCCLIENT