01: package JSci.tests;
02:
03: import java.util.Iterator;
04: import java.util.HashMap;
05: import java.util.Map;
06: import junit.framework.Test;
07: import junit.framework.TestResult;
08: import junitx.extensions.TestSetup;
09:
10: /**
11: * A TestSetup which provides additional fixture state in the form of thread-local properties.
12: * A TestCase can use these properties in its setUp() method to customize its fixture.
13: * <pre>
14: * public static Test suite() {
15: * TestSuite suite = new TestSuite();
16: * Map test1Properties = new HashMap();
17: * test1Properties.put("test.object", new MyFirstTestObject());
18: * suite.addTest(new TestProperties(new TestSuite(MyTestCase.class), test1Properties));
19: *
20: * Map test2Properties = new HashMap();
21: * test2Properties.put("test.object", new MySecondTestObject());
22: * suite.addTest(new TestProperties(new TestSuite(MyTestCase.class), test2Properties));
23: * return suite;
24: * }
25: *
26: * protected void setUp() throws Exception {
27: * Map properties = TestProperties.getProperties();
28: * Object obj = properties.get("test.object");
29: * // do fixture setup with obj
30: * }
31: * </pre>
32: * @author Mark Hale
33: */
34: public class TestProperties extends TestSetup {
35: /** Thread-local properties. */
36: private static final ThreadLocal PROPERTIES = new ThreadLocal() {
37: protected Object initialValue() {
38: return new HashMap();
39: }
40: };
41:
42: /**
43: * Returns the properties for this fixture.
44: */
45: public static Map getProperties() {
46: return (Map) PROPERTIES.get();
47: }
48:
49: private final Map properties;
50:
51: /**
52: * @param properties the properties to be made available at <code>setUp()</code> time.
53: */
54: public TestProperties(Test test, Map properties) {
55: super (test);
56: this .properties = properties;
57: }
58:
59: /**
60: * Adds <code>this</code> properties to the fixture properties.
61: */
62: protected void setUp() throws Exception {
63: getProperties().putAll(properties);
64: }
65:
66: /**
67: * Removes <code>this</code> properties from the fixture properties.
68: */
69: protected void tearDown() throws Exception {
70: final Map threadLocalProperties = getProperties();
71: for (Iterator iter = properties.keySet().iterator(); iter
72: .hasNext();) {
73: threadLocalProperties.remove(iter.next());
74: }
75: }
76: }
|