Add basic tests for Log4J. Still need to add the multi-classloader
scenarios. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/logging/trunk@138960 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
65
build.xml
65
build.xml
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
<!--
|
<!--
|
||||||
"Logging" component of the Jakarta Commons Subproject
|
"Logging" component of the Jakarta Commons Subproject
|
||||||
$Id: build.xml,v 1.27 2003/03/30 02:30:36 craigmcc Exp $
|
$Id: build.xml,v 1.28 2003/03/30 05:22:50 craigmcc Exp $
|
||||||
-->
|
-->
|
||||||
|
|
||||||
|
|
||||||
@@ -103,6 +103,14 @@
|
|||||||
<pathelement location="${junit.jar}"/>
|
<pathelement location="${junit.jar}"/>
|
||||||
</path>
|
</path>
|
||||||
|
|
||||||
|
<!-- Construct unit test classpath (Log4J tests) -->
|
||||||
|
<path id="test.classpath.log4j">
|
||||||
|
<pathelement location="${build.home}/classes"/>
|
||||||
|
<pathelement location="${build.home}/tests"/>
|
||||||
|
<pathelement location="${junit.jar}"/>
|
||||||
|
<pathelement location="${log4j.jar}"/>
|
||||||
|
</path>
|
||||||
|
|
||||||
<!-- Construct unit test classpath (Minimal Wrapper) -->
|
<!-- Construct unit test classpath (Minimal Wrapper) -->
|
||||||
<path id="test.classpath.wrap">
|
<path id="test.classpath.wrap">
|
||||||
<pathelement location="${build.home}/commons-logging-wrapper.jar"/>
|
<pathelement location="${build.home}/commons-logging-wrapper.jar"/>
|
||||||
@@ -226,6 +234,8 @@
|
|||||||
basedir="${build.home}/tests"
|
basedir="${build.home}/tests"
|
||||||
manifest="${conf.home}/MANIFEST.MF">
|
manifest="${conf.home}/MANIFEST.MF">
|
||||||
<exclude name="org/apache/commons/logging/Wrapper.class"/>
|
<exclude name="org/apache/commons/logging/Wrapper.class"/>
|
||||||
|
<exclude name="org/apache/commons/logging/jdk14/TestHandler.class"
|
||||||
|
if="jdk.1.4.present"/>
|
||||||
</jar>
|
</jar>
|
||||||
<jar jarfile="${build.home}/commons-${component.name}-wrapper.jar"
|
<jar jarfile="${build.home}/commons-${component.name}-wrapper.jar"
|
||||||
basedir="${build.home}/tests"
|
basedir="${build.home}/tests"
|
||||||
@@ -291,7 +301,8 @@
|
|||||||
<!-- ========== Unit Test Targets ========================================= -->
|
<!-- ========== Unit Test Targets ========================================= -->
|
||||||
|
|
||||||
|
|
||||||
<target name="test" depends="compile.tests,test.jdk14" if="test.entry"
|
<target name="test" depends="compile.tests,test.jdk14,test.log4j
|
||||||
|
" if="test.entry"
|
||||||
description="Run all unit test cases">
|
description="Run all unit test cases">
|
||||||
<java classname="${test.runner}" fork="yes" failonerror="${test.failonerror}">
|
<java classname="${test.runner}" fork="yes" failonerror="${test.failonerror}">
|
||||||
<arg value="${test.entry}"/>
|
<arg value="${test.entry}"/>
|
||||||
@@ -417,4 +428,54 @@
|
|||||||
</target>
|
</target>
|
||||||
|
|
||||||
|
|
||||||
|
<target name="test.log4j" depends="compile.tests" if="log4j.present"
|
||||||
|
description="Run unit tests specific to Log4J logging">
|
||||||
|
|
||||||
|
<echo message="Default Configuration (Log4J Auto-Recognized)"/>
|
||||||
|
<java classname="${test.runner}" fork="yes" failonerror="${test.failonerror}">
|
||||||
|
<arg value="org.apache.commons.logging.log4j.DefaultConfigTestCase"/>
|
||||||
|
<classpath refid="test.classpath.log4j"/>
|
||||||
|
</java>
|
||||||
|
|
||||||
|
<echo message="Default Configuration (Log4J LogFactoryImpl Selected)"/>
|
||||||
|
<java classname="${test.runner}" fork="yes" failonerror="${test.failonerror}">
|
||||||
|
<sysproperty key="org.apache.commons.logging.LogFactory"
|
||||||
|
value="org.apache.commons.logging.impl.LogFactoryImpl"/>
|
||||||
|
<arg value="org.apache.commons.logging.log4j.DefaultConfigTestCase"/>
|
||||||
|
<classpath refid="test.classpath.log4j"/>
|
||||||
|
</java>
|
||||||
|
|
||||||
|
<echo message="Default Configuration (Log4J Log4JLogger Selected)"/>
|
||||||
|
<java classname="${test.runner}" fork="yes" failonerror="${test.failonerror}">
|
||||||
|
<sysproperty key="org.apache.commons.logging.Log"
|
||||||
|
value="org.apache.commons.logging.impl.Log4JLogger"/>
|
||||||
|
<arg value="org.apache.commons.logging.log4j.DefaultConfigTestCase"/>
|
||||||
|
<classpath refid="test.classpath.log4j"/>
|
||||||
|
</java>
|
||||||
|
|
||||||
|
<echo message="Custom Configuration (Log4J Auto-Recognized)"/>
|
||||||
|
<java classname="${test.runner}" fork="yes" failonerror="${test.failonerror}">
|
||||||
|
<arg value="org.apache.commons.logging.log4j.CustomConfigTestCase"/>
|
||||||
|
<classpath refid="test.classpath.log4j"/>
|
||||||
|
</java>
|
||||||
|
|
||||||
|
<echo message="Custom Configuration (Log4J LogFactoryImpl Selected)"/>
|
||||||
|
<java classname="${test.runner}" fork="yes" failonerror="${test.failonerror}">
|
||||||
|
<sysproperty key="org.apache.commons.logging.LogFactory"
|
||||||
|
value="org.apache.commons.logging.impl.LogFactoryImpl"/>
|
||||||
|
<arg value="org.apache.commons.logging.log4j.CustomConfigTestCase"/>
|
||||||
|
<classpath refid="test.classpath.log4j"/>
|
||||||
|
</java>
|
||||||
|
|
||||||
|
<echo message="Custom Configuration (Log4J Log4JLogger Selected)"/>
|
||||||
|
<java classname="${test.runner}" fork="yes" failonerror="${test.failonerror}">
|
||||||
|
<sysproperty key="org.apache.commons.logging.Log"
|
||||||
|
value="org.apache.commons.logging.impl.Log4JLogger"/>
|
||||||
|
<arg value="org.apache.commons.logging.log4j.CustomConfigTestCase"/>
|
||||||
|
<classpath refid="test.classpath.log4j"/>
|
||||||
|
</java>
|
||||||
|
|
||||||
|
</target>
|
||||||
|
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//logging/src/test/org/apache/commons/logging/jdk14/CustomConfigTestCase.java,v 1.2 2003/03/30 02:30:37 craigmcc Exp $
|
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//logging/src/test/org/apache/commons/logging/jdk14/CustomConfigTestCase.java,v 1.3 2003/03/30 05:22:50 craigmcc Exp $
|
||||||
* $Revision: 1.2 $
|
* $Revision: 1.3 $
|
||||||
* $Date: 2003/03/30 02:30:37 $
|
* $Date: 2003/03/30 05:22:50 $
|
||||||
*
|
*
|
||||||
* ====================================================================
|
* ====================================================================
|
||||||
*
|
*
|
||||||
@@ -84,7 +84,7 @@ import org.apache.commons.logging.LogFactory;
|
|||||||
* logger configured per the configuration properties.</p>
|
* logger configured per the configuration properties.</p>
|
||||||
*
|
*
|
||||||
* @author Craig R. McClanahan
|
* @author Craig R. McClanahan
|
||||||
* @version $Revision: 1.2 $ $Date: 2003/03/30 02:30:37 $
|
* @version $Revision: 1.3 $ $Date: 2003/03/30 05:22:50 $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class CustomConfigTestCase extends DefaultConfigTestCase {
|
public class CustomConfigTestCase extends DefaultConfigTestCase {
|
||||||
@@ -142,14 +142,14 @@ public class CustomConfigTestCase extends DefaultConfigTestCase {
|
|||||||
* <p>The message levels that should have been logged.</p>
|
* <p>The message levels that should have been logged.</p>
|
||||||
*/
|
*/
|
||||||
protected Level testLevels[] =
|
protected Level testLevels[] =
|
||||||
{ Level.FINE, Level.INFO, Level.WARNING, Level.SEVERE };
|
{ Level.FINE, Level.INFO, Level.WARNING, Level.SEVERE, Level.SEVERE };
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>The message strings that should have been logged.</p>
|
* <p>The message strings that should have been logged.</p>
|
||||||
*/
|
*/
|
||||||
protected String testMessages[] =
|
protected String testMessages[] =
|
||||||
{ "debug", "info", "warn", "error" };
|
{ "debug", "info", "warn", "error", "fatal" };
|
||||||
|
|
||||||
|
|
||||||
// ------------------------------------------- JUnit Infrastructure Methods
|
// ------------------------------------------- JUnit Infrastructure Methods
|
||||||
@@ -224,6 +224,7 @@ public class CustomConfigTestCase extends DefaultConfigTestCase {
|
|||||||
super.testPristineLog();
|
super.testPristineLog();
|
||||||
|
|
||||||
// Assert which logging levels have been enabled
|
// Assert which logging levels have been enabled
|
||||||
|
assertTrue(log.isFatalEnabled());
|
||||||
assertTrue(log.isErrorEnabled());
|
assertTrue(log.isErrorEnabled());
|
||||||
assertTrue(log.isWarnEnabled());
|
assertTrue(log.isWarnEnabled());
|
||||||
assertTrue(log.isInfoEnabled());
|
assertTrue(log.isInfoEnabled());
|
||||||
@@ -298,6 +299,7 @@ public class CustomConfigTestCase extends DefaultConfigTestCase {
|
|||||||
log.info("info", t);
|
log.info("info", t);
|
||||||
log.warn("warn", t);
|
log.warn("warn", t);
|
||||||
log.error("error", t);
|
log.error("error", t);
|
||||||
|
log.fatal("fatal", t);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -308,6 +310,7 @@ public class CustomConfigTestCase extends DefaultConfigTestCase {
|
|||||||
log.info("info");
|
log.info("info");
|
||||||
log.warn("warn");
|
log.warn("warn");
|
||||||
log.error("error");
|
log.error("error");
|
||||||
|
log.fatal("fatal");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
# This is the custom configuration properties for the Log4J logger tests
|
||||||
|
# in CustomConfigTestCase.
|
||||||
|
|
||||||
|
# Configure the root logger's level and appender
|
||||||
|
log4j.rootLogger = INFO, A1
|
||||||
|
|
||||||
|
# Configure the default appender
|
||||||
|
log4j.appender.A1 = org.apache.commons.logging.log4j.TestAppender
|
||||||
@@ -0,0 +1,318 @@
|
|||||||
|
/*
|
||||||
|
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//logging/src/test/org/apache/commons/logging/log4j/CustomConfigTestCase.java,v 1.1 2003/03/30 05:22:50 craigmcc Exp $
|
||||||
|
* $Revision: 1.1 $
|
||||||
|
* $Date: 2003/03/30 05:22:50 $
|
||||||
|
*
|
||||||
|
* ====================================================================
|
||||||
|
*
|
||||||
|
* The Apache Software License, Version 1.1
|
||||||
|
*
|
||||||
|
* Copyright (c) 1999-2003 The Apache Software Foundation. All rights
|
||||||
|
* reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in
|
||||||
|
* the documentation and/or other materials provided with the
|
||||||
|
* distribution.
|
||||||
|
*
|
||||||
|
* 3. The end-user documentation included with the redistribution, if
|
||||||
|
* any, must include the following acknowlegement:
|
||||||
|
* "This product includes software developed by the
|
||||||
|
* Apache Software Foundation (http://www.apache.org/)."
|
||||||
|
* Alternately, this acknowlegement may appear in the software itself,
|
||||||
|
* if and wherever such third-party acknowlegements normally appear.
|
||||||
|
*
|
||||||
|
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||||
|
* Foundation" must not be used to endorse or promote products derived
|
||||||
|
* from this software without prior written permission. For written
|
||||||
|
* permission, please contact apache@apache.org.
|
||||||
|
*
|
||||||
|
* 5. Products derived from this software may not be called "Apache"
|
||||||
|
* nor may "Apache" appear in their names without prior written
|
||||||
|
* permission of the Apache Group.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||||
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||||
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||||
|
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||||
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||||
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
|
* SUCH DAMAGE.
|
||||||
|
* ====================================================================
|
||||||
|
*
|
||||||
|
* This software consists of voluntary contributions made by many
|
||||||
|
* individuals on behalf of the Apache Software Foundation. For more
|
||||||
|
* information on the Apache Software Foundation, please see
|
||||||
|
* <http://www.apache.org/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.apache.commons.logging.log4j;
|
||||||
|
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.Enumeration;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import junit.framework.Test;
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
import junit.framework.TestSuite;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.apache.log4j.Level;
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
import org.apache.log4j.PropertyConfigurator;
|
||||||
|
import org.apache.log4j.spi.LoggingEvent;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>TestCase for Log4J logging when running on a system with Log4J present,
|
||||||
|
* so that Log4J should be selected and an appropriate
|
||||||
|
* logger configured per the configuration properties.</p>
|
||||||
|
*
|
||||||
|
* @author Craig R. McClanahan
|
||||||
|
* @version $Revision: 1.1 $ $Date: 2003/03/30 05:22:50 $
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class CustomConfigTestCase extends DefaultConfigTestCase {
|
||||||
|
|
||||||
|
|
||||||
|
// ----------------------------------------------------------- Constructors
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Construct a new instance of this test case.</p>
|
||||||
|
*/
|
||||||
|
public CustomConfigTestCase() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Construct a new instance of this test case.</p>
|
||||||
|
*
|
||||||
|
* @param name Name of the test case
|
||||||
|
*/
|
||||||
|
public CustomConfigTestCase(String name) {
|
||||||
|
super(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ----------------------------------------------------- Instance Variables
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>The <code>Appender</code> we are utilizing.</p>
|
||||||
|
*/
|
||||||
|
protected TestAppender appender = null;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>The <code>Logger</code> we are utilizing.</p>
|
||||||
|
*/
|
||||||
|
protected Logger logger = null;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>The message levels that should have been logged.</p>
|
||||||
|
*/
|
||||||
|
protected Level testLevels[] =
|
||||||
|
{ Level.INFO, Level.WARN, Level.ERROR, Level.FATAL };
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>The message strings that should have been logged.</p>
|
||||||
|
*/
|
||||||
|
protected String testMessages[] =
|
||||||
|
{ "info", "warn", "error", "fatal" };
|
||||||
|
|
||||||
|
|
||||||
|
// ------------------------------------------- JUnit Infrastructure Methods
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set up instance variables required by this test case.
|
||||||
|
*/
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
setUpAppender
|
||||||
|
("org/apache/commons/logging/log4j/CustomConfig.properties");
|
||||||
|
setUpLogger("TestLogger");
|
||||||
|
setUpFactory();
|
||||||
|
setUpLog("TestLogger");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the tests included in this test suite.
|
||||||
|
*/
|
||||||
|
public static Test suite() {
|
||||||
|
return (new TestSuite(CustomConfigTestCase.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tear down instance variables required by this test case.
|
||||||
|
*/
|
||||||
|
public void tearDown() {
|
||||||
|
super.tearDown();
|
||||||
|
Logger.getRootLogger().removeAppender(appender);
|
||||||
|
appender = null;
|
||||||
|
logger = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ----------------------------------------------------------- Test Methods
|
||||||
|
|
||||||
|
|
||||||
|
// Test logging message strings with exceptions
|
||||||
|
public void testExceptionMessages() throws Exception {
|
||||||
|
|
||||||
|
logExceptionMessages();
|
||||||
|
checkLoggingEvents(true);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Test logging plain message strings
|
||||||
|
public void testPlainMessages() throws Exception {
|
||||||
|
|
||||||
|
logPlainMessages();
|
||||||
|
checkLoggingEvents(false);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Test pristine Appender instance
|
||||||
|
public void testPristineAppender() {
|
||||||
|
|
||||||
|
assertNotNull("Appender exists", appender);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Test pristine Log instance
|
||||||
|
public void testPristineLog() {
|
||||||
|
|
||||||
|
super.testPristineLog();
|
||||||
|
|
||||||
|
// Assert which logging levels have been enabled
|
||||||
|
assertTrue(log.isErrorEnabled());
|
||||||
|
assertTrue(log.isWarnEnabled());
|
||||||
|
assertTrue(log.isInfoEnabled());
|
||||||
|
assertTrue(!log.isDebugEnabled());
|
||||||
|
assertTrue(!log.isTraceEnabled());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Test pristine Logger instance
|
||||||
|
public void testPristineLogger() {
|
||||||
|
|
||||||
|
assertNotNull("Logger exists", logger);
|
||||||
|
assertEquals("Logger level", Level.INFO, logger.getEffectiveLevel());
|
||||||
|
assertEquals("Logger name", "TestLogger", logger.getName());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// -------------------------------------------------------- Support Methods
|
||||||
|
|
||||||
|
|
||||||
|
// Check the recorded messages
|
||||||
|
protected void checkLoggingEvents(boolean thrown) {
|
||||||
|
Iterator events = appender.events();
|
||||||
|
for (int i = 0; i < testMessages.length; i++) {
|
||||||
|
assertTrue(events.hasNext());
|
||||||
|
LoggingEvent event = (LoggingEvent) events.next();
|
||||||
|
assertEquals("LoggingEvent level",
|
||||||
|
testLevels[i], event.getLevel());
|
||||||
|
assertEquals("LoggingEvent message",
|
||||||
|
testMessages[i], event.getMessage());
|
||||||
|
/* Does not appear to be logged correctly?
|
||||||
|
assertEquals("LoggingEvent class",
|
||||||
|
this.getClass().getName(),
|
||||||
|
event.getLocationInformation().getClassName());
|
||||||
|
*/
|
||||||
|
/* Does not appear to be logged correctly?
|
||||||
|
if (thrown) {
|
||||||
|
assertEquals("LoggingEvent method",
|
||||||
|
"logExceptionMessages",
|
||||||
|
event.getLocationInformation().getMethodName());
|
||||||
|
} else {
|
||||||
|
assertEquals("LoggingEvent method",
|
||||||
|
"logPlainMessages",
|
||||||
|
event.getLocationInformation().getMethodName());
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
if (thrown) {
|
||||||
|
assertNotNull("LoggingEvent thrown",
|
||||||
|
event.getThrowableInformation().getThrowable());
|
||||||
|
assertTrue("LoggingEvent thrown type",
|
||||||
|
event.getThrowableInformation().getThrowable()
|
||||||
|
instanceof IndexOutOfBoundsException);
|
||||||
|
} else {
|
||||||
|
assertNull("LoggingEvent thrown",
|
||||||
|
event.getThrowableInformation());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertTrue(!events.hasNext());
|
||||||
|
appender.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Log the messages with exceptions
|
||||||
|
protected void logExceptionMessages() {
|
||||||
|
Throwable t = new IndexOutOfBoundsException();
|
||||||
|
log.trace("trace", t); // Should not actually get logged
|
||||||
|
log.debug("debug", t); // Should not actually get logged
|
||||||
|
log.info("info", t);
|
||||||
|
log.warn("warn", t);
|
||||||
|
log.error("error", t);
|
||||||
|
log.fatal("fatal", t);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Log the plain messages
|
||||||
|
protected void logPlainMessages() {
|
||||||
|
log.trace("trace"); // Should not actually get logged
|
||||||
|
log.debug("debug"); // Should not actually get logged
|
||||||
|
log.info("info");
|
||||||
|
log.warn("warn");
|
||||||
|
log.error("error");
|
||||||
|
log.fatal("fatal");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Set up our custom Appender
|
||||||
|
protected void setUpAppender(String config) throws Exception {
|
||||||
|
Properties props = new Properties();
|
||||||
|
InputStream is =
|
||||||
|
this.getClass().getClassLoader().getResourceAsStream(config);
|
||||||
|
props.load(is);
|
||||||
|
is.close();
|
||||||
|
PropertyConfigurator.configure(props);
|
||||||
|
Enumeration appenders = Logger.getRootLogger().getAllAppenders();
|
||||||
|
appender = (TestAppender) appenders.nextElement();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Set up our custom Logger
|
||||||
|
protected void setUpLogger(String name) throws Exception {
|
||||||
|
logger = Logger.getLogger(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,205 @@
|
|||||||
|
/*
|
||||||
|
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//logging/src/test/org/apache/commons/logging/log4j/DefaultConfigTestCase.java,v 1.1 2003/03/30 05:22:50 craigmcc Exp $
|
||||||
|
* $Revision: 1.1 $
|
||||||
|
* $Date: 2003/03/30 05:22:50 $
|
||||||
|
*
|
||||||
|
* ====================================================================
|
||||||
|
*
|
||||||
|
* The Apache Software License, Version 1.1
|
||||||
|
*
|
||||||
|
* Copyright (c) 1999-2003 The Apache Software Foundation. All rights
|
||||||
|
* reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in
|
||||||
|
* the documentation and/or other materials provided with the
|
||||||
|
* distribution.
|
||||||
|
*
|
||||||
|
* 3. The end-user documentation included with the redistribution, if
|
||||||
|
* any, must include the following acknowlegement:
|
||||||
|
* "This product includes software developed by the
|
||||||
|
* Apache Software Foundation (http://www.apache.org/)."
|
||||||
|
* Alternately, this acknowlegement may appear in the software itself,
|
||||||
|
* if and wherever such third-party acknowlegements normally appear.
|
||||||
|
*
|
||||||
|
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||||
|
* Foundation" must not be used to endorse or promote products derived
|
||||||
|
* from this software without prior written permission. For written
|
||||||
|
* permission, please contact apache@apache.org.
|
||||||
|
*
|
||||||
|
* 5. Products derived from this software may not be called "Apache"
|
||||||
|
* nor may "Apache" appear in their names without prior written
|
||||||
|
* permission of the Apache Group.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||||
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||||
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||||
|
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||||
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||||
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
|
* SUCH DAMAGE.
|
||||||
|
* ====================================================================
|
||||||
|
*
|
||||||
|
* This software consists of voluntary contributions made by many
|
||||||
|
* individuals on behalf of the Apache Software Foundation. For more
|
||||||
|
* information on the Apache Software Foundation, please see
|
||||||
|
* <http://www.apache.org/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.apache.commons.logging.log4j;
|
||||||
|
|
||||||
|
|
||||||
|
import junit.framework.Test;
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
import junit.framework.TestSuite;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>TestCase for Log4J logging when running on a system with
|
||||||
|
* zero configuration, and with Log4J present (so Log4J logging
|
||||||
|
* should be automatically configured).</p>
|
||||||
|
*
|
||||||
|
* @author Craig R. McClanahan
|
||||||
|
* @version $Revision: 1.1 $ $Date: 2003/03/30 05:22:50 $
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class DefaultConfigTestCase extends TestCase {
|
||||||
|
|
||||||
|
|
||||||
|
// ----------------------------------------------------------- Constructors
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Construct a new instance of this test case.</p>
|
||||||
|
*/
|
||||||
|
public DefaultConfigTestCase() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Construct a new instance of this test case.</p>
|
||||||
|
*
|
||||||
|
* @param name Name of the test case
|
||||||
|
*/
|
||||||
|
public DefaultConfigTestCase(String name) {
|
||||||
|
super(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ----------------------------------------------------- Instance Variables
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>The {@link LogFactory} implementation we have selected.</p>
|
||||||
|
*/
|
||||||
|
protected LogFactory factory = null;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>The {@link Log} implementation we have selected.</p>
|
||||||
|
*/
|
||||||
|
protected Log log = null;
|
||||||
|
|
||||||
|
|
||||||
|
// ------------------------------------------- JUnit Infrastructure Methods
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set up instance variables required by this test case.
|
||||||
|
*/
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
setUpFactory();
|
||||||
|
setUpLog("TestLogger");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the tests included in this test suite.
|
||||||
|
*/
|
||||||
|
public static Test suite() {
|
||||||
|
return (new TestSuite(DefaultConfigTestCase.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tear down instance variables required by this test case.
|
||||||
|
*/
|
||||||
|
public void tearDown() {
|
||||||
|
log = null;
|
||||||
|
if (factory != null) {
|
||||||
|
factory.releaseAll();
|
||||||
|
factory = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ----------------------------------------------------------- Test Methods
|
||||||
|
|
||||||
|
|
||||||
|
// Test pristine Log instance
|
||||||
|
public void testPristineLog() {
|
||||||
|
|
||||||
|
assertNotNull("Log exists", log);
|
||||||
|
assertEquals("Log class",
|
||||||
|
"org.apache.commons.logging.impl.Log4JLogger",
|
||||||
|
log.getClass().getName());
|
||||||
|
|
||||||
|
// Can we call level checkers with no exceptions?
|
||||||
|
log.isDebugEnabled();
|
||||||
|
log.isErrorEnabled();
|
||||||
|
log.isFatalEnabled();
|
||||||
|
log.isInfoEnabled();
|
||||||
|
log.isTraceEnabled();
|
||||||
|
log.isWarnEnabled();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Test pristine LogFactory instance
|
||||||
|
public void testPristineFactory() {
|
||||||
|
|
||||||
|
assertNotNull("LogFactory exists", factory);
|
||||||
|
assertEquals("LogFactory class",
|
||||||
|
"org.apache.commons.logging.impl.LogFactoryImpl",
|
||||||
|
factory.getClass().getName());
|
||||||
|
|
||||||
|
String names[] = factory.getAttributeNames();
|
||||||
|
assertNotNull("Names exists", names);
|
||||||
|
assertEquals("Names empty", 0, names.length);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// -------------------------------------------------------- Support Methods
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Set up factory instance
|
||||||
|
protected void setUpFactory() throws Exception {
|
||||||
|
factory = LogFactory.getFactory();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Set up log instance
|
||||||
|
protected void setUpLog(String name) throws Exception {
|
||||||
|
log = factory.getLog(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
120
src/test/org/apache/commons/logging/log4j/TestAppender.java
Normal file
120
src/test/org/apache/commons/logging/log4j/TestAppender.java
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
/*
|
||||||
|
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//logging/src/test/org/apache/commons/logging/log4j/TestAppender.java,v 1.1 2003/03/30 05:22:50 craigmcc Exp $
|
||||||
|
* $Revision: 1.1 $
|
||||||
|
* $Date: 2003/03/30 05:22:50 $
|
||||||
|
*
|
||||||
|
* ====================================================================
|
||||||
|
*
|
||||||
|
* The Apache Software License, Version 1.1
|
||||||
|
*
|
||||||
|
* Copyright (c) 1999-2003 The Apache Software Foundation. All rights
|
||||||
|
* reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in
|
||||||
|
* the documentation and/or other materials provided with the
|
||||||
|
* distribution.
|
||||||
|
*
|
||||||
|
* 3. The end-user documentation included with the redistribution, if
|
||||||
|
* any, must include the following acknowlegement:
|
||||||
|
* "This product includes software developed by the
|
||||||
|
* Apache Software Foundation (http://www.apache.org/)."
|
||||||
|
* Alternately, this acknowlegement may appear in the software itself,
|
||||||
|
* if and wherever such third-party acknowlegements normally appear.
|
||||||
|
*
|
||||||
|
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||||
|
* Foundation" must not be used to endorse or promote products derived
|
||||||
|
* from this software without prior written permission. For written
|
||||||
|
* permission, please contact apache@apache.org.
|
||||||
|
*
|
||||||
|
* 5. Products derived from this software may not be called "Apache"
|
||||||
|
* nor may "Apache" appear in their names without prior written
|
||||||
|
* permission of the Apache Group.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||||
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||||
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||||
|
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||||
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||||
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
|
* SUCH DAMAGE.
|
||||||
|
* ====================================================================
|
||||||
|
*
|
||||||
|
* This software consists of voluntary contributions made by many
|
||||||
|
* individuals on behalf of the Apache Software Foundation. For more
|
||||||
|
* information on the Apache Software Foundation, please see
|
||||||
|
* <http://www.apache.org/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.apache.commons.logging.log4j;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
import org.apache.log4j.AppenderSkeleton;
|
||||||
|
import org.apache.log4j.spi.LoggingEvent;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Test implementation of <code>org.apache.log4j.Appender</code>.</p>
|
||||||
|
*
|
||||||
|
* @author Craig R. McClanahan
|
||||||
|
* @version $Revision: 1.1 $ $Date: 2003/03/30 05:22:50 $
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class TestAppender extends AppenderSkeleton {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// ----------------------------------------------------- Instance Variables
|
||||||
|
|
||||||
|
|
||||||
|
// The set of logged events for this appender
|
||||||
|
private List events = new ArrayList();
|
||||||
|
|
||||||
|
|
||||||
|
// --------------------------------------------------------- Public Methods
|
||||||
|
|
||||||
|
|
||||||
|
public Iterator events() {
|
||||||
|
return (events.iterator());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void flush() {
|
||||||
|
events.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ------------------------------------------------------- Appender Methods
|
||||||
|
|
||||||
|
|
||||||
|
protected void append(LoggingEvent event) {
|
||||||
|
events.add(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void close() {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public boolean requiresLayout() {
|
||||||
|
return (false);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user