001: /*
002: * Copyright (c) 2005 Oculus Technologies Corporation, All Rights Reserved
003: */
004: package abbot.script;
005:
006: import java.util.HashMap;
007: import abbot.DynamicLoadingConstants;
008: import abbot.finder.*;
009: import abbot.finder.AWTHierarchy;
010: import junit.framework.TestCase;
011: import junit.extensions.abbot.TestHelper;
012:
013: /** ScriptFixture must only restore system state on terminate;
014: * SystemState needs to be static, or preserved across ScriptFixture invocations
015: * @author twall
016: *
017: */
018: public class FixtureTest extends TestCase {
019: private Script script;
020: private TestFixture fixture;
021: private Script script2;
022: private TestFixture fixture2;
023: private Script scriptDup;
024: private TestFixture fixtureDup;
025: private StepRunner runner;
026:
027: protected void setUp() {
028: script = new Script(new AWTHierarchy());
029: script.addStep(fixture = new TestFixture(script, "1"));
030: scriptDup = new Script(new AWTHierarchy());
031: scriptDup.addStep(fixtureDup = new TestFixture(scriptDup, "1"));
032: script2 = new Script(new AWTHierarchy());
033: script2.addStep(fixture2 = new TestFixture(script2, "2"));
034: runner = new StepRunner();
035: }
036:
037: protected void tearDown() {
038: fixture.terminate();
039: fixtureDup.terminate();
040: fixture2.terminate();
041: }
042:
043: public void testXML() {
044: String EXPECTED = "<fixture filename=\""
045: + fixture.getFilename() + "\" />";
046: assertEquals("Wrong XML prefix", EXPECTED, Step
047: .toXMLString(fixture));
048: }
049:
050: public void testFixturesEquivalent() {
051: assertTrue("Fixtures should be equivalent", fixture
052: .equivalent(fixtureDup));
053: assertTrue("Fixtures should not be equivalent", !fixture
054: .equivalent(fixture2));
055: }
056:
057: public void testLaunchWithNoExtantFixture() throws Throwable {
058: UIContext context = script.getUIContext();
059: context.launch(runner);
060: assertTrue("Script context should indicate launched", context
061: .isLaunched());
062: assertTrue("Fixture should have been run", fixture.wasRun);
063: context.terminate();
064: assertFalse("Context should not be launched after terminate",
065: context.isLaunched());
066: }
067:
068: public void testRunWithNoExtantFixture() throws Throwable {
069: UIContext context = script.getUIContext();
070: runner.run(script);
071: assertTrue("Context should indicate launched", context
072: .isLaunched());
073: assertTrue("Fixture run should have run", fixture.wasRun);
074: context.terminate();
075: assertFalse("Context should not be launched after terminate",
076: context.isLaunched());
077: }
078:
079: public void testLaunchWithExtantSameFixture() throws Throwable {
080: runner.launch(script);
081: fixture.wasRun = false;
082: runner.launch(scriptDup);
083: assertFalse("Same fixture should not be launched twice",
084: fixtureDup.wasRun);
085: assertTrue("Script 1 should be considered launched", script
086: .getUIContext().isLaunched());
087: assertTrue("Script 2 should be considered launched", scriptDup
088: .getUIContext().isLaunched());
089: }
090:
091: public void testRunWithExtantSameFixture() throws Throwable {
092: runner.run(script);
093: fixture.wasRun = false;
094: runner.run(scriptDup);
095: assertFalse("Fixture should not be run twice", fixture.wasRun);
096: assertTrue("Script should still be considered launched", script
097: .getUIContext().isLaunched());
098: assertTrue("Script should still be considered launched",
099: scriptDup.getUIContext().isLaunched());
100: }
101:
102: public void testLaunchWithExtantDifferentFixture() throws Throwable {
103: script.getUIContext().launch(runner);
104: script2.getUIContext().launch(runner);
105: assertFalse(
106: "First script should have terminated on launch of second",
107: script.getUIContext().isLaunched());
108: assertTrue("Second script should indicate launched", script2
109: .getUIContext().isLaunched());
110: assertTrue("Second fixture should have run", fixture2.wasRun);
111: }
112:
113: public void testRunWithExtantDifferentFixture() throws Throwable {
114: runner.run(script);
115: runner.run(script2);
116: assertFalse(
117: "First script should have terminated on launch of second",
118: script.getUIContext().isLaunched());
119: assertTrue("Second script should indicate launched", script2
120: .getUIContext().isLaunched());
121: assertTrue("Second fixture should have run", fixture2.wasRun);
122: }
123:
124: public void testPreserveHierarchyDuringContext() throws Throwable {
125: Hierarchy runnerHierarchy = runner.getHierarchy();
126: assertTrue("Wrong runner default Hierarchy before run: "
127: + runnerHierarchy,
128: runnerHierarchy instanceof TestHierarchy);
129: runner.run(script);
130: Hierarchy scriptHierarchy = script.getHierarchy();
131: assertEquals("Runner should set the script Hierarchy",
132: runnerHierarchy, scriptHierarchy);
133:
134: UIContext context = script.getUIContext();
135: Hierarchy fixtureHierarchy = context.getHierarchy();
136: assertEquals("Runner and fixture (" + context
137: + ") should share a Hierarchy", runnerHierarchy,
138: fixtureHierarchy);
139: assertEquals("Runner Hierarchy should not change",
140: runnerHierarchy, runner.getHierarchy());
141:
142: StepRunner runner2 = new StepRunner();
143: runner2.run(scriptDup);
144: assertEquals("New runner should get Hierarchy from fixture",
145: fixtureHierarchy, runner2.getHierarchy());
146: assertEquals(
147: "New runner should set the new script's Hierarchy",
148: fixtureHierarchy, scriptDup.getHierarchy());
149: }
150:
151: public void testParseScriptWithFixture() throws Throwable {
152: String SCRIPT = "<AWTTestScript>" + "<fixture filename=\""
153: + fixture.getFilename() + "\"/>" + "</AWTTestScript>";
154: script.load(ScriptTest.stringToReader(SCRIPT));
155: assertEquals("Wrong number of steps", 1, script.steps().size());
156: assertTrue("First step should be a script", script.steps().get(
157: 0) instanceof Script);
158: assertTrue("First step should be a fixture", ((Script) script
159: .steps().get(0)) instanceof Fixture);
160: }
161:
162: private class TestFixture extends Fixture implements
163: DynamicLoadingConstants {
164: public boolean wasRun;
165:
166: public TestFixture(Script parent, String id) {
167: super (parent, new HashMap());
168: setDescription("TestFixture " + id);
169: addStep(new Launch(this , id, DYNAMIC_CLASSNAME, "main",
170: new String[] { "[]" }, DYNAMIC_CLASSPATH, false));
171: addStep(new Comment(this , "comment: " + id) {
172: protected void runStep() {
173: wasRun = true;
174: }
175: });
176: }
177: }
178:
179: public static void main(String[] args) {
180: TestHelper.runTests(args, FixtureTest.class);
181: }
182: }
|