Update of /cvsroot/objecthandler/log4cxx-0.9.7/tests/src/varia
In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv25784/tests/src/varia
Added Files:
Makefile.am errorhandlertestcase.cpp
levelmatchfiltertestcase.cpp levelrangefiltertestcase.cpp
Log Message:
--- NEW FILE: levelrangefiltertestcase.cpp ---
/*
* Copyright 2003,2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <log4cxx/logger.h>
#include <log4cxx/simplelayout.h>
#include <log4cxx/fileappender.h>
#include <log4cxx/level.h>
#include <log4cxx/varia/levelrangefilter.h>
#include "../util/compare.h"
using namespace log4cxx;
using namespace log4cxx::helpers;
using namespace log4cxx::varia;
#define ACCEPT_FILE _T("output/LevelRangeFilter_accept")
#define ACCEPT_WITNESS _T("witness/LevelRangeFilter_accept")
#define NEUTRAL_FILE _T("output/LevelRangeFilter_neutral")
#define NEUTRAL_WITNESS _T("witness/LevelMatchFilter_neutral")
class LevelRangeFilterTestCase : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE(LevelRangeFilterTestCase);
CPPUNIT_TEST(accept);
CPPUNIT_TEST(neutral);
CPPUNIT_TEST_SUITE_END();
LoggerPtr root;
LoggerPtr logger;
public:
void setUp()
{
root = Logger::getRootLogger();
root->removeAllAppenders();
logger = Logger::getLogger(_T("test"));
}
void tearDown()
{
root->getLoggerRepository()->resetConfiguration();
}
void accept()
{
// set up appender
LayoutPtr layout = new SimpleLayout();
AppenderPtr appender = new FileAppender(layout, ACCEPT_FILE, false);
// create LevelMatchFilter
LevelRangeFilterPtr rangeFilter = new LevelRangeFilter();
// set it to accept on a match
rangeFilter->setAcceptOnMatch(true);
// attach match filter to appender
appender->addFilter(rangeFilter);
// set appender on root and set level to debug
root->addAppender(appender);
root->setLevel(Level::DEBUG);
int passCount = 0;
StringBuffer sbuf;
// test with no min or max set
sbuf << _T("pass ") << passCount << _T("; no min or max set");
common(sbuf.str());
passCount++;
// test with a min set
rangeFilter->setLevelMin(Level::WARN);
sbuf.str(_T(""));
sbuf << _T("pass ") << passCount << _T("; min set to WARN, max not set");
common(sbuf.str());
passCount++;
// create a clean filter
appender->clearFilters();
rangeFilter = new LevelRangeFilter();
appender->addFilter(rangeFilter);
//test with max set
rangeFilter->setLevelMax(Level::WARN);
sbuf.str(_T(""));
sbuf << _T("pass ") << passCount << _T("; min not set, max set to WARN");
common(sbuf.str());
passCount++;
LevelPtr levelArray[] =
{ Level::DEBUG, Level::INFO, Level::WARN, Level::ERROR, Level::FATAL };
int length = sizeof(levelArray)/sizeof(levelArray[0]);
for (int x = 0; x < length; x++)
{
// set the min level to match
rangeFilter->setLevelMin(levelArray[x]);
for (int y = length - 1; y >= 0; y--)
{
// set max level to match
rangeFilter->setLevelMax(levelArray[y]);
sbuf.str(_T(""));
sbuf << _T("pass ") << passCount
<< _T("; filter set to accept between ")
<< levelArray[x]->toString() << _T(" and ")
<< levelArray[y]->toString() << _T(" msgs");
common(sbuf.str());
// increment passCount
passCount++;
}
}
CPPUNIT_ASSERT(Compare::compare(ACCEPT_FILE, ACCEPT_WITNESS));
}
void neutral()
{
// set up appender
LayoutPtr layout = new SimpleLayout();
AppenderPtr appender = new FileAppender(layout, ACCEPT_FILE, false);
// create LevelMatchFilter
LevelRangeFilterPtr rangeFilter = new LevelRangeFilter();
// set it to accept on a match
rangeFilter->setAcceptOnMatch(true);
// attach match filter to appender
appender->addFilter(rangeFilter);
// set appender on root and set level to debug
root->addAppender(appender);
root->setLevel(Level::DEBUG);
int passCount = 0;
StringBuffer sbuf;
// test with no min or max set
sbuf << _T("pass ") << passCount << _T("; no min or max set");
common(sbuf.str());
passCount++;
// test with a min set
rangeFilter->setLevelMin(Level::WARN);
sbuf.str(_T(""));
sbuf << _T("pass ") << passCount << _T("; min set to WARN, max not set");
common(sbuf.str());
passCount++;
// create a clean filter
appender->clearFilters();
rangeFilter = new LevelRangeFilter();
appender->addFilter(rangeFilter);
//test with max set
rangeFilter->setLevelMax(Level::WARN);
sbuf.str(_T(""));
sbuf << _T("pass ") << passCount << _T("; min not set, max set to WARN");
common(sbuf.str());
passCount++;
LevelPtr levelArray[] =
{ Level::DEBUG, Level::INFO, Level::WARN, Level::ERROR, Level::FATAL };
int length = sizeof(levelArray)/sizeof(levelArray[0]);
for (int x = 0; x < length; x++)
{
// set the min level to match
rangeFilter->setLevelMin(levelArray[x]);
for (int y = length - 1; y >= 0; y--)
{
// set max level to match
rangeFilter->setLevelMax(levelArray[y]);
sbuf.str(_T(""));
sbuf << _T("pass ") << passCount
<< _T("; filter set to accept between ")
<< levelArray[x]->toString() << _T(" and ")
<< levelArray[y]->toString() << _T(" msgs");
common(sbuf.str());
// increment passCount
passCount++;
}
}
CPPUNIT_ASSERT(Compare::compare(NEUTRAL_FILE, NEUTRAL_WITNESS));
}
void common(const String& msg)
{
logger->debug(msg);
logger->info(msg);
logger->warn(msg);
logger->error(msg);
logger->fatal(msg);
}
};
CPPUNIT_TEST_SUITE_REGISTRATION(LevelRangeFilterTestCase);
--- NEW FILE: Makefile.am ---
EXTRA_DIST = *.cpp
if TESTS
noinst_LIBRARIES = libvaria.a
INCLUDES = -I$(top_srcdir)/include
libvaria_a_SOURCES = \
errorhandlertestcase.cpp \
levelmatchfiltertestcase.cpp \
levelrangefiltertestcase.cpp
check: libvaria.a
endif
--- NEW FILE: levelmatchfiltertestcase.cpp ---
/*
* Copyright 2003,2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <log4cxx/logger.h>
#include <log4cxx/simplelayout.h>
#include <log4cxx/fileappender.h>
#include <log4cxx/level.h>
#include <log4cxx/varia/levelmatchfilter.h>
#include <log4cxx/varia/denyallfilter.h>
#include "../util/compare.h"
using namespace log4cxx;
using namespace log4cxx::helpers;
using namespace log4cxx::varia;
#define ACCEPT_FILE _T("output/LevelMatchFilter_accept")
#define ACCEPT_WITNESS _T("witness/LevelMatchFilter_accept")
#define DENY_FILE _T("output/LevelMatchFilter_deny")
#define DENY_WITNESS _T("witness/LevelMatchFilter_deny")
class LevelMatchFilterTestCase : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE(LevelMatchFilterTestCase);
CPPUNIT_TEST(accept);
CPPUNIT_TEST(deny);
CPPUNIT_TEST_SUITE_END();
LoggerPtr root;
LoggerPtr logger;
public:
void setUp()
{
root = Logger::getRootLogger();
root->removeAllAppenders();
logger = Logger::getLogger(_T("test"));
}
void tearDown()
{
root->getLoggerRepository()->resetConfiguration();
}
void accept()
{
// set up appender
LayoutPtr layout = new SimpleLayout();
AppenderPtr appender = new FileAppender(layout, ACCEPT_FILE, false);
// create LevelMatchFilter
LevelMatchFilterPtr matchFilter = new LevelMatchFilter();
// attach match filter to appender
appender->addFilter(matchFilter);
// attach DenyAllFilter to end of filter chain to deny neutral
// (non matching) messages
appender->addFilter(new DenyAllFilter());
// set appender on root and set level to debug
root->addAppender(appender);
root->setLevel(Level::DEBUG);
LevelPtr levelArray[] =
{ Level::DEBUG, Level::INFO, Level::WARN, Level::ERROR, Level::FATAL };
int length = sizeof(levelArray)/sizeof(levelArray[0]);
for (int x = 0; x < length; x++)
{
// set the level to match
matchFilter->setLevelToMatch(levelArray[x]->toString());
StringBuffer sbuf;
sbuf << _T("pass ") << x << _T("; filter set to accept only ")
<< levelArray[x]->toString() << _T(" msgs");
common(sbuf.str());
}
CPPUNIT_ASSERT(Compare::compare(ACCEPT_FILE, ACCEPT_WITNESS));
}
void deny()
{
// set up appender
LayoutPtr layout = new SimpleLayout();
AppenderPtr appender = new FileAppender(layout, DENY_FILE, false);
// create LevelMatchFilter, set to deny matches
LevelMatchFilterPtr matchFilter = new LevelMatchFilter();
matchFilter->setAcceptOnMatch(false);
// attach match filter to appender
appender->addFilter(matchFilter);
// set appender on root and set level to debug
root->addAppender(appender);
root->setLevel(Level::DEBUG);
LevelPtr levelArray[] =
{ Level::DEBUG, Level::INFO, Level::WARN, Level::ERROR, Level::FATAL };
int length = sizeof(levelArray)/sizeof(levelArray[0]);
for (int x = 0; x < length; x++)
{
// set the level to match
matchFilter->setLevelToMatch(levelArray[x]->toString());
StringBuffer sbuf;
sbuf << _T("pass ") << x << _T("; filter set to deny only ")
<< levelArray[x]->toString() << _T(" msgs");
common(sbuf.str());
}
CPPUNIT_ASSERT(Compare::compare(DENY_FILE, DENY_WITNESS));
}
void common(const String& msg)
{
logger->debug(msg);
logger->info(msg);
logger->warn(msg);
logger->error(msg);
logger->fatal(msg);
}
};
CPPUNIT_TEST_SUITE_REGISTRATION(LevelMatchFilterTestCase);
--- NEW FILE: errorhandlertestcase.cpp ---
/*
* Copyright 2003,2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <log4cxx/config.h>
#ifdef HAVE_XML
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <log4cxx/logger.h>
#include <log4cxx/xml/domconfigurator.h>
#include "../util/transformer.h"
#include "../util/compare.h"
#include "../util/controlfilter.h"
#include "../util/threadfilter.h"
#include "../util/linenumberfilter.h"
using namespace log4cxx;
using namespace log4cxx::helpers;
using namespace log4cxx::xml;
#define TEMP _T("output/temp")
#define FILTERED _T("output/filtered")
#define TEST1_A_PAT _T("FALLBACK - test - Message \\d")
#define TEST1_B_PAT _T("FALLBACK - root - Message \\d")
#define TEST1_2_PAT \
_T("^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2},\\d{3} ") \
_T("\\[main]\\ (DEBUG|INFO|WARN|ERROR|FATAL) .* - Message \\d")
class ErrorHandlerTestCase : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE(ErrorHandlerTestCase);
CPPUNIT_TEST(test1);
CPPUNIT_TEST_SUITE_END();
LoggerPtr root;
LoggerPtr logger;
public:
void setUp()
{
root = Logger::getRootLogger();
logger = Logger::getLogger(_T("test"));
}
void tearDown()
{
logger->getLoggerRepository()->resetConfiguration();
}
void test1()
{
DOMConfigurator::configure(_T("input/xml/fallback1.xml"));
common();
ControlFilter cf;
cf << TEST1_A_PAT << TEST1_B_PAT << TEST1_2_PAT;
ThreadFilter threadFilter;
LineNumberFilter lineNumberFilter;
std::vector<Filter *> filters;
filters.push_back(&cf);
filters.push_back(&threadFilter);
filters.push_back(&lineNumberFilter);
try
{
Transformer::transform(TEMP, FILTERED, filters);
}
catch(UnexpectedFormatException& e)
{
tcout << _T("UnexpectedFormatException :") << e.getMessage() << std::endl;
throw;
}
CPPUNIT_ASSERT(Compare::compare(FILTERED, _T("witness/fallback")));
}
void common()
{
int i = -1;
LOG4CXX_DEBUG(logger, _T("Message ") << ++i);
LOG4CXX_DEBUG(root, _T("Message ") << i);
LOG4CXX_INFO(logger, _T("Message ") << ++i);
LOG4CXX_INFO(root, _T("Message ") << i);
LOG4CXX_WARN(logger, _T("Message ") << ++i);
LOG4CXX_WARN(root, _T("Message ") << i);
LOG4CXX_ERROR(logger, _T("Message ") << ++i);
LOG4CXX_ERROR(root, _T("Message ") << i);
LOG4CXX_FATAL(logger, _T("Message ") << ++i);
LOG4CXX_FATAL(root, _T("Message ") << i);
}
};
CPPUNIT_TEST_SUITE_REGISTRATION(ErrorHandlerTestCase);
#endif //HAVE_XML
|