KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > tests > TestProperties


1 package JSci.tests;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Map JavaDoc;
6 import junit.framework.Test;
7 import junit.framework.TestResult;
8 import junitx.extensions.TestSetup;
9
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 JavaDoc PROPERTIES = new ThreadLocal JavaDoc() {
37                 protected Object JavaDoc initialValue() {
38                         return new HashMap JavaDoc();
39                 }
40         };
41         /**
42          * Returns the properties for this fixture.
43          */

44         public static Map JavaDoc getProperties() {
45                 return (Map JavaDoc) PROPERTIES.get();
46         }
47
48         private final Map JavaDoc properties;
49         /**
50          * @param properties the properties to be made available at <code>setUp()</code> time.
51          */

52         public TestProperties(Test test, Map JavaDoc properties) {
53                 super(test);
54                 this.properties = properties;
55         }
56         /**
57          * Adds <code>this</code> properties to the fixture properties.
58          */

59         protected void setUp() throws Exception JavaDoc {
60                 getProperties().putAll(properties);
61         }
62         /**
63          * Removes <code>this</code> properties from the fixture properties.
64          */

65         protected void tearDown() throws Exception JavaDoc {
66                 final Map JavaDoc threadLocalProperties = getProperties();
67                 for(Iterator JavaDoc iter = properties.keySet().iterator(); iter.hasNext(); ) {
68                         threadLocalProperties.remove(iter.next());
69                 }
70         }
71 }
72
73
Popular Tags