1
0

Add test scenarios where two levels of class loader hierarchies are created -

one exactly like the default configuration of Tomcat 4.1 (with c-l placed
inside the webapp) and one where you put c-l in the parent class loader
(in place of commons-logging-api.jar) so webapps do not have to.


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/logging/trunk@138959 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Craig R. McClanahan
2003-03-30 02:30:37 +00:00
parent 916223fc8d
commit 4ed6819dc3
4 changed files with 367 additions and 28 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.26 2003/03/29 22:04:54 craigmcc Exp $ $Id: build.xml,v 1.27 2003/03/30 02:30:36 craigmcc Exp $
--> -->
@@ -48,7 +48,7 @@
<property name="component.version" value="1.0.2"/> <property name="component.version" value="1.0.2"/>
<!-- The base directory for compilation targets --> <!-- The base directory for compilation targets -->
<property name="build.home" value="target"/> <property name="build.home" value="${basedir}/target"/>
<!-- The base directory for component configuration files --> <!-- The base directory for component configuration files -->
<property name="conf.home" value="src/conf"/> <property name="conf.home" value="src/conf"/>
@@ -103,12 +103,18 @@
<pathelement location="${junit.jar}"/> <pathelement location="${junit.jar}"/>
</path> </path>
<!-- Construct unit test classpath (Minimal Wrapper) -->
<path id="test.classpath.wrap">
<pathelement location="${build.home}/commons-logging-wrapper.jar"/>
</path>
<!-- Should all tests fail if one does? --> <!-- Should all tests fail if one does? -->
<property name="test.failonerror" value="true"/> <property name="test.failonerror" value="true"/>
<!-- The test runner to execute --> <!-- The test runner to execute -->
<property name="test.runner" value="junit.textui.TestRunner"/> <property name="test.runner" value="junit.textui.TestRunner"/>
<property name="test.entry" value="org.apache.commons.logging.TestAll"/> <property name="test.entry" value="org.apache.commons.logging.TestAll"/>
<property name="test.wrapper" value="org.apache.commons.logging.Wrapper"/>
<!-- ========== Executable Targets ======================================== --> <!-- ========== Executable Targets ======================================== -->
@@ -216,6 +222,19 @@
<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}-tests.jar"
basedir="${build.home}/tests"
manifest="${conf.home}/MANIFEST.MF">
<exclude name="org/apache/commons/logging/Wrapper.class"/>
</jar>
<jar jarfile="${build.home}/commons-${component.name}-wrapper.jar"
basedir="${build.home}/tests"
manifest="${conf.home}/MANIFEST.MF">
<include name="org/apache/commons/logging/Wrapper.class"/>
<include name="org/apache/commons/logging/jdk14/TestHandler.class"
if="jdk.1.4.present"/>
</jar>
</target> </target>
@@ -272,7 +291,7 @@
<!-- ========== Unit Test Targets ========================================= --> <!-- ========== Unit Test Targets ========================================= -->
<target name="test" depends="compile.tests" if="test.entry" <target name="test" depends="compile.tests,test.jdk14" 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}"/>
@@ -327,6 +346,74 @@
<classpath refid="test.classpath.jdk14"/> <classpath refid="test.classpath.jdk14"/>
</java> </java>
<echo message="Hierarchy Configuration API (JDK 1.4 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.target" value="${build.home}"/>
<sysproperty key="wrapper.testcase"
value="org.apache.commons.logging.jdk14.CustomConfigTestCase"/>
<classpath refid="test.classpath.wrap"/>
</java>
<echo message="Hierarchy Configuration API (JDK 1.4 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.target" value="${build.home}"/>
<sysproperty key="wrapper.testcase"
value="org.apache.commons.logging.jdk14.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 (JDK 1.4 Jdk14Logger 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.target" value="${build.home}"/>
<sysproperty key="wrapper.testcase"
value="org.apache.commons.logging.jdk14.CustomConfigTestCase"/>
<sysproperty key="org.apache.commons.logging.Log"
value="org.apache.commons.logging.impl.Jdk14Logger"/>
<classpath refid="test.classpath.wrap"/>
</java>
<echo message="Hierarchy Configuration FULL (JDK 1.4 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.target" value="${build.home}"/>
<sysproperty key="wrapper.testcase"
value="org.apache.commons.logging.jdk14.CustomConfigTestCase"/>
<classpath refid="test.classpath.wrap"/>
</java>
<echo message="Hierarchy Configuration FULL (JDK 1.4 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.target" value="${build.home}"/>
<sysproperty key="wrapper.testcase"
value="org.apache.commons.logging.jdk14.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 (JDK 1.4 Jdk14Logger 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.target" value="${build.home}"/>
<sysproperty key="wrapper.testcase"
value="org.apache.commons.logging.jdk14.CustomConfigTestCase"/>
<sysproperty key="org.apache.commons.logging.Log"
value="org.apache.commons.logging.impl.Jdk14Logger"/>
<classpath refid="test.classpath.wrap"/>
</java>
</target> </target>

View File

@@ -0,0 +1,196 @@
/*
* $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 $
* $Revision: 1.1 $
* $Date: 2003/03/30 02:30:36 $
*
* ====================================================================
*
* 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;
import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
/**
* <p>Wrapper around test cases that need to have a custom class loader
* hierarchy assembled. The wrapper is configured by the following
* system properties:</p>
* <ul>
* <li><strong>wrapper.hierarchy</strong> - Descriptive code describing how
* the class loader hierarchy should be assembled:
* <ul>
* <li><strong>API</strong> - Parent class loader contains
* <code>commons-logging-api.jar</code> and child class loader
* contains <code>commons-logging.jar</code>. This is like the
* default configuration for Tomcat 4.1.</li>
* <li><strong>FULL</strong> - Parent class loader contains
* <code>commons-logging.jar</code>. This is what would happen
* if you replaced <code>commons-logging-api.jar</code> with
* <code>commons-logging.jar</code> so that you did not need to
* include the latter with your application.</li>
* </ul>
* The child class loader also unconditionally includes
* <code>commons-logging-tests.jar</code>.</li>
* <li><strong>wrapper.junit</strong> - Fully qualified pathname of the
* JUnit JAR file.</li>
* <li><strong>wrapper.log4j</strong> - Fully qualified pathname of the
* Log4J JAR file, which will be placed in whichever class loader
* <code>commons-logging.jar</code> is placed in, if specified.</li>
* <li><strong>wrapper.target</strong> - Fully qualified pathname of the
* "target" directory created by the build process. This directory
* must contain the <code>commons-logging.jar</code>,
* <code>commons-logging-api.jar</code>, and
* <code>commons-logging-tests.jar</code> files resulting from the
* execution of the <code>compile.tests</code> target.</li>
* <li><strong>wrapper.testcase</strong> - Fully qualified Java class name
* of a TestCase that will ultimately be executed. This class must
* exist in the <code>commons-logging-tests.jar</code> file.</li>
* </ul>
*
* <p>When executed, the system classpath for the wrapper should include
* only the wrapper class itself.</p>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2003/03/30 02:30:36 $
*/
public class Wrapper {
public static void main(String args[]) {
try {
// Construct URLs for the various JAR files
File target = new File(System.getProperty("wrapper.target"));
URL commonsLogging =
(new File(target, "commons-logging.jar")).toURL();
URL commonsLoggingApi =
(new File(target, "commons-logging-api.jar")).toURL();
URL commonsLoggingTests =
(new File(target, "commons-logging-tests.jar")).toURL();
URL junit =
(new File(System.getProperty("wrapper.junit"))).toURL();
URL log4j = null;
if (System.getProperty("wrapper.log4j") != null) {
log4j = (new File(System.getProperty("wrapper.log4j"))).toURL();
}
// Configure the parent class loader
URL parentURLs[] = null;
if ("API".equals(System.getProperty("wrapper.hierarchy"))) {
parentURLs = new URL[1];
parentURLs[0] = commonsLoggingApi;
} else {
if (log4j != null) {
parentURLs = new URL[2];
parentURLs[0] = commonsLogging;
parentURLs[1] = log4j;
} else {
parentURLs = new URL[1];
parentURLs[0] = commonsLogging;
}
}
ClassLoader parent =
new URLClassLoader(parentURLs,
ClassLoader.getSystemClassLoader());
// Configure the child class loader
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
Class clazz = child.loadClass("junit.textui.TestRunner");
String params[] = new String[1];
params[0] = System.getProperty("wrapper.testcase");
Method method = clazz.getMethod("main",
new Class[] { params.getClass() });
method.invoke(null, new Object[] { params });
} catch (Exception e) {
System.out.println("Exception Occurred: " + e);
e.printStackTrace(System.out);
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/jdk14/CustomConfigTestCase.java,v 1.1 2003/03/29 22:04:54 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.2 2003/03/30 02:30:37 craigmcc Exp $
* $Revision: 1.1 $ * $Revision: 1.2 $
* $Date: 2003/03/29 22:04:54 $ * $Date: 2003/03/30 02:30:37 $
* *
* ==================================================================== * ====================================================================
* *
@@ -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.1 $ $Date: 2003/03/29 22:04:54 $ * @version $Revision: 1.2 $ $Date: 2003/03/30 02:30:37 $
*/ */
public class CustomConfigTestCase extends DefaultConfigTestCase { public class CustomConfigTestCase extends DefaultConfigTestCase {
@@ -93,6 +93,14 @@ public class CustomConfigTestCase extends DefaultConfigTestCase {
// ----------------------------------------------------------- Constructors // ----------------------------------------------------------- Constructors
/**
* <p>Construct a new instance of this test case.</p>
*/
public CustomConfigTestCase() {
super();
}
/** /**
* <p>Construct a new instance of this test case.</p> * <p>Construct a new instance of this test case.</p>
* *
@@ -181,27 +189,20 @@ public class CustomConfigTestCase extends DefaultConfigTestCase {
// ----------------------------------------------------------- Test Methods // ----------------------------------------------------------- Test Methods
// Test logging message strings with exceptions
public void testExceptionMessages() throws Exception {
logExceptionMessages();
checkLogRecords(true);
}
// Test logging plain message strings // Test logging plain message strings
public void testPlainMessages() throws Exception { public void testPlainMessages() throws Exception {
logPlainMessages(); logPlainMessages();
Iterator records = handler.records(); checkLogRecords(false);
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();
} }
@@ -253,6 +254,53 @@ public class CustomConfigTestCase extends DefaultConfigTestCase {
// -------------------------------------------------------- Support Methods // -------------------------------------------------------- Support Methods
// Check the recorded messages
protected void checkLogRecords(boolean thrown) {
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());
if (thrown) {
assertEquals("LogRecord method",
"logExceptionMessages",
record.getSourceMethodName());
} else {
assertEquals("LogRecord method",
"logPlainMessages",
record.getSourceMethodName());
}
if (thrown) {
assertNotNull("LogRecord thrown", record.getThrown());
assertTrue("LogRecord thrown type",
record.getThrown() instanceof IndexOutOfBoundsException);
} else {
assertNull("LogRecord thrown",
record.getThrown());
}
}
assertTrue(!records.hasNext());
handler.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);
log.info("info", t);
log.warn("warn", t);
log.error("error", t);
}
// Log the plain messages // Log the plain messages
protected void logPlainMessages() { protected void logPlainMessages() {
log.trace("trace"); // Should not actually get logged log.trace("trace"); // Should not actually get logged

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/jdk14/DefaultConfigTestCase.java,v 1.1 2003/03/29 22:04:54 craigmcc Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//logging/src/test/org/apache/commons/logging/jdk14/DefaultConfigTestCase.java,v 1.2 2003/03/30 02:30:37 craigmcc Exp $
* $Revision: 1.1 $ * $Revision: 1.2 $
* $Date: 2003/03/29 22:04:54 $ * $Date: 2003/03/30 02:30:37 $
* *
* ==================================================================== * ====================================================================
* *
@@ -76,7 +76,7 @@ import org.apache.commons.logging.LogFactory;
* should be automatically configured.</p> * should be automatically configured.</p>
* *
* @author Craig R. McClanahan * @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2003/03/29 22:04:54 $ * @version $Revision: 1.2 $ $Date: 2003/03/30 02:30:37 $
*/ */
public class DefaultConfigTestCase extends TestCase { public class DefaultConfigTestCase extends TestCase {
@@ -85,6 +85,14 @@ public class DefaultConfigTestCase extends TestCase {
// ----------------------------------------------------------- Constructors // ----------------------------------------------------------- Constructors
/**
* <p>Construct a new instance of this test case.</p>
*/
public DefaultConfigTestCase() {
super();
}
/** /**
* <p>Construct a new instance of this test case.</p> * <p>Construct a new instance of this test case.</p>
* *