[SF 2632636] FileChannel Purge Unit Tests Broken

This commit is contained in:
Aleksandar Fabijanic
2009-04-02 13:10:20 +00:00
parent 614c87b4be
commit 8fe9757195
3 changed files with 46 additions and 10 deletions

View File

@@ -154,6 +154,7 @@ class Foundation_API FileChannel: public Channel
/// ///
/// The purgeAge property can have the following values: /// The purgeAge property can have the following values:
/// ///
/// * "none" or "" no purging
/// * <n> [seconds] the maximum age is <n> seconds. /// * <n> [seconds] the maximum age is <n> seconds.
/// * <n> minutes: the maximum age is <n> minutes. /// * <n> minutes: the maximum age is <n> minutes.
/// * <n> hours: the maximum age is <n> hours. /// * <n> hours: the maximum age is <n> hours.
@@ -161,10 +162,10 @@ class Foundation_API FileChannel: public Channel
/// * <n> weeks: the maximum age is <n> weeks. /// * <n> weeks: the maximum age is <n> weeks.
/// * <n> months: the maximum age is <n> months, where a month has 30 days. /// * <n> months: the maximum age is <n> months, where a month has 30 days.
/// ///
/// The purgeCount property has an integer value that /// The purgeCount property has an integer value that specifies the maximum number
/// specifies the maximum number of archived log files. /// of archived log files. If the number is exceeded, archived log files are
/// If the number is exceeded, archived log files are /// deleted, starting with the oldest. When "none" or empty string are
/// deleted, starting with the oldest. /// supplied, they reset purgeCount to none (no purging).
/// ///
/// For a more lightweight file channel class, see SimpleFileChannel. /// For a more lightweight file channel class, see SimpleFileChannel.
{ {

View File

@@ -302,8 +302,14 @@ void FileChannel::setCompress(const std::string& compress)
void FileChannel::setPurgeAge(const std::string& age) void FileChannel::setPurgeAge(const std::string& age)
{ {
delete _pPurgeStrategy;
_pPurgeStrategy = 0;
_purgeAge = "none";
if (age.empty() || 0 == icompare(age, "none")) return;
std::string::const_iterator it = age.begin(); std::string::const_iterator it = age.begin();
std::string::const_iterator end = age.end(); std::string::const_iterator end = age.end();
int n = 0; int n = 0;
while (it != end && std::isspace(*it)) ++it; while (it != end && std::isspace(*it)) ++it;
while (it != end && std::isdigit(*it)) { n *= 10; n += *it++ - '0'; } while (it != end && std::isdigit(*it)) { n *= 10; n += *it++ - '0'; }
@@ -324,8 +330,10 @@ void FileChannel::setPurgeAge(const std::string& age)
factor = 30*Timespan::DAYS; factor = 30*Timespan::DAYS;
else if (unit != "seconds") else if (unit != "seconds")
throw InvalidArgumentException("purgeAge", age); throw InvalidArgumentException("purgeAge", age);
delete _pPurgeStrategy; if (0 == n)
throw InvalidArgumentException("Zero is not valid purge age.");
_pPurgeStrategy = new PurgeByAgeStrategy(Timespan(factor*n)); _pPurgeStrategy = new PurgeByAgeStrategy(Timespan(factor*n));
_purgeAge = age; _purgeAge = age;
} }
@@ -333,14 +341,22 @@ void FileChannel::setPurgeAge(const std::string& age)
void FileChannel::setPurgeCount(const std::string& count) void FileChannel::setPurgeCount(const std::string& count)
{ {
delete _pPurgeStrategy;
_pPurgeStrategy = 0;
_purgeAge = "none";
if (count.empty() || 0 == icompare(count, "none")) return;
int n = 0;
std::string::const_iterator it = count.begin(); std::string::const_iterator it = count.begin();
std::string::const_iterator end = count.end(); std::string::const_iterator end = count.end();
int n = 0;
while (it != end && std::isspace(*it)) ++it; while (it != end && std::isspace(*it)) ++it;
while (it != end && std::isdigit(*it)) { n *= 10; n += *it++ - '0'; } while (it != end && std::isdigit(*it)) { n *= 10; n += *it++ - '0'; }
while (it != end && std::isspace(*it)) ++it; while (it != end && std::isspace(*it)) ++it;
delete _pPurgeStrategy; if (0 == n)
throw InvalidArgumentException("Zero is not valid purge count.");
_pPurgeStrategy = new PurgeByCountStrategy(n); _pPurgeStrategy = new PurgeByCountStrategy(n);
_purgeCount = count; _purgeCount = count;
} }

View File

@@ -47,6 +47,7 @@
#include "Poco/DateTimeFormat.h" #include "Poco/DateTimeFormat.h"
#include "Poco/NumberFormatter.h" #include "Poco/NumberFormatter.h"
#include "Poco/DirectoryIterator.h" #include "Poco/DirectoryIterator.h"
#include "Poco/Exception.h"
#include <vector> #include <vector>
@@ -64,6 +65,7 @@ using Poco::LocalDateTime;
using Poco::DateTimeFormatter; using Poco::DateTimeFormatter;
using Poco::DateTimeFormat; using Poco::DateTimeFormat;
using Poco::DirectoryIterator; using Poco::DirectoryIterator;
using Poco::InvalidArgumentException;
FileChannelTest::FileChannelTest(const std::string& name): CppUnit::TestCase(name) FileChannelTest::FileChannelTest(const std::string& name): CppUnit::TestCase(name)
@@ -405,6 +407,7 @@ void FileChannelTest::purgeAge(const std::string& pa)
void FileChannelTest::noPurgeAge(const std::string& npa) void FileChannelTest::noPurgeAge(const std::string& npa)
{ {
std::string name = filename(); std::string name = filename();
try try
{ {
AutoPtr<FileChannel> pChannel = new FileChannel(name); AutoPtr<FileChannel> pChannel = new FileChannel(name);
@@ -444,8 +447,16 @@ void FileChannelTest::noPurgeAge(const std::string& npa)
void FileChannelTest::testPurgeAge() void FileChannelTest::testPurgeAge()
{ {
purgeAge("5 seconds"); purgeAge("5 seconds");
noPurgeAge("0 seconds"); try
{
noPurgeAge("0 seconds");
fail ("must fail");
} catch (InvalidArgumentException&)
{
}
noPurgeAge(""); noPurgeAge("");
noPurgeAge("none");
} }
@@ -516,8 +527,16 @@ void FileChannelTest::noPurgeCount(const std::string& npc)
void FileChannelTest::testPurgeCount() void FileChannelTest::testPurgeCount()
{ {
purgeCount("2"); purgeCount("2");
try
{
noPurgeCount("0");
fail ("must fail");
} catch (InvalidArgumentException&)
{
}
noPurgeCount(""); noPurgeCount("");
noPurgeCount("0"); noPurgeCount("none");
} }