1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
/*
Copyright 2014-2014 Andreas Xavier <andxav at zoho dot com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
#include "keduvocdocument.h"
#include <KTemporaryFile>
#include <qtest_kde.h>
#include <qobject.h>
#include <qvalidator.h>
#include <QtXml/QDomDocument>
/** Unit tests to verify that 2 kvocdocs can't access a single file at the same time.*/
class KEduVocDocumentFileLockingTest
: public QObject
{
Q_OBJECT
private slots:
/** Checks that the lock is released in the destructor*/
void testVocFileLockReleaseOnDestruction();
/** Checks that the lock is released on close()*/
void testVocFileLockReleaseOnClose();
/** Checks that the lock is released if another url is opened.*/
void testVocFileLockReleaseOnSecondOpen();
/** Checks that the lock is released when it saves as another filename*/
void testVocFileLockReleaseOnSaveAs();
/** Checks that the kvocdoc doesn't lock itself out with a second open*/
void testVocFileLockOpenOpenCycle();
/** Checks that the kvoc doc doesn't lock itself out with a second save*/
void testVocFileLockOpenSaveSaveCycle();
/** Checks that the kvoc doc can steal the lock with an open function*/
void testVocFileLockOpenStealWOpen();
/** Checks that the kvoc doc can steal the lock with a saveAs function*/
void testVocFileLockOpenStealWSaveAs();
private :
/** Class to manage creation/destruction of a kvtml temp doc*/
class TemporaryVocDoc : public KTemporaryFile
{
public :
/** Create the file, fix the suffix and instantiate it.*/
TemporaryVocDoc() {
this->setSuffix(".kvtml");
this->open();
this->close();
}
/** Destructor*/
~TemporaryVocDoc() {}
};
/** Creates a minimal doc that will save and load error free.*/
class MinimalTempVocDoc : public TemporaryVocDoc
{
public :
/** The minimal doc has generator, author, lang and local */
MinimalTempVocDoc() {
const QString generator = QString::fromLatin1( "File Locking Unit Tests" );
const QString author = QString::fromLatin1( "File Locking Test" );
const QString lang = QString::fromLatin1( "File Locking Language Name" );
const QString locale = QString::fromLatin1( "en" ) ;
KEduVocIdentifier lang0;
lang0.setName( lang );
lang0.setLocale( locale);
KEduVocDocument *doc = new KEduVocDocument();
doc->setAuthor( author );
doc->appendIdentifier( lang0 );
KUrl filename = this->fileName();
doc->saveAs(filename, KEduVocDocument::Kvtml, generator);
delete doc;
}
/** Destructor*/
~MinimalTempVocDoc() {}
};
};
void KEduVocDocumentFileLockingTest::testVocFileLockReleaseOnDestruction()
{
MinimalTempVocDoc tempfile;
KEduVocDocument *doc1 = new KEduVocDocument();
int docError = doc1->open(tempfile.fileName());
QCOMPARE( docError, int( KEduVocDocument::NoError ) );
const QString generator = QString::fromLatin1( "File Locking Unit Tests" );
QCOMPARE( doc1->generator(), generator );
KEduVocDocument *doc2 = new KEduVocDocument();
docError = doc2->open(tempfile.fileName());
QCOMPARE( docError, int( KEduVocDocument::FileLocked ) );
delete doc2;
delete doc1;
KEduVocDocument *doc3 = new KEduVocDocument();
docError = doc3->open(tempfile.fileName());
QCOMPARE( docError, int( KEduVocDocument::NoError ) );
QCOMPARE( doc3->generator(), generator );
delete doc3;
}
void KEduVocDocumentFileLockingTest::testVocFileLockReleaseOnClose()
{
MinimalTempVocDoc tempfile;
KEduVocDocument *doc1 = new KEduVocDocument();
int docError = doc1->open(tempfile.fileName());
QCOMPARE( docError, int( KEduVocDocument::NoError ) );
KEduVocDocument *doc2 = new KEduVocDocument();
docError = doc2->open(tempfile.fileName());
QCOMPARE( docError, int( KEduVocDocument::FileLocked ) );
delete doc2;
doc1->close();
KEduVocDocument *doc3 = new KEduVocDocument();
docError = doc3->open(tempfile.fileName());
QCOMPARE( docError, int( KEduVocDocument::NoError ) );
delete doc3;
delete doc1;
}
void KEduVocDocumentFileLockingTest::testVocFileLockReleaseOnSecondOpen()
{
MinimalTempVocDoc tempfile, tempfile2;
KEduVocDocument *doc1 = new KEduVocDocument();
int docError = doc1->open(tempfile.fileName());
QCOMPARE( docError, int( KEduVocDocument::NoError ) );
KEduVocDocument *doc2 = new KEduVocDocument();
docError = doc2->open(tempfile.fileName());
QCOMPARE( docError, int( KEduVocDocument::FileLocked ) );
delete doc2;
doc1->open( tempfile2.fileName() );
KEduVocDocument *doc3 = new KEduVocDocument();
docError = doc3->open(tempfile.fileName());
QCOMPARE( docError, int( KEduVocDocument::NoError ) );
delete doc1;
delete doc3;
}
void KEduVocDocumentFileLockingTest::testVocFileLockReleaseOnSaveAs()
{
MinimalTempVocDoc tempfile;
TemporaryVocDoc tempfile2;
KEduVocDocument *doc1 = new KEduVocDocument();
int docError = doc1->open(tempfile.fileName());
QCOMPARE( docError, int( KEduVocDocument::NoError ) );
KEduVocDocument *doc2 = new KEduVocDocument();
docError = doc2->open(tempfile.fileName());
QCOMPARE( docError, int( KEduVocDocument::FileLocked ) );
delete doc2;
const QString generator = QString::fromLatin1( "File Locking Unit Tests" );
docError = doc1->saveAs( tempfile2.fileName() ,KEduVocDocument::Kvtml, generator);
QCOMPARE( docError, int( KEduVocDocument::NoError ) );
KEduVocDocument *doc3 = new KEduVocDocument();
docError = doc3->open(tempfile.fileName());
QCOMPARE( docError, int( KEduVocDocument::NoError ) );
delete doc1;
delete doc3;
}
void KEduVocDocumentFileLockingTest::testVocFileLockOpenOpenCycle()
{
MinimalTempVocDoc tempfile;
const QString generator = QString::fromLatin1( "File Locking Unit Tests" );
KEduVocDocument *doc = new KEduVocDocument();
int docError = doc->open(tempfile.fileName());
docError = doc->open(tempfile.fileName());
QCOMPARE( docError, int( KEduVocDocument::NoError ) );
delete doc;
}
void KEduVocDocumentFileLockingTest::testVocFileLockOpenSaveSaveCycle()
{
MinimalTempVocDoc tempfile;
const QString generator = QString::fromLatin1( "File Locking Unit Tests" );
KEduVocDocument *doc = new KEduVocDocument();
int docError = doc->open(tempfile.fileName());
doc->setAuthor( QString::fromLatin1( "Author1" ) );
docError = doc->saveAs( tempfile.fileName() ,KEduVocDocument::Kvtml, generator);
QCOMPARE( docError, int( KEduVocDocument::NoError ) );
const QString author2 = QString::fromLatin1( "Author2" );
doc->setAuthor( author2 );
docError = doc->saveAs( tempfile.fileName() ,KEduVocDocument::Kvtml, generator);
QCOMPARE( docError, int( KEduVocDocument::NoError ) );
delete doc;
KEduVocDocument docRead;
docRead.open(tempfile.fileName());
QCOMPARE( docRead.author(), author2 );
}
void KEduVocDocumentFileLockingTest::testVocFileLockOpenStealWOpen()
{
MinimalTempVocDoc tempfile;
const QString generator = QString::fromLatin1( "File Locking Unit Tests" );
KEduVocDocument *doc1 = new KEduVocDocument();
int docError = doc1->open(tempfile.fileName());
QCOMPARE( docError, int( KEduVocDocument::NoError ) );
KEduVocDocument *doc2 = new KEduVocDocument();
docError = doc2->open(tempfile.fileName());
QCOMPARE( docError, int( KEduVocDocument::FileLocked ) );
docError = doc2->open(tempfile.fileName(), KEduVocDocument::FileIgnoreLock);
QCOMPARE( docError, int( KEduVocDocument::NoError ) );
docError = doc1->saveAs(tempfile.fileName(), KEduVocDocument::Kvtml, generator );
QCOMPARE( docError, int( KEduVocDocument::FileCannotLock ) );
delete doc2;
delete doc1;
}
void KEduVocDocumentFileLockingTest::testVocFileLockOpenStealWSaveAs()
{
MinimalTempVocDoc tempfile, tempfile2;
const QString generator = QString::fromLatin1( "File Locking Unit Tests Save w Stolen Lock" );
KEduVocDocument *doc1 = new KEduVocDocument();
int docError = doc1->open(tempfile.fileName());
QCOMPARE( docError, int( KEduVocDocument::NoError ) );
KEduVocDocument *doc2 = new KEduVocDocument();
docError = doc2->open(tempfile2.fileName());
QCOMPARE( docError, int( KEduVocDocument::NoError ) );
docError = doc2->saveAs(tempfile.fileName(), KEduVocDocument::Kvtml, generator );
QCOMPARE( docError, int( KEduVocDocument::FileLocked ) );
docError = doc2->saveAs(tempfile.fileName(), KEduVocDocument::Kvtml, KEduVocDocument::FileIgnoreLock );
QCOMPARE( docError, int( KEduVocDocument::NoError ) );
docError = doc1->saveAs(tempfile.fileName(), KEduVocDocument::Kvtml, generator );
QCOMPARE( docError, int( KEduVocDocument::FileCannotLock ) );
delete doc2;
delete doc1;
}
QTEST_MAIN( KEduVocDocumentFileLockingTest )
#include "keduvocdocumentfilelockingtest.moc"
|