Update of /cvsroot/objecthandler/log4cxx-0.9.7/tests/src/xml
In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv25784/tests/src/xml
Added Files:
Makefile.am customleveltestcase.cpp domtestcase.cpp xlevel.cpp
xlevel.h xmllayouttestcase.cpp
Log Message:
--- NEW FILE: xlevel.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 "xlevel.h"
#include <log4cxx/helpers/stringhelper.h>
using namespace log4cxx;
using namespace log4cxx::helpers;
IMPLEMENT_LOG4CXX_LEVEL(XLevel)
#define TRACE_STR _T("TRACE")
#define LETHAL_STR _T("LETHAL")
const LevelPtr XLevel::TRACE = new XLevel(XLevel::TRACE_INT, TRACE_STR, 7);
const LevelPtr XLevel::LETHAL = new XLevel(XLevel::LETHAL_INT, LETHAL_STR, 0);
XLevel::XLevel(int level, const String& levelStr, int syslogEquivalent)
: Level(level, levelStr, syslogEquivalent)
{
}
const LevelPtr& XLevel::toLevel(const String& sArg)
{
return toLevel(sArg, TRACE);
}
const LevelPtr& XLevel::toLevel(int val)
{
return toLevel(val, TRACE);
}
const LevelPtr& XLevel::toLevel(int val, const LevelPtr& defaultLevel)
{
switch(val)
{
case TRACE_INT: return TRACE;
case LETHAL_INT: return LETHAL;
default: return defaultLevel;
}
}
const LevelPtr& XLevel::toLevel(const String& sArg, const LevelPtr& defaultLevel)
{
if (sArg.empty())
{
return defaultLevel;
}
String s = StringHelper::toUpperCase(sArg);
if(s == (TRACE_STR)) return TRACE;
if(s == (LETHAL_STR)) return LETHAL;
return defaultLevel;
}
--- NEW FILE: customleveltestcase.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 <log4cxx/consoleappender.h>
#include <log4cxx/patternlayout.h>
#include "../util/compare.h"
#include "xlevel.h"
using namespace log4cxx;
using namespace log4cxx::helpers;
using namespace log4cxx::xml;
#define TEMP _T("output/temp")
class CustomLevelTestCase : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE(CustomLevelTestCase);
CPPUNIT_TEST(test1);
CPPUNIT_TEST(test2);
CPPUNIT_TEST(test3);
CPPUNIT_TEST(test4);
CPPUNIT_TEST_SUITE_END();
LoggerPtr root;
LoggerPtr logger;
public:
void setUp()
{
root = Logger::getRootLogger();
logger = Logger::getLogger(_T("xml.CustomLevelTestCase"));
}
void tearDown()
{
root->getLoggerRepository()->resetConfiguration();
LoggerPtr logger = Logger::getLogger(_T("LOG4J"));
logger->setAdditivity(false);
logger->addAppender(
new ConsoleAppender(new PatternLayout(_T("log4j: %-22c{2} - %m%n"))));
}
void test1()
{
DOMConfigurator::configure(_T("input/xml/customLevel1.xml"));
common();
CPPUNIT_ASSERT(Compare::compare(TEMP, _T("witness/customLevel.1")));
}
void test2()
{
DOMConfigurator::configure(_T("input/xml/customLevel2.xml"));
common();
CPPUNIT_ASSERT(Compare::compare(TEMP, _T("witness/customLevel.2")));
}
void test3()
{
DOMConfigurator::configure(_T("input/xml/customLevel3.xml"));
common();
CPPUNIT_ASSERT(Compare::compare(TEMP, _T("witness/customLevel.3")));
}
void test4()
{
DOMConfigurator::configure(_T("input/xml/customLevel4.xml"));
common();
CPPUNIT_ASSERT(Compare::compare(TEMP, _T("witness/customLevel.4")));
}
void common()
{
int i = 0;
LOG4CXX_DEBUG(logger, _T("Message ") << ++i);
LOG4CXX_INFO(logger, _T("Message ") << ++i);
LOG4CXX_WARN(logger, _T("Message ") << ++i);
LOG4CXX_ERROR(logger, _T("Message ") << ++i);
LOG4CXX_LOG(logger, XLevel::TRACE, _T("Message ") << ++i);
}
};
CPPUNIT_TEST_SUITE_REGISTRATION(CustomLevelTestCase);
#endif //HAVE_XML
--- NEW FILE: xmllayouttestcase.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/xml/xmllayout.h>
#include <log4cxx/fileappender.h>
#include <log4cxx/mdc.h>
#include "../util/transformer.h"
#include "../util/compare.h"
#include "../util/xmltimestampfilter.h"
#include "../util/xmllineattributefilter.h"
#include "../util/xmlthreadfilter.h"
using namespace log4cxx;
using namespace log4cxx::helpers;
using namespace log4cxx::xml;
#define FILTERED _T("output/filtered")
#define TEMP _T("output/temp")
class X
{
public:
X()
{
LoggerPtr logger =
Logger::getLogger(_T("org.apache.log4j.xml.XMLLayoutTestCase$X"));
LOG4CXX_INFO(logger, _T("in X() constructor"));
}
};
class XMLLayoutTestCase : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE(XMLLayoutTestCase);
CPPUNIT_TEST(basic);
CPPUNIT_TEST(locationInfo);
CPPUNIT_TEST(testCDATA);
CPPUNIT_TEST(testNULL);
CPPUNIT_TEST(testMDC);
CPPUNIT_TEST_SUITE_END();
LoggerPtr root;
LoggerPtr logger;
public:
void setUp()
{
root = Logger::getRootLogger();
logger = Logger::getLogger(_T("org.apache.log4j.xml.XMLLayoutTestCase"));
}
void tearDown()
{
logger->getLoggerRepository()->resetConfiguration();
}
void basic()
{
XMLLayoutPtr xmlLayout = new XMLLayout();
root->addAppender(new FileAppender(xmlLayout, TEMP, false));
common();
XMLTimestampFilter xmlTimestampFilter;
XMLThreadFilter xmlThreadFilter;
std::vector<Filter *> filters;
filters.push_back(&xmlTimestampFilter);
filters.push_back(&xmlThreadFilter);
try
{
Transformer::transform(TEMP, FILTERED, filters);
}
catch(UnexpectedFormatException& e)
{
tcout << _T("UnexpectedFormatException :") << e.getMessage() << std::endl;
throw;
}
CPPUNIT_ASSERT(Compare::compare(FILTERED, _T("witness/xmlLayout.1")));
}
void locationInfo()
{
XMLLayoutPtr xmlLayout = new XMLLayout();
xmlLayout->setLocationInfo(true);
root->addAppender(new FileAppender(xmlLayout, TEMP, false));
common();
XMLTimestampFilter xmlTimestampFilter;
XMLLineAttributeFilter xmlLineAttributeFilter;
XMLThreadFilter xmlThreadFilter;
std::vector<Filter *> filters;
filters.push_back(&xmlTimestampFilter);
filters.push_back(&xmlLineAttributeFilter);
filters.push_back(&xmlThreadFilter);
try
{
Transformer::transform(TEMP, FILTERED, filters);
}
catch(UnexpectedFormatException& e)
{
tcout << _T("UnexpectedFormatException :") << e.getMessage() << std::endl;
throw;
}
CPPUNIT_ASSERT(Compare::compare(FILTERED, _T("witness/xmlLayout.2")));
}
void testCDATA()
{
XMLLayoutPtr xmlLayout = new XMLLayout();
xmlLayout->setLocationInfo(true);
root->addAppender(new FileAppender(xmlLayout, TEMP, false));
LOG4CXX_DEBUG(logger,
_T("Message with embedded <![CDATA[<hello>hi</hello>]]>."));
XMLTimestampFilter xmlTimestampFilter;
XMLLineAttributeFilter xmlLineAttributeFilter;
XMLThreadFilter xmlThreadFilter;
std::vector<Filter *> filters;
filters.push_back(&xmlTimestampFilter);
filters.push_back(&xmlLineAttributeFilter);
filters.push_back(&xmlThreadFilter);
try
{
Transformer::transform(TEMP, FILTERED, filters);
}
catch(UnexpectedFormatException& e)
{
tcout << _T("UnexpectedFormatException :") << e.getMessage() << std::endl;
throw;
}
CPPUNIT_ASSERT(Compare::compare(FILTERED, _T("witness/xmlLayout.3")));
}
void testNULL()
{
XMLLayoutPtr xmlLayout = new XMLLayout();
root->addAppender(new FileAppender(xmlLayout, TEMP, false));
LOG4CXX_DEBUG(logger, _T("hi"));
LOG4CXX_DEBUG(logger, _T(""));
XMLTimestampFilter xmlTimestampFilter;
XMLThreadFilter xmlThreadFilter;
std::vector<Filter *> filters;
filters.push_back(&xmlTimestampFilter);
filters.push_back(&xmlThreadFilter);
try
{
Transformer::transform(TEMP, FILTERED, filters);
}
catch(UnexpectedFormatException& e)
{
tcout << _T("UnexpectedFormatException :") << e.getMessage() << std::endl;
throw;
}
CPPUNIT_ASSERT(Compare::compare(FILTERED, _T("witness/xmlLayout.null")));
}
void testMDC()
{
XMLLayoutPtr xmlLayout = new XMLLayout();
root->addAppender(new FileAppender(xmlLayout, TEMP, false));
MDC::clear();
MDC::put(_T("key1"), _T("val1"));
MDC::put(_T("key2"), _T("val2"));
LOG4CXX_DEBUG(logger, _T("Hello"));
MDC::clear();
XMLTimestampFilter xmlTimestampFilter;
XMLThreadFilter xmlThreadFilter;
std::vector<Filter *> filters;
filters.push_back(&xmlTimestampFilter);
filters.push_back(&xmlThreadFilter);
try
{
Transformer::transform(TEMP, FILTERED, filters);
}
catch(UnexpectedFormatException& e)
{
tcout << _T("UnexpectedFormatException :") << e.getMessage() << std::endl;
throw;
}
CPPUNIT_ASSERT(Compare::compare(FILTERED, _T("witness/xmlLayout.mdc.1")));
}
// not incuded in the tests for the moment !
void holdTestMDCEscaped()
{
XMLLayoutPtr xmlLayout = new XMLLayout();
root->addAppender(new FileAppender(xmlLayout, TEMP, false));
MDC::clear();
MDC::put(_T("blahAttribute"), _T("<blah value=\"blah\">"));
MDC::put(_T("<blahKey value=\"blah\"/>"), _T("blahValue"));
LOG4CXX_DEBUG(logger, _T("Hello"));
MDC::clear();
XMLTimestampFilter xmlTimestampFilter;
XMLThreadFilter xmlThreadFilter;
std::vector<Filter *> filters;
filters.push_back(&xmlTimestampFilter);
filters.push_back(&xmlThreadFilter);
try
{
Transformer::transform(TEMP, FILTERED, filters);
}
catch(UnexpectedFormatException& e)
{
tcout << _T("UnexpectedFormatException :") << e.getMessage() << std::endl;
throw;
}
CPPUNIT_ASSERT(Compare::compare(FILTERED, _T("witness/xmlLayout.mdc.2")));
}
void common()
{
int i = -1;
X x;
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(XMLLayoutTestCase);
--- NEW FILE: domtestcase.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/compare.h"
#include "xlevel.h"
#include "../util/controlfilter.h"
#include "../util/iso8601filter.h"
#include "../util/threadfilter.h"
#include "../util/transformer.h"
using namespace log4cxx;
using namespace log4cxx::helpers;
using namespace log4cxx::xml;
#define TEMP_A1 _T("output/temp.A1")
#define TEMP_A2 _T("output/temp.A2")
#define FILTERED_A1 _T("output/filtered.A1")
#define FILTERED_A2 _T("output/filtered.A2")
#define TEST1_1A_PAT \
_T("(DEBUG|INFO |WARN |ERROR|FATAL) \\w*\\.\\w* - Message \\d")
#define TEST1_1B_PAT _T("(DEBUG|INFO |WARN |ERROR|FATAL) root - Message \\d")
#define TEST1_2_PAT _T("^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2},\\d{3} ") \
_T("\\[\\d*]\\ (DEBUG|INFO|WARN|ERROR|FATAL) .* - Message \\d")
class DOMTestCase : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE(DOMTestCase);
CPPUNIT_TEST(test1);
CPPUNIT_TEST_SUITE_END();
LoggerPtr root;
LoggerPtr logger;
public:
void setUp()
{
root = Logger::getRootLogger();
logger = Logger::getLogger(_T("org.apache.log4j.xml.DOMTestCase"));
}
void tearDown()
{
root->getLoggerRepository()->resetConfiguration();
}
void test1()
{
DOMConfigurator::configure(_T("input/xml/DOMTestCase1.xml"));
common();
ControlFilter cf1;
cf1 << TEST1_1A_PAT << TEST1_1B_PAT;
ControlFilter cf2;
cf2 << TEST1_2_PAT;
ThreadFilter threadFilter;
ISO8601Filter iso8601Filter;
std::vector<Filter *> filters1;
filters1.push_back(&cf1);
std::vector<Filter *> filters2;
filters2.push_back(&cf2);
filters2.push_back(&threadFilter);
filters2.push_back(&iso8601Filter);
try
{
Transformer::transform(TEMP_A1, FILTERED_A1, filters1);
Transformer::transform(TEMP_A2, FILTERED_A2, filters2);
}
catch(UnexpectedFormatException& e)
{
tcout << _T("UnexpectedFormatException :") << e.getMessage() << std::endl;
throw;
}
CPPUNIT_ASSERT(Compare::compare(FILTERED_A1, _T("witness/dom.A1.1")));
CPPUNIT_ASSERT(Compare::compare(FILTERED_A2, _T("witness/dom.A2.1")));
}
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(DOMTestCase);
#endif //HAVE_XML
--- NEW FILE: xlevel.h ---
/*
* 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/level.h>
namespace log4cxx
{
class XLevel : public Level
{
DECLARE_LOG4CXX_LEVEL(XLevel)
public:
enum
{
TRACE_INT = Level::DEBUG_INT - 1,
LETHAL_INT = Level::FATAL_INT + 1
};
static const LevelPtr TRACE;
static const LevelPtr LETHAL;
XLevel(int level, const String& levelStr, int syslogEquivalent);
/**
Convert the string passed as argument to a level. If the
conversion fails, then this method returns #DEBUG.
*/
static const LevelPtr& toLevel(const String& sArg);
/**
Convert an integer passed as argument to a level. If the
conversion fails, then this method returns #DEBUG.
*/
static const LevelPtr& toLevel(int val);
/**
Convert an integer passed as argument to a level. If the
conversion fails, then this method returns the specified default.
*/
static const LevelPtr& toLevel(int val, const LevelPtr& defaultLevel);
/**
Convert the string passed as argument to a level. If the
conversion fails, then this method returns the value of
<code>defaultLevel</code>.
*/
static const LevelPtr& toLevel(const String& sArg,
const LevelPtr& defaultLevel);
};
}
--- NEW FILE: Makefile.am ---
EXTRA_DIST = $(top_srcdir)/tests/src/xml/*.cpp
noinst_HEADERS= $(top_srcdir)/tests/src/xml/*.h
if TESTS
noinst_LIBRARIES = libxml.a
INCLUDES = -I$(top_srcdir)/include
libxml_a_SOURCES = \
customleveltestcase.cpp \
domtestcase.cpp \
xlevel.cpp \
xmllayouttestcase.cpp
check: libxml.a
endif
|