01: package com.clarkware.junitperf;
02:
03: import junit.framework.Test;
04: import junit.framework.TestCase;
05: import junit.framework.TestSuite;
06:
07: /**
08: * The <code>ExampleStatefulTestCase</code> is an example
09: * stateful <code>TestCase</code>.
10: * <p>
11: * If the <code>testState()</code> test is run without a
12: * <code>TestMethodFactory</code>, then all threads of a
13: * <code>LoadTest</code> will share the objects in the
14: * test fixture and may cause some tests to fail.
15: * <p>
16: * To ensure that each thread running the test method has a
17: * thread-local test fixture, use:
18: * <blockquote>
19: * <pre>
20: * Test factory =
21: * new TestMethodFactory(ExampleStatefulTestCase.class, "testState");
22: * LoadTest test = new LoadTest(factory, numberOfUsers, ...);
23: * ...
24: * </pre>
25: * </blockquote>
26: * </p>
27: *
28: * @author <b>Mike Clark</b>
29: * @author Clarkware Consulting, Inc.
30: */
31:
32: public class ExampleStatefulTestCase extends TestCase {
33:
34: private boolean _flag;
35: private int _data;
36:
37: public ExampleStatefulTestCase(String name) {
38: super (name);
39: }
40:
41: protected void setUp() {
42: _flag = true;
43: _data = 1;
44: }
45:
46: protected void tearDown() {
47: _flag = false;
48: _data = 0;
49: }
50:
51: /**
52: * This test may fail in a <code>LoadTest</code> if run
53: * without a <code>TestMethodFactory</code>.
54: */
55: public void testState() throws Exception {
56: assertEquals(true, _flag);
57: Thread.yield();
58: assertEquals(1, _data);
59: }
60:
61: public static Test suite() {
62: return new TestSuite(ExampleStatefulTestCase.class);
63: }
64:
65: public static void main(String args[]) {
66: junit.textui.TestRunner.run(suite());
67: }
68: }
|