1
0

Save and restore system properties around tests.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/logging/trunk@209412 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Simon Kitching
2005-07-06 06:19:32 +00:00
parent 1996c5af1b
commit 4a0c892957

View File

@@ -16,6 +16,7 @@
package org.apache.commons.logging; package org.apache.commons.logging;
import java.util.Properties;
import junit.framework.Test; import junit.framework.Test;
import junit.framework.TestSuite; import junit.framework.TestSuite;
import junit.framework.TestResult; import junit.framework.TestResult;
@@ -66,7 +67,9 @@ import junit.framework.TestResult;
* </pre> * </pre>
* This class ensures that any context classloader changes applied by a test * This class ensures that any context classloader changes applied by a test
* is undone after the test is run, so tests don't need to worry about * is undone after the test is run, so tests don't need to worry about
* restoring the context classloader on exit. * restoring the context classloader on exit. This class also ensures that
* the system properties are restored to their original settings after each
* test, so tests that manipulate those don't need to worry about resetting them.
* <p> * <p>
* This class does not provide facilities for manipulating system properties; * This class does not provide facilities for manipulating system properties;
* tests that need specific system properties can simply set them in the * tests that need specific system properties can simply set them in the
@@ -114,13 +117,18 @@ public class PathableTestSuite extends TestSuite {
* This method is invoked once for each Test in the current TestSuite. * This method is invoked once for each Test in the current TestSuite.
* Note that a Test may itself be a TestSuite object (ie a collection * Note that a Test may itself be a TestSuite object (ie a collection
* of tests). * of tests).
* <p>
* The context classloader and system properties are saved before each
* test, and restored after the test completes to better isolate tests.
*/ */
public void runTest(Test test, TestResult result) { public void runTest(Test test, TestResult result) {
ClassLoader origContext = Thread.currentThread().getContextClassLoader(); ClassLoader origContext = Thread.currentThread().getContextClassLoader();
Properties oldSysProps = (Properties) System.getProperties().clone();
try { try {
Thread.currentThread().setContextClassLoader(contextLoader); Thread.currentThread().setContextClassLoader(contextLoader);
test.run(result); test.run(result);
} finally { } finally {
System.setProperties(oldSysProps);
Thread.currentThread().setContextClassLoader(origContext); Thread.currentThread().setContextClassLoader(origContext);
} }
} }