01: package abbot.script;
02:
03: import java.util.Map;
04: import abbot.Log;
05: import abbot.finder.Hierarchy;
06: import abbot.i18n.Strings;
07: import abbot.util.SystemState;
08:
09: /** Provides a method of defining a single script as the UI application test
10: * context for multiple scripts. A script which uses a fixture step (as
11: * opposed to an explicit launch) will only instantiate the fixture if it does
12: * not already exist.<p>
13: * A Fixture will only be run once for any consecutive group of
14: * <code>Script</code>s that refer to it. The {@link StepRunner} class is
15: * normally used to control execution and will manage fixture setup/teardown
16: * as needed.
17: */
18: public class Fixture extends Script implements UIContext {
19:
20: private static Fixture currentFixture = null;
21:
22: public Fixture(String filename, Hierarchy h) {
23: super (filename, h);
24: }
25:
26: /** Construct a <code>Fixture</code> from its XML attributes. */
27: public Fixture(Resolver parent, Map attributes) {
28: super (parent, attributes);
29: setHierarchy(parent.getHierarchy());
30: }
31:
32: /** Run the entire fixture, using the given runner as a controller/monitor. */
33: public void launch(StepRunner runner) throws Throwable {
34: runner.run(this );
35: }
36:
37: /** @return Whether this fixture is currently launched. */
38: public boolean isLaunched() {
39: return equivalent(currentFixture);
40: }
41:
42: /** Don't re-run if already launched. */
43: protected void runStep(StepRunner runner) throws Throwable {
44: if (!isLaunched()) {
45: if (currentFixture != null)
46: currentFixture.terminate();
47: currentFixture = this ;
48: super .runStep(runner);
49: }
50: }
51:
52: public void terminate() {
53: Log.debug("fixture terminate");
54: if (equivalent(currentFixture)) {
55: if (currentFixture != this ) {
56: currentFixture.terminate();
57: } else {
58: UIContext context = getUIContext();
59: if (context != null)
60: context.terminate();
61: currentFixture = null;
62: }
63: }
64: }
65:
66: public String getXMLTag() {
67: return TAG_FIXTURE;
68: }
69:
70: public String getDefaultDescription() {
71: String ext = isForked() ? " &" : "";
72: String desc = Strings.get("fixture.desc", new Object[] {
73: getFilename(), ext });
74: return desc.indexOf(UNTITLED_FILE) != -1 ? UNTITLED : desc;
75: }
76:
77: /** Two fixtures are equivalent if they have the same XML representation. */
78: public boolean equivalent(UIContext f) {
79: return f instanceof Fixture
80: && (equals(f) || getFullXMLString().equals(
81: ((Fixture) f).getFullXMLString()));
82: }
83: }
|