1
0
- Refactor the setup of class loaders so that it is clearer what is
  going on
- For the Log4J tests, add the custom Appender implementation to
  whichever class loader Log4J is put in
- Set and reset the thread context class loader to more accurately
  simulate the environment of a servlet container.

build.xml:
- Implement the multi-classloader test scenarios for Log4J, equivalent
  to those for JDK 1.4.

We now have a robust testing environment to catch any possible regressions
when modifying the functionality (such as ripping out Log4jFactory, as
discussed on COMMONS-DEV).

I don't have time to do the corresponding test scenarios for Avalon LogKit,
but anyone who wants to do so is welcome to create them.


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/logging/trunk@138965 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Craig R. McClanahan
2003-04-02 00:50:49 +00:00
parent 5347bd9e55
commit 7c71a7d917
3 changed files with 129 additions and 47 deletions

View File

@@ -3,7 +3,7 @@
<!-- <!--
"Logging" component of the Jakarta Commons Subproject "Logging" component of the Jakarta Commons Subproject
$Id: build.xml,v 1.31 2003/03/31 00:38:22 craigmcc Exp $ $Id: build.xml,v 1.32 2003/04/02 00:50:49 craigmcc Exp $
--> -->
@@ -236,6 +236,14 @@
<copy todir="${build.home}/tests" filtering="on"> <copy todir="${build.home}/tests" filtering="on">
<fileset dir="${test.home}" excludes="**/*.java"/> <fileset dir="${test.home}" excludes="**/*.java"/>
</copy> </copy>
<jar jarfile="${build.home}/commons-${component.name}-appender.jar"
basedir="${build.home}/tests"
manifest="${build.home}/conf/MANIFEST.MF">
<include name="org/apache/commons/logging/log4j/CustomConfig.properties"
if="log4j.present"/>
<include name="org/apache/commons/logging/log4j/TestAppender.class"
if="log4j.present"/>
</jar>
<jar jarfile="${build.home}/commons-${component.name}-tests.jar" <jar jarfile="${build.home}/commons-${component.name}-tests.jar"
basedir="${build.home}/tests" basedir="${build.home}/tests"
manifest="${build.home}/conf/MANIFEST.MF"> manifest="${build.home}/conf/MANIFEST.MF">
@@ -478,6 +486,80 @@
<classpath refid="test.classpath.log4j"/> <classpath refid="test.classpath.log4j"/>
</java> </java>
<echo message="Hierarchy Configuration API (Log4J Auto-Recognized)"/>
<java classname="${test.wrapper}" fork="yes" failonerror="${test.failonerror}">
<sysproperty key="wrapper.hierarchy" value="API"/>
<sysproperty key="wrapper.junit" value="${junit.jar}"/>
<sysproperty key="wrapper.log4j" value="${log4j.jar}"/>
<sysproperty key="wrapper.target" value="${build.home}"/>
<sysproperty key="wrapper.testcase"
value="org.apache.commons.logging.log4j.CustomConfigTestCase"/>
<classpath refid="test.classpath.wrap"/>
</java>
<echo message="Hierarchy Configuration API (Log4J LogFactoryImpl Selected)"/>
<java classname="${test.wrapper}" fork="yes" failonerror="${test.failonerror}">
<sysproperty key="wrapper.hierarchy" value="API"/>
<sysproperty key="wrapper.junit" value="${junit.jar}"/>
<sysproperty key="wrapper.log4j" value="${log4j.jar}"/>
<sysproperty key="wrapper.target" value="${build.home}"/>
<sysproperty key="wrapper.testcase"
value="org.apache.commons.logging.log4j.CustomConfigTestCase"/>
<sysproperty key="org.apache.commons.logging.LogFactory"
value="org.apache.commons.logging.impl.LogFactoryImpl"/>
<classpath refid="test.classpath.wrap"/>
</java>
<echo message="Hierarchy Configuration API (Log4J Log4JLogger Selected)"/>
<java classname="${test.wrapper}" fork="yes" failonerror="${test.failonerror}">
<sysproperty key="wrapper.hierarchy" value="API"/>
<sysproperty key="wrapper.junit" value="${junit.jar}"/>
<sysproperty key="wrapper.log4j" value="${log4j.jar}"/>
<sysproperty key="wrapper.target" value="${build.home}"/>
<sysproperty key="wrapper.testcase"
value="org.apache.commons.logging.log4j.CustomConfigTestCase"/>
<sysproperty key="org.apache.commons.logging.Log"
value="org.apache.commons.logging.impl.Log4JLogger"/>
<classpath refid="test.classpath.wrap"/>
</java>
<echo message="Hierarchy Configuration FULL (Log4J Auto-Recognized)"/>
<java classname="${test.wrapper}" fork="yes" failonerror="${test.failonerror}">
<sysproperty key="wrapper.hierarchy" value="FULL"/>
<sysproperty key="wrapper.junit" value="${junit.jar}"/>
<sysproperty key="wrapper.log4j" value="${log4j.jar}"/>
<sysproperty key="wrapper.target" value="${build.home}"/>
<sysproperty key="wrapper.testcase"
value="org.apache.commons.logging.log4j.CustomConfigTestCase"/>
<classpath refid="test.classpath.wrap"/>
</java>
<echo message="Hierarchy Configuration FULL (Log4J LogFactoryImpl Selected)"/>
<java classname="${test.wrapper}" fork="yes" failonerror="${test.failonerror}">
<sysproperty key="wrapper.hierarchy" value="FULL"/>
<sysproperty key="wrapper.junit" value="${junit.jar}"/>
<sysproperty key="wrapper.log4j" value="${log4j.jar}"/>
<sysproperty key="wrapper.target" value="${build.home}"/>
<sysproperty key="wrapper.testcase"
value="org.apache.commons.logging.log4j.CustomConfigTestCase"/>
<sysproperty key="org.apache.commons.logging.LogFactory"
value="org.apache.commons.logging.impl.LogFactoryImpl"/>
<classpath refid="test.classpath.wrap"/>
</java>
<echo message="Hierarchy Configuration FULL (Log4J Log4JLogger Selected)"/>
<java classname="${test.wrapper}" fork="yes" failonerror="${test.failonerror}">
<sysproperty key="wrapper.hierarchy" value="FULL"/>
<sysproperty key="wrapper.junit" value="${junit.jar}"/>
<sysproperty key="wrapper.log4j" value="${log4j.jar}"/>
<sysproperty key="wrapper.target" value="${build.home}"/>
<sysproperty key="wrapper.testcase"
value="org.apache.commons.logging.log4j.CustomConfigTestCase"/>
<sysproperty key="org.apache.commons.logging.Log"
value="org.apache.commons.logging.impl.Log4JLogger"/>
<classpath refid="test.classpath.wrap"/>
</java>
</target> </target>

View File

@@ -1,7 +1,7 @@
/* /*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//logging/src/test/org/apache/commons/logging/Wrapper.java,v 1.1 2003/03/30 02:30:36 craigmcc Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//logging/src/test/org/apache/commons/logging/Wrapper.java,v 1.2 2003/04/02 00:50:49 craigmcc Exp $
* $Revision: 1.1 $ * $Revision: 1.2 $
* $Date: 2003/03/30 02:30:36 $ * $Date: 2003/04/02 00:50:49 $
* *
* ==================================================================== * ====================================================================
* *
@@ -66,6 +66,8 @@ import java.io.File;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
/** /**
@@ -108,7 +110,7 @@ import java.net.URLClassLoader;
* only the wrapper class itself.</p> * only the wrapper class itself.</p>
* *
* @author Craig R. McClanahan * @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2003/03/30 02:30:36 $ * @version $Revision: 1.2 $ $Date: 2003/04/02 00:50:49 $
*/ */
public class Wrapper { public class Wrapper {
@@ -118,6 +120,11 @@ public class Wrapper {
try { try {
// Create variables we will need
List parentList = new ArrayList();
List childList = new ArrayList();
URL urls[] = null;
// Construct URLs for the various JAR files // Construct URLs for the various JAR files
File target = new File(System.getProperty("wrapper.target")); File target = new File(System.getProperty("wrapper.target"));
URL commonsLogging = URL commonsLogging =
@@ -128,62 +135,55 @@ public class Wrapper {
(new File(target, "commons-logging-tests.jar")).toURL(); (new File(target, "commons-logging-tests.jar")).toURL();
URL junit = URL junit =
(new File(System.getProperty("wrapper.junit"))).toURL(); (new File(System.getProperty("wrapper.junit"))).toURL();
URL appender = null;
URL log4j = null; URL log4j = null;
if (System.getProperty("wrapper.log4j") != null) { if (System.getProperty("wrapper.log4j") != null) {
log4j = (new File(System.getProperty("wrapper.log4j"))).toURL(); log4j =
(new File(System.getProperty("wrapper.log4j"))).toURL();
appender =
(new File(target, "commons-logging-appender.jar")).toURL();
} }
// Configure the parent class loader // Construct class loader repository lists for supported scenarios
URL parentURLs[] = null;
if ("API".equals(System.getProperty("wrapper.hierarchy"))) { if ("API".equals(System.getProperty("wrapper.hierarchy"))) {
parentURLs = new URL[1]; parentList.add(commonsLoggingApi);
parentURLs[0] = commonsLoggingApi; childList.add(commonsLogging);
} else {
if (log4j != null) { if (log4j != null) {
parentURLs = new URL[2]; childList.add(log4j);
parentURLs[0] = commonsLogging; childList.add(appender);
parentURLs[1] = log4j; }
} else { } else { // Assumes "FULL"
parentURLs = new URL[1]; parentList.add(commonsLogging);
parentURLs[0] = commonsLogging; if (log4j != null) {
parentList.add(log4j);
childList.add(appender);
} }
} }
childList.add(commonsLoggingTests);
childList.add(junit);
// Construt the parent and child class loaders
urls = (URL[]) parentList.toArray(new URL[parentList.size()]);
ClassLoader parent = ClassLoader parent =
new URLClassLoader(parentURLs, new URLClassLoader(urls,
ClassLoader.getSystemClassLoader()); ClassLoader.getSystemClassLoader());
urls = (URL[]) childList.toArray(new URL[childList.size()]);
// Configure the child class loader ClassLoader child = new URLClassLoader(urls, parent);
URL childURLs[] = null;
if ("API".equals(System.getProperty("wrapper.hierarchy"))) {
if (log4j != null) {
childURLs = new URL[4];
} else {
childURLs = new URL[3];
}
childURLs[0] = commonsLogging;
childURLs[1] = commonsLoggingTests;
childURLs[2] = junit;
if (log4j != null) {
childURLs[3] = log4j;
}
} else {
childURLs = new URL[2];
childURLs[0] = commonsLoggingTests;
childURLs[1] = junit;
}
ClassLoader child = new URLClassLoader(childURLs, parent);
// Execute the test runner for this TestCase // Execute the test runner for this TestCase
ClassLoader old = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(child);
Class clazz = child.loadClass("junit.textui.TestRunner"); Class clazz = child.loadClass("junit.textui.TestRunner");
String params[] = new String[1]; String params[] = new String[1];
params[0] = System.getProperty("wrapper.testcase"); params[0] = System.getProperty("wrapper.testcase");
Method method = clazz.getMethod("main", Method method = clazz.getMethod("main",
new Class[] { params.getClass() }); new Class[] { params.getClass() });
method.invoke(null, new Object[] { params }); method.invoke(null, new Object[] { params });
Thread.currentThread().setContextClassLoader(old);
} catch (Exception e) { } catch (Exception e) {
System.out.println("Exception Occurred: " + e); System.out.println("Wrapper Exception Occurred: " + e);
e.printStackTrace(System.out); e.printStackTrace(System.out);
System.exit(1); System.exit(1);

View File

@@ -1,7 +1,7 @@
/* /*
* $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 $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//logging/src/test/org/apache/commons/logging/log4j/CustomConfigTestCase.java,v 1.2 2003/04/02 00:50:49 craigmcc Exp $
* $Revision: 1.1 $ * $Revision: 1.2 $
* $Date: 2003/03/30 05:22:50 $ * $Date: 2003/04/02 00:50:49 $
* *
* ==================================================================== * ====================================================================
* *
@@ -85,7 +85,7 @@ import org.apache.log4j.spi.LoggingEvent;
* 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.1 $ $Date: 2003/03/30 05:22:50 $ * @version $Revision: 1.2 $ $Date: 2003/04/02 00:50:49 $
*/ */
public class CustomConfigTestCase extends DefaultConfigTestCase { public class CustomConfigTestCase extends DefaultConfigTestCase {
@@ -235,7 +235,7 @@ public class CustomConfigTestCase extends DefaultConfigTestCase {
protected void checkLoggingEvents(boolean thrown) { protected void checkLoggingEvents(boolean thrown) {
Iterator events = appender.events(); Iterator events = appender.events();
for (int i = 0; i < testMessages.length; i++) { for (int i = 0; i < testMessages.length; i++) {
assertTrue(events.hasNext()); assertTrue("Logged event " + i + " exists",events.hasNext());
LoggingEvent event = (LoggingEvent) events.next(); LoggingEvent event = (LoggingEvent) events.next();
assertEquals("LoggingEvent level", assertEquals("LoggingEvent level",
testLevels[i], event.getLevel()); testLevels[i], event.getLevel());