00001 #include "util/tempfile.hh"
00002
00003 #include <fstream>
00004
00005 #include <boost/filesystem.hpp>
00006
00007 #define BOOST_TEST_MODULE TempFileTest
00008 #include <boost/test/unit_test.hpp>
00009
00010 namespace util
00011 {
00012 namespace
00013 {
00014
00015 BOOST_AUTO_TEST_CASE(temp_dir_has_path)
00016 {
00017 BOOST_CHECK(temp_dir().path().size() > 0);
00018 }
00019
00020 BOOST_AUTO_TEST_CASE(temp_dir_creates_temp_directory)
00021 {
00022 const temp_dir t;
00023 BOOST_CHECK(boost::filesystem::exists(t.path()));
00024 BOOST_CHECK(boost::filesystem::is_directory(t.path()));
00025 }
00026
00027 BOOST_AUTO_TEST_CASE(temp_dir_creates_unique_directory)
00028 {
00029 BOOST_CHECK(temp_dir().path() != temp_dir().path());
00030 }
00031
00032 BOOST_AUTO_TEST_CASE(temp_dir_cleans_up_directory)
00033 {
00034 std::string path;
00035 {
00036 const temp_dir t;
00037 path = t.path();
00038 }
00039 BOOST_CHECK(!boost::filesystem::exists(path));
00040 }
00041
00042 BOOST_AUTO_TEST_CASE(temp_dir_cleanup_succeeds_if_directory_contains_file)
00043 {
00044 std::string path;
00045 {
00046 const temp_dir t;
00047 path = t.path();
00048 boost::filesystem::create_directory(path + "/directory");
00049 std::ofstream file((path + "/file").c_str());
00050 file << "Text";
00051 file.flush();
00052 }
00053 BOOST_CHECK(!boost::filesystem::exists(path));
00054 }
00055
00056 BOOST_AUTO_TEST_CASE(temp_dir_cleanup_succeeds_if_directory_is_gone)
00057 {
00058 std::string path;
00059 {
00060 const temp_dir t;
00061 path = t.path();
00062 boost::filesystem::remove_all(path);
00063 }
00064 BOOST_CHECK(!boost::filesystem::exists(path));
00065 }
00066
00067 BOOST_AUTO_TEST_CASE(temp_file_has_path)
00068 {
00069 BOOST_CHECK(temp_file().path().size() > 0);
00070 }
00071
00072 BOOST_AUTO_TEST_CASE(temp_file_creates_temp_file)
00073 {
00074 const temp_file f;
00075 BOOST_CHECK(boost::filesystem::exists(f.path()));
00076 BOOST_CHECK(boost::filesystem::is_regular_file(f.path()));
00077 }
00078
00079 BOOST_AUTO_TEST_CASE(temp_file_creates_unique_file)
00080 {
00081 BOOST_CHECK(temp_file().path() != temp_file().path());
00082 }
00083
00084 BOOST_AUTO_TEST_CASE(temp_file_creates_writable_file)
00085 {
00086 const std::string data = "Test-data-goes-here";
00087 const temp_file f;
00088 std::ofstream outfile(f.path().c_str());
00089 outfile << data;
00090 outfile.flush();
00091 std::string read_data;
00092 std::ifstream infile(f.path().c_str());
00093 infile >> read_data;
00094 BOOST_CHECK_EQUAL(data, read_data);
00095 }
00096
00097 BOOST_AUTO_TEST_CASE(temp_file_cleans_up_file)
00098 {
00099 std::string path;
00100 {
00101 const temp_file f;
00102 path = f.path();
00103 }
00104 BOOST_CHECK(!boost::filesystem::exists(path));
00105 }
00106
00107 BOOST_AUTO_TEST_CASE(temp_file_cleanup_succeeds_if_file_is_gone)
00108 {
00109 std::string path;
00110 {
00111 const temp_file t;
00112 path = t.path();
00113 boost::filesystem::remove(path);
00114 }
00115 BOOST_CHECK(!boost::filesystem::exists(path));
00116 }
00117
00118 }
00119 }