001: package com.canoo.webtest.extension.applet.runner;
002:
003: import org.apache.log4j.Logger;
004: import org.netbeans.jemmy.*;
005:
006: /**
007: * @author Denis N. Antonioli
008: * @see org.netbeans.jemmy.Test
009: */
010: public class ScenarioRunner extends ActionProducer implements
011: Timeoutable, Outputable {
012: private static final Logger LOG = Logger
013: .getLogger(ScenarioRunner.class);
014: /**
015: * Jetty timeout for a scenario.
016: */
017: public static final String WHOLE_TEST_TIMEOUT_NAME = "ScenarioRunner.WholeTestTimeout";
018: /**
019: * Internal Jetty timeout for an action producer.
020: */
021: static final String ACTION_PRODUCER_MAX_ACTION_TIME_NAME = "ActionProducer.MaxActionTime";
022:
023: /**
024: * Test timeouts.
025: */
026: private Timeouts fTimeouts;
027:
028: /**
029: * Test output.
030: */
031: private TestOut fOutput;
032:
033: /**
034: * The test scenario to execute.
035: */
036: private final Scenario fScenario;
037:
038: /**
039: * Exception thrown by jemmy.
040: */
041: private JemmyException fJemmyException;
042:
043: /**
044: * The jemmy description for this class.
045: */
046: static final String DESCRIPTION = "Jemmy scenario runner for Webtest's AppletRunner.";
047:
048: static {
049: Timeouts.initDefault(WHOLE_TEST_TIMEOUT_NAME, 3600000);
050: }
051:
052: /**
053: * Creates a subclass of {@link org.netbeans.jemmy.ActionProducer} and {@link java.lang.Thread} that runs in a separate
054: * thread of execution and waits for execution to finish. The current output stream assignments and timeouts are used.
055: *
056: * @param scenario A test scenario
057: * @see org.netbeans.jemmy.Scenario
058: */
059: public ScenarioRunner(Scenario scenario) {
060: super (true);
061: setTimeouts(JemmyProperties.getCurrentTimeouts());
062: fScenario = scenario;
063: setName("ScenarioRunner for " + fScenario.getClass().getName());
064: }
065:
066: /**
067: * Set the timeouts used by this <code>ScenarioRunner</code>.
068: *
069: * @param timeouts A collection of timeout assignments.
070: * @see org.netbeans.jemmy.Timeoutable
071: * @see org.netbeans.jemmy.Timeouts
072: * @see #getTimeouts
073: */
074: public void setTimeouts(Timeouts timeouts) {
075: fTimeouts = timeouts;
076: Timeouts times = timeouts.cloneThis();
077: times.setTimeout(ACTION_PRODUCER_MAX_ACTION_TIME_NAME, timeouts
078: .getTimeout(WHOLE_TEST_TIMEOUT_NAME));
079: super .setTimeouts(times);
080: }
081:
082: /**
083: * Get the timeouts used by this test.
084: *
085: * @see org.netbeans.jemmy.Timeoutable
086: * @see org.netbeans.jemmy.Timeouts
087: * @see #setTimeouts
088: */
089: public Timeouts getTimeouts() {
090: return fTimeouts;
091: }
092:
093: /**
094: * Executes test.
095: */
096: public void startTest() throws InterruptedException {
097: LOG.info("Test " + fScenario.getClass().getName()
098: + " has been started.");
099: try {
100: produceAction(null);
101: } catch (JemmyException e) {
102: fJemmyException = e;
103: LOG.info(e.getMessage(), e);
104: }
105: }
106:
107: /**
108: * Launch an action. Pass arguments to and execute a test <code>Scenario</code>.
109: *
110: * @param obj An argument object that controls test execution. This might be a <code>java.lang.String[]</code>
111: * containing command line arguments.
112: * @return an Integer containing test status.
113: * @see org.netbeans.jemmy.Action
114: */
115: public final Object launch(Object obj) {
116: setTimeouts(fTimeouts);
117: final int result = fScenario.runIt(obj);
118: LOG.info(fScenario.getClass().getName() + " ends with "
119: + result);
120: return new Integer(result);
121: }
122:
123: public void printSynopsis() {
124: fOutput.printLine(DESCRIPTION);
125: }
126:
127: public final String getDescription() {
128: return "Scenario " + fScenario.getClass().getName();
129: }
130:
131: public TestOut getOutput() {
132: return fOutput;
133: }
134:
135: public void setOutput(TestOut output) {
136: fOutput = output;
137: super .setOutput(output);
138: }
139:
140: public JemmyException getJemmyException() {
141: return fJemmyException;
142: }
143: }
|