?? filetest.cpp
字號:
//
// FileTest.cpp
//
// $Id: //poco/Main/Foundation/testsuite/src/FileTest.cpp#5 $
//
// Copyright (c) 2004, Guenter Obiltschnig/Applied Informatics.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Redistributions in any form must be accompanied by information on
// how to obtain complete source code for this software and any
// accompanying software that uses this software. The source code
// must either be included in the distribution or be available for no
// more than the cost of distribution plus a nominal fee, and must be
// freely redistributable under reasonable conditions. For an
// executable file, complete source code means the source code for all
// modules it contains. It does not include source code for modules or
// files that typically accompany the major components of the operating
// system on which the executable file runs.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
#include "FileTest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Foundation/File.h"
#include "Foundation/TemporaryFile.h"
#include "Foundation/Path.h"
#include "Foundation/Exception.h"
#include "Foundation/Thread.h"
#include <fstream>
#include <set>
using Foundation::File;
using Foundation::TemporaryFile;
using Foundation::Path;
using Foundation::Exception;
using Foundation::Timestamp;
using Foundation::Thread;
FileTest::FileTest(const std::string& name): CppUnit::TestCase(name)
{
}
FileTest::~FileTest()
{
}
void FileTest::testFileAttributes1()
{
File f("testfile.dat");
assert (!f.exists());
try
{
bool flag = f.canRead();
failmsg("file does not exist - must throw exception");
}
catch (Exception&)
{
}
try
{
bool flag = f.canWrite();
failmsg("file does not exist - must throw exception");
}
catch (Exception&)
{
}
try
{
bool flag = f.isFile();
failmsg("file does not exist - must throw exception");
}
catch (Exception&)
{
}
try
{
bool flag = f.isDirectory();
failmsg("file does not exist - must throw exception");
}
catch (Exception&)
{
}
try
{
Timestamp ts = f.created();
failmsg("file does not exist - must throw exception");
}
catch (Exception&)
{
}
try
{
Timestamp ts = f.getLastModified();
failmsg("file does not exist - must throw exception");
}
catch (Exception&)
{
}
try
{
Timestamp ts;
f.setLastModified(ts);
failmsg("file does not exist - must throw exception");
}
catch (Exception&)
{
}
try
{
File::FileSize fs = f.getSize();
failmsg("file does not exist - must throw exception");
}
catch (Exception&)
{
}
try
{
f.setSize(0);
failmsg("file does not exist - must throw exception");
}
catch (Exception&)
{
}
try
{
f.setWriteable();
failmsg("file does not exist - must throw exception");
}
catch (Exception&)
{
}
try
{
f.setReadOnly();
failmsg("file does not exist - must throw exception");
}
catch (Exception&)
{
}
try
{
f.copyTo("copy.dat");
failmsg("file does not exist - must throw exception");
}
catch (Exception&)
{
}
try
{
f.moveTo("copy.dat");
failmsg("file does not exist - must throw exception");
}
catch (Exception&)
{
}
try
{
f.renameTo("copy.dat");
failmsg("file does not exist - must throw exception");
}
catch (Exception&)
{
}
try
{
f.remove();
failmsg("file does not exist - must throw exception");
}
catch (Exception&)
{
}
}
void FileTest::testCreateFile()
{
File f("testfile.dat");
bool created = f.createFile();
assert (created);
created = f.createFile();
assert (!created);
}
void FileTest::testFileAttributes2()
{
TemporaryFile f;
bool created = f.createFile();
Timestamp ts;
assert (created);
assert (f.exists());
assert (f.canRead());
assert (f.canWrite());
assert (f.isFile());
assert (!f.isDirectory());
Timestamp tsc = f.created();
Timestamp tsm = f.getLastModified();
assert (tsc - ts >= -2000000 && tsc - ts <= 2000000);
assert (tsm - ts >= -2000000 && tsm - ts <= 2000000);
f.setWriteable(false);
assert (!f.canWrite());
assert (f.canRead());
f.setReadOnly(false);
assert (f.canWrite());
assert (f.canRead());
ts = Timestamp::fromEpochTime(1000000);
f.setLastModified(ts);
assert (f.getLastModified() == ts);
}
void FileTest::testCompare()
{
File f1("abc.txt");
File f2("def.txt");
File f3("abc.txt");
assert (f1 == f3);
assert (!(f1 == f2));
assert (f1 != f2);
assert (!(f1 != f3));
assert (!(f1 == f2));
assert (f1 < f2);
assert (f1 <= f2);
assert (!(f2 < f1));
assert (!(f2 <= f1));
assert (f2 > f1);
assert (f2 >= f1);
assert (!(f1 > f2));
assert (!(f1 >= f2));
assert (f1 <= f3);
assert (f1 >= f3);
}
void FileTest::testSize()
{
std::ofstream ostr("testfile.dat");
ostr << "Hello, world!" << std::endl;
ostr.close();
File f("testfile.dat");
assert (f.getSize() > 0);
f.setSize(0);
assert (f.getSize() == 0);
}
void FileTest::testDirectory()
{
File d("testdir");
try
{
d.remove(true);
}
catch (...)
{
}
TemporaryFile::registerForDeletion("testdir");
bool created = d.createDirectory();
assert (created);
assert (d.isDirectory());
assert (!d.isFile());
std::vector<std::string> files;
d.list(files);
assert (files.empty());
File f = Path("testdir/file1", Path::PATH_UNIX);
f.createFile();
f = Path("testdir/file2", Path::PATH_UNIX);
f.createFile();
f = Path("testdir/file3", Path::PATH_UNIX);
f.createFile();
d.list(files);
assert (files.size() == 3);
std::set<std::string> fs;
fs.insert(files.begin(), files.end());
assert (fs.find("file1") != fs.end());
assert (fs.find("file2") != fs.end());
assert (fs.find("file3") != fs.end());
File dd(Path("testdir/testdir2/testdir3", Path::PATH_UNIX));
dd.createDirectories();
assert (dd.exists());
assert (dd.isDirectory());
d.remove(true);
}
void FileTest::testCopy()
{
std::ofstream ostr("testfile.dat");
ostr << "Hello, world!" << std::endl;
ostr.close();
File f1("testfile.dat");
TemporaryFile f2;
f1.copyTo(f2.path());
assert (f2.exists());
assert (f1.getSize() == f2.getSize());
}
void FileTest::testMove()
{
std::ofstream ostr("testfile.dat");
ostr << "Hello, world!" << std::endl;
ostr.close();
File f1("testfile.dat");
File::FileSize sz = f1.getSize();
TemporaryFile f2;
f1.moveTo(f2.path());
assert (f2.exists());
assert (f2.getSize() == sz);
assert (f1.exists());
assert (f1 == f2);
}
void FileTest::testRename()
{
std::ofstream ostr("testfile.dat");
ostr << "Hello, world!" << std::endl;
ostr.close();
File f1("testfile.dat");
File f2("testfile2.dat");
f1.renameTo(f2.path());
assert (f2.exists());
assert (f1.exists());
assert (f1 == f2);
f2.remove();
}
void FileTest::setUp()
{
File f("testfile.dat");
try
{
f.remove();
}
catch (...)
{
}
}
void FileTest::tearDown()
{
File f("testfile.dat");
try
{
f.remove();
}
catch (...)
{
}
}
CppUnit::Test* FileTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("FileTest");
CppUnit_addTest(pSuite, FileTest, testFileAttributes1);
CppUnit_addTest(pSuite, FileTest, testFileAttributes2);
CppUnit_addTest(pSuite, FileTest, testCompare);
CppUnit_addTest(pSuite, FileTest, testSize);
CppUnit_addTest(pSuite, FileTest, testDirectory);
CppUnit_addTest(pSuite, FileTest, testCopy);
CppUnit_addTest(pSuite, FileTest, testMove);
CppUnit_addTest(pSuite, FileTest, testRename);
return pSuite;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -