From b1d6285f152f804bf5df469f9b854edd8cbb3143 Mon Sep 17 00:00:00 2001 From: Simon Kitching Date: Thu, 21 Jul 2005 06:07:30 +0000 Subject: [PATCH] Implement a cleaner mechanism for setting up a test suite for classes whose dependent libs are not present in the system classpath. The class containing the suite() method *does not have to be* the class that contains the test methods. By taking advantage of this, we can avoid the reflection stuff and the trivial helper class that was introduced earlier to solve this same problem. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/logging/trunk@220001 13f79535-47bb-0310-9956-ffa450edef68 --- .../commons/logging/log4j/StandardTests.java | 50 ++++--------------- .../log4j12/ApiClasspathStandardTestCase.java | 15 ++---- .../log4j12/AppClasspathStandardTestCase.java | 15 ++---- .../ChildClasspathStandardTestCase.java | 15 ++---- .../ParentClasspathStandardTestCase.java | 15 ++---- .../logging/log4j/log4j12/TestHelper.java | 38 -------------- 6 files changed, 21 insertions(+), 127 deletions(-) delete mode 100644 src/test/org/apache/commons/logging/log4j/log4j12/TestHelper.java diff --git a/src/test/org/apache/commons/logging/log4j/StandardTests.java b/src/test/org/apache/commons/logging/log4j/StandardTests.java index 9456393..a35fe67 100644 --- a/src/test/org/apache/commons/logging/log4j/StandardTests.java +++ b/src/test/org/apache/commons/logging/log4j/StandardTests.java @@ -54,31 +54,6 @@ public abstract class StandardTests extends TestCase { public String level; public Throwable throwable; } - - /** - * Simple helper class that can configure log4j to redirect all logged - * messages into a list of LogEvent messages. - *

- * The TestCase classes that junit will run later have two roles: they - * hold the tests to run, and they also provide the suite() method that - * indicates which tests to run. This causes complications for us in the - * case of log4j because of the binary-incompatible log4j versions. We - * can't have any version of log4j to be in the classpath until we are - * actually running the tests returned by suite() - but junit can't load - * the class to call suite() on it if the class or any of its ancestors - * have direct references to log4j APIs (or NoClassDefFound occurs). - *

- * The answer is to move all the direct log4j calls out of the TestCase - * classes into a helper which is only loaded via reflection during the - * test runs (and not during calls to suite()). This class defines the - * interface required of that helper. - *

- * See also method getTestHelperClassName. - */ - - public static interface TestHelper { - public void forwardMessages(List logEvents); - } // ------------------------------------------------------------------- // JUnit Infrastructure Methods @@ -102,8 +77,15 @@ public abstract class StandardTests extends TestCase { // abstract methods // ----------------------------------------------------------- - protected abstract String getTestHelperClassName(); - + /** + * Modify log4j's setup so that all messages actually logged get redirected + * into the specified list. + *

+ * This method also sets the logging level to INFO so that we + * can test whether messages are getting properly filtered. + */ + public abstract void setUpTestAppender(List logEvents) throws Exception; + // ----------------------------------------------------------- Test Methods /** @@ -168,20 +150,6 @@ public abstract class StandardTests extends TestCase { // -------------------------------------------------------- Support Methods - /** - * Modify log4j's setup so that all messages actually logged get redirected - * into the specified list. - *

- * This method also sets the logging level to INFO so that we - * can test whether messages are getting properly filtered. - */ - private void setUpTestAppender(List logEvents) throws Exception { - String testHelperClassName = getTestHelperClassName(); - Class clazz = this.getClass().getClassLoader().loadClass(testHelperClassName); - TestHelper testHelper = (TestHelper) clazz.newInstance(); - testHelper.forwardMessages(logEvents); - } - /** * Verify that the TestAppender has received the expected * number of messages. This assumes that: diff --git a/src/test/org/apache/commons/logging/log4j/log4j12/ApiClasspathStandardTestCase.java b/src/test/org/apache/commons/logging/log4j/log4j12/ApiClasspathStandardTestCase.java index e182e95..248148c 100644 --- a/src/test/org/apache/commons/logging/log4j/log4j12/ApiClasspathStandardTestCase.java +++ b/src/test/org/apache/commons/logging/log4j/log4j12/ApiClasspathStandardTestCase.java @@ -29,14 +29,12 @@ import org.apache.commons.logging.log4j.StandardTests; * the parent classpath and commons-logging.jar is in the child. */ -public class ApiClasspathStandardTestCase extends StandardTests { +public class ApiClasspathStandardTestCase { /** * Return the tests included in this test suite. */ public static Test suite() throws Exception { - Class thisClass = ApiClasspathStandardTestCase.class; - PathableClassLoader parent = new PathableClassLoader(null); parent.useSystemLoader("junit."); parent.addLogicalLib("commons-logging-api"); @@ -46,15 +44,8 @@ public class ApiClasspathStandardTestCase extends StandardTests { child.addLogicalLib("commons-logging"); child.addLogicalLib("testclasses"); - Class testClass = child.loadClass(thisClass.getName()); + Class testClass = child.loadClass( + "org.apache.commons.logging.log4j.log4j12.Log4j12StandardTests"); return new PathableTestSuite(testClass, child); } - - /** - * Return the name of a class that makes all direct calls to log4j - * apis. See StandardTests.TestHelper for more information. - */ - public String getTestHelperClassName() { - return "org.apache.commons.logging.log4j.log4j12.TestHelper"; - } } diff --git a/src/test/org/apache/commons/logging/log4j/log4j12/AppClasspathStandardTestCase.java b/src/test/org/apache/commons/logging/log4j/log4j12/AppClasspathStandardTestCase.java index ffed12d..ef49eab 100644 --- a/src/test/org/apache/commons/logging/log4j/log4j12/AppClasspathStandardTestCase.java +++ b/src/test/org/apache/commons/logging/log4j/log4j12/AppClasspathStandardTestCase.java @@ -27,29 +27,20 @@ import org.apache.commons.logging.log4j.StandardTests; * is in it, as would be the situation for a standalone application. */ -public class AppClasspathStandardTestCase extends StandardTests { +public class AppClasspathStandardTestCase { /** * Return the tests included in this test suite. */ public static Test suite() throws Exception { - Class thisClass = AppClasspathStandardTestCase.class; - PathableClassLoader loader = new PathableClassLoader(null); loader.useSystemLoader("junit."); loader.addLogicalLib("testclasses"); loader.addLogicalLib("log4j12"); loader.addLogicalLib("commons-logging"); - Class testClass = loader.loadClass(thisClass.getName()); + Class testClass = loader.loadClass( + "org.apache.commons.logging.log4j.log4j12.Log4j12StandardTests"); return new PathableTestSuite(testClass, loader); } - - /** - * Return the name of a class that makes all direct calls to log4j - * apis. See StandardTests.TestHelper for more information. - */ - public String getTestHelperClassName() { - return "org.apache.commons.logging.log4j.log4j12.TestHelper"; - } } diff --git a/src/test/org/apache/commons/logging/log4j/log4j12/ChildClasspathStandardTestCase.java b/src/test/org/apache/commons/logging/log4j/log4j12/ChildClasspathStandardTestCase.java index d9abb2a..040d708 100644 --- a/src/test/org/apache/commons/logging/log4j/log4j12/ChildClasspathStandardTestCase.java +++ b/src/test/org/apache/commons/logging/log4j/log4j12/ChildClasspathStandardTestCase.java @@ -28,14 +28,12 @@ import org.apache.commons.logging.log4j.StandardTests; * a container where all the necessary libs are in the child. */ -public class ChildClasspathStandardTestCase extends StandardTests { +public class ChildClasspathStandardTestCase { /** * Return the tests included in this test suite. */ public static Test suite() throws Exception { - Class thisClass = ChildClasspathStandardTestCase.class; - PathableClassLoader parent = new PathableClassLoader(null); parent.useSystemLoader("junit."); @@ -44,15 +42,8 @@ public class ChildClasspathStandardTestCase extends StandardTests { child.addLogicalLib("log4j12"); child.addLogicalLib("commons-logging"); - Class testClass = child.loadClass(thisClass.getName()); + Class testClass = child.loadClass( + "org.apache.commons.logging.log4j.log4j12.Log4j12StandardTests"); return new PathableTestSuite(testClass, child); } - - /** - * Return the name of a class that makes all direct calls to log4j - * apis. See StandardTests.TestHelper for more information. - */ - public String getTestHelperClassName() { - return "org.apache.commons.logging.log4j.log4j12.TestHelper"; - } } diff --git a/src/test/org/apache/commons/logging/log4j/log4j12/ParentClasspathStandardTestCase.java b/src/test/org/apache/commons/logging/log4j/log4j12/ParentClasspathStandardTestCase.java index aaa4e9c..d5c9e41 100644 --- a/src/test/org/apache/commons/logging/log4j/log4j12/ParentClasspathStandardTestCase.java +++ b/src/test/org/apache/commons/logging/log4j/log4j12/ParentClasspathStandardTestCase.java @@ -27,14 +27,12 @@ import org.apache.commons.logging.log4j.StandardTests; * a container where all the necessary libs are in the parent. */ -public class ParentClasspathStandardTestCase extends StandardTests { +public class ParentClasspathStandardTestCase { /** * Return the tests included in this test suite. */ public static Test suite() throws Exception { - Class thisClass = ParentClasspathStandardTestCase.class; - PathableClassLoader parent = new PathableClassLoader(null); parent.useSystemLoader("junit."); parent.addLogicalLib("commons-logging"); @@ -43,15 +41,8 @@ public class ParentClasspathStandardTestCase extends StandardTests { PathableClassLoader child = new PathableClassLoader(parent); child.addLogicalLib("testclasses"); - Class testClass = child.loadClass(thisClass.getName()); + Class testClass = child.loadClass( + "org.apache.commons.logging.log4j.log4j12.Log4j12StandardTests"); return new PathableTestSuite(testClass, child); } - - /** - * Return the name of a class that makes all direct calls to log4j - * apis. See StandardTests.TestHelper for more information. - */ - public String getTestHelperClassName() { - return "org.apache.commons.logging.log4j.log4j12.TestHelper"; - } } diff --git a/src/test/org/apache/commons/logging/log4j/log4j12/TestHelper.java b/src/test/org/apache/commons/logging/log4j/log4j12/TestHelper.java deleted file mode 100644 index b0cb253..0000000 --- a/src/test/org/apache/commons/logging/log4j/log4j12/TestHelper.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2005 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.commons.logging.log4j.log4j12; - -import java.util.List; - -import org.apache.log4j.Logger; -import org.apache.log4j.Level; -import org.apache.commons.logging.log4j.StandardTests; - -/** - * See StandardTests.TestHelper for information on this class. - */ - -public class TestHelper implements StandardTests.TestHelper { - - public void forwardMessages(List logEvents) { - TestAppender appender = new TestAppender(logEvents); - Logger rootLogger = Logger.getRootLogger(); - rootLogger.removeAllAppenders(); - rootLogger.addAppender(appender); - rootLogger.setLevel(Level.INFO); - } -}