File indexing completed on 2024-06-16 05:25:08

0001 /*
0002     This file is part of the Okteta Kasten module, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2007-2008 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 "bytearrayrawfilesynchronizerfactorytest.hpp"
0010 
0011 // test object
0012 #include <bytearrayrawfilesynchronizerfactory.hpp>
0013 // lib
0014 #include <bytearraydocument.hpp>
0015 // test utils
0016 #include <util/filesystem.hpp>
0017 #include <util/fill.hpp>
0018 // Okteta core
0019 #include <Okteta/PieceTableByteArrayModel>
0020 // Qt
0021 #include <QTest>
0022 #include <QSignalSpy>
0023 #include <QByteArray>
0024 #include <QFile>
0025 #include <QDataStream>
0026 
0027 static constexpr char TestDirectory[] = "bytearrayrawfilesynchronizerfactorytest";
0028 static constexpr char TestFileName[] = "test.data";
0029 #if 0
0030 static constexpr char NotExistingUrl[] = "notexisting://";
0031 #endif
0032 static constexpr int TestDataSize = 50;
0033 static constexpr char TestDataChar = 0;
0034 
0035 void ByteArrayRawFileSynchronizerFactoryTest::initTestCase()
0036 {
0037     QByteArray byteArray(TestDataSize, TestDataChar);
0038     ::textureByteArray(&byteArray);
0039 
0040     mFileSystem = new TestFileSystem(QLatin1String(TestDirectory));
0041     const QString filePath = mFileSystem->createFilePath(QLatin1String(TestFileName));
0042     QFile file;
0043     file.setFileName(filePath);
0044     file.open(QIODevice::WriteOnly);
0045 
0046     QDataStream outStream(&file);
0047     outStream.writeRawData(byteArray.data(), byteArray.size());
0048 
0049     file.close();
0050 
0051 //     QDir dir(mDataDir);
0052 //     QVERIFY(dir.mkdir("Europe"));
0053 //     QFile::copy(QString::fromLatin1(KDESRCDIR) + QStringLiteral("/Paris"), mDataDir + QStringLiteral("/Europe/Paris"));
0054 }
0055 
0056 void ByteArrayRawFileSynchronizerFactoryTest::cleanupTestCase()
0057 {
0058     delete mFileSystem;
0059 }
0060 
0061 #if 0
0062 void ByteArrayRawFileSynchronizerFactoryTest::init()
0063 {
0064     ByteArrayModel = createByteArrayModel();
0065 
0066     mModifiedSpy =  new QSignalSpy(ByteArrayModel, SIGNAL(modified(bool)));
0067 }
0068 #endif
0069 
0070 void ByteArrayRawFileSynchronizerFactoryTest::testCreate()
0071 {
0072     auto* factory = new Kasten::ByteArrayRawFileSynchronizerFactory();
0073 
0074     QVERIFY(factory != nullptr);
0075 
0076     delete factory;
0077 }
0078 #if 0
0079 void ByteArrayRawFileSynchronizerFactoryTest::testLoadFromUrl()
0080 {
0081     const QUrl fileUrl = QUrl::fromLocalFile(mFileSystem->createFilePath(QStringLiteral(TestFileName)));
0082     Kasten::ByteArrayRawFileSynchronizerFactory* factory = new Kasten::ByteArrayRawFileSynchronizerFactory();
0083     AbstractDocument* document = factory->loadNewDocument(fileUrl);
0084 
0085     ByteArrayDocument* byteArrayDocument = qobject_cast<ByteArrayDocument*>(document);
0086 
0087     QVERIFY(document != 0);
0088     QVERIFY(byteArrayDocument != 0);
0089     QVERIFY(document->synchronizer() != 0);
0090     QCOMPARE(document->synchronizer()->document(), document);
0091     QCOMPARE(document->hasLocalChanges(), false);
0092 
0093     QCOMPARE(document->synchronizer()->url(), fileUrl);
0094 
0095     delete document;
0096     delete factory;
0097 }
0098 
0099 void ByteArrayRawFileSynchronizerFactoryTest::testLoadFromNotExistingUrl()
0100 {
0101     const QUrl fileUrl = QUrl(QStringLiteral(NotExistingUrl));
0102 
0103     Kasten::ByteArrayRawFileSynchronizerFactory* factory = new Kasten::ByteArrayRawFileSynchronizerFactory();
0104     AbstractDocument* document = factory->loadNewDocument(fileUrl);
0105 
0106     QVERIFY(document == 0);
0107 
0108     delete factory;
0109 }
0110 #endif
0111 #if 0
0112 void ByteArrayRawFileSynchronizerFactoryTest::testSaveToFile()
0113 {
0114     const QString filePath = mFileSystem->createFilePath(QStringLiteral(TestFileName));
0115 
0116     ByteArrayDocument* document = new ByteArrayDocument();
0117     Okteta::PieceTableByteArrayModel* byteArray = document->content();
0118 
0119     // fill array
0120     byteArray->
0121 
0122     // save
0123     document->setLocalFilePath(filePath);
0124     document->save();
0125 TODO: save mit path als Parameter ? Oder separat setzen ? Wie Kopie speichern ?
0126 
0127     // load into other and...
0128     ByteArrayDocument * otherDocument = new ByteArrayDocument(filePath);
0129 
0130     QVERIFY(document != 0);
0131 
0132     // compare with old
0133     Okteta::PieceTableByteArrayModel* otherByteArray = document->content();
0134     QCOMPARE(byteArray->size(), otherByteArray->size());
0135     QVERIFY(qstrncmp(byteArray->data(), otherByteArray->data(), byteArray->size()) == 0);
0136 
0137     delete document;
0138 }
0139 #endif
0140 
0141 QTEST_GUILESS_MAIN(ByteArrayRawFileSynchronizerFactoryTest)
0142 
0143 #include "moc_bytearrayrawfilesynchronizerfactorytest.cpp"