Author: kpvdr
Date: 2007-11-14 23:21:08 -0500 (Wed, 14 Nov 2007)
New Revision: 1316
Modified:
store/trunk/cpp/tests/jrnl/unit_test_jdir.cpp
store/trunk/cpp/tests/jrnl/unit_test_jinf.cpp
Log:
Added to unit tests
Modified: store/trunk/cpp/tests/jrnl/unit_test_jdir.cpp
===================================================================
--- store/trunk/cpp/tests/jrnl/unit_test_jdir.cpp 2007-11-14 22:20:42 UTC (rev 1315)
+++ store/trunk/cpp/tests/jrnl/unit_test_jdir.cpp 2007-11-15 04:21:08 UTC (rev 1316)
@@ -32,16 +32,22 @@
#include <boost/test/results_reporter.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/test/unit_test_log.hpp>
+#include <fstream>
#include <iostream>
#include <jrnl/jdir.hpp>
#include <jrnl/jerrno.hpp>
#include <jrnl/jexception.hpp>
+#include <sys/stat.h>
#define ERRORSTR(e) ::strerror(ret) << " (" << ret <<
")"
using namespace boost::unit_test;
using namespace rhm::journal;
+// Helper function declarations
+void make_file(const char* filename, mode_t fmode = S_IRUSR | S_IWUSR | S_IRGRP |
S_IROTH);
+void make_jrnl_files(const char* dirname);
+
// Test functions of the form
// void fn() {...}
@@ -56,17 +62,11 @@
void test_create_delete_dir()
{
+ // Use instance
std::string dir_A("/tmp/A");
std::string dir_Ats("/tmp/A/"); // trailing '/'
- std::string dir_Bts("/tmp/B/"); // trailing '/'
- std::string dir_C("/tmp/C");
- std::string dir_Dts("/tmp/D/"); // trailing '/'
- std::string dir_X("/tmp/X");
- std::string dir_Xts("/tmp/X/"); // trailing '/'
- std::string bfn("test_base");
-
- // Use instance
- jdir dir1(dir_A, bfn);
+ BOOST_CHECK(!jdir::exists(dir_A));
+ jdir dir1(dir_A, "test_base");
dir1.create_dir();
// check all combos of jdir::exists and jdir::is_dir()
BOOST_CHECK(jdir::exists(dir_A));
@@ -77,102 +77,232 @@
BOOST_CHECK(jdir::is_dir(dir_Ats));
BOOST_CHECK(jdir::is_dir(dir_Ats.c_str()));
BOOST_CHECK(jdir::is_dir(dir_Ats.c_str()));
+ dir1.delete_dir();
+ BOOST_CHECK(!jdir::exists(dir_A));
+
+ // Use static fn
+ BOOST_CHECK(!jdir::exists("/tmp/B"));
+ jdir::create_dir("/tmp/B");
+ BOOST_CHECK(jdir::is_dir("/tmp/B"));
+ jdir::delete_dir("/tmp/B");
+ BOOST_CHECK(!jdir::exists("/tmp/B"));
+
+ // Non-empty dirs
+ BOOST_CHECK(!jdir::exists("/tmp/C"));
+ jdir::create_dir("/tmp/C");
+ BOOST_CHECK(jdir::is_dir("/tmp/C"));
+ make_file("/tmp/C/test_file_1.txt"); // mode 644 (default)
+ make_file("/tmp/C/test_file_2.txt", S_IRWXU | S_IRWXG | S_IRWXO); // mode
777
+ make_file("/tmp/C/test_file_3.txt", S_IRUSR | S_IRGRP | S_IROTH); // mode
444 (read-only)
+ make_file("/tmp/C/test_file_4.txt", 0); // mode 000 (no permissions)
+ BOOST_CHECK(jdir::is_dir("/tmp/C"));
+ jdir::delete_dir("/tmp/C");
+ BOOST_CHECK(!jdir::exists("/tmp/C"));
// Check non-existent dirs fail
- BOOST_CHECK(!jdir::exists(dir_X));
- BOOST_CHECK(!jdir::exists(dir_Xts));
+ BOOST_CHECK(!jdir::exists("/tmp/D"));
try
{
- jdir::is_dir(dir_X);
+ jdir::is_dir("/tmp/D");
BOOST_ERROR("jdir::is_dir() failed to throw jexeption for non-existent
directory.");
}
catch(const jexception e)
{
BOOST_CHECK_EQUAL(e.err_code(), jerrno::JERR_JDIR_STAT);
}
+}
+
+void test_create_delete_dir_recursive()
+{
+ // Use instances
+ BOOST_CHECK(!jdir::exists("/tmp/E"));
+ jdir dir1("/tmp/E/F/G/H", "test_base");
+ dir1.create_dir();
+ BOOST_CHECK(jdir::is_dir("/tmp/E/F/G/H"));
+ dir1.delete_dir();
+ BOOST_CHECK(!jdir::exists("/tmp/E/F/G/H")); // only H deleted, E/F/G
remain
+ BOOST_CHECK(jdir::exists("/tmp/E"));
+ jdir::delete_dir("/tmp/E"); // delete remaining dirs
+ BOOST_CHECK(!jdir::exists("/tmp/E"));
+
+ BOOST_CHECK(!jdir::exists("/tmp/F"));
+ jdir dir2("/tmp/F/G/H/I/", "test_base"); // trailing '/'
+ dir2.create_dir();
+ BOOST_CHECK(jdir::is_dir("/tmp/F/G/H/I/"));
+ dir2.delete_dir();
+ BOOST_CHECK(!jdir::exists("/tmp/F/G/H/I/"));
+ BOOST_CHECK(jdir::exists("/tmp/F"));
+ jdir::delete_dir("/tmp/F");
+ BOOST_CHECK(!jdir::exists("/tmp/F"));
+
+ // Use static fn
+ BOOST_CHECK(!jdir::exists("/tmp/G"));
+ jdir::create_dir("/tmp/G/H/I/J");
+ BOOST_CHECK(jdir::is_dir("/tmp/G/H/I/J"));
+ jdir::delete_dir("/tmp/G/H/I/J");
+ BOOST_CHECK(!jdir::exists("/tmp/G/H/I/J"));
+ BOOST_CHECK(jdir::exists("/tmp/G"));
+ jdir::delete_dir("/tmp/G");
+ BOOST_CHECK(!jdir::exists("/tmp/G"));
+
+ BOOST_CHECK(!jdir::exists("/tmp/H"));
+ jdir::create_dir("/tmp/H/I/J/K/");
+ BOOST_CHECK(jdir::is_dir("/tmp/H/I/J/K/"));
+ jdir::delete_dir("/tmp/H/I/J/K/");
+ BOOST_CHECK(!jdir::exists("/tmp/H/I/J/K/"));
+ BOOST_CHECK(jdir::exists("/tmp/H"));
+ jdir::delete_dir("/tmp/H");
+ BOOST_CHECK(!jdir::exists("/tmp/H"));
+
+ // Non-empty dirs
+ BOOST_CHECK(!jdir::exists("/tmp/I"));
+ jdir::create_dir("/tmp/I/J/K1/L1");
+ jdir::create_dir("/tmp/I/J/K1/L2");
+ jdir::create_dir("/tmp/I/J/K1/L3");
+ jdir::create_dir("/tmp/I/J/K1/L4");
+ make_file("/tmp/I/J/K1/L4/test_file_1.txt"); // mode 644 (default)
+ make_file("/tmp/I/J/K1/L4/test_file_2.txt", S_IRWXU | S_IRWXG | S_IRWXO);
// mode 777
+ make_file("/tmp/I/J/K1/L4/test_file_3.txt", S_IRUSR | S_IRGRP | S_IROTH);
// mode 444
+ make_file("/tmp/I/J/K1/L4/test_file_4.txt", 0); // mode 000 (no
permissions)
+ jdir::create_dir("/tmp/I/J/K2");
+ jdir::create_dir("/tmp/I/J/K3/L5");
+ jdir::create_dir("/tmp/I/J/K3/L6");
+ BOOST_CHECK(jdir::is_dir("/tmp/I/J/K1/L1"));
+ BOOST_CHECK(jdir::is_dir("/tmp/I/J/K1/L2"));
+ BOOST_CHECK(jdir::is_dir("/tmp/I/J/K1/L3"));
+ BOOST_CHECK(jdir::is_dir("/tmp/I/J/K1/L4"));
+ BOOST_CHECK(jdir::is_dir("/tmp/I/J/K2"));
+ BOOST_CHECK(jdir::is_dir("/tmp/I/J/K3/L5"));
+ BOOST_CHECK(jdir::is_dir("/tmp/I/J/K3/L6"));
+ jdir::delete_dir("/tmp/I");
+ BOOST_CHECK(!jdir::exists("/tmp/I"));
+}
+
+void test_clear_dir()
+{
+ // Use instances
+ BOOST_CHECK(!jdir::exists("/tmp/test_dir_1"));
+ jdir test_dir_1("/tmp/test_dir_1", "test_base");
+ test_dir_1.create_dir();
+ BOOST_CHECK(jdir::is_dir("/tmp/test_dir_1"));
+ // add some journal files
+ make_jrnl_files("/tmp/test_dir_1");
+ // clear dir
+ test_dir_1.clear_dir();
+ BOOST_CHECK(jdir::is_dir("/tmp/test_dir_1"));
+ BOOST_CHECK(jdir::is_dir("/tmp/test_dir_1/_test_base.bak.0001"));
+
BOOST_CHECK(jdir::exists("/tmp/test_dir_1/_test_base.bak.0001/test_base.jinf"));
+ // add some more journal files
+ make_jrnl_files("/tmp/test_dir_1");
+ // clear dir
+ test_dir_1.clear_dir();
+ BOOST_CHECK(jdir::is_dir("/tmp/test_dir_1"));
+ BOOST_CHECK(jdir::is_dir("/tmp/test_dir_1/_test_base.bak.0001"));
+
BOOST_CHECK(jdir::exists("/tmp/test_dir_1/_test_base.bak.0001/test_base.jinf"));
+ BOOST_CHECK(jdir::is_dir("/tmp/test_dir_1/_test_base.bak.0002"));
+
BOOST_CHECK(jdir::exists("/tmp/test_dir_1/_test_base.bak.0002/test_base.jinf"));
+ // add some more journal files
+ make_jrnl_files("/tmp/test_dir_1");
+ // clear dir
+ test_dir_1.clear_dir();
+ BOOST_CHECK(jdir::is_dir("/tmp/test_dir_1"));
+ BOOST_CHECK(jdir::is_dir("/tmp/test_dir_1/_test_base.bak.0001"));
+
BOOST_CHECK(jdir::exists("/tmp/test_dir_1/_test_base.bak.0001/test_base.jinf"));
+ BOOST_CHECK(jdir::is_dir("/tmp/test_dir_1/_test_base.bak.0002"));
+
BOOST_CHECK(jdir::exists("/tmp/test_dir_1/_test_base.bak.0002/test_base.jinf"));
+ BOOST_CHECK(jdir::is_dir("/tmp/test_dir_1/_test_base.bak.0003"));
+
BOOST_CHECK(jdir::exists("/tmp/test_dir_1/_test_base.bak.0003/test_base.jinf"));
+ // clean up
+ test_dir_1.delete_dir();
+ BOOST_CHECK(!jdir::exists("/tmp/test_dir_1"));
+
+ // non-existent dir with auto-create true
+ BOOST_CHECK(!jdir::exists("/tmp/test_dir_2"));
+ jdir test_dir_2("/tmp/test_dir_2", "test_base");
+ // clear dir
+ test_dir_2.clear_dir(); // create flag is true by default
+ BOOST_CHECK(jdir::is_dir("/tmp/test_dir_2"));
+ // add some journal files
+ make_jrnl_files("/tmp/test_dir_2");
+ // clear dir
+ test_dir_2.clear_dir();
+ BOOST_CHECK(jdir::is_dir("/tmp/test_dir_2"));
+ BOOST_CHECK(jdir::is_dir("/tmp/test_dir_2/_test_base.bak.0001"));
+
BOOST_CHECK(jdir::exists("/tmp/test_dir_2/_test_base.bak.0001/test_base.jinf"));
+ // clean up
+ test_dir_2.delete_dir();
+ BOOST_CHECK(!jdir::exists("/tmp/test_dir_2"));
+
+ // non-existent dir with auto-create false
+ BOOST_CHECK(!jdir::exists("/tmp/test_dir_3"));
+ jdir test_dir_3("/tmp/test_dir_3", "test_base");
try
{
- jdir::is_dir(dir_Xts);
- BOOST_ERROR("jdir::is_dir() failed to throw jexeption for non-existent
directory.");
+ test_dir_3.clear_dir(false);
+ BOOST_ERROR("jdir::clear_dir(flase) failed to throw jexeption for
non-existent directory.");
}
catch(const jexception e)
{
- BOOST_CHECK_EQUAL(e.err_code(), jerrno::JERR_JDIR_STAT);
+ BOOST_CHECK_EQUAL(e.err_code(), jerrno::JERR_JDIR_OPENDIR);
}
- jdir dir2(dir_Bts, bfn);
- dir2.create_dir();
- BOOST_CHECK(jdir::is_dir(dir_Bts));
-
// static fn
- jdir::create_dir(dir_C);
- BOOST_CHECK(jdir::is_dir(dir_C));
- jdir::create_dir(dir_Dts);
- BOOST_CHECK(jdir::is_dir(dir_Dts));
-
- // Clean up with delete_dir
- dir1.delete_dir();
- BOOST_CHECK(!jdir::exists(dir_A));
- dir2.delete_dir();
- BOOST_CHECK(!jdir::exists(dir_Bts));
- // static fn
- jdir::delete_dir(dir_C);
- BOOST_CHECK(!jdir::exists(dir_C));
- jdir::delete_dir(dir_Dts);
- BOOST_CHECK(!jdir::exists(dir_Dts));
+ BOOST_CHECK(!jdir::exists("/tmp/test_dir_4"));
+ jdir::create_dir("/tmp/test_dir_4");
+ BOOST_CHECK(jdir::is_dir("/tmp/test_dir_4"));
+ // add some journal files
+ make_jrnl_files("/tmp/test_dir_4");
+ // clear dir
+ jdir::clear_dir("/tmp/test_dir_4", "test_base");
+ BOOST_CHECK(jdir::is_dir("/tmp/test_dir_4"));
+ BOOST_CHECK(jdir::is_dir("/tmp/test_dir_4/_test_base.bak.0001"));
+
BOOST_CHECK(jdir::exists("/tmp/test_dir_4/_test_base.bak.0001/test_base.jinf"));
+ // add some more journal files
+ make_jrnl_files("/tmp/test_dir_4");
+ // clear dir
+ jdir::clear_dir("/tmp/test_dir_4", "test_base");
+ BOOST_CHECK(jdir::is_dir("/tmp/test_dir_4"));
+ BOOST_CHECK(jdir::is_dir("/tmp/test_dir_4/_test_base.bak.0001"));
+
BOOST_CHECK(jdir::exists("/tmp/test_dir_4/_test_base.bak.0001/test_base.jinf"));
+ BOOST_CHECK(jdir::is_dir("/tmp/test_dir_4/_test_base.bak.0002"));
+
BOOST_CHECK(jdir::exists("/tmp/test_dir_4/_test_base.bak.0002/test_base.jinf"));
+ // add some more journal files
+ make_jrnl_files("/tmp/test_dir_4");
+ // clear dir
+ jdir::clear_dir("/tmp/test_dir_4", "test_base");
+ BOOST_CHECK(jdir::is_dir("/tmp/test_dir_4"));
+ BOOST_CHECK(jdir::is_dir("/tmp/test_dir_4/_test_base.bak.0001"));
+
BOOST_CHECK(jdir::exists("/tmp/test_dir_4/_test_base.bak.0001/test_base.jinf"));
+ BOOST_CHECK(jdir::is_dir("/tmp/test_dir_4/_test_base.bak.0002"));
+
BOOST_CHECK(jdir::exists("/tmp/test_dir_4/_test_base.bak.0002/test_base.jinf"));
+ BOOST_CHECK(jdir::is_dir("/tmp/test_dir_4/_test_base.bak.0003"));
+
BOOST_CHECK(jdir::exists("/tmp/test_dir_4/_test_base.bak.0003/test_base.jinf"));
+ // clean up
+ jdir::delete_dir("/tmp/test_dir_4");
+ BOOST_CHECK(!jdir::exists("/tmp/test_dir_4"));
}
-void test_create_delete_dir_recursive()
+// Helper functions
+
+void make_file(const char* filename, mode_t fmode)
{
- std::string dir_E("/tmp/E/F/G/H");
- std::string dir_Eb("/tmp/E"); // base dir
- std::string dir_F("/tmp/F/G/H/I/"); // trailing '/'
- std::string dir_Fb("/tmp/F"); // base dir
- std::string dir_G("/tmp/G/H/I/J");
- std::string dir_Gb("/tmp/G"); // base dir
- std::string dir_H("/tmp/H/I/J/K/"); // trailing '/'
- std::string dir_Hb("/tmp/H"); // base dir
- std::string bfn("test_base");
+ std::ofstream of(filename, std::ofstream::out | std::ofstream::trunc);
+ if (!of.good())
+ BOOST_FAIL("Unable to open file " << filename << "
for writing.");
+ of.write(filename, ::strlen(filename));
+ of.close();
+ ::chmod(filename, fmode);
+}
- // Use instances
- jdir dir1(dir_E, bfn);
- dir1.create_dir();
- BOOST_CHECK(jdir::is_dir(dir_E));
- jdir dir2(dir_F, bfn);
- dir2.create_dir();
- BOOST_CHECK(jdir::is_dir(dir_F));
-
- // static fn
- jdir::create_dir(dir_G);
- BOOST_CHECK(jdir::is_dir(dir_G));
- jdir::create_dir(dir_H);
- BOOST_CHECK(jdir::is_dir(dir_H));
-
- // Clean up with delete_dir
- dir1.delete_dir();
- BOOST_CHECK(!jdir::exists(dir_E)); // only H deleted, E/F/G remain
- BOOST_CHECK(jdir::exists(dir_Eb));
- jdir::delete_dir(dir_Eb); // delete remaining dirs
- BOOST_CHECK(!jdir::exists(dir_Eb));
-
- dir2.delete_dir();
- BOOST_CHECK(!jdir::exists(dir_F));
- BOOST_CHECK(jdir::exists(dir_Fb));
- jdir::delete_dir(dir_Fb);
- BOOST_CHECK(!jdir::exists(dir_Fb));
-
- // static fn
- jdir::delete_dir(dir_G);
- BOOST_CHECK(!jdir::exists(dir_G));
- BOOST_CHECK(jdir::exists(dir_Gb));
- jdir::delete_dir(dir_Gb);
- BOOST_CHECK(!jdir::exists(dir_Gb));
-
- jdir::delete_dir(dir_H);
- BOOST_CHECK(!jdir::exists(dir_H));
- BOOST_CHECK(jdir::exists(dir_Hb));
- jdir::delete_dir(dir_Hb);
- BOOST_CHECK(!jdir::exists(dir_Hb));
+void make_jrnl_files(const char* dirname)
+{
+ make_file(std::string(dirname).append("/test_base.0000.jdat").c_str());
+ make_file(std::string(dirname).append("/test_base.0001.jdat").c_str());
+ make_file(std::string(dirname).append("/test_base.0002.jdat").c_str());
+ make_file(std::string(dirname).append("/test_base.0003.jdat").c_str());
+ make_file(std::string(dirname).append("/test_base.0004.jdat").c_str());
+ make_file(std::string(dirname).append("/test_base.jinf").c_str());
+
make_file(std::string(dirname).append("/test_base.12345.abcdef.crazy-filename").c_str());
}
// Initialize test suite and include test functions
@@ -188,6 +318,7 @@
ts->add(BOOST_TEST_CASE(&test_constructor));
ts->add(BOOST_TEST_CASE(&test_create_delete_dir));
ts->add(BOOST_TEST_CASE(&test_create_delete_dir_recursive));
+ ts->add(BOOST_TEST_CASE(&test_clear_dir));
return ts;
}
Modified: store/trunk/cpp/tests/jrnl/unit_test_jinf.cpp
===================================================================
--- store/trunk/cpp/tests/jrnl/unit_test_jinf.cpp 2007-11-14 22:20:42 UTC (rev 1315)
+++ store/trunk/cpp/tests/jrnl/unit_test_jinf.cpp 2007-11-15 04:21:08 UTC (rev 1316)
@@ -43,6 +43,7 @@
using namespace boost::unit_test;
using namespace rhm::journal;
+// Helper function declarations
const std::string jid("test journal id");
const std::string jdir("/tmp");
const std::string base_filename("test_base");
Show replies by date