~ubuntu-branches/ubuntu/gutsy/poco/gutsy

« back to all changes in this revision

Viewing changes to Foundation/testsuite/src/FileTest.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2007-04-27 18:33:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070427183348-xgnpct0qd6a2ip34
Tags: upstream-1.2.9
ImportĀ upstreamĀ versionĀ 1.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// FileTest.cpp
 
3
//
 
4
// $Id: //poco/1.2/Foundation/testsuite/src/FileTest.cpp#3 $
 
5
//
 
6
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
 
7
// and Contributors.
 
8
//
 
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:
 
15
// 
 
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.
 
22
// 
 
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.
 
30
//
 
31
 
 
32
 
 
33
#include "FileTest.h"
 
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"
 
41
#include <fstream>
 
42
#include <set>
 
43
 
 
44
 
 
45
using Poco::File;
 
46
using Poco::TemporaryFile;
 
47
using Poco::Path;
 
48
using Poco::Exception;
 
49
using Poco::Timestamp;
 
50
using Poco::Thread;
 
51
 
 
52
 
 
53
FileTest::FileTest(const std::string& name): CppUnit::TestCase(name)
 
54
{
 
55
}
 
56
 
 
57
 
 
58
FileTest::~FileTest()
 
59
{
 
60
}
 
61
 
 
62
 
 
63
void FileTest::testFileAttributes1()
 
64
{
 
65
        File f("testfile.dat");
 
66
        assert (!f.exists());
 
67
        
 
68
        try
 
69
        {
 
70
                bool flag = f.canRead();
 
71
                failmsg("file does not exist - must throw exception");
 
72
        }
 
73
        catch (Exception&)
 
74
        {
 
75
        }
 
76
 
 
77
        try
 
78
        {
 
79
                bool flag = f.canWrite();
 
80
                failmsg("file does not exist - must throw exception");
 
81
        }
 
82
        catch (Exception&)
 
83
        {
 
84
        }
 
85
 
 
86
        try
 
87
        {
 
88
                bool flag = f.isFile();
 
89
                failmsg("file does not exist - must throw exception");
 
90
        }
 
91
        catch (Exception&)
 
92
        {
 
93
        }
 
94
 
 
95
        try
 
96
        {
 
97
                bool flag = f.isDirectory();
 
98
                failmsg("file does not exist - must throw exception");
 
99
        }
 
100
        catch (Exception&)
 
101
        {
 
102
        }
 
103
 
 
104
        try
 
105
        {
 
106
                Timestamp ts = f.created();
 
107
                failmsg("file does not exist - must throw exception");
 
108
        }
 
109
        catch (Exception&)
 
110
        {
 
111
        }
 
112
 
 
113
        try
 
114
        {
 
115
                Timestamp ts = f.getLastModified();
 
116
                failmsg("file does not exist - must throw exception");
 
117
        }
 
118
        catch (Exception&)
 
119
        {
 
120
        }
 
121
 
 
122
        try
 
123
        {
 
124
                Timestamp ts;
 
125
                f.setLastModified(ts);
 
126
                failmsg("file does not exist - must throw exception");
 
127
        }
 
128
        catch (Exception&)
 
129
        {
 
130
        }
 
131
 
 
132
        try
 
133
        {
 
134
                File::FileSize fs = f.getSize();
 
135
                failmsg("file does not exist - must throw exception");
 
136
        }
 
137
        catch (Exception&)
 
138
        {
 
139
        }
 
140
 
 
141
        try
 
142
        {
 
143
                f.setSize(0);
 
144
                failmsg("file does not exist - must throw exception");
 
145
        }
 
146
        catch (Exception&)
 
147
        {
 
148
        }
 
149
 
 
150
        try
 
151
        {
 
152
                f.setWriteable();
 
153
                failmsg("file does not exist - must throw exception");
 
154
        }
 
155
        catch (Exception&)
 
156
        {
 
157
        }
 
158
 
 
159
        try
 
160
        {
 
161
                f.setReadOnly();
 
162
                failmsg("file does not exist - must throw exception");
 
163
        }
 
164
        catch (Exception&)
 
165
        {
 
166
        }
 
167
 
 
168
        try
 
169
        {
 
170
                f.copyTo("copy.dat");
 
171
                failmsg("file does not exist - must throw exception");
 
172
        }
 
173
        catch (Exception&)
 
174
        {
 
175
        }
 
176
 
 
177
        try
 
178
        {
 
179
                f.moveTo("copy.dat");
 
180
                failmsg("file does not exist - must throw exception");
 
181
        }
 
182
        catch (Exception&)
 
183
        {
 
184
        }
 
185
 
 
186
        try
 
187
        {
 
188
                f.renameTo("copy.dat");
 
189
                failmsg("file does not exist - must throw exception");
 
190
        }
 
191
        catch (Exception&)
 
192
        {
 
193
        }
 
194
 
 
195
        try
 
196
        {
 
197
                f.remove();
 
198
                failmsg("file does not exist - must throw exception");
 
199
        }
 
200
        catch (Exception&)
 
201
        {
 
202
        }
 
203
}
 
204
 
 
205
 
 
206
void FileTest::testCreateFile()
 
207
{
 
208
        File f("testfile.dat");
 
209
        bool created = f.createFile();
 
210
        assert (created);
 
211
        created = f.createFile();
 
212
        assert (!created);
 
213
}
 
214
 
 
215
 
 
216
void FileTest::testFileAttributes2()
 
217
{
 
218
        TemporaryFile f;
 
219
        bool created = f.createFile();
 
220
        Timestamp ts;
 
221
        assert (created);
 
222
        
 
223
        assert (f.exists());
 
224
        assert (f.canRead());
 
225
        assert (f.canWrite());
 
226
        assert (f.isFile());
 
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);
 
232
        
 
233
        f.setWriteable(false);
 
234
        assert (!f.canWrite());
 
235
        assert (f.canRead());
 
236
 
 
237
        f.setReadOnly(false);   
 
238
        assert (f.canWrite());
 
239
        assert (f.canRead());
 
240
        
 
241
        ts = Timestamp::fromEpochTime(1000000);
 
242
        f.setLastModified(ts);
 
243
        assert (f.getLastModified() == ts);
 
244
}
 
245
 
 
246
 
 
247
void FileTest::testCompare()
 
248
{
 
249
        File f1("abc.txt");
 
250
        File f2("def.txt");
 
251
        File f3("abc.txt");
 
252
        
 
253
        assert (f1 == f3);
 
254
        assert (!(f1 == f2));
 
255
        assert (f1 != f2);
 
256
        assert (!(f1 != f3));
 
257
        assert (!(f1 == f2));
 
258
        assert (f1 < f2);
 
259
        assert (f1 <= f2);
 
260
        assert (!(f2 < f1));
 
261
        assert (!(f2 <= f1));
 
262
        assert (f2 > f1);
 
263
        assert (f2 >= f1);
 
264
        assert (!(f1 > f2));
 
265
        assert (!(f1 >= f2));
 
266
        
 
267
        assert (f1 <= f3);
 
268
        assert (f1 >= f3);
 
269
}
 
270
 
 
271
 
 
272
void FileTest::testRootDir()
 
273
{
 
274
#if defined(POCO_OS_FAMILY_WINDOWS)
 
275
        File f1("/");
 
276
        File f2("c:/");
 
277
        File f3("c:\\");
 
278
        File f4("\\");
 
279
        assert (f1.exists());
 
280
        assert (f2.exists());
 
281
        assert (f3.exists());
 
282
        assert (f4.exists());
 
283
#else
 
284
        File f1("/");
 
285
        assert (f1.exists());
 
286
#endif
 
287
}
 
288
 
 
289
 
 
290
void FileTest::testSwap()
 
291
{
 
292
        File f1("abc.txt");
 
293
        File f2("def.txt");
 
294
        f1.swap(f2);
 
295
        assert (f1.path() == "def.txt");
 
296
        assert (f2.path() == "abc.txt");
 
297
}
 
298
 
 
299
 
 
300
void FileTest::testSize()
 
301
{
 
302
        std::ofstream ostr("testfile.dat");
 
303
        ostr << "Hello, world!" << std::endl;
 
304
        ostr.close();
 
305
        File f("testfile.dat");
 
306
        assert (f.getSize() > 0);
 
307
        f.setSize(0);
 
308
        assert (f.getSize() == 0);
 
309
}
 
310
 
 
311
 
 
312
void FileTest::testDirectory()
 
313
{
 
314
        File d("testdir");
 
315
        try
 
316
        {
 
317
                d.remove(true);
 
318
        }
 
319
        catch (...)
 
320
        {
 
321
        }
 
322
        TemporaryFile::registerForDeletion("testdir");
 
323
        
 
324
        bool created = d.createDirectory();
 
325
        assert (created);
 
326
        assert (d.isDirectory());
 
327
        assert (!d.isFile());
 
328
        std::vector<std::string> files;
 
329
        d.list(files);
 
330
        assert (files.empty());
 
331
        
 
332
        File f = Path("testdir/file1", Path::PATH_UNIX);
 
333
        f.createFile();
 
334
        f = Path("testdir/file2", Path::PATH_UNIX);
 
335
        f.createFile();
 
336
        f = Path("testdir/file3", Path::PATH_UNIX);
 
337
        f.createFile();
 
338
        
 
339
        d.list(files);
 
340
        assert (files.size() == 3);
 
341
        
 
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());
 
347
        
 
348
        File dd(Path("testdir/testdir2/testdir3", Path::PATH_UNIX));
 
349
        dd.createDirectories();
 
350
        assert (dd.exists());
 
351
        assert (dd.isDirectory());
 
352
 
 
353
        File ddd(Path("testdir/testdirB/testdirC/testdirD", Path::PATH_UNIX));
 
354
        ddd.createDirectories();
 
355
        assert (ddd.exists());
 
356
        assert (ddd.isDirectory());
 
357
        
 
358
        d.remove(true);
 
359
}
 
360
 
 
361
 
 
362
void FileTest::testCopy()
 
363
{
 
364
        std::ofstream ostr("testfile.dat");
 
365
        ostr << "Hello, world!" << std::endl;
 
366
        ostr.close();
 
367
 
 
368
        File f1("testfile.dat");
 
369
        TemporaryFile f2;
 
370
        f1.copyTo(f2.path());
 
371
        assert (f2.exists());
 
372
        assert (f1.getSize() == f2.getSize());
 
373
}
 
374
 
 
375
 
 
376
void FileTest::testMove()
 
377
{
 
378
        std::ofstream ostr("testfile.dat");
 
379
        ostr << "Hello, world!" << std::endl;
 
380
        ostr.close();
 
381
 
 
382
        File f1("testfile.dat");
 
383
        File::FileSize sz = f1.getSize();
 
384
        TemporaryFile f2;
 
385
        f1.moveTo(f2.path());
 
386
        assert (f2.exists());
 
387
        assert (f2.getSize() == sz);
 
388
        assert (f1.exists());
 
389
        assert (f1 == f2);
 
390
}
 
391
 
 
392
 
 
393
void FileTest::testRename()
 
394
{
 
395
        std::ofstream ostr("testfile.dat");
 
396
        ostr << "Hello, world!" << std::endl;
 
397
        ostr.close();
 
398
 
 
399
        File f1("testfile.dat");
 
400
        File f2("testfile2.dat");
 
401
        f1.renameTo(f2.path());
 
402
 
 
403
        assert (f2.exists());
 
404
        assert (f1.exists());
 
405
        assert (f1 == f2);
 
406
        
 
407
        f2.remove();
 
408
}
 
409
 
 
410
 
 
411
void FileTest::setUp()
 
412
{
 
413
        File f("testfile.dat");
 
414
        try
 
415
        {
 
416
                f.remove();
 
417
        }
 
418
        catch (...)
 
419
        {
 
420
        }
 
421
}
 
422
 
 
423
 
 
424
void FileTest::tearDown()
 
425
{
 
426
        File f("testfile.dat");
 
427
        try
 
428
        {
 
429
                f.remove();
 
430
        }
 
431
        catch (...)
 
432
        {
 
433
        }
 
434
}
 
435
 
 
436
 
 
437
CppUnit::Test* FileTest::suite()
 
438
{
 
439
        CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("FileTest");
 
440
 
 
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);
 
451
 
 
452
        return pSuite;
 
453
}