⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clienttest.cpp

📁 This SDK allows to integrate a syncml stack in a C++ application on a variety of platforms. Current
💻 CPP
📖 第 1 页 / 共 5 页
字号:
        CPPUNIT_ASSERT(data);

        // create source
        std::auto_ptr<SyncSource> source(createSource());
        CPPUNIT_ASSERT(source.get() != 0);
        SOURCE_ASSERT(source.get(), source->beginSync() == 0);

        // get existing item, then update it
        SOURCE_ASSERT(source.get(), source->beginSync() == 0 );
        std::auto_ptr<SyncItem> item;
        SOURCE_ASSERT_NO_FAILURE(source.get(), item.reset(source->getFirstItem()) );
        CPPUNIT_ASSERT(item.get());
        item->setData(data, (long)strlen(data) + 1);
        item->setDataType(TEXT(""));
        SOURCE_ASSERT_EQUAL(source.get(), (int)STC_OK, source->updateItem(*item));
        SOURCE_ASSERT(source.get(), source->endSync() == 0);
        CPPUNIT_ASSERT_NO_THROW(source.reset());

        // check that the right changes are reported when reopening the source
        SOURCE_ASSERT_NO_FAILURE(source.get(), source.reset(createSource()));
        SOURCE_ASSERT(source.get(), source->beginSync() == 0 );
        CPPUNIT_ASSERT_EQUAL(1, countItems(source.get()));
        CPPUNIT_ASSERT_EQUAL(0, countNewItems(source.get()));
        CPPUNIT_ASSERT_EQUAL(0, countUpdatedItems(source.get()));
        CPPUNIT_ASSERT_EQUAL(0, countDeletedItems(source.get()));
        std::auto_ptr<SyncItem> modifiedItem;
        SOURCE_ASSERT_NO_FAILURE(source.get(), modifiedItem.reset(source->getFirstItem()) );
        CPPUNIT_ASSERT(modifiedItem.get());
        CPPUNIT_ASSERT( wcslen( item->getKey() ) );
        CPPUNIT_ASSERT( !wcscmp( item->getKey(), modifiedItem->getKey() ) );
    }

    /** deletes all items locally via sync source */
    void deleteAll(CreateSource createSource) {
        CPPUNIT_ASSERT(createSource.createSource);

        // create source
        std::auto_ptr<SyncSource> source(createSource());
        CPPUNIT_ASSERT(source.get() != 0);
        SOURCE_ASSERT(source.get(), source->beginSync() == 0);

        // delete all items
        std::auto_ptr<SyncItem> item;
        SOURCE_ASSERT_NO_FAILURE(source.get(), item.reset(source->getFirstItem()));
        while (item.get()) {
            SOURCE_ASSERT_EQUAL(source.get(), (int)STC_OK, source->deleteItem(*item));
            SOURCE_ASSERT_NO_FAILURE(source.get(), item.reset(source->getNextItem()));
        }
        SOURCE_ASSERT(source.get(), source->endSync() == 0);
        CPPUNIT_ASSERT_NO_THROW(source.reset());

        // check that all items are gone
        SOURCE_ASSERT_NO_FAILURE(source.get(), source.reset(createSource()));
        SOURCE_ASSERT_EQUAL(source.get(), 0, source->beginSync());
        SOURCE_ASSERT_MESSAGE(
            "should be empty now",
            source.get(),
            countItems(source.get()) == 0);
        CPPUNIT_ASSERT_EQUAL( 0, countNewItems(source.get()) );
        CPPUNIT_ASSERT_EQUAL( 0, countUpdatedItems(source.get()) );
        CPPUNIT_ASSERT_EQUAL( 0, countDeletedItems(source.get()) );
        SOURCE_ASSERT_EQUAL(source.get(), 0, source->endSync());
        CPPUNIT_ASSERT_NO_THROW(source.reset());
    }

    /**
     * takes two databases, exports them,
     * then compares them using synccompare
     *
     * @param refFile      existing file with source reference items, NULL uses a dump of sync source A instead
     * @param copy         a sync source which contains the copied items, begin/endSync will be called
     * @param raiseAssert  raise assertion if comparison yields differences (defaults to true)
     */
    void compareDatabases(const char *refFile, SyncSource &copy, bool raiseAssert = true) {
        CPPUNIT_ASSERT(config.dump);

        std::string sourceFile, copyFile;
        
        if (refFile) {
            sourceFile = refFile;
        } else {
            sourceFile = getCurrentTest() + ".source.test.dat";
            simplifyFilename(sourceFile);
            std::auto_ptr<SyncSource> source;
            SOURCE_ASSERT_NO_FAILURE(source.get(), source.reset(createSourceA()));
            SOURCE_ASSERT_EQUAL(source.get(), 0, source->beginSync());
            SOURCE_ASSERT_EQUAL(source.get(), 0, config.dump(client, *source.get(), sourceFile.c_str()));
            SOURCE_ASSERT_EQUAL(source.get(), 0, source->endSync());
            CPPUNIT_ASSERT_NO_THROW(source.reset());
        }

        copyFile = getCurrentTest() + ".copy.test.dat";
        simplifyFilename(copyFile);
        SOURCE_ASSERT_EQUAL(&copy, 0, copy.beginSync());
        SOURCE_ASSERT_EQUAL(&copy, 0, config.dump(client, copy, copyFile.c_str()));
        SOURCE_ASSERT_EQUAL(&copy, 0, copy.endSync());

        CPPUNIT_ASSERT(config.compare(client, sourceFile.c_str(), copyFile.c_str()));
    }
    
    /**
     * insert artificial items, number of them determined by TEST_EVOLUTION_NUM_ITEMS
     * unless passed explicitly
     *
     * @param createSource    a factory for the sync source that is to be used
     * @param startIndex      IDs are generated starting with this value
     * @param numItems        number of items to be inserted if non-null, otherwise TEST_EVOLUTION_NUM_ITEMS is used
     * @param size            minimum size for new items
     * @return number of items inserted
     */
    int insertManyItems(CreateSource createSource, int startIndex = 1, int numItems = 0, int size = -1) {
        CPPUNIT_ASSERT(config.templateItem);
        CPPUNIT_ASSERT(config.uniqueProperties);

        std::auto_ptr<SyncSource> source;
        SOURCE_ASSERT_NO_FAILURE(source.get(), source.reset(createSourceA()));
        SOURCE_ASSERT_EQUAL(source.get(), 0, source->beginSync());
        CPPUNIT_ASSERT(startIndex > 1 || !countItems(source.get()));

        int firstIndex = startIndex;
        if (firstIndex < 0) {
            firstIndex = 1;
        }
        int lastIndex = firstIndex + (numItems >= 1 ? numItems : config.numItems) - 1;
        for (int item = firstIndex; item <= lastIndex; item++) {
            std::string data = config.templateItem;
            std::stringstream prefix;

            prefix << std::setfill('0') << std::setw(3) << item << " ";

            
            const char *prop = config.uniqueProperties;
            const char *nextProp;
            while (*prop) {
                std::string curProp;
                nextProp = strchr(prop, ':');
                if (!nextProp) {
                    curProp = prop;
                } else {
                    curProp = std::string(prop, 0, nextProp - prop);
                }

                std::string property;
                // property is expected to not start directly at the
                // beginning
                property = "\n";
                property += curProp;
                property += ":";
                size_t off = data.find(property);
                if (off != data.npos) {
                    data.insert(off + property.size(), prefix.str());
                }

                if (!nextProp) {
                    break;
                }
                prop = nextProp + 1;
            }
            if (size > 0 && (int)data.size() < size) {
                int additionalBytes = size - (int)data.size();
                int added = 0;
                /* vCard 2.1 and vCal 1.0 need quoted-printable line breaks */
                bool quoted = data.find("VERSION:1.0") != data.npos ||
                    data.find("VERSION:2.1") != data.npos;
                size_t toreplace = 1;

                CPPUNIT_ASSERT(config.sizeProperty);
                
                /* stuff the item so that it reaches at least that size */
                size_t off = data.find(config.sizeProperty);
                CPPUNIT_ASSERT(off != data.npos);
                std::stringstream stuffing;
                if (quoted) {
                    stuffing << ";ENCODING=QUOTED-PRINTABLE:";
                } else {
                    stuffing << ":";
                }

                // insert after the first line, it often acts as the summary
                if (data.find("BEGIN:VJOURNAL") != data.npos) {
                    size_t start = data.find(":", off);
                    CPPUNIT_ASSERT( start != data.npos );
                    size_t eol = data.find("\\n", off);
                    CPPUNIT_ASSERT( eol != data.npos );
                    stuffing << data.substr(start + 1, eol - start + 1);
                    toreplace += eol - start + 1;
                }
                
                while(added < additionalBytes) {
                    int linelen = 0;
                   
                    while(added + 4 < additionalBytes &&
                          linelen < 60) {
                        stuffing << 'x';
                        added++;
                        linelen++;
                    }
                    // insert line breaks to allow folding
                    if (quoted) {
                        stuffing << "x=0D=0Ax";
                        added += 8;
                    } else {
                        stuffing << "x\\nx";
                        added += 4;
                    }
                }
                off = data.find(":", off);
                data.replace(off, toreplace, stuffing.str());
            }
            
            importItem(source.get(), data);
            data = "";
        }

        SOURCE_ASSERT_EQUAL(source.get(), 0, source->endSync());
        CPPUNIT_ASSERT_NO_THROW(source.reset());
        return lastIndex - firstIndex + 1;
    }
    
    // creating sync source
    void testOpen() {
        // check requirements
        CPPUNIT_ASSERT(config.createSourceA);

        std::auto_ptr<SyncSource> source(createSourceA());
        CPPUNIT_ASSERT(source.get() != 0);
        CPPUNIT_ASSERT_NO_THROW(source.reset());
    }

    // restart scanning of items
    void testIterateTwice() {
        // check requirements
        CPPUNIT_ASSERT(config.createSourceA);

        // open source
        std::auto_ptr<SyncSource> source(createSourceA());
        SOURCE_ASSERT_EQUAL(source.get(), 0, source->beginSync());
        SOURCE_ASSERT_MESSAGE(
            "iterating twice should produce identical results",
            source.get(),
            countItems(source.get()) == countItems(source.get()));
    }

    // insert one contact without clearing the source first
    void testSimpleInsert() {
        // check requirements
        CPPUNIT_ASSERT(config.insertItem);
        CPPUNIT_ASSERT(config.createSourceA);

        insert(createSourceA, config.insertItem);
    }

    // delete all items
    void testLocalDeleteAll() {
        // check requirements
        CPPUNIT_ASSERT(config.insertItem);
        CPPUNIT_ASSERT(config.createSourceA);

        // make sure there is something to delete, then delete again
        insert(createSourceA, config.insertItem);
        deleteAll(createSourceA);
    }

    // clean database, then insert
    void testComplexInsert() {
        testLocalDeleteAll();
        testSimpleInsert();
        testIterateTwice();
    }

    // clean database, insert item, update it
    void testLocalUpdate() {
        // check additional requirements
        CPPUNIT_ASSERT(config.updateItem);
        
        testLocalDeleteAll();
        testSimpleInsert();
        update(createSourceA, config.updateItem);
    }
        
    // complex sequence of changes
    void testChanges() {
        // check additional requirements
        CPPUNIT_ASSERT(config.createSourceB);

        testLocalDeleteAll();
        testSimpleInsert();

        // clean changes in sync source B by creating and closing it
        std::auto_ptr<SyncSource> source;
        SOURCE_ASSERT_NO_FAILURE(source.get(), source.reset(createSourceB()));
        SOURCE_ASSERT_EQUAL(source.get(), 0, source->beginSync());
        SOURCE_ASSERT_EQUAL(source.get(), 0, source->endSync());
        CPPUNIT_ASSERT_NO_THROW(source.reset());

        // no new changes now
        SOURCE_ASSERT_NO_FAILURE(source.get(), source.reset(createSourceB()));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -