Skip to content

Commit

Permalink
JBEHAVE-548: Added support for annotated steps execution before and a…
Browse files Browse the repository at this point in the history
…fter example scenario.
  • Loading branch information
maurotalevi committed Sep 7, 2011
1 parent 437ae4d commit c33497b
Show file tree
Hide file tree
Showing 14 changed files with 255 additions and 115 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package org.jbehave.examples.trader.steps;

import org.jbehave.core.annotations.*;
import org.jbehave.core.annotations.AfterScenario;
import org.jbehave.core.annotations.AfterScenario.Outcome;
import org.jbehave.core.annotations.AfterStories;
import org.jbehave.core.annotations.AfterStory;
import org.jbehave.core.annotations.BeforeScenario;
import org.jbehave.core.annotations.BeforeStories;
import org.jbehave.core.annotations.BeforeStory;
import org.jbehave.core.annotations.Named;
import org.jbehave.core.annotations.ScenarioType;

/**
* Steps executed before and after stories/story/scenario
Expand Down Expand Up @@ -35,17 +42,17 @@ public void afterStory(@Named("theme") String theme) {
System.out.println("After Story ...");
}
}
@BeforeStory(uponGivenStory=true)

@BeforeStory(uponGivenStory = true)
public void beforeGivenStory() {
System.out.println("Before Given Story ...");
}

@AfterStory(uponGivenStory=true)
@AfterStory(uponGivenStory = true)
public void afterGivenStory() {
System.out.println("After Given Story ...");
}

@BeforeScenario
public void beforeScenario(@Named("theme") String theme) {
if (theme.length() > 0) {
Expand All @@ -55,6 +62,11 @@ public void beforeScenario(@Named("theme") String theme) {
}
}

@BeforeScenario(uponType = ScenarioType.EXAMPLE)
public void beforeExampleScenario() {
System.out.println("Before Example Scenario ...");
}

@AfterScenario
public void afterScenario(@Named("variant") String variant, @Named("theme") String theme) {
if (variant.length() > 0 && theme.length() > 0) {
Expand All @@ -64,7 +76,7 @@ public void afterScenario(@Named("variant") String variant, @Named("theme") Stri
}
}

@AfterScenario(uponOutcome=Outcome.FAILURE)
@AfterScenario(uponOutcome = Outcome.FAILURE)
public void afterFailedScenario(@Named("theme") String theme) {
if ("parametrisation".equals(theme)) {
System.out.println("Wow, something failed in a scenario with theme 'parametrisation'.");
Expand All @@ -73,9 +85,14 @@ public void afterFailedScenario(@Named("theme") String theme) {
}
}

@AfterScenario(uponOutcome=Outcome.SUCCESS)
@AfterScenario(uponOutcome = Outcome.SUCCESS)
public void afterSuccessfulScenario() {
System.out.println("After Successful Scenario ...");
}

@AfterScenario(uponType = ScenarioType.EXAMPLE)
public void afterExampleScenario() {
System.out.println("After Example Scenario ...");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,11 @@ enum Outcome { ANY, SUCCESS, FAILURE }
*/
Outcome uponOutcome() default ANY;

/**
* Signals that the annoated method should be invoked only upon given type
*
* @return A ScenarioType upon which the method should be invoked
*/
ScenarioType uponType() default ScenarioType.NORMAL;

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@
@Target(ElementType.METHOD)
public @interface BeforeScenario {

/**
* Signals that the annoated method should be invoked only upon given type
*
* @return A ScenarioType upon which the method should be invoked
*/
ScenarioType uponType() default ScenarioType.NORMAL;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.jbehave.core.annotations;

public enum ScenarioType {

/**
* Scenario that is part of normal story execution
*/
NORMAL,

/**
* Scenario that is parametrised by example
*/
EXAMPLE

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jbehave.core.embedder;

import org.jbehave.core.annotations.ScenarioType;
import org.jbehave.core.configuration.Configuration;
import org.jbehave.core.failures.FailureStrategy;
import org.jbehave.core.failures.PendingStepFound;
Expand Down Expand Up @@ -243,22 +244,22 @@ private void runIt(RunContext context, Story story, Map<String, String> storyPar
}
// run before scenario steps, if allowed
if (runBeforeAndAfterScenarioSteps) {
runBeforeOrAfterScenarioSteps(context, scenario, storyAndScenarioMeta, Stage.BEFORE);
runBeforeOrAfterScenarioSteps(context, scenario, storyAndScenarioMeta, Stage.BEFORE, ScenarioType.NORMAL);
}

// run given stories, if any
runGivenStories(scenario, context);
if (isParameterisedByExamples(scenario)) {
// run parametrised scenarios by examples
runParametrisedScenariosByExamples(context, scenario);
runParametrisedScenariosByExamples(context, scenario, storyAndScenarioMeta);
} else { // run as plain old scenario
addMetaParameters(storyParameters, storyAndScenarioMeta);
runScenarioSteps(context, scenario, storyParameters);
}

// run after scenario steps, if allowed
if (runBeforeAndAfterScenarioSteps) {
runBeforeOrAfterScenarioSteps(context, scenario, storyAndScenarioMeta, Stage.AFTER);
runBeforeOrAfterScenarioSteps(context, scenario, storyAndScenarioMeta, Stage.AFTER, ScenarioType.NORMAL);
}

}
Expand Down Expand Up @@ -340,15 +341,17 @@ private boolean isParameterisedByExamples(Scenario scenario) {
return scenario.getExamplesTable().getRowCount() > 0 && !scenario.getGivenStories().requireParameters();
}

private void runParametrisedScenariosByExamples(RunContext context, Scenario scenario) {
private void runParametrisedScenariosByExamples(RunContext context, Scenario scenario, Meta storyAndScenarioMeta) {
ExamplesTable table = scenario.getExamplesTable();
reporter.get().beforeExamples(scenario.getSteps(), table);
for (Map<String, String> scenarioParameters : table.getRows()) {
reporter.get().example(scenarioParameters);
if (context.configuration().storyControls().resetStateBeforeScenario()) {
context.resetState();
}
runBeforeOrAfterScenarioSteps(context, scenario, storyAndScenarioMeta, Stage.BEFORE, ScenarioType.EXAMPLE);
runScenarioSteps(context, scenario, scenarioParameters);
runBeforeOrAfterScenarioSteps(context, scenario, storyAndScenarioMeta, Stage.AFTER, ScenarioType.EXAMPLE);
}
reporter.get().afterExamples();
}
Expand All @@ -357,8 +360,8 @@ private void runBeforeOrAfterStorySteps(RunContext context, Story story, Stage s
runStepsWhileKeepingState(context, context.collectBeforeOrAfterStorySteps(story, stage));
}

private void runBeforeOrAfterScenarioSteps(RunContext context, Scenario scenario, Meta storyAndScenarioMeta, Stage stage) {
runStepsWhileKeepingState(context, context.collectBeforeOrAfterScenarioSteps(storyAndScenarioMeta, stage));
private void runBeforeOrAfterScenarioSteps(RunContext context, Scenario scenario, Meta storyAndScenarioMeta, Stage stage, ScenarioType type) {
runStepsWhileKeepingState(context, context.collectBeforeOrAfterScenarioSteps(storyAndScenarioMeta, stage, type));
}

private void runScenarioSteps(RunContext context, Scenario scenario, Map<String, String> scenarioParameters) {
Expand Down Expand Up @@ -527,8 +530,8 @@ public List<Step> collectBeforeOrAfterStorySteps(Story story, Stage stage) {
return configuration.stepCollector().collectBeforeOrAfterStorySteps(candidateSteps, story, stage, givenStory);
}

public List<Step> collectBeforeOrAfterScenarioSteps(Meta storyAndScenarioMeta, Stage stage) {
return configuration.stepCollector().collectBeforeOrAfterScenarioSteps(candidateSteps, storyAndScenarioMeta, stage);
public List<Step> collectBeforeOrAfterScenarioSteps(Meta storyAndScenarioMeta, Stage stage, ScenarioType type) {
return configuration.stepCollector().collectBeforeOrAfterScenarioSteps(candidateSteps, storyAndScenarioMeta, stage, type);
}

public List<Step> collectScenarioSteps(Scenario scenario, Map<String, String> parameters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import org.jbehave.core.annotations.ScenarioType;
import org.jbehave.core.configuration.Configuration;

/**
Expand Down Expand Up @@ -40,10 +41,11 @@ public interface CandidateSteps {

/**
* Returns the before or after scenario steps
* @param type TODO
*
* @return The list of before or after steps
*/
List<BeforeOrAfterStep> listBeforeOrAfterScenario();
List<BeforeOrAfterStep> listBeforeOrAfterScenario(ScenarioType type);

/**
* Returns the configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;
import java.util.Map;

import org.jbehave.core.annotations.ScenarioType;
import org.jbehave.core.configuration.Keywords;
import org.jbehave.core.i18n.LocalizedKeywords;
import org.jbehave.core.model.Meta;
Expand Down Expand Up @@ -55,10 +56,10 @@ public List<Step> collectBeforeOrAfterStorySteps(List<CandidateSteps> candidateS
return steps;
}

public List<Step> collectBeforeOrAfterScenarioSteps(List<CandidateSteps> candidateSteps, Meta storyAndScenarioMeta, Stage stage) {
public List<Step> collectBeforeOrAfterScenarioSteps(List<CandidateSteps> candidateSteps, Meta storyAndScenarioMeta, Stage stage, ScenarioType type) {
List<Step> steps = new ArrayList<Step>();
for (CandidateSteps candidates : candidateSteps) {
List<BeforeOrAfterStep> beforeOrAfterScenarioSteps = candidates.listBeforeOrAfterScenario();
List<BeforeOrAfterStep> beforeOrAfterScenarioSteps = candidates.listBeforeOrAfterScenario(type);
if (stage == Stage.BEFORE) {
steps.addAll(createSteps(beforeOrAfterScenarioSteps, storyAndScenarioMeta, stage));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.jbehave.core.annotations.BeforeScenario;
import org.jbehave.core.annotations.BeforeStories;
import org.jbehave.core.annotations.BeforeStory;
import org.jbehave.core.annotations.ScenarioType;
import org.jbehave.core.model.Meta;
import org.jbehave.core.model.Scenario;
import org.jbehave.core.model.Story;
Expand Down Expand Up @@ -49,8 +50,9 @@ enum Stage {
*
* @param candidateSteps the {@link CandidateSteps}.
* @param storyAndScenarioMeta the story and scenario {@link org.jbehave.core.model.Meta} parameters
* @param type the ScenarioType
*/
List<Step> collectBeforeOrAfterScenarioSteps(List<CandidateSteps> candidateSteps, Meta storyAndScenarioMeta, Stage stage);
List<Step> collectBeforeOrAfterScenarioSteps(List<CandidateSteps> candidateSteps, Meta storyAndScenarioMeta, Stage stage, ScenarioType type);

/**
* Collects all of the {@link Step}s to execute for a scenario.
Expand Down

0 comments on commit c33497b

Please sign in to comment.