4
// $Id: //poco/1.2/Foundation/testsuite/src/FileTest.cpp#3 $
6
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
9
// Permission is hereby granted, free of charge, to any person or organization
10
// obtaining a copy of the software and accompanying documentation covered by
11
// this license (the "Software") to use, reproduce, display, distribute,
12
// execute, and transmit the Software, and to prepare derivative works of the
13
// Software, and to permit third-parties to whom the Software is furnished to
14
// do so, all subject to the following:
16
// The copyright notices in the Software and this entire statement, including
17
// the above license grant, this restriction and the following disclaimer,
18
// must be included in all copies of the Software, in whole or in part, and
19
// all derivative works of the Software, unless such copies or derivative
20
// works are solely in the form of machine-executable object code generated by
21
// a source language processor.
23
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
26
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
27
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
28
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29
// DEALINGS IN THE SOFTWARE.
34
#include "CppUnit/TestCaller.h"
35
#include "CppUnit/TestSuite.h"
36
#include "Poco/File.h"
37
#include "Poco/TemporaryFile.h"
38
#include "Poco/Path.h"
39
#include "Poco/Exception.h"
40
#include "Poco/Thread.h"
46
using Poco::TemporaryFile;
48
using Poco::Exception;
49
using Poco::Timestamp;
53
FileTest::FileTest(const std::string& name): CppUnit::TestCase(name)
63
void FileTest::testFileAttributes1()
65
File f("testfile.dat");
70
bool flag = f.canRead();
71
failmsg("file does not exist - must throw exception");
79
bool flag = f.canWrite();
80
failmsg("file does not exist - must throw exception");
88
bool flag = f.isFile();
89
failmsg("file does not exist - must throw exception");
97
bool flag = f.isDirectory();
98
failmsg("file does not exist - must throw exception");
106
Timestamp ts = f.created();
107
failmsg("file does not exist - must throw exception");
115
Timestamp ts = f.getLastModified();
116
failmsg("file does not exist - must throw exception");
125
f.setLastModified(ts);
126
failmsg("file does not exist - must throw exception");
134
File::FileSize fs = f.getSize();
135
failmsg("file does not exist - must throw exception");
144
failmsg("file does not exist - must throw exception");
153
failmsg("file does not exist - must throw exception");
162
failmsg("file does not exist - must throw exception");
170
f.copyTo("copy.dat");
171
failmsg("file does not exist - must throw exception");
179
f.moveTo("copy.dat");
180
failmsg("file does not exist - must throw exception");
188
f.renameTo("copy.dat");
189
failmsg("file does not exist - must throw exception");
198
failmsg("file does not exist - must throw exception");
206
void FileTest::testCreateFile()
208
File f("testfile.dat");
209
bool created = f.createFile();
211
created = f.createFile();
216
void FileTest::testFileAttributes2()
219
bool created = f.createFile();
224
assert (f.canRead());
225
assert (f.canWrite());
227
assert (!f.isDirectory());
228
Timestamp tsc = f.created();
229
Timestamp tsm = f.getLastModified();
230
assert (tsc - ts >= -2000000 && tsc - ts <= 2000000);
231
assert (tsm - ts >= -2000000 && tsm - ts <= 2000000);
233
f.setWriteable(false);
234
assert (!f.canWrite());
235
assert (f.canRead());
237
f.setReadOnly(false);
238
assert (f.canWrite());
239
assert (f.canRead());
241
ts = Timestamp::fromEpochTime(1000000);
242
f.setLastModified(ts);
243
assert (f.getLastModified() == ts);
247
void FileTest::testCompare()
254
assert (!(f1 == f2));
256
assert (!(f1 != f3));
257
assert (!(f1 == f2));
261
assert (!(f2 <= f1));
265
assert (!(f1 >= f2));
272
void FileTest::testRootDir()
274
#if defined(POCO_OS_FAMILY_WINDOWS)
279
assert (f1.exists());
280
assert (f2.exists());
281
assert (f3.exists());
282
assert (f4.exists());
285
assert (f1.exists());
290
void FileTest::testSwap()
295
assert (f1.path() == "def.txt");
296
assert (f2.path() == "abc.txt");
300
void FileTest::testSize()
302
std::ofstream ostr("testfile.dat");
303
ostr << "Hello, world!" << std::endl;
305
File f("testfile.dat");
306
assert (f.getSize() > 0);
308
assert (f.getSize() == 0);
312
void FileTest::testDirectory()
322
TemporaryFile::registerForDeletion("testdir");
324
bool created = d.createDirectory();
326
assert (d.isDirectory());
327
assert (!d.isFile());
328
std::vector<std::string> files;
330
assert (files.empty());
332
File f = Path("testdir/file1", Path::PATH_UNIX);
334
f = Path("testdir/file2", Path::PATH_UNIX);
336
f = Path("testdir/file3", Path::PATH_UNIX);
340
assert (files.size() == 3);
342
std::set<std::string> fs;
343
fs.insert(files.begin(), files.end());
344
assert (fs.find("file1") != fs.end());
345
assert (fs.find("file2") != fs.end());
346
assert (fs.find("file3") != fs.end());
348
File dd(Path("testdir/testdir2/testdir3", Path::PATH_UNIX));
349
dd.createDirectories();
350
assert (dd.exists());
351
assert (dd.isDirectory());
353
File ddd(Path("testdir/testdirB/testdirC/testdirD", Path::PATH_UNIX));
354
ddd.createDirectories();
355
assert (ddd.exists());
356
assert (ddd.isDirectory());
362
void FileTest::testCopy()
364
std::ofstream ostr("testfile.dat");
365
ostr << "Hello, world!" << std::endl;
368
File f1("testfile.dat");
370
f1.copyTo(f2.path());
371
assert (f2.exists());
372
assert (f1.getSize() == f2.getSize());
376
void FileTest::testMove()
378
std::ofstream ostr("testfile.dat");
379
ostr << "Hello, world!" << std::endl;
382
File f1("testfile.dat");
383
File::FileSize sz = f1.getSize();
385
f1.moveTo(f2.path());
386
assert (f2.exists());
387
assert (f2.getSize() == sz);
388
assert (f1.exists());
393
void FileTest::testRename()
395
std::ofstream ostr("testfile.dat");
396
ostr << "Hello, world!" << std::endl;
399
File f1("testfile.dat");
400
File f2("testfile2.dat");
401
f1.renameTo(f2.path());
403
assert (f2.exists());
404
assert (f1.exists());
411
void FileTest::setUp()
413
File f("testfile.dat");
424
void FileTest::tearDown()
426
File f("testfile.dat");
437
CppUnit::Test* FileTest::suite()
439
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("FileTest");
441
CppUnit_addTest(pSuite, FileTest, testFileAttributes1);
442
CppUnit_addTest(pSuite, FileTest, testFileAttributes2);
443
CppUnit_addTest(pSuite, FileTest, testCompare);
444
CppUnit_addTest(pSuite, FileTest, testSwap);
445
CppUnit_addTest(pSuite, FileTest, testSize);
446
CppUnit_addTest(pSuite, FileTest, testDirectory);
447
CppUnit_addTest(pSuite, FileTest, testCopy);
448
CppUnit_addTest(pSuite, FileTest, testMove);
449
CppUnit_addTest(pSuite, FileTest, testRename);
450
CppUnit_addTest(pSuite, FileTest, testRootDir);