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 #include "MappingsTest.h"
00055 #include "ConfigSyncSourceTest.h"
00056 #include "SyncManagerTest.h"
00057
00058 USE_NAMESPACE
00059
00069 class CheckSyncReport {
00070 public:
00071 CheckSyncReport(int clAdded = -1, int clUpdated = -1, int clDeleted = -1,
00072 int srAdded = -1, int srUpdated = -1, int srDeleted = -1) :
00073 clientAdded(clAdded),
00074 clientUpdated(clUpdated),
00075 clientDeleted(clDeleted),
00076 serverAdded(srAdded),
00077 serverUpdated(srUpdated),
00078 serverDeleted(srDeleted)
00079 {}
00080
00081 virtual ~CheckSyncReport() {}
00082
00083 const int clientAdded, clientUpdated, clientDeleted,
00084 serverAdded, serverUpdated, serverDeleted;
00085
00093 virtual void check(int res, SyncReport &report) const;
00094 };
00095
00096 class LocalTests;
00097 class SyncTests;
00098
00142 class ClientTest {
00143 public:
00144 ClientTest(int serverSleepSec = 0, const std::string &serverLog= "");
00145 virtual ~ClientTest();
00146
00155 virtual void registerTests();
00156
00157 struct Config;
00158
00169 virtual LocalTests *createLocalTests(const std::string &name, int sourceParam, ClientTest::Config &co);
00170
00179 virtual SyncTests *createSyncTests(const std::string &name, std::vector<int> sourceIndices, bool isClientA = true);
00180
00184 static int dump(ClientTest &client, SyncSource &source, const char *file);
00185
00189 static int import(ClientTest &client, SyncSource &source, const char *file);
00190
00195 static bool compare(ClientTest &client, const char *fileA, const char *fileB);
00196
00211 static void getTestData(const char *type, Config &config);
00212
00220 struct Config {
00224 const char *sourceName;
00225
00229 const char *uri;
00230
00254 typedef SyncSource *(*createsource_t)(ClientTest &client, int source, bool isSourceA);
00255
00261 createsource_t createSourceA;
00262
00279 createsource_t createSourceB;
00280
00288 const char *templateItem;
00289
00294 const char *uniqueProperties;
00295
00299 int numItems;
00300
00305 const char *sizeProperty;
00306
00311 const char *insertItem;
00312
00318 const char *updateItem;
00319
00325 const char *complexUpdateItem;
00326
00331 const char *mergeItem1;
00332
00339 const char *mergeItem2;
00340
00357 const char *parentItem, *childItem;
00358
00364 #ifndef LINKED_ITEMS_RELAXED_SEMANTIC
00365 # define LINKED_ITEMS_RELAXED_SEMANTIC 1
00366 #endif
00367
00379 int (*dump)(ClientTest &client, SyncSource &source, const char *file);
00380
00393 int (*import)(ClientTest &client, SyncSource &source, const char *file);
00394
00402 bool (*compare)(ClientTest &client, const char *fileA, const char *fileB);
00403
00407 const char *testcases;
00408
00413 const char *type;
00414 };
00415
00420 virtual int getNumSources() = 0;
00421
00428 virtual void getSourceConfig(int source, Config &config) = 0;
00429
00439 virtual ClientTest *getClientB() = 0;
00440
00446 virtual bool isB64Enabled() = 0;
00447
00467 virtual int sync(
00468 const int *activeSources,
00469 SyncMode syncMode,
00470 const CheckSyncReport &checkReport,
00471 long maxMsgSize = 0,
00472 long maxObjSize = 0,
00473 bool loSupport = false,
00474 const char *encoding = "") = 0;
00475
00485 virtual void postSync(int res, const std::string &logname);
00486
00487 protected:
00491 int serverSleepSeconds;
00492
00498 std::string serverLogFileName;
00499
00500 private:
00506 void *factory;
00507 };
00508
00513 class CreateSource {
00514 public:
00515 CreateSource(ClientTest::Config::createsource_t createSourceParam, ClientTest &clientParam, int sourceParam, bool isSourceAParam) :
00516 createSource(createSourceParam),
00517 client(clientParam),
00518 source(sourceParam),
00519 isSourceA(isSourceAParam) {}
00520
00521 SyncSource *operator() () {
00522 CPPUNIT_ASSERT(createSource);
00523 return createSource(client, source, isSourceA);
00524 }
00525
00526 const ClientTest::Config::createsource_t createSource;
00527 ClientTest &client;
00528 const int source;
00529 const bool isSourceA;
00530 };
00531
00532
00537 class LocalTests : public CppUnit::TestSuite, public CppUnit::TestFixture {
00538 public:
00540 ClientTest &client;
00541
00543 const int source;
00544
00546 const ClientTest::Config config;
00547
00549 CreateSource createSourceA, createSourceB;
00550
00551 LocalTests(const std::string &name, ClientTest &cl, int sourceParam, ClientTest::Config &co) :
00552 CppUnit::TestSuite(name),
00553 client(cl),
00554 source(sourceParam),
00555 config(co),
00556 createSourceA(co.createSourceA, cl, sourceParam, true),
00557 createSourceB(co.createSourceB, cl, sourceParam, false)
00558 {}
00559
00565 virtual void addTests();
00566
00577 virtual std::string insert(CreateSource createSource, const char *data, bool relaxed = false);
00578
00586 virtual void update(CreateSource createSource, const char *data, bool check = true);
00587
00589 virtual void deleteAll(CreateSource createSource);
00590
00599 virtual void compareDatabases(const char *refFile, SyncSource ©, bool raiseAssert = true);
00600
00611 virtual int insertManyItems(CreateSource createSource, int startIndex = 1, int numItems = 0, int size = -1);
00612
00613
00614
00615
00616 virtual void testOpen();
00617 virtual void testIterateTwice();
00618 virtual void testSimpleInsert();
00619 virtual void testLocalDeleteAll();
00620 virtual void testComplexInsert();
00621 virtual void testLocalUpdate();
00622 virtual void testChanges();
00623 virtual void testImport();
00624 virtual void testImportDelete();
00625 virtual void testManyChanges();
00626 virtual void testLinkedItemsParent();
00627 virtual void testLinkedItemsChild();
00628 virtual void testLinkedItemsParentChild();
00629 virtual void testLinkedItemsChildParent();
00630 virtual void testLinkedItemsChildChangesParent();
00631 virtual void testLinkedItemsRemoveParentFirst();
00632 virtual void testLinkedItemsRemoveNormal();
00633 virtual void testLinkedItemsInsertParentTwice();
00634 virtual void testLinkedItemsInsertChildTwice();
00635 virtual void testLinkedItemsParentUpdate();
00636 virtual void testLinkedItemsUpdateChild();
00637 virtual void testLinkedItemsInsertBothUpdateChild();
00638 virtual void testLinkedItemsInsertBothUpdateParent();
00639
00640 };
00641
00642 enum itemType {
00643 NEW_ITEMS,
00644 UPDATED_ITEMS,
00645 DELETED_ITEMS,
00646 TOTAL_ITEMS
00647 };
00648
00655 int countItemsOfType(SyncSource *source, itemType type);
00656
00657 typedef std::list<std::string> UIDList;
00661 UIDList listItemsOfType(SyncSource *source, itemType type);
00662
00668 class SyncTests : public CppUnit::TestSuite, public CppUnit::TestFixture {
00669 public:
00671 ClientTest &client;
00672
00673 SyncTests(const std::string &name, ClientTest &cl, std::vector<int> sourceIndices, bool isClientA = true);
00674 ~SyncTests();
00675
00677 virtual void addTests();
00678
00679 protected:
00681 std::vector< std::pair<int, LocalTests *> > sources;
00682 typedef std::vector< std::pair<int, LocalTests *> >::iterator source_it;
00683
00685 int *sourceArray;
00686
00688 SyncTests *accessClientB;
00689
00691 SyncItemListener* itemListener;
00692
00693
00694 enum DeleteAllMode {
00695 DELETE_ALL_SYNC,
00698 DELETE_ALL_REFRESH
00699 };
00700
00702 virtual void compareDatabases();
00703
00705 virtual void deleteAll(DeleteAllMode mode = DELETE_ALL_SYNC);
00706
00708 virtual void doCopy();
00709
00715 virtual void refreshClient();
00716
00717
00718
00719
00720 virtual void testTwoWaySync() {
00721 sync(SYNC_TWO_WAY);
00722 }
00723
00724
00725 virtual void testSlowSync() {
00726 sync(SYNC_SLOW);
00727 }
00728
00729 virtual void testRefreshFromServerSync() {
00730 sync(SYNC_REFRESH_FROM_SERVER);
00731 }
00732
00733
00734 virtual void testRefreshFromClientSync() {
00735 sync(SYNC_REFRESH_FROM_CLIENT);
00736 }
00737
00738
00739 virtual void testDeleteAllSync() {
00740 deleteAll(DELETE_ALL_SYNC);
00741 }
00742
00743 virtual void testDeleteAllRefresh();
00744 virtual void testRefreshSemantic();
00745 virtual void testRefreshStatus();
00746
00747
00748 void testCopy() {
00749 doCopy();
00750 compareDatabases();
00751 }
00752
00753 virtual void testUpdate();
00754 virtual void testComplexUpdate();
00755 virtual void testDelete();
00756 virtual void testMerge();
00757 virtual void testTwinning();
00758 virtual void testOneWayFromServer();
00759 virtual void testOneWayFromClient();
00760 virtual void testItems();
00761 virtual void testAddUpdate();
00762
00763
00764
00765 void testMaxMsg() {
00766 doVarSizes(true, false, NULL);
00767 }
00768
00769 void testLargeObject() {
00770 doVarSizes(true, true, NULL);
00771 }
00772
00773 void testLargeObjectBin() {
00774 doVarSizes(true, true, "bin");
00775 }
00776
00777 void testLargeObjectEncoded() {
00778 doVarSizes(true, true, "b64");
00779 }
00780
00781 virtual void testManyItems();
00782
00783
00784 virtual void testMappings() {
00785 MappingsTest mapping;
00786 mapping.runTests();
00787 }
00788
00789 virtual void testConfigSyncSource() {
00790 ConfigSyncSourceTest css;
00791 css.runTests();
00792 }
00793
00794 virtual void testServerError506() {
00795 SyncManagerTest test;
00796 test.runTests();
00797 }
00798
00803 virtual void doVarSizes(bool withMaxMsgSize,
00804 bool withLargeObject,
00805 const char *encoding);
00806
00811 virtual void sync(SyncMode syncMode,
00812 const std::string &logprefix = "",
00813 CheckSyncReport checkReport = CheckSyncReport(),
00814 long maxMsgSize = 0,
00815 long maxObjSize = 0,
00816 bool loSupport = false,
00817 const char *encoding = "");
00818 };
00819
00820
00822 #define CLIENT_TEST_EQUAL( _prefix, \
00823 _expected, \
00824 _actual ) \
00825 CPPUNIT_ASSERT_EQUAL_MESSAGE( std::string(_prefix) + ": " + #_expected + " == " + #_actual, \
00826 _expected, \
00827 _actual )
00828
00830 #define SOURCE_ASSERT_NO_FAILURE(_source, _x) \
00831 { \
00832 CPPUNIT_ASSERT_NO_THROW(_x); \
00833 CPPUNIT_ASSERT((_source) && (!(_source)->getReport() || (_source)->getReport()->getState() != SOURCE_ERROR)); \
00834 }
00835
00837 #define SOURCE_ASSERT(_source, _x) \
00838 { \
00839 CPPUNIT_ASSERT(_x); \
00840 CPPUNIT_ASSERT((_source) && (!(_source)->getReport() || (_source)->getReport()->getState() != SOURCE_ERROR)); \
00841 }
00842
00844 #define SOURCE_ASSERT_EQUAL(_source, _value, _x) \
00845 { \
00846 CPPUNIT_ASSERT_EQUAL(_value, _x); \
00847 CPPUNIT_ASSERT((_source) && (!(_source)->getReport() || (_source)->getReport()->getState() != SOURCE_ERROR)); \
00848 }
00849
00851 #define SOURCE_ASSERT_MESSAGE(_message, _source, _x) \
00852 { \
00853 CPPUNIT_ASSERT_MESSAGE((_message), (_x)); \
00854 CPPUNIT_ASSERT((_source) && (!(_source)->getReport() || (_source)->getReport()->getState() != SOURCE_ERROR)); \
00855 }
00856
00857
00865 #define ADD_TEST(_class, _function) \
00866 addTest(new CppUnit::TestCaller<_class>(getName() + "::" #_function, &_class::_function, *this))
00867
00868
00869 #endif // ENABLE_INTEGRATION_TESTS
00870
00871
00874 #endif // INCL_TESTSYNCCLIENT