- Revision
- 932
- Author
- mauro
- Date
- 2008-09-19 08:06:25 -0500 (Fri, 19 Sep 2008)
Log Message
JBEHAVE-132: Refactored JUnitScenario to be decorator/adapter of an AbstractScenario, which is test-framework agnostic.
Modified Paths
- trunk/jbehave-core/src/java/org/jbehave/scenario/JUnitScenario.java
- trunk/jbehave-core/src/java/org/jbehave/scenario/Scenario.java
Added Paths
Diff
Added: trunk/jbehave-core/src/java/org/jbehave/scenario/AbstractScenario.java (0 => 932)
--- trunk/jbehave-core/src/java/org/jbehave/scenario/AbstractScenario.java (rev 0) +++ trunk/jbehave-core/src/java/org/jbehave/scenario/AbstractScenario.java 2008-09-19 13:06:25 UTC (rev 932) @@ -0,0 +1,68 @@ +package org.jbehave.scenario; + +import org.jbehave.scenario.definition.KeyWords; +import org.jbehave.scenario.definition.StoryDefinition; +import org.jbehave.scenario.parser.ScenarioNameResolver; +import org.jbehave.scenario.steps.Steps; + +/** + * <p> + * Abstract implementation of Scenario which is primarily intended as a base + * class for delegate implementations of Scenarios. As such, it has no explicit + * supports for any test framework, ie it requires the [EMAIL PROTECTED] runScenario} + * method to be invoked directly, and the class of the scenario being run needs + * to be provided explicitly. + * </p> + * <p> + * Typically, users will find it easier to extend decorator scenarios, such as + * [EMAIL PROTECTED] JUnitScenario} which also provide support for test frameworks and also + * provide the scenario class as the one being implemented by the user. + * </p> + * <p> + * Whichever Scenario class one chooses to extends, the steps for running a + * scenario are the same: + * <ol> + * <li>Extend the chosen scenario class and name it after your scenario, eg + * "ICanLogin.java" (note that there is no obligation to have the name of the + * class end in "Scenario" although you may choose to).</li> + * <li>The scenario class should be in a matching text file in the same place, + * eg "i_can_login" (this uses the default name resolution, although the it can + * be configured via the [EMAIL PROTECTED] ScenarioNameResolver}).</li> + * <li>Write some steps in your text scenario, starting each new step with + * Given, When, Then or And. The keywords can be configured via the + * [EMAIL PROTECTED] KeyWords} class, eg they can be translated/localized to other + * languages.</li> + * <li>Then move on to extending the Steps class and providing matching methods + * for the steps defined in the text scenario.</li> + * <ol> + */ +public abstract class AbstractScenario implements Scenario { + + private final Configuration configuration; + private final ScenarioRunner scenarioRunner; + private final Steps[] candidateSteps; + private final Class<? extends Scenario> scenarioClass; + + public AbstractScenario(Class<? extends Scenario> scenarioClass, Steps... candidateSteps) { + this(scenarioClass, new PropertyBasedConfiguration(), candidateSteps); + } + + public AbstractScenario(Class<? extends Scenario> scenarioClass, Configuration configuration, + Steps... candidateSteps) { + this(scenarioClass, new ScenarioRunner(), configuration, candidateSteps); + } + + public AbstractScenario(Class<? extends Scenario> scenarioClass, ScenarioRunner scenarioRunner, + Configuration configuration, Steps... candidateSteps) { + this.scenarioClass = scenarioClass; + this.configuration = configuration; + this.scenarioRunner = scenarioRunner; + this.candidateSteps = candidateSteps; + } + + public void runScenario() throws Throwable { + StoryDefinition story = configuration.forDefiningScenarios().loadScenarioDefinitionsFor(scenarioClass); + scenarioRunner.run(story, configuration, candidateSteps); + } + +}
Modified: trunk/jbehave-core/src/java/org/jbehave/scenario/JUnitScenario.java (931 => 932)
--- trunk/jbehave-core/src/java/org/jbehave/scenario/JUnitScenario.java 2008-09-19 10:27:02 UTC (rev 931) +++ trunk/jbehave-core/src/java/org/jbehave/scenario/JUnitScenario.java 2008-09-19 13:06:25 UTC (rev 932) @@ -2,63 +2,79 @@ import junit.framework.TestCase; -import org.jbehave.scenario.definition.KeyWords; -import org.jbehave.scenario.definition.StoryDefinition; -import org.jbehave.scenario.parser.ScenarioNameResolver; import org.jbehave.scenario.steps.Steps; import org.junit.Test; /** * <p> - * <a href="" implementation of Scenario. Both - * JUnit 4.x (via @Test annotation) and JUnit 3.8.x (via TestCase inheritance) - * are supported. + * Scenario decorator that add supports for running scenarios as <a + * href="" tests. Both JUnit 4.x (via @Test + * annotation) and JUnit 3.8.x (via TestCase inheritance) are supported. * </p> * <p> - * Extend this class to run your scenario. Call the class after your scenario, - * eg: "ICanLogin.java". + * Users requiring JUnit support will extends this class instead of + * [EMAIL PROTECTED] AbstractScenario}, while providing the same dependencies and following + * the same Scenario specification logic as described in + * [EMAIL PROTECTED] AbstractScenario}. The only difference in the dependencies provided is + * that the scenario class is automatically set to the one being implemented by + * the user, ie the concrete decorator class. * </p> - * <p> - * The Scenario should be in a matching text file in the same place, eg: - * "i_can_login". The scenario name used can be configured via the - * [EMAIL PROTECTED] ScenarioNameResolver}. - * </p> - * <p> - * Write some steps in your text scenario, starting each new step with Given, - * When, Then or And. The keywords can be configured via the [EMAIL PROTECTED] KeyWords} - * class, eg they can be translated/localized to other languages. - * </p> - * <p> - * Then move on to extending the Steps class. - * </p> + * + * @see AbstractScenario */ public abstract class JUnitScenario extends TestCase implements Scenario { - private final Configuration configuration; - private final ScenarioRunner scenarioRunner; - private final Steps[] candidateSteps; + private final Class<? extends JUnitScenario> decoratorClass = this.getClass(); + private final Scenario delegate; public JUnitScenario(Steps... candidateSteps) { - this(new PropertyBasedConfiguration(), candidateSteps); + this.delegate = new JUnitScenarioDelegate(decoratorClass, candidateSteps); } public JUnitScenario(Configuration configuration, Steps... candidateSteps) { - this(new ScenarioRunner(), configuration, candidateSteps); + this.delegate = new JUnitScenarioDelegate(decoratorClass, configuration, candidateSteps); } public JUnitScenario(ScenarioRunner scenarioRunner, Configuration configuration, Steps... candidateSteps) { - this.configuration = configuration; - this.scenarioRunner = scenarioRunner; - this.candidateSteps = candidateSteps; + this.delegate = new JUnitScenarioDelegate(decoratorClass, scenarioRunner, configuration, candidateSteps); } + public JUnitScenario(Scenario delegate) { + this.delegate = delegate; + + } + @Test public void runScenario() throws Throwable { - StoryDefinition story = configuration.forDefiningScenarios().loadScenarioDefinitionsFor(this.getClass()); - scenarioRunner.run(story, configuration, candidateSteps); + this.delegate.runScenario(); } - public void testRun() throws Throwable { + /** + * A JUnit 3-compatibile runnable method which simply delegates + * [EMAIL PROTECTED] Scenario#runScenario()} + * + * @throws Throwable + */ + public void testScenario() throws Throwable { runScenario(); } + + public static class JUnitScenarioDelegate extends AbstractScenario { + + public JUnitScenarioDelegate(Class<? extends Scenario> decoratorClass, Steps... candidateSteps) { + super(decoratorClass, candidateSteps); + } + + public JUnitScenarioDelegate(Class<? extends Scenario> decoratorClass, Configuration configuration, + Steps... candidateSteps) { + super(decoratorClass, configuration, candidateSteps); + } + + public JUnitScenarioDelegate(Class<? extends Scenario> decoratorClass, ScenarioRunner scenarioRunner, + Configuration configuration, Steps... candidateSteps) { + super(decoratorClass, scenarioRunner, configuration, candidateSteps); + } + + } + }
Modified: trunk/jbehave-core/src/java/org/jbehave/scenario/Scenario.java (931 => 932)
--- trunk/jbehave-core/src/java/org/jbehave/scenario/Scenario.java 2008-09-19 10:27:02 UTC (rev 931) +++ trunk/jbehave-core/src/java/org/jbehave/scenario/Scenario.java 2008-09-19 13:06:25 UTC (rev 932) @@ -1,29 +1,17 @@ package org.jbehave.scenario; -import org.jbehave.scenario.definition.KeyWords; -import org.jbehave.scenario.parser.ScenarioNameResolver; - /** * <p> * Scenario represents the main interface to run a scenario. * </p> * <p> - * Extend the implementation that uses the test framework of your choice, eg - * JUnitScenario, and call the class after your scenario, eg: "ICanLogin.java". + * Typically users will need to extend an abstract implementation, such as + * [EMAIL PROTECTED] AbstractScenario} or a decorator scenarios, such as + * [EMAIL PROTECTED] JUnitScenario}, which also provide support for test frameworks. * </p> - * <p> - * Your Scenario class should be in a matching text file in the same place, eg - * "i_can_login". The scenario name used can be configured via the - * [EMAIL PROTECTED] ScenarioNameResolver}. - * </p> - * <p> - * Write some steps in your text scenario, starting each new step with Given, - * When, Then or And. The keywords can be configured via the [EMAIL PROTECTED] KeyWords} - * class, eg they can be translated/localized to other languages. - * </p> - * <p> - * Then move on to extending the Steps class. - * </p> + * + * @see AbstractScenario + * @see JUnitScenario */ public interface Scenario {
To unsubscribe from this list please visit:
