craigmcc 2003/03/29 14:04:55
Modified: logging build.xml
Added: logging/src/test/org/apache/commons/logging/jdk14
CustomConfig.properties CustomConfigTestCase.java
DefaultConfigTestCase.java TestHandler.java
Log:
Add a more thorough set of unit tests for using JDK 1.4 logging. Right now,
the tests run when everything is in the system class loader -- need to add
execution wrappers for two-tier class loaders where:
* commons-logging.jar is in the parent and test execution is in the child
* commons-logging-api.jar is in the parent and test execution plus
commons-logging.jar is in the child
to simulate container environments like that you get running inside Tomcat.
Later on, we'll want an analogous set of tests for Log4J.
Revision Changes Path
1.26 +59 -2 jakarta-commons/logging/build.xml
Index: build.xml
===================================================================
RCS file: /home/cvs/jakarta-commons/logging/build.xml,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -r1.25 -r1.26
--- build.xml 17 Mar 2003 03:24:14 -0000 1.25
+++ build.xml 29 Mar 2003 22:04:54 -0000 1.26
@@ -86,7 +86,7 @@
<!-- ========== Test Execution Defaults =================================== -->
- <!-- Construct unit test classpath -->
+ <!-- Construct unit test classpath (generic tests) -->
<path id="test.classpath">
<pathelement location="${build.home}/classes"/>
<pathelement location="${build.home}/tests"/>
@@ -96,6 +96,13 @@
<pathelement location="${conf.home}"/>
</path>
+ <!-- Construct unit test classpath (JDK 1.4 tests) -->
+ <path id="test.classpath.jdk14">
+ <pathelement location="${build.home}/classes"/>
+ <pathelement location="${build.home}/tests"/>
+ <pathelement location="${junit.jar}"/>
+ </path>
+
<!-- Should all tests fail if one does? -->
<property name="test.failonerror" value="true"/>
@@ -272,5 +279,55 @@
<classpath refid="test.classpath"/>
</java>
</target>
+
+ <target name="test.jdk14" depends="compile.tests" if="jdk.1.4.present"
+ description="Run unit tests specific to JDK 1.4 logging">
+
+ <echo message="Default Configuration (JDK 1.4 Auto-Recognized)"/>
+ <java classname="${test.runner}" fork="yes" failonerror="${test.failonerror}">
+ <arg value="org.apache.commons.logging.jdk14.DefaultConfigTestCase"/>
+ <classpath refid="test.classpath.jdk14"/>
+ </java>
+
+ <echo message="Default Configuration (JDK 1.4 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.jdk14.DefaultConfigTestCase"/>
+ <classpath refid="test.classpath.jdk14"/>
+ </java>
+
+ <echo message="Default Configuration (JDK 1.4 Jdk14Logger Selected)"/>
+ <java classname="${test.runner}" fork="yes" failonerror="${test.failonerror}">
+ <sysproperty key="org.apache.commons.logging.Log"
+ value="org.apache.commons.logging.impl.Jdk14Logger"/>
+ <arg value="org.apache.commons.logging.jdk14.DefaultConfigTestCase"/>
+ <classpath refid="test.classpath.jdk14"/>
+ </java>
+
+ <echo message="Custom Configuration (JDK 1.4 Auto-Recognized)"/>
+ <java classname="${test.runner}" fork="yes" failonerror="${test.failonerror}">
+ <arg value="org.apache.commons.logging.jdk14.CustomConfigTestCase"/>
+ <classpath refid="test.classpath.jdk14"/>
+ </java>
+
+ <echo message="Custom Configuration (JDK 1.4 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.jdk14.CustomConfigTestCase"/>
+ <classpath refid="test.classpath.jdk14"/>
+ </java>
+
+ <echo message="Custom Configuration (JDK 1.4 Jdk14Logger Selected)"/>
+ <java classname="${test.runner}" fork="yes" failonerror="${test.failonerror}">
+ <sysproperty key="org.apache.commons.logging.Log"
+ value="org.apache.commons.logging.impl.Jdk14Logger"/>
+ <arg value="org.apache.commons.logging.jdk14.CustomConfigTestCase"/>
+ <classpath refid="test.classpath.jdk14"/>
+ </java>
+
+ </target>
+
</project>
1.1
jakarta-commons/logging/src/test/org/apache/commons/logging/jdk14/CustomConfig.properties
Index: CustomConfig.properties
===================================================================
# This is the custom configuration properties for the JDK 1.4 logger tests
# in CustomConfigTestCase.
# Configure the Handler so we can examine the logged messages
handlers = org.apache.commons.logging.jdk14.TestHandler
# Configre the default logging level to be FINE so we should get
# everything except trace messages
.level = FINE
1.1
jakarta-commons/logging/src/test/org/apache/commons/logging/jdk14/CustomConfigTestCase.java
Index: CustomConfigTestCase.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons/logging/src/test/org/apache/commons/logging/jdk14/CustomConfigTestCase.java,v
1.1 2003/03/29 22:04:54 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2003/03/29 22:04:54 $
*
* ====================================================================
*
* 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 [EMAIL PROTECTED]
*
* 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.jdk14;
import java.io.InputStream;
import java.util.Iterator;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
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 JDK 1.4 logging when running on a JDK 1.4 system with
* custom configuration, so that JDK 1.4 should be selected and an appropriate
* logger configured per the configuration properties.</p>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2003/03/29 22:04:54 $
*/
public class CustomConfigTestCase extends DefaultConfigTestCase {
// ----------------------------------------------------------- Constructors
/**
* <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 customized <code>Handler</code> we will be using.</p>
*/
protected TestHandler handler = null;
/**
* <p>The underlying <code>Handler</code>s we will be using.</p>
*/
protected Handler handlers[] = null;
/**
* <p>The underlying <code>Logger</code> we will be using.</p>
*/
protected Logger logger = null;
/**
* <p>The underlying <code>LogManager</code> we will be using.</p>
*/
protected LogManager manager = null;
/**
* <p>The message levels that should have been logged.</p>
*/
protected Level testLevels[] =
{ Level.FINE, Level.INFO, Level.WARNING, Level.SEVERE };
/**
* <p>The message strings that should have been logged.</p>
*/
protected String testMessages[] =
{ "debug", "info", "warn", "error" };
// ------------------------------------------- JUnit Infrastructure Methods
/**
* Set up instance variables required by this test case.
*/
public void setUp() throws Exception {
setUpManager
("org/apache/commons/logging/jdk14/CustomConfig.properties");
setUpLogger("TestLogger");
setUpHandlers();
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();
handlers = null;
logger = null;
manager = null;
}
// ----------------------------------------------------------- Test Methods
// Test logging plain message strings
public void testPlainMessages() throws Exception {
logPlainMessages();
Iterator records = handler.records();
for (int i = 0; i < testMessages.length; i++) {
assertTrue(records.hasNext());
LogRecord record = (LogRecord) records.next();
assertEquals("LogRecord level",
testLevels[i], record.getLevel());
assertEquals("LogRecord message",
testMessages[i], record.getMessage());
assertEquals("LogRecord class",
this.getClass().getName(),
record.getSourceClassName());
assertEquals("LogRecord method",
"logPlainMessages",
record.getSourceMethodName());
}
assertTrue(!records.hasNext());
handler.flush();
}
// Test pristine Handlers instances
public void testPristineHandlers() {
assertNotNull(handlers);
assertEquals(1, handlers.length);
assertTrue(handlers[0] instanceof TestHandler);
assertNotNull(handler);
}
// 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 name", "TestLogger", logger.getName());
// Assert which logging levels have been enabled
assertTrue(logger.isLoggable(Level.SEVERE));
assertTrue(logger.isLoggable(Level.WARNING));
assertTrue(logger.isLoggable(Level.INFO));
assertTrue(logger.isLoggable(Level.CONFIG));
assertTrue(logger.isLoggable(Level.FINE));
assertTrue(!logger.isLoggable(Level.FINER));
assertTrue(!logger.isLoggable(Level.FINEST));
}
// -------------------------------------------------------- Support Methods
// Log the plain messages
protected void logPlainMessages() {
log.trace("trace"); // Should not actually get logged
log.debug("debug");
log.info("info");
log.warn("warn");
log.error("error");
}
// Set up handlers instance
protected void setUpHandlers() throws Exception {
Logger parent = logger;
while (parent.getParent() != null) {
parent = parent.getParent();
}
handlers = parent.getHandlers();
if ((handlers != null) && (handlers.length == 1) &&
(handlers[0] instanceof TestHandler)) {
handler = (TestHandler) handlers[0];
}
}
// Set up logger instance
protected void setUpLogger(String name) throws Exception {
logger = Logger.getLogger(name);
}
// Set up LogManager instance
protected void setUpManager(String config) throws Exception {
manager = LogManager.getLogManager();
InputStream is =
this.getClass().getClassLoader().getResourceAsStream(config);
manager.readConfiguration(is);
is.close();
}
}
1.1
jakarta-commons/logging/src/test/org/apache/commons/logging/jdk14/DefaultConfigTestCase.java
Index: DefaultConfigTestCase.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons/logging/src/test/org/apache/commons/logging/jdk14/DefaultConfigTestCase.java,v
1.1 2003/03/29 22:04:54 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2003/03/29 22:04:54 $
*
* ====================================================================
*
* 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 [EMAIL PROTECTED]
*
* 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.jdk14;
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 JDK 1.4 logging when running on a JDK 1.4 system with
* zero configuration, and with Log4J not present (so JDK 1.4 logging
* should be automatically configured.</p>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2003/03/29 22:04:54 $
*/
public class DefaultConfigTestCase extends TestCase {
// ----------------------------------------------------------- Constructors
/**
* <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 [EMAIL PROTECTED] LogFactory} implementation we have selected.</p>
*/
protected LogFactory factory = null;
/**
* <p>The [EMAIL PROTECTED] 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.Jdk14Logger",
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);
}
}
1.1
jakarta-commons/logging/src/test/org/apache/commons/logging/jdk14/TestHandler.java
Index: TestHandler.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons/logging/src/test/org/apache/commons/logging/jdk14/TestHandler.java,v
1.1 2003/03/29 22:04:54 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2003/03/29 22:04:54 $
*
* ====================================================================
*
* 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 [EMAIL PROTECTED]
*
* 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.jdk14;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
/**
* <p>Test implementation of <code>java.util.logging.Handler</code>.</p>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2003/03/29 22:04:54 $
*/
public class TestHandler extends Handler {
// ----------------------------------------------------- Instance Variables
// The set of logged records for this handler
private List records = new ArrayList();
// --------------------------------------------------------- Public Methods
public Iterator records() {
return (records.iterator());
}
// -------------------------------------------------------- Handler Methods
public void close() {
}
public void flush() {
records.clear();
}
public void publish(LogRecord record) {
records.add(record);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]