01: package junit.extensions.abbot;
02:
03: import junit.framework.TestCase;
04:
05: import abbot.Log;
06: import abbot.script.*;
07: import abbot.util.AWTFixtureHelper;
08: import abbot.finder.*;
09:
10: /** Simple wrapper for a test script to run under JUnit. If the script
11: * does not contain a launch step, the hierarchy used will include existing
12: * components. No automatic cleanup of components is performed, since it is
13: * assumed that a Terminate step within the script will trigger that operation
14: * if it is required.<p>
15: */
16: public class ScriptFixture extends TestCase {
17:
18: private static AWTFixtureHelper oldContext = null;
19: private static final Hierarchy DUMMY_HIERARCHY = new AWTHierarchy();
20: private StepRunner runner;
21:
22: /** Construct a test case with the given name, which <i>must</i> be the
23: * filename of the script to run.
24: */
25: public ScriptFixture(String filename) {
26: // It is essential that the name be passed to super() unmodified, or
27: // the JUnit GUI will consider it a different test.
28: super (filename);
29: }
30:
31: /** Saves the current UI state for restoration when the
32: fixture (if any) is terminated. Also sets up a
33: {@link TestHierarchy} for the duration of the test.
34: */
35: protected void setUp() throws Exception {
36: if (oldContext == null) {
37: oldContext = new AWTFixtureHelper();
38: }
39: runner = new StepRunner(oldContext);
40: // Support for deprecated ComponentTester.assertFrameShowing usage
41: // only. Eventually this will go away.
42: AWTHierarchy.setDefault(runner.getHierarchy());
43: }
44:
45: protected void tearDown() throws Exception {
46: AWTHierarchy.setDefault(null);
47: runner = null;
48: }
49:
50: /** Override the default TestCase runTest method to invoke the script.
51: The {@link Script} is created and a default {@link StepRunner} is used
52: to run it.
53: @see junit.framework.TestCase#runTest
54: */
55: protected void runTest() throws Throwable {
56: Script script = new Script(getName(), DUMMY_HIERARCHY);
57: Log.log("Running " + script + " with " + getClass());
58:
59: try {
60: runner.run(script);
61: } finally {
62: Log.log(script.toString() + " finished");
63: }
64: }
65:
66: /** Assumes each argument is an Abbot script. Runs each one. */
67: public static void main(String[] args) {
68: ScriptTestSuite.main(args);
69: }
70: }
|