001: package abbot.script;
002:
003: import java.io.*;
004: import java.util.ArrayList;
005: import javax.swing.*;
006:
007: import junit.extensions.abbot.*;
008: import abbot.AssertionFailedError;
009:
010: public class StepRunnerTest extends ResolverFixture {
011:
012: private StepRunner runner;
013: private ArrayList events;
014: private Resolver resolver;
015:
016: protected void setUp() {
017: events = new ArrayList();
018: resolver = getResolver();
019: runner = new StepRunner();
020: runner.addStepListener(new StepListener() {
021: public void stateChanged(StepEvent ev) {
022: events.add(ev);
023: }
024: });
025: }
026:
027: protected void tearDown() {
028: runner.terminate();
029: runner = null;
030: events = null;
031: resolver = null;
032: }
033:
034: public void testRunStep() throws Throwable {
035: runner.runStep(new DefaultStep("simple step"));
036: assertEquals("Wrong number of step events generated", 2, events
037: .size());
038: assertEquals("Missing step start", StepEvent.STEP_START,
039: ((StepEvent) events.get(0)).getType());
040: assertEquals("Missing step end", StepEvent.STEP_END,
041: ((StepEvent) events.get(1)).getType());
042: }
043:
044: public void testRunSequence() throws Throwable {
045: Sequence seq = new Sequence(getResolver(), "sequence of 2",
046: null);
047: Step st1 = new DefaultStep("step 1");
048: seq.addStep(st1);
049: Step st2 = new DefaultStep("step 2");
050: seq.addStep(st2);
051: runner.runStep(seq);
052: assertEquals("Wrong number of step events generated", 6, events
053: .size());
054: assertEquals("Missing sequence start", StepEvent.STEP_START,
055: ((StepEvent) events.get(0)).getType());
056: assertEquals("Expected sequence start, wrong source", seq,
057: ((StepEvent) events.get(0)).getStep());
058: assertEquals("Missing step 1 start", StepEvent.STEP_START,
059: ((StepEvent) events.get(1)).getType());
060: assertEquals("Wrong source", st1, ((StepEvent) events.get(1))
061: .getStep());
062: assertEquals("Missing step 1 end", StepEvent.STEP_END,
063: ((StepEvent) events.get(2)).getType());
064: assertEquals("Wrong source", st2, ((StepEvent) events.get(3))
065: .getStep());
066: assertEquals("Missing step 2 start", StepEvent.STEP_START,
067: ((StepEvent) events.get(3)).getType());
068: assertEquals("Missing step 2 end", StepEvent.STEP_END,
069: ((StepEvent) events.get(4)).getType());
070: assertEquals("Missing sequence end", StepEvent.STEP_END,
071: ((StepEvent) events.get(5)).getType());
072: }
073:
074: public void testRunFailedStep() throws Throwable {
075: Step step = new FailingStep("fail");
076: Throwable failure = null;
077: try {
078: runner.runStep(step);
079: } catch (AssertionFailedError afe) {
080: failure = afe;
081: }
082: assertTrue("step failure should have been caught", runner
083: .getError(step) != null);
084: assertEquals("wrong failure", failure, runner.getError(step));
085: assertEquals("Wrong number of events", 2, events.size());
086: assertEquals("Missing step start", StepEvent.STEP_START,
087: ((StepEvent) events.get(0)).getType());
088: assertEquals("Missing step failure", StepEvent.STEP_FAILURE,
089: ((StepEvent) events.get(1)).getType());
090: }
091:
092: public void testRunFailedSequence() throws Throwable {
093: Sequence seq = new Sequence(getResolver(), "sequence of 1",
094: null);
095: Step step = new FailingStep("fail");
096: seq.addStep(step);
097: Throwable failure = null;
098: try {
099: runner.runStep(seq);
100: } catch (AssertionFailedError afe) {
101: failure = afe;
102: }
103: assertTrue("step failure should have been caught", runner
104: .getError(step) != null);
105: assertEquals("wrong failure", failure, runner.getError(step));
106: assertEquals("Wrong number of events", 4, events.size());
107: assertEquals("Missing sequence start", StepEvent.STEP_START,
108: ((StepEvent) events.get(0)).getType());
109: assertEquals("Wrong event source", seq, ((StepEvent) events
110: .get(0)).getStep());
111: assertEquals("Missing step start", StepEvent.STEP_START,
112: ((StepEvent) events.get(1)).getType());
113: assertEquals("Wrong event source", step, ((StepEvent) events
114: .get(1)).getStep());
115: assertEquals("Missing step failure", StepEvent.STEP_FAILURE,
116: ((StepEvent) events.get(2)).getType());
117: assertEquals("Wrong event source", step, ((StepEvent) events
118: .get(2)).getStep());
119: assertEquals("Missing step failure", StepEvent.STEP_FAILURE,
120: ((StepEvent) events.get(3)).getType());
121: assertEquals("Wrong event source", seq, ((StepEvent) events
122: .get(3)).getStep());
123: }
124:
125: public void testRunErrorStep() {
126: Step step = new ErrorStep("error");
127: Throwable error = null;
128: try {
129: runner.runStep(step);
130: } catch (Throwable thr) {
131: error = thr;
132: }
133: assertNotNull("step error should have been caught", error);
134: assertNotNull("step error should be stored with runner", runner
135: .getError(step));
136: assertEquals("wrong error", error, runner.getError(step));
137: assertEquals("Wrong number of events", 2, events.size());
138: assertEquals("Missing step start", StepEvent.STEP_START,
139: ((StepEvent) events.get(0)).getType());
140: assertEquals("Missing step failure", StepEvent.STEP_ERROR,
141: ((StepEvent) events.get(1)).getType());
142: }
143:
144: public void testRunEDTError() {
145: Step step = new EDTErrorStep();
146: Throwable error = null;
147: try {
148: runner.runStep(step);
149: } catch (Throwable t) {
150: error = t;
151: }
152: assertNotNull("EDT error should have been thrown by runner",
153: error);
154: assertNotNull("EDT error should be stored with runner", runner
155: .getError(step));
156: }
157:
158: public void testRunErrorSequence() throws Throwable {
159: Sequence seq = new Sequence(getResolver(), "sequence of 1",
160: null);
161: Step step = new ErrorStep("error");
162: seq.addStep(step);
163: Throwable failure = null;
164: try {
165: runner.runStep(seq);
166: } catch (Throwable thr) {
167: failure = thr;
168: }
169: assertTrue("step error should have been caught", runner
170: .getError(step) != null);
171: assertEquals("wrong failure", failure, runner.getError(step));
172: assertEquals("Wrong number of events", 4, events.size());
173: assertEquals("Missing sequence start", StepEvent.STEP_START,
174: ((StepEvent) events.get(0)).getType());
175: assertEquals("Wrong event source", seq, ((StepEvent) events
176: .get(0)).getStep());
177: assertEquals("Missing step start", StepEvent.STEP_START,
178: ((StepEvent) events.get(1)).getType());
179: assertEquals("Wrong event source", step, ((StepEvent) events
180: .get(1)).getStep());
181: assertEquals("Missing step failure", StepEvent.STEP_ERROR,
182: ((StepEvent) events.get(2)).getType());
183: assertEquals("Wrong event source", step, ((StepEvent) events
184: .get(2)).getStep());
185: assertEquals("Missing step failure", StepEvent.STEP_ERROR,
186: ((StepEvent) events.get(3)).getType());
187: assertEquals("Wrong event source", seq, ((StepEvent) events
188: .get(3)).getStep());
189: }
190:
191: /** If the script is set to run to completion, make sure it throws an
192: * error at the end.
193: */
194: public void testThrowMultipleError() throws Throwable {
195: Sequence seq = new Sequence(getResolver(), "sequence of 2",
196: null);
197: Step error1 = new ErrorStep("error 1");
198: seq.addStep(error1);
199: Step error2 = new ErrorStep("error 2");
200: seq.addStep(error2);
201: runner.setStopOnError(false);
202: try {
203: runner.runStep(seq);
204: } catch (Throwable thr) {
205: StepEvent lastEvent = (StepEvent) events
206: .get(events.size() - 1);
207: assertEquals("No error on event", thr, lastEvent.getError());
208: assertEquals("Wrong event source", seq, lastEvent.getStep());
209: }
210: }
211:
212: public void testNoTerminate() throws Throwable {
213: Script script = new Script(getHierarchy());
214: final JFrame f = new JFrame(getName());
215: f.getContentPane().add(new JLabel(getName()));
216: script.addStep(new DefaultStep("Window Shower") {
217: public void runStep() {
218: f.setVisible(true);
219: }
220: });
221: runner.run(script);
222: assertTrue(
223: "Frame should not be disposed when script has no terminate",
224: f.isShowing());
225: }
226:
227: // StepRunner.stop should leave the SUT in its state at the time of stop.
228: public void testRunToStep() throws Throwable {
229: Script script = new Script(getHierarchy());
230: Step one = new DefaultStep("one");
231: final Step two = new DefaultStep("two");
232: script.addStep(one);
233: script.addStep(two);
234: final TestStepRunner runner = new TestStepRunner();
235: runner.addStepListener(new StepListener() {
236: public void stateChanged(StepEvent ev) {
237: if (ev.getSource() == two)
238: runner.stop();
239: }
240: });
241: try {
242: runner.run(script);
243: } finally {
244: runner.realTerminate();
245: }
246: }
247:
248: /** If a nested file does not exist, an exception should be thrown. */
249: public void testScriptNotFound() throws Throwable {
250: Script script = new Script(getHierarchy());
251: Script nested = new Script(getHierarchy());
252: nested.setFile(new File("somewhere/relative/file.xml"));
253: script.addStep(nested);
254: try {
255: runner.run(script);
256: fail("Exception should have been thrown");
257: } catch (InvalidScriptException ise) {
258: }
259: }
260:
261: private class DefaultStep extends Step {
262: public DefaultStep(String desc) {
263: super (resolver, desc);
264: }
265:
266: public void runStep() throws Throwable {
267: }
268:
269: public String getXMLTag() {
270: return "";
271: }
272:
273: public String getUsage() {
274: return "";
275: }
276:
277: public String getDefaultDescription() {
278: return "";
279: }
280: }
281:
282: private class FailingStep extends DefaultStep {
283: public FailingStep(String desc) {
284: super (desc);
285: }
286:
287: public void runStep() throws Throwable {
288: throw new AssertionFailedError(getDescription());
289: }
290: }
291:
292: private class ErrorStep extends DefaultStep {
293: public ErrorStep(String desc) {
294: super (desc);
295: }
296:
297: public void runStep() throws Throwable {
298: throw new Error(getDescription());
299: }
300: }
301:
302: private class EDTErrorStep extends DefaultStep {
303: public EDTErrorStep() {
304: super ("EDT error");
305: }
306:
307: public void runStep() throws Throwable {
308: SwingUtilities.invokeLater(new Runnable() {
309: public void run() {
310: throw new TestEDTError();
311: }
312: });
313: SwingUtilities.invokeAndWait(new Runnable() {
314: public void run() {
315: }
316: });
317: }
318: }
319:
320: private class TestStepRunner extends StepRunner {
321: public void terminate() {
322: fail("Step runner should not terminate when stopped");
323: }
324:
325: public void realTerminate() {
326: super .terminate();
327: }
328: }
329:
330: private class TestEDTError extends Error {
331: public TestEDTError() {
332: super ("Test EDT Error");
333: }
334: }
335:
336: public static void main(String[] args) {
337: TestHelper.runTests(args, StepRunnerTest.class);
338: }
339: }
|