1
0

Use custom classloader setups to work around SimpleLog static insanity.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/logging/trunk@209452 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Simon Kitching
2005-07-06 12:37:25 +00:00
parent 2649aa8c6d
commit 4c8d747d37
3 changed files with 105 additions and 19 deletions

View File

@@ -25,6 +25,8 @@ import junit.framework.Test;
import junit.framework.TestSuite; import junit.framework.TestSuite;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.PathableClassLoader;
import org.apache.commons.logging.PathableTestSuite;
import org.apache.commons.logging.impl.SimpleLog; import org.apache.commons.logging.impl.SimpleLog;
@@ -93,9 +95,26 @@ public class CustomConfigTestCase extends DefaultConfigTestCase {
/** /**
* Return the tests included in this test suite. * Return the tests included in this test suite.
* <p>
* We need to use a PathableClassLoader here because the SimpleLog class
* is a pile of junk and chock-full of static variables. Any other test
* (like simple.CustomConfigTestCase) that has used the SimpleLog class
* will already have caused it to do once-only initialisation that we
* can't reset, even by calling LogFactory.releaseAll, because of those
* ugly statics. The only clean solution is to load a clean copy of
* commons-logging including SimpleLog via a nice clean classloader.
* Or we could fix SimpleLog to be sane...
*/ */
public static Test suite() { public static Test suite() throws Exception {
return (new TestSuite(CustomConfigTestCase.class)); Class thisClass = CustomConfigTestCase.class;
PathableClassLoader loader = new PathableClassLoader(null);
loader.useSystemLoader("junit.");
loader.addLogicalLib("testclasses");
loader.addLogicalLib("commons-logging");
Class testClass = loader.loadClass(thisClass.getName());
return new PathableTestSuite(testClass, loader);
} }
/** /**

View File

@@ -16,6 +16,7 @@
package org.apache.commons.logging.simple; package org.apache.commons.logging.simple;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.text.DateFormat; import java.text.DateFormat;
@@ -23,6 +24,10 @@ import java.text.DateFormat;
import junit.framework.Test; import junit.framework.Test;
import junit.framework.TestSuite; import junit.framework.TestSuite;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.PathableTestSuite;
import org.apache.commons.logging.PathableClassLoader;
/** /**
* Tests custom date time format configuration * Tests custom date time format configuration
@@ -33,21 +38,53 @@ public class DateTimeCustomConfigTestCase extends CustomConfigTestCase {
/** /**
* Return the tests included in this test suite. * Return the tests included in this test suite.
* <p>
* We need to use a PathableClassLoader here because the SimpleLog class
* is a pile of junk and chock-full of static variables. Any other test
* (like simple.CustomConfigTestCase) that has used the SimpleLog class
* will already have caused it to do once-only initialisation that we
* can't reset, even by calling LogFactory.releaseAll, because of those
* ugly statics. The only clean solution is to load a clean copy of
* commons-logging including SimpleLog via a nice clean classloader.
* Or we could fix SimpleLog to be sane...
*/ */
public static Test suite() { public static Test suite() throws Exception {
return (new TestSuite(DateTimeCustomConfigTestCase.class)); Class thisClass = DateTimeCustomConfigTestCase.class;
PathableClassLoader loader = new PathableClassLoader(null);
loader.useSystemLoader("junit.");
loader.addLogicalLib("testclasses");
loader.addLogicalLib("commons-logging");
Class testClass = loader.loadClass(thisClass.getName());
return new PathableTestSuite(testClass, loader);
} }
/** /**
* <p>Construct a new instance of this test case.</p> * Set up system properties required by this unit test. Here, we
* * set up the props defined in the parent class setProperties method,
* @param name Name of the test case * and add a few to configure the SimpleLog class date/time output.
*/ */
public DateTimeCustomConfigTestCase(String name) { public void setProperties() {
super(name); super.setProperties();
System.setProperty(
"org.apache.commons.logging.simplelog.dateTimeFormat",
"dd.mm.yyyy");
System.setProperty(
"org.apache.commons.logging.simplelog.showdatetime",
"true");
} }
/**
* Set up instance variables required by this test case.
*/
public void setUp() throws Exception {
super.setUp();
}
// ----------------------------------------------------------- Methods // ----------------------------------------------------------- Methods
/** Checks that the date time format has been successfully set */ /** Checks that the date time format has been successfully set */
@@ -62,7 +99,7 @@ public class DateTimeCustomConfigTestCase extends CustomConfigTestCase {
assertEquals("Date should be formatters to pattern dd.mm.yyyy", sampleFormatter.format(now), formatter.format(now)); assertEquals("Date should be formatters to pattern dd.mm.yyyy", sampleFormatter.format(now), formatter.format(now));
} }
/** Hook for subclassses */ /** Hook for subclassses */
protected void checkShowDateTime() { protected void checkShowDateTime() {
assertTrue(((DecoratedSimpleLog) log).getShowDateTime()); assertTrue(((DecoratedSimpleLog) log).getShowDateTime());
} }

View File

@@ -28,6 +28,8 @@ import junit.framework.TestSuite;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.PathableClassLoader;
import org.apache.commons.logging.PathableTestSuite;
import org.apache.commons.logging.impl.SimpleLog; import org.apache.commons.logging.impl.SimpleLog;
@@ -60,23 +62,51 @@ public class DefaultConfigTestCase extends TestCase {
// ------------------------------------------- JUnit Infrastructure Methods // ------------------------------------------- JUnit Infrastructure Methods
/**
* Return the tests included in this test suite.
* <p>
* We need to use a PathableClassLoader here because the SimpleLog class
* is a pile of junk and chock-full of static variables. Any other test
* (like simple.CustomConfigTestCase) that has used the SimpleLog class
* will already have caused it to do once-only initialisation that we
* can't reset, even by calling LogFactory.releaseAll, because of those
* ugly statics. The only clean solution is to load a clean copy of
* commons-logging including SimpleLog via a nice clean classloader.
* Or we could fix SimpleLog to be sane...
*/
public static Test suite() throws Exception {
Class thisClass = DefaultConfigTestCase.class;
PathableClassLoader loader = new PathableClassLoader(null);
loader.useSystemLoader("junit.");
loader.addLogicalLib("testclasses");
loader.addLogicalLib("commons-logging");
Class testClass = loader.loadClass(thisClass.getName());
return new PathableTestSuite(testClass, loader);
}
/**
* Set system properties that will control the LogFactory/Log objects
* when they are created. Subclasses can override this method to
* define properties that suit them.
*/
public void setProperties() {
System.setProperty(
"org.apache.commons.logging.Log",
"org.apache.commons.logging.impl.SimpleLog");
}
/** /**
* Set up instance variables required by this test case. * Set up instance variables required by this test case.
*/ */
public void setUp() throws Exception { public void setUp() throws Exception {
LogFactory.releaseAll(); LogFactory.releaseAll();
setProperties();
setUpFactory(); setUpFactory();
setUpLog("TestLogger"); setUpLog("TestLogger");
} }
/**
* Return the tests included in this test suite.
*/
public static Test suite() {
return (new TestSuite(DefaultConfigTestCase.class));
}
/** /**
* Tear down instance variables required by this test case. * Tear down instance variables required by this test case.
*/ */