File indexing completed on 2024-04-21 03:48:17
0001 /* 0002 * SPDX-FileCopyrightText: 2014-2014 Andreas Xavier <andxav at zoho dot com> 0003 * SPDX-License-Identifier: GPL-2.0-or-later 0004 */ 0005 0006 #include "keduvocdocument.h" 0007 0008 #include <KTemporaryFile> 0009 0010 #include <qtest_kde.h> 0011 0012 #include <QDomDocument> 0013 #include <QObject> 0014 #include <QValidator> 0015 0016 /** Unit tests to verify that 2 kvocdocs can't access a single file at the same time.*/ 0017 class KEduVocDocumentFileLockingTest : public QObject 0018 { 0019 Q_OBJECT 0020 0021 private slots: 0022 /** Checks that the lock is released in the destructor*/ 0023 void testVocFileLockReleaseOnDestruction(); 0024 /** Checks that the lock is released on close()*/ 0025 void testVocFileLockReleaseOnClose(); 0026 /** Checks that the lock is released if another url is opened.*/ 0027 void testVocFileLockReleaseOnSecondOpen(); 0028 /** Checks that the lock is released when it saves as another filename*/ 0029 void testVocFileLockReleaseOnSaveAs(); 0030 0031 /** Checks that the kvocdoc doesn't lock itself out with a second open*/ 0032 void testVocFileLockOpenOpenCycle(); 0033 /** Checks that the kvoc doc doesn't lock itself out with a second save*/ 0034 void testVocFileLockOpenSaveSaveCycle(); 0035 0036 /** Checks that the kvoc doc can steal the lock with an open function*/ 0037 void testVocFileLockOpenStealWOpen(); 0038 /** Checks that the kvoc doc can steal the lock with a saveAs function*/ 0039 void testVocFileLockOpenStealWSaveAs(); 0040 0041 private: 0042 /** Class to manage creation/destruction of a kvtml temp doc*/ 0043 class TemporaryVocDoc : public KTemporaryFile 0044 { 0045 public: 0046 /** Create the file, fix the suffix and instantiate it.*/ 0047 TemporaryVocDoc() 0048 { 0049 this->setSuffix(".kvtml"); 0050 this->open(); 0051 this->close(); 0052 } 0053 /** Destructor*/ 0054 ~TemporaryVocDoc() 0055 { 0056 } 0057 }; 0058 0059 /** Creates a minimal doc that will save and load error free.*/ 0060 class MinimalTempVocDoc : public TemporaryVocDoc 0061 { 0062 public: 0063 /** The minimal doc has generator, author, lang and local */ 0064 MinimalTempVocDoc() 0065 { 0066 const QString generator = QString::fromLatin1("File Locking Unit Tests"); 0067 const QString author = QString::fromLatin1("File Locking Test"); 0068 const QString lang = QString::fromLatin1("File Locking Language Name"); 0069 const QString locale = QString::fromLatin1("en"); 0070 0071 KEduVocIdentifier lang0; 0072 lang0.setName(lang); 0073 lang0.setLocale(locale); 0074 0075 KEduVocDocument *doc = new KEduVocDocument(); 0076 doc->setAuthor(author); 0077 doc->appendIdentifier(lang0); 0078 KUrl filename = this->fileName(); 0079 doc->saveAs(filename, KEduVocDocument::Kvtml, generator); 0080 delete doc; 0081 } 0082 /** Destructor*/ 0083 ~MinimalTempVocDoc() 0084 { 0085 } 0086 }; 0087 }; 0088 0089 void KEduVocDocumentFileLockingTest::testVocFileLockReleaseOnDestruction() 0090 { 0091 MinimalTempVocDoc tempfile; 0092 0093 KEduVocDocument *doc1 = new KEduVocDocument(); 0094 int docError = doc1->open(tempfile.fileName()); 0095 QCOMPARE(docError, int(KEduVocDocument::NoError)); 0096 0097 const QString generator = QString::fromLatin1("File Locking Unit Tests"); 0098 QCOMPARE(doc1->generator(), generator); 0099 0100 KEduVocDocument *doc2 = new KEduVocDocument(); 0101 docError = doc2->open(tempfile.fileName()); 0102 QCOMPARE(docError, int(KEduVocDocument::FileLocked)); 0103 delete doc2; 0104 0105 delete doc1; 0106 KEduVocDocument *doc3 = new KEduVocDocument(); 0107 docError = doc3->open(tempfile.fileName()); 0108 QCOMPARE(docError, int(KEduVocDocument::NoError)); 0109 QCOMPARE(doc3->generator(), generator); 0110 delete doc3; 0111 } 0112 0113 void KEduVocDocumentFileLockingTest::testVocFileLockReleaseOnClose() 0114 { 0115 MinimalTempVocDoc tempfile; 0116 0117 KEduVocDocument *doc1 = new KEduVocDocument(); 0118 int docError = doc1->open(tempfile.fileName()); 0119 QCOMPARE(docError, int(KEduVocDocument::NoError)); 0120 0121 KEduVocDocument *doc2 = new KEduVocDocument(); 0122 docError = doc2->open(tempfile.fileName()); 0123 QCOMPARE(docError, int(KEduVocDocument::FileLocked)); 0124 delete doc2; 0125 0126 doc1->close(); 0127 KEduVocDocument *doc3 = new KEduVocDocument(); 0128 docError = doc3->open(tempfile.fileName()); 0129 QCOMPARE(docError, int(KEduVocDocument::NoError)); 0130 0131 delete doc3; 0132 delete doc1; 0133 } 0134 0135 void KEduVocDocumentFileLockingTest::testVocFileLockReleaseOnSecondOpen() 0136 { 0137 MinimalTempVocDoc tempfile, tempfile2; 0138 0139 KEduVocDocument *doc1 = new KEduVocDocument(); 0140 int docError = doc1->open(tempfile.fileName()); 0141 QCOMPARE(docError, int(KEduVocDocument::NoError)); 0142 0143 KEduVocDocument *doc2 = new KEduVocDocument(); 0144 docError = doc2->open(tempfile.fileName()); 0145 QCOMPARE(docError, int(KEduVocDocument::FileLocked)); 0146 delete doc2; 0147 0148 doc1->open(tempfile2.fileName()); 0149 KEduVocDocument *doc3 = new KEduVocDocument(); 0150 docError = doc3->open(tempfile.fileName()); 0151 QCOMPARE(docError, int(KEduVocDocument::NoError)); 0152 0153 delete doc1; 0154 delete doc3; 0155 } 0156 0157 void KEduVocDocumentFileLockingTest::testVocFileLockReleaseOnSaveAs() 0158 { 0159 MinimalTempVocDoc tempfile; 0160 TemporaryVocDoc tempfile2; 0161 0162 KEduVocDocument *doc1 = new KEduVocDocument(); 0163 int docError = doc1->open(tempfile.fileName()); 0164 QCOMPARE(docError, int(KEduVocDocument::NoError)); 0165 0166 KEduVocDocument *doc2 = new KEduVocDocument(); 0167 docError = doc2->open(tempfile.fileName()); 0168 QCOMPARE(docError, int(KEduVocDocument::FileLocked)); 0169 delete doc2; 0170 0171 const QString generator = QString::fromLatin1("File Locking Unit Tests"); 0172 docError = doc1->saveAs(tempfile2.fileName(), KEduVocDocument::Kvtml, generator); 0173 QCOMPARE(docError, int(KEduVocDocument::NoError)); 0174 0175 KEduVocDocument *doc3 = new KEduVocDocument(); 0176 docError = doc3->open(tempfile.fileName()); 0177 QCOMPARE(docError, int(KEduVocDocument::NoError)); 0178 0179 delete doc1; 0180 delete doc3; 0181 } 0182 0183 void KEduVocDocumentFileLockingTest::testVocFileLockOpenOpenCycle() 0184 { 0185 MinimalTempVocDoc tempfile; 0186 const QString generator = QString::fromLatin1("File Locking Unit Tests"); 0187 0188 KEduVocDocument *doc = new KEduVocDocument(); 0189 int docError = doc->open(tempfile.fileName()); 0190 docError = doc->open(tempfile.fileName()); 0191 QCOMPARE(docError, int(KEduVocDocument::NoError)); 0192 0193 delete doc; 0194 } 0195 0196 void KEduVocDocumentFileLockingTest::testVocFileLockOpenSaveSaveCycle() 0197 { 0198 MinimalTempVocDoc tempfile; 0199 const QString generator = QString::fromLatin1("File Locking Unit Tests"); 0200 0201 KEduVocDocument *doc = new KEduVocDocument(); 0202 int docError = doc->open(tempfile.fileName()); 0203 0204 doc->setAuthor(QString::fromLatin1("Author1")); 0205 docError = doc->saveAs(tempfile.fileName(), KEduVocDocument::Kvtml, generator); 0206 QCOMPARE(docError, int(KEduVocDocument::NoError)); 0207 0208 const QString author2 = QString::fromLatin1("Author2"); 0209 doc->setAuthor(author2); 0210 docError = doc->saveAs(tempfile.fileName(), KEduVocDocument::Kvtml, generator); 0211 QCOMPARE(docError, int(KEduVocDocument::NoError)); 0212 0213 delete doc; 0214 0215 KEduVocDocument docRead; 0216 docRead.open(tempfile.fileName()); 0217 QCOMPARE(docRead.author(), author2); 0218 } 0219 0220 void KEduVocDocumentFileLockingTest::testVocFileLockOpenStealWOpen() 0221 { 0222 MinimalTempVocDoc tempfile; 0223 const QString generator = QString::fromLatin1("File Locking Unit Tests"); 0224 0225 KEduVocDocument *doc1 = new KEduVocDocument(); 0226 int docError = doc1->open(tempfile.fileName()); 0227 QCOMPARE(docError, int(KEduVocDocument::NoError)); 0228 0229 KEduVocDocument *doc2 = new KEduVocDocument(); 0230 docError = doc2->open(tempfile.fileName()); 0231 QCOMPARE(docError, int(KEduVocDocument::FileLocked)); 0232 0233 docError = doc2->open(tempfile.fileName(), KEduVocDocument::FileIgnoreLock); 0234 QCOMPARE(docError, int(KEduVocDocument::NoError)); 0235 0236 docError = doc1->saveAs(tempfile.fileName(), KEduVocDocument::Kvtml, generator); 0237 QCOMPARE(docError, int(KEduVocDocument::FileCannotLock)); 0238 0239 delete doc2; 0240 delete doc1; 0241 } 0242 0243 void KEduVocDocumentFileLockingTest::testVocFileLockOpenStealWSaveAs() 0244 { 0245 MinimalTempVocDoc tempfile, tempfile2; 0246 const QString generator = QString::fromLatin1("File Locking Unit Tests Save w Stolen Lock"); 0247 0248 KEduVocDocument *doc1 = new KEduVocDocument(); 0249 int docError = doc1->open(tempfile.fileName()); 0250 QCOMPARE(docError, int(KEduVocDocument::NoError)); 0251 0252 KEduVocDocument *doc2 = new KEduVocDocument(); 0253 docError = doc2->open(tempfile2.fileName()); 0254 QCOMPARE(docError, int(KEduVocDocument::NoError)); 0255 0256 docError = doc2->saveAs(tempfile.fileName(), KEduVocDocument::Kvtml, generator); 0257 QCOMPARE(docError, int(KEduVocDocument::FileLocked)); 0258 0259 docError = doc2->saveAs(tempfile.fileName(), KEduVocDocument::Kvtml, KEduVocDocument::FileIgnoreLock); 0260 QCOMPARE(docError, int(KEduVocDocument::NoError)); 0261 0262 docError = doc1->saveAs(tempfile.fileName(), KEduVocDocument::Kvtml, generator); 0263 QCOMPARE(docError, int(KEduVocDocument::FileCannotLock)); 0264 0265 delete doc2; 0266 delete doc1; 0267 } 0268 0269 QTEST_MAIN(KEduVocDocumentFileLockingTest) 0270 0271 #include "keduvocdocumentfilelockingtest.moc"