File indexing completed on 2024-05-26 05:56:53

0001 /*
0002     This file is part of the Okteta Kasten module, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2007-2008, 2011 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "bytearrayrawfilesynchronizertest.hpp"
0010 
0011 // test object
0012 #include <bytearrayrawfilesynchronizer.hpp>
0013 // lib
0014 #include <bytearraydocument.hpp>
0015 // test utils
0016 #include <util/filesystem.hpp>
0017 #include <util/fill.hpp>
0018 // Kasten core
0019 #include <Kasten/AbstractLoadJob>
0020 #include <Kasten/AbstractConnectJob>
0021 #include <Kasten/AbstractSyncToRemoteJob>
0022 #include <Kasten/AbstractSyncWithRemoteJob>
0023 // Okteta core
0024 #include <Okteta/PieceTableByteArrayModel>
0025 // Qt
0026 #include <QTest>
0027 #include <QSignalSpy>
0028 #include <QByteArray>
0029 #include <QFile>
0030 #include <QDataStream>
0031 
0032 namespace Kasten {
0033 
0034 static constexpr char TestDirectory[] = "bytearrayrawfilesynchronizertest";
0035 static constexpr char TestFileName[] = "test.data";
0036 static constexpr char NotExistingUrl[] = "notexisting://";
0037 static constexpr int TestDataSize = 50;
0038 static constexpr char TestDataChar = 0;
0039 
0040 void ByteArrayRawFileSynchronizerTest::initTestCase()
0041 {
0042     QByteArray byteArray(TestDataSize, TestDataChar);
0043     ::textureByteArray(&byteArray);
0044 
0045     mFileSystem = new TestFileSystem(QLatin1String(TestDirectory));
0046     const QString filePath = mFileSystem->createFilePath(QLatin1String(TestFileName));
0047     QFile file;
0048     file.setFileName(filePath);
0049     file.open(QIODevice::WriteOnly);
0050 
0051     QDataStream outStream(&file);
0052     outStream.writeRawData(byteArray.data(), byteArray.size());
0053 
0054     file.close();
0055 
0056 //     QDir dir(mDataDir);
0057 //     QVERIFY(dir.mkdir("Europe"));
0058 //     QFile::copy(QString::fromLatin1(KDESRCDIR) + QStringLiteral("/Paris"), mDataDir + QStringLiteral("/Europe/Paris"));
0059 }
0060 
0061 void ByteArrayRawFileSynchronizerTest::cleanupTestCase()
0062 {
0063     delete mFileSystem;
0064 }
0065 
0066 #if 0
0067 void ByteArrayRawFileSynchronizerTest::init()
0068 {
0069     ByteArrayModel = createByteArrayModel();
0070 
0071     mModifiedSpy =  new QSignalSpy(ByteArrayModel, SIGNAL(modified(bool)));
0072 }
0073 #endif
0074 
0075 void ByteArrayRawFileSynchronizerTest::testLoadFromUrl()
0076 {
0077     const QUrl fileUrl = QUrl::fromLocalFile(mFileSystem->createFilePath(QLatin1String(TestFileName)));
0078     auto* synchronizer = new ByteArrayRawFileSynchronizer();
0079     synchronizer->startLoad(fileUrl)->exec();
0080     AbstractDocument* document = synchronizer->document();
0081 
0082     auto* byteArrayDocument = qobject_cast<ByteArrayDocument*>(document);
0083 
0084     QVERIFY(document != nullptr);
0085     QVERIFY(byteArrayDocument != nullptr);
0086     QVERIFY(document->synchronizer() != nullptr);
0087     QCOMPARE(document->synchronizer()->document(), document);
0088     QCOMPARE(document->contentFlags(), Kasten::ContentStateNormal);
0089     QCOMPARE(document->synchronizer()->localSyncState(), Kasten::LocalInSync);
0090     QCOMPARE(document->synchronizer()->remoteSyncState(), Kasten::RemoteInSync);
0091 
0092     QCOMPARE(document->synchronizer()->url(), fileUrl);
0093 
0094     delete document;
0095 }
0096 
0097 void ByteArrayRawFileSynchronizerTest::testLoadFromNotExistingUrl()
0098 {
0099     const QUrl fileUrl = QUrl(QLatin1String(NotExistingUrl));
0100 
0101     auto* synchronizer = new ByteArrayRawFileSynchronizer();
0102     synchronizer->startLoad(fileUrl)->exec();
0103     AbstractDocument* document = synchronizer->document();
0104 
0105     QVERIFY(document == nullptr);
0106     delete synchronizer;
0107 }
0108 
0109 void ByteArrayRawFileSynchronizerTest::testNewSaveAsToUrl()
0110 {
0111     const QUrl fileUrl = QUrl::fromLocalFile(mFileSystem->createFilePath(QLatin1String(TestFileName)));
0112 
0113     auto* document = new Kasten::ByteArrayDocument(QStringLiteral("New created for test."));
0114     auto* byteArray = qobject_cast<Okteta::PieceTableByteArrayModel*>(document->content());
0115 
0116     // fill array
0117     QByteArray testData(TestDataSize, TestDataChar);
0118     ::textureByteArray(&testData);
0119     byteArray->setData(testData);
0120 
0121     // save
0122     auto* synchronizer = new ByteArrayRawFileSynchronizer();
0123     synchronizer->startConnect(document, fileUrl, AbstractModelSynchronizer::ReplaceRemote)->exec();
0124     QCOMPARE(synchronizer->document(), document);
0125 
0126 //     // load into other and...
0127 //     ByteArrayDocument* otherDocument = new ByteArrayDocument( filePath );
0128 
0129 //     QVERIFY( document != 0 );
0130 
0131 //     // compare with old
0132 //     Okteta::PieceTableByteArrayModel *otherByteArray = document->content();
0133 //     QCOMPARE( byteArray->size(), otherByteArray->size() );
0134 //     QVERIFY( qstrncmp(byteArray->data(),otherByteArray->data(),byteArray->size()) == 0 );
0135 
0136     delete document;
0137 }
0138 
0139 }
0140 
0141 QTEST_GUILESS_MAIN(Kasten::ByteArrayRawFileSynchronizerTest)
0142 
0143 #include "moc_bytearrayrawfilesynchronizertest.cpp"