From 4ed6819dc363d83931d8904d8570f576ead5d4f0 Mon Sep 17 00:00:00 2001 From: "Craig R. McClanahan" Date: Sun, 30 Mar 2003 02:30:37 +0000 Subject: [PATCH] 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 --- build.xml | 93 ++++++++- .../org/apache/commons/logging/Wrapper.java | 196 ++++++++++++++++++ .../logging/jdk14/CustomConfigTestCase.java | 90 ++++++-- .../logging/jdk14/DefaultConfigTestCase.java | 16 +- 4 files changed, 367 insertions(+), 28 deletions(-) create mode 100644 src/test/org/apache/commons/logging/Wrapper.java diff --git a/build.xml b/build.xml index a49da6b..4739f35 100644 --- a/build.xml +++ b/build.xml @@ -3,7 +3,7 @@ @@ -48,7 +48,7 @@ - + @@ -103,12 +103,18 @@ + + + + + + @@ -216,6 +222,19 @@ + + + + + + + + @@ -272,7 +291,7 @@ - @@ -327,6 +346,74 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/org/apache/commons/logging/Wrapper.java b/src/test/org/apache/commons/logging/Wrapper.java new file mode 100644 index 0000000..eadfa4d --- /dev/null +++ b/src/test/org/apache/commons/logging/Wrapper.java @@ -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 + * . + * + */ + +package org.apache.commons.logging; + + +import java.io.File; +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; + + +/** + *

Wrapper around test cases that need to have a custom class loader + * hierarchy assembled. The wrapper is configured by the following + * system properties:

+ *
    + *
  • wrapper.hierarchy - Descriptive code describing how + * the class loader hierarchy should be assembled: + *
      + *
    • API - Parent class loader contains + * commons-logging-api.jar and child class loader + * contains commons-logging.jar. This is like the + * default configuration for Tomcat 4.1.
    • + *
    • FULL - Parent class loader contains + * commons-logging.jar. This is what would happen + * if you replaced commons-logging-api.jar with + * commons-logging.jar so that you did not need to + * include the latter with your application.
    • + *
    + * The child class loader also unconditionally includes + * commons-logging-tests.jar.
  • + *
  • wrapper.junit - Fully qualified pathname of the + * JUnit JAR file.
  • + *
  • wrapper.log4j - Fully qualified pathname of the + * Log4J JAR file, which will be placed in whichever class loader + * commons-logging.jar is placed in, if specified.
  • + *
  • wrapper.target - Fully qualified pathname of the + * "target" directory created by the build process. This directory + * must contain the commons-logging.jar, + * commons-logging-api.jar, and + * commons-logging-tests.jar files resulting from the + * execution of the compile.tests target.
  • + *
  • wrapper.testcase - Fully qualified Java class name + * of a TestCase that will ultimately be executed. This class must + * exist in the commons-logging-tests.jar file.
  • + *
+ * + *

When executed, the system classpath for the wrapper should include + * only the wrapper class itself.

+ * + * @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); + + } + + } + + + +} diff --git a/src/test/org/apache/commons/logging/jdk14/CustomConfigTestCase.java b/src/test/org/apache/commons/logging/jdk14/CustomConfigTestCase.java index 2490a4a..4e95a24 100644 --- a/src/test/org/apache/commons/logging/jdk14/CustomConfigTestCase.java +++ b/src/test/org/apache/commons/logging/jdk14/CustomConfigTestCase.java @@ -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 $ - * $Revision: 1.1 $ - * $Date: 2003/03/29 22:04:54 $ + * $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.2 $ + * $Date: 2003/03/30 02:30:37 $ * * ==================================================================== * @@ -84,7 +84,7 @@ import org.apache.commons.logging.LogFactory; * logger configured per the configuration properties.

* * @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 { @@ -93,6 +93,14 @@ public class CustomConfigTestCase extends DefaultConfigTestCase { // ----------------------------------------------------------- Constructors + /** + *

Construct a new instance of this test case.

+ */ + public CustomConfigTestCase() { + super(); + } + + /** *

Construct a new instance of this test case.

* @@ -181,27 +189,20 @@ public class CustomConfigTestCase extends DefaultConfigTestCase { // ----------------------------------------------------------- Test Methods + // Test logging message strings with exceptions + public void testExceptionMessages() throws Exception { + + logExceptionMessages(); + checkLogRecords(true); + + } + + // 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(); + checkLogRecords(false); } @@ -253,6 +254,53 @@ public class CustomConfigTestCase extends DefaultConfigTestCase { // -------------------------------------------------------- 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 protected void logPlainMessages() { log.trace("trace"); // Should not actually get logged diff --git a/src/test/org/apache/commons/logging/jdk14/DefaultConfigTestCase.java b/src/test/org/apache/commons/logging/jdk14/DefaultConfigTestCase.java index e00cc78..dc9f602 100644 --- a/src/test/org/apache/commons/logging/jdk14/DefaultConfigTestCase.java +++ b/src/test/org/apache/commons/logging/jdk14/DefaultConfigTestCase.java @@ -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 $ - * $Revision: 1.1 $ - * $Date: 2003/03/29 22:04:54 $ + * $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.2 $ + * $Date: 2003/03/30 02:30:37 $ * * ==================================================================== * @@ -76,7 +76,7 @@ import org.apache.commons.logging.LogFactory; * should be automatically configured.

* * @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 { @@ -85,6 +85,14 @@ public class DefaultConfigTestCase extends TestCase { // ----------------------------------------------------------- Constructors + /** + *

Construct a new instance of this test case.

+ */ + public DefaultConfigTestCase() { + super(); + } + + /** *

Construct a new instance of this test case.

*