diff --git a/src/test/org/apache/commons/logging/simple/CustomConfigTestCase.java b/src/test/org/apache/commons/logging/simple/CustomConfigTestCase.java index 367d368..a826d1e 100644 --- a/src/test/org/apache/commons/logging/simple/CustomConfigTestCase.java +++ b/src/test/org/apache/commons/logging/simple/CustomConfigTestCase.java @@ -25,6 +25,8 @@ import junit.framework.Test; import junit.framework.TestSuite; 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; @@ -93,9 +95,26 @@ public class CustomConfigTestCase extends DefaultConfigTestCase { /** * Return the tests included in this test suite. + *

+ * 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() { - return (new TestSuite(CustomConfigTestCase.class)); + public static Test suite() throws Exception { + 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); } /** diff --git a/src/test/org/apache/commons/logging/simple/DateTimeCustomConfigTestCase.java b/src/test/org/apache/commons/logging/simple/DateTimeCustomConfigTestCase.java index 6578559..b74b925 100644 --- a/src/test/org/apache/commons/logging/simple/DateTimeCustomConfigTestCase.java +++ b/src/test/org/apache/commons/logging/simple/DateTimeCustomConfigTestCase.java @@ -16,6 +16,7 @@ package org.apache.commons.logging.simple; +import java.util.ArrayList; import java.util.Date; import java.text.SimpleDateFormat; import java.text.DateFormat; @@ -23,6 +24,10 @@ import java.text.DateFormat; import junit.framework.Test; 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 @@ -33,21 +38,53 @@ public class DateTimeCustomConfigTestCase extends CustomConfigTestCase { /** * Return the tests included in this test suite. + *

+ * 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() { - return (new TestSuite(DateTimeCustomConfigTestCase.class)); + public static Test suite() throws Exception { + 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); } /** - *

Construct a new instance of this test case.

- * - * @param name Name of the test case + * Set up system properties required by this unit test. Here, we + * set up the props defined in the parent class setProperties method, + * and add a few to configure the SimpleLog class date/time output. */ - public DateTimeCustomConfigTestCase(String name) { - super(name); + public void setProperties() { + 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 /** 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)); } - /** Hook for subclassses */ + /** Hook for subclassses */ protected void checkShowDateTime() { assertTrue(((DecoratedSimpleLog) log).getShowDateTime()); } diff --git a/src/test/org/apache/commons/logging/simple/DefaultConfigTestCase.java b/src/test/org/apache/commons/logging/simple/DefaultConfigTestCase.java index b0d4e81..b49c11c 100644 --- a/src/test/org/apache/commons/logging/simple/DefaultConfigTestCase.java +++ b/src/test/org/apache/commons/logging/simple/DefaultConfigTestCase.java @@ -28,6 +28,8 @@ import junit.framework.TestSuite; import org.apache.commons.logging.Log; 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; @@ -60,23 +62,51 @@ public class DefaultConfigTestCase extends TestCase { // ------------------------------------------- JUnit Infrastructure Methods + /** + * Return the tests included in this test suite. + *

+ * 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. */ public void setUp() throws Exception { LogFactory.releaseAll(); + setProperties(); setUpFactory(); 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. */