Test loading a not existing blob (and fix behaviour for that case)
This commit is contained in:
parent
155bbeaba3
commit
b9c2f43afb
@ -1,3 +1,3 @@
|
||||
add_library(blobstore_ondisk Data.cpp OnDiskBlob.cpp OnDiskBlobStore.cpp FileAlreadyExistsException.cpp)
|
||||
add_library(blobstore_ondisk Data.cpp OnDiskBlob.cpp OnDiskBlobStore.cpp FileAlreadyExistsException.cpp FileDoesntExistException.cpp)
|
||||
|
||||
target_link_libraries(blobstore_ondisk boost_filesystem boost_system)
|
||||
|
@ -1,4 +1,6 @@
|
||||
#include <blobstore/implementations/ondisk/Data.h>
|
||||
#include "Data.h"
|
||||
|
||||
#include "FileDoesntExistException.h"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <fstream>
|
||||
@ -56,6 +58,7 @@ void Data::StoreToFile(const bf::path &filepath) const {
|
||||
|
||||
Data Data::LoadFromFile(const bf::path &filepath) {
|
||||
ifstream file(filepath.c_str(), ios::binary);
|
||||
_assertFileExists(file, filepath);
|
||||
size_t size = _getStreamSize(file);
|
||||
|
||||
Data result(size);
|
||||
@ -63,6 +66,12 @@ Data Data::LoadFromFile(const bf::path &filepath) {
|
||||
return result;
|
||||
}
|
||||
|
||||
void Data::_assertFileExists(const ifstream &file, const bf::path &filepath) {
|
||||
if (!file.good()) {
|
||||
throw FileDoesntExistException(filepath);
|
||||
}
|
||||
}
|
||||
|
||||
size_t Data::_getStreamSize(istream &stream) {
|
||||
auto current_pos = stream.tellg();
|
||||
|
||||
|
@ -32,6 +32,7 @@ private:
|
||||
size_t _size;
|
||||
void *_data;
|
||||
|
||||
static void _assertFileExists(const std::ifstream &file, const boost::filesystem::path &filepath);
|
||||
static size_t _getStreamSize(std::istream &stream);
|
||||
void _readFromStream(std::istream &stream);
|
||||
|
||||
|
@ -0,0 +1,19 @@
|
||||
#include <blobstore/implementations/ondisk/FileDoesntExistException.h>
|
||||
|
||||
namespace bf = boost::filesystem;
|
||||
|
||||
using std::runtime_error;
|
||||
using std::string;
|
||||
|
||||
namespace blobstore {
|
||||
namespace ondisk {
|
||||
|
||||
FileDoesntExistException::FileDoesntExistException(const bf::path &filepath)
|
||||
: runtime_error(string("The file ")+filepath.c_str()+" doesn't exist") {
|
||||
}
|
||||
|
||||
FileDoesntExistException::~FileDoesntExistException() {
|
||||
}
|
||||
|
||||
} /* namespace ondisk */
|
||||
} /* namespace blobstore */
|
@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
#ifndef BLOBSTORE_IMPLEMENTATIONS_ONDISK_FILEDOESNTEXISTEXCEPTION_H_
|
||||
#define BLOBSTORE_IMPLEMENTATIONS_ONDISK_FILEDOESNTEXISTEXCEPTION_H_
|
||||
|
||||
#include <boost/filesystem/path.hpp>
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
namespace blobstore {
|
||||
namespace ondisk {
|
||||
|
||||
class FileDoesntExistException: public std::runtime_error {
|
||||
public:
|
||||
FileDoesntExistException(const boost::filesystem::path &filepath);
|
||||
virtual ~FileDoesntExistException();
|
||||
};
|
||||
|
||||
} /* namespace ondisk */
|
||||
} /* namespace blobstore */
|
||||
|
||||
#endif
|
@ -5,6 +5,7 @@
|
||||
|
||||
#include "blobstore/implementations/ondisk/OnDiskBlob.h"
|
||||
#include "blobstore/implementations/ondisk/Data.h"
|
||||
#include "blobstore/implementations/ondisk/FileDoesntExistException.h"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
@ -64,4 +65,10 @@ TEST_P(OnDiskBlobLoadTest, LoadedDataIsCorrect) {
|
||||
EXPECT_BLOB_DATA_EQ(randomData, *blob);
|
||||
}
|
||||
|
||||
//TODO Test file doesn't exist
|
||||
TEST_F(OnDiskBlobLoadTest, LoadNotExistingBlob) {
|
||||
TempFile file2(false); // Pass false, so the file isn't created.
|
||||
EXPECT_THROW(
|
||||
OnDiskBlob::LoadFromDisk(file2.path()),
|
||||
FileDoesntExistException
|
||||
);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user