Refactor: FsppFileTest/FsppOpenFileTest use common base class

This commit is contained in:
Sebastian Meßmer 2015-03-19 05:47:06 +01:00
parent faf7f4a68d
commit 0ffb419bf8
3 changed files with 56 additions and 40 deletions

View File

@ -4,51 +4,15 @@
#include <sys/fcntl.h> #include <sys/fcntl.h>
#include <sys/stat.h> #include <sys/stat.h>
#include "testutils/FileTest.h"
//TODO I don't really want a dependency fspp -> blockstore. I probably should take the blockstore::Data class //TODO I don't really want a dependency fspp -> blockstore. I probably should take the blockstore::Data class
// (which is the only reason for the dependency here) and put it into a different package (cpp-utils?) // (which is the only reason for the dependency here) and put it into a different package (cpp-utils?)
#include <messmer/blockstore/utils/Data.h> #include <messmer/blockstore/utils/Data.h>
template<class ConcreteFileSystemTestFixture> template<class ConcreteFileSystemTestFixture>
class FsppFileTest: public FileSystemTest<ConcreteFileSystemTestFixture> { class FsppFileTest: public FileTest<ConcreteFileSystemTestFixture> {
public: public:
FsppFileTest() {
this->LoadDir("/")->createAndOpenFile("myfile", this->MODE_PUBLIC);
file_root = this->LoadFile("/myfile");
this->LoadDir("/")->createDir("mydir", this->MODE_PUBLIC);
this->LoadDir("/mydir")->createAndOpenFile("mynestedfile", this->MODE_PUBLIC);
file_nested = this->LoadFile("/mydir/mynestedfile");
}
std::unique_ptr<fspp::File> file_root;
std::unique_ptr<fspp::File> file_nested;
void EXPECT_SIZE(uint64_t expectedSize, const fspp::File &file) {
EXPECT_SIZE_IN_FILE(expectedSize, file);
auto openFile = file.open(O_RDONLY);
EXPECT_SIZE_IN_OPEN_FILE(expectedSize, *openFile);
EXPECT_NUMBYTES_READABLE(expectedSize, *openFile);
}
void EXPECT_SIZE_IN_FILE(uint64_t expectedSize, const fspp::File &file) {
struct stat st;
file.stat(&st);
EXPECT_EQ(expectedSize, st.st_size);
}
void EXPECT_SIZE_IN_OPEN_FILE(uint64_t expectedSize, const fspp::OpenFile &file) {
struct stat st;
file.stat(&st);
EXPECT_EQ(expectedSize, st.st_size);
}
void EXPECT_NUMBYTES_READABLE(uint64_t expectedSize, const fspp::OpenFile &file) {
blockstore::Data data(expectedSize);
//Try to read one byte more than the expected size
ssize_t readBytes = file.read(data.data(), expectedSize+1, 0);
//and check that it only read the expected size (but also not less)
EXPECT_EQ(expectedSize, readBytes);
}
void Test_Open_RDONLY(fspp::File *file) { void Test_Open_RDONLY(fspp::File *file) {
file->open(O_RDONLY); file->open(O_RDONLY);
} }

View File

@ -1,8 +1,10 @@
#ifndef MESSMER_FSPP_FSTEST_FSPPOPENFILETEST_H_ #ifndef MESSMER_FSPP_FSTEST_FSPPOPENFILETEST_H_
#define MESSMER_FSPP_FSTEST_FSPPOPENFILETEST_H_ #define MESSMER_FSPP_FSTEST_FSPPOPENFILETEST_H_
#include "testutils/FileTest.h"
template<class ConcreteFileSystemTestFixture> template<class ConcreteFileSystemTestFixture>
class FsppOpenFileTest: public FileSystemTest<ConcreteFileSystemTestFixture> { class FsppOpenFileTest: public FileTest<ConcreteFileSystemTestFixture> {
public: public:
}; };

View File

@ -0,0 +1,50 @@
#ifndef MESSMER_FSPP_FSTEST_TESTUTILS_FILETEST_H_
#define MESSMER_FSPP_FSTEST_TESTUTILS_FILETEST_H_
#include "FileSystemTest.h"
#include <memory>
template<class ConcreteFileSystemTestFixture>
class FileTest: public FileSystemTest<ConcreteFileSystemTestFixture> {
public:
FileTest() {
this->LoadDir("/")->createAndOpenFile("myfile", this->MODE_PUBLIC);
file_root = this->LoadFile("/myfile");
this->LoadDir("/")->createDir("mydir", this->MODE_PUBLIC);
this->LoadDir("/mydir")->createAndOpenFile("mynestedfile", this->MODE_PUBLIC);
file_nested = this->LoadFile("/mydir/mynestedfile");
}
std::unique_ptr<fspp::File> file_root;
std::unique_ptr<fspp::File> file_nested;
void EXPECT_SIZE(uint64_t expectedSize, const fspp::File &file) {
EXPECT_SIZE_IN_FILE(expectedSize, file);
auto openFile = file.open(O_RDONLY);
EXPECT_SIZE_IN_OPEN_FILE(expectedSize, *openFile);
EXPECT_NUMBYTES_READABLE(expectedSize, *openFile);
}
void EXPECT_SIZE_IN_FILE(uint64_t expectedSize, const fspp::File &file) {
struct stat st;
file.stat(&st);
EXPECT_EQ(expectedSize, st.st_size);
}
void EXPECT_SIZE_IN_OPEN_FILE(uint64_t expectedSize, const fspp::OpenFile &file) {
struct stat st;
file.stat(&st);
EXPECT_EQ(expectedSize, st.st_size);
}
void EXPECT_NUMBYTES_READABLE(uint64_t expectedSize, const fspp::OpenFile &file) {
blockstore::Data data(expectedSize);
//Try to read one byte more than the expected size
ssize_t readBytes = file.read(data.data(), expectedSize+1, 0);
//and check that it only read the expected size (but also not less)
EXPECT_EQ(expectedSize, readBytes);
}
};
#endif