Test loading a not existing blob (and fix behaviour for that case)

This commit is contained in:
Sebastian Messmer 2014-12-05 11:59:59 +01:00
parent 155bbeaba3
commit b9c2f43afb
6 changed files with 60 additions and 3 deletions

View File

@ -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)

View File

@ -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();

View File

@ -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);

View File

@ -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 */

View File

@ -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

View File

@ -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
);
}