DWORD getFileSize() const
{
DWORD fileSize = INVALID_FILE_SIZE;
HANDLE file = CreateFile( mFileName.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL );
if( file != INVALID_HANDLE_VALUE )
{
fileSize = GetFileSize( file, NULL );
CloseHandle( file );
}
if( fileSize == INVALID_FILE_SIZE )
throw FileStatusError();
return fileSize;
}
bool fileExist() const
{
return GetFileAttributes( mFileName.c_str() ) != INVALID_FILE_ATTRIBUTES;
}
void setFileModifyDate( const FILETIME* fileTime )
{
BOOL result = FALSE;
HANDLE file = CreateFile( mFileName.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL );
if( file != INVALID_HANDLE_VALUE )
{
result = SetFileTime( file, NULL, NULL, fileTime );
int i = GetLastError();
CloseHandle( file );
}
if( ! result )
throw FileStatusError();
}
FILETIME getFileModifyDate() const
{
FILETIME time;
BOOL result = FALSE;
HANDLE file = CreateFile( mFileName.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL );
if( file != INVALID_HANDLE_VALUE )
{
result = GetFileTime( file, NULL, NULL, &time );
CloseHandle( file );
}
if( ! result )
throw FileStatusError();
return time;
}
std::string getFileName() const
{
return mFileName;
}
};
class MyTestCase:public CPPUNIT_NS::TestFixture
{
std::string mFileNameExist;
std::string mFileNameNotExist;
std::string mTestFolder;
enum DUMMY
{
FILE_SIZE = 1011
};
public:
virtual void setUp()
{
mTestFolder = "c:justfortest";
mFileNameExist = mTestFolder + "exist.dat";
mFileNameNotExist = mTestFolder + "notexist.dat";
if( GetFileAttributes( mTestFolder.c_str() ) != INVALID_FILE_ATTRIBUTES )
throw std::exception( "test folder already exists" );
if( ! CreateDirectory( mTestFolder.c_str() ,NULL ) )
throw std::exception( "cannot create folder" );
HANDLE file = CreateFile( mFileNameExist.c_str(), GENERIC_READ | GENERIC_WRITE,
0, NULL, CREATE_NEW, 0, NULL );
if( file == INVALID_HANDLE_VALUE )
throw std::exception( "cannot create file" );
char buffer[FILE_SIZE];
DWORD bytesWritten;
if( !WriteFile( file, buffer, FILE_SIZE, &bytesWritten, NULL ) ||
bytesWritten != FILE_SIZE )
{
CloseHandle( file );
throw std::exception( "cannot write file" );
}
CloseHandle( file );
}
virtual void tearDown()
{
if( ! DeleteFile( mFileNameExist.c_str() ) )
throw std::exception( "cannot delete file" );
if( ! RemoveDirectory( mTestFolder.c_str() ) )
throw std::exception( "cannot remove folder" );
}
void testCtorAndGetName()
文章来源于领测软件测试网 https://www.ltesting.net/