1
0

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
This commit is contained in:
Simon Kitching
2005-07-21 06:07:30 +00:00
parent 72a6e4f9f6
commit b1d6285f15
6 changed files with 21 additions and 127 deletions

View File

@@ -54,31 +54,6 @@ public abstract class StandardTests extends TestCase {
public String level; public String level;
public Throwable throwable; public Throwable throwable;
} }
/**
* Simple helper class that can configure log4j to redirect all logged
* messages into a list of LogEvent messages.
* <p>
* 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).
* <p>
* 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.
* <p>
* See also method getTestHelperClassName.
*/
public static interface TestHelper {
public void forwardMessages(List logEvents);
}
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// JUnit Infrastructure Methods // JUnit Infrastructure Methods
@@ -102,8 +77,15 @@ public abstract class StandardTests extends TestCase {
// abstract methods // abstract methods
// ----------------------------------------------------------- // -----------------------------------------------------------
protected abstract String getTestHelperClassName(); /**
* Modify log4j's setup so that all messages actually logged get redirected
* into the specified list.
* <p>
* 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 // ----------------------------------------------------------- Test Methods
/** /**
@@ -168,20 +150,6 @@ public abstract class StandardTests extends TestCase {
// -------------------------------------------------------- Support Methods // -------------------------------------------------------- Support Methods
/**
* Modify log4j's setup so that all messages actually logged get redirected
* into the specified list.
* <p>
* 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 * Verify that the TestAppender has received the expected
* number of messages. This assumes that: * number of messages. This assumes that:

View File

@@ -29,14 +29,12 @@ import org.apache.commons.logging.log4j.StandardTests;
* the parent classpath and commons-logging.jar is in the child. * 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. * Return the tests included in this test suite.
*/ */
public static Test suite() throws Exception { public static Test suite() throws Exception {
Class thisClass = ApiClasspathStandardTestCase.class;
PathableClassLoader parent = new PathableClassLoader(null); PathableClassLoader parent = new PathableClassLoader(null);
parent.useSystemLoader("junit."); parent.useSystemLoader("junit.");
parent.addLogicalLib("commons-logging-api"); parent.addLogicalLib("commons-logging-api");
@@ -46,15 +44,8 @@ public class ApiClasspathStandardTestCase extends StandardTests {
child.addLogicalLib("commons-logging"); child.addLogicalLib("commons-logging");
child.addLogicalLib("testclasses"); 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 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";
}
} }

View File

@@ -27,29 +27,20 @@ import org.apache.commons.logging.log4j.StandardTests;
* is in it, as would be the situation for a standalone application. * 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. * Return the tests included in this test suite.
*/ */
public static Test suite() throws Exception { public static Test suite() throws Exception {
Class thisClass = AppClasspathStandardTestCase.class;
PathableClassLoader loader = new PathableClassLoader(null); PathableClassLoader loader = new PathableClassLoader(null);
loader.useSystemLoader("junit."); loader.useSystemLoader("junit.");
loader.addLogicalLib("testclasses"); loader.addLogicalLib("testclasses");
loader.addLogicalLib("log4j12"); loader.addLogicalLib("log4j12");
loader.addLogicalLib("commons-logging"); 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 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";
}
} }

View File

@@ -28,14 +28,12 @@ import org.apache.commons.logging.log4j.StandardTests;
* a container where all the necessary libs are in the child. * 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. * Return the tests included in this test suite.
*/ */
public static Test suite() throws Exception { public static Test suite() throws Exception {
Class thisClass = ChildClasspathStandardTestCase.class;
PathableClassLoader parent = new PathableClassLoader(null); PathableClassLoader parent = new PathableClassLoader(null);
parent.useSystemLoader("junit."); parent.useSystemLoader("junit.");
@@ -44,15 +42,8 @@ public class ChildClasspathStandardTestCase extends StandardTests {
child.addLogicalLib("log4j12"); child.addLogicalLib("log4j12");
child.addLogicalLib("commons-logging"); 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 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";
}
} }

View File

@@ -27,14 +27,12 @@ import org.apache.commons.logging.log4j.StandardTests;
* a container where all the necessary libs are in the parent. * 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. * Return the tests included in this test suite.
*/ */
public static Test suite() throws Exception { public static Test suite() throws Exception {
Class thisClass = ParentClasspathStandardTestCase.class;
PathableClassLoader parent = new PathableClassLoader(null); PathableClassLoader parent = new PathableClassLoader(null);
parent.useSystemLoader("junit."); parent.useSystemLoader("junit.");
parent.addLogicalLib("commons-logging"); parent.addLogicalLib("commons-logging");
@@ -43,15 +41,8 @@ public class ParentClasspathStandardTestCase extends StandardTests {
PathableClassLoader child = new PathableClassLoader(parent); PathableClassLoader child = new PathableClassLoader(parent);
child.addLogicalLib("testclasses"); 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 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";
}
} }

View File

@@ -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);
}
}