- Revision
- 1222
- Author
- mauro
- Date
- 2009-09-05 10:11:22 -0500 (Sat, 05 Sep 2009)
Log Message
JBEHAVE-126: Support the running given scenarios, as specified by scenario paths, as pre-conditions for another scenario.
Modified Paths
- trunk/core/examples/noughtsandcrosses/src/scenario/com/lunivore/noughtsandcrosses/PlayersCanHazTurns.java
- trunk/core/examples/trader/pom.xml
- trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/TraderScenario.java
- trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/TraderSteps.java
- trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/trader_sells_all_stocks.scenario
- trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/ScenarioRunnerBehaviour.java
- trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/parser/PatternScenarioParserBehaviour.java
- trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/steps/UnmatchedToPendingStepCreatorBehaviour.java
- trunk/core/jbehave-core/src/java/org/jbehave/scenario/ScenarioRunner.java
- trunk/core/jbehave-core/src/java/org/jbehave/scenario/definition/KeyWords.java
- trunk/core/jbehave-core/src/java/org/jbehave/scenario/definition/ScenarioDefinition.java
- trunk/core/jbehave-core/src/java/org/jbehave/scenario/definition/ScenarioGivenWhenThenAnd.java
- trunk/core/jbehave-core/src/java/org/jbehave/scenario/parser/ClasspathScenarioDefiner.java
- trunk/core/jbehave-core/src/java/org/jbehave/scenario/parser/PatternScenarioParser.java
- trunk/core/jbehave-core/src/java/org/jbehave/scenario/parser/ScenarioDefiner.java
- trunk/core/jbehave-core/src/java/org/jbehave/scenario/reporters/PassSilentlyDecorator.java
- trunk/core/jbehave-core/src/java/org/jbehave/scenario/reporters/PrintStreamScenarioReporter.java
- trunk/core/jbehave-core/src/java/org/jbehave/scenario/reporters/ScenarioReporter.java
- trunk/core/jbehave-core/src/java/org/jbehave/scenario/reporters/StepFailureScenarioReporterDecorator.java
Diff
Modified: trunk/core/examples/noughtsandcrosses/src/scenario/com/lunivore/noughtsandcrosses/PlayersCanHazTurns.java (1221 => 1222)
--- trunk/core/examples/noughtsandcrosses/src/scenario/com/lunivore/noughtsandcrosses/PlayersCanHazTurns.java 2009-09-05 11:25:34 UTC (rev 1221) +++ trunk/core/examples/noughtsandcrosses/src/scenario/com/lunivore/noughtsandcrosses/PlayersCanHazTurns.java 2009-09-05 15:11:22 UTC (rev 1222) @@ -28,7 +28,7 @@ public PlayersCanHazTurns(final ClassLoader classLoader, OAndXUniverse universe) { super(new MostUsefulConfiguration() { public KeyWords keywords() { - return new KeyWords("I can haz:", "Gief", "Wen", "Den", "And", "Tbl"); + return new KeyWords("I can haz:", "Gief I can haz:", "Gief", "Wen", "Den", "Tbl", "And"); } public ClasspathScenarioDefiner forDefiningScenarios() { return new ClasspathScenarioDefiner(new UnderscoredCamelCaseResolver(), new PatternScenarioParser(this),
Modified: trunk/core/examples/trader/pom.xml (1221 => 1222)
--- trunk/core/examples/trader/pom.xml 2009-09-05 11:25:34 UTC (rev 1221) +++ trunk/core/examples/trader/pom.xml 2009-09-05 15:11:22 UTC (rev 1222) @@ -52,7 +52,7 @@ <scenarioExcludes> <scenarioExclude>**/*Steps.java</scenarioExclude> </scenarioExcludes> - <skip>false</skip> + <skip>true</skip> </configuration> <goals> <goal>stepdoc</goal>
Modified: trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/TraderScenario.java (1221 => 1222)
--- trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/TraderScenario.java 2009-09-05 11:25:34 UTC (rev 1221) +++ trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/TraderScenario.java 2009-09-05 15:11:22 UTC (rev 1222) @@ -6,6 +6,8 @@ import org.jbehave.scenario.parser.PatternScenarioParser; import org.jbehave.scenario.parser.ScenarioDefiner; import org.jbehave.scenario.parser.UnderscoredCamelCaseResolver; +import org.jbehave.scenario.reporters.PrintStreamScenarioReporter; +import org.jbehave.scenario.reporters.ScenarioReporter; public class TraderScenario extends JUnitScenario { @@ -19,6 +21,12 @@ public ScenarioDefiner forDefiningScenarios() { return new ClasspathScenarioDefiner(new UnderscoredCamelCaseResolver(".scenario"), new PatternScenarioParser(this), classLoader); } + + @Override + public ScenarioReporter forReportingScenarios() { + return new PrintStreamScenarioReporter(); + } + }, new TraderSteps(classLoader)); }
Modified: trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/TraderSteps.java (1221 => 1222)
--- trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/TraderSteps.java 2009-09-05 11:25:34 UTC (rev 1221) +++ trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/TraderSteps.java 2009-09-05 15:11:22 UTC (rev 1222) @@ -18,7 +18,7 @@ import org.jbehave.scenario.annotations.When; import org.jbehave.scenario.parser.PrefixCapturingPatternBuilder; import org.jbehave.scenario.steps.ParameterConverters; -import org.jbehave.scenario.steps.PrintStreamStepMonitor; +import org.jbehave.scenario.steps.SilentStepMonitor; import org.jbehave.scenario.steps.StepMonitor; import org.jbehave.scenario.steps.Steps; import org.jbehave.scenario.steps.StepsConfiguration; @@ -31,7 +31,7 @@ public TraderSteps(ClassLoader classLoader) { super(configuration); - StepMonitor monitor = new PrintStreamStepMonitor(); + StepMonitor monitor = new SilentStepMonitor(); configuration.useParameterConverters(new ParameterConverters( monitor, new TraderConverter(mockTradePersister()))); configuration.usePatternBuilder(new PrefixCapturingPatternBuilder("%"));
Modified: trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/trader_sells_all_stocks.scenario (1221 => 1222)
--- trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/trader_sells_all_stocks.scenario 2009-09-05 11:25:34 UTC (rev 1221) +++ trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/trader_sells_all_stocks.scenario 2009-09-05 15:11:22 UTC (rev 1222) @@ -1,3 +1,7 @@ +Scenario: Trader sells it all and goes to Bermuda + +GivenScenarios: org/jbehave/examples/trader/scenarios/trader_is_alerted_of_status.scenario + Given a trader of name Mauro Given a stock of prices 0.5,1.0 and a threshold of 1.5 When the stock is traded at 2.0
Modified: trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/ScenarioRunnerBehaviour.java (1221 => 1222)
--- trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/ScenarioRunnerBehaviour.java 2009-09-05 11:25:34 UTC (rev 1221) +++ trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/ScenarioRunnerBehaviour.java 2009-09-05 15:11:22 UTC (rev 1222) @@ -1,5 +1,6 @@ package org.jbehave.scenario; +import static java.util.Arrays.asList; import static org.mockito.Matchers.anyObject; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.inOrder; @@ -17,6 +18,8 @@ import org.jbehave.scenario.errors.ErrorStrategy; import org.jbehave.scenario.errors.ErrorStrategyInWhichWeTrustTheReporter; import org.jbehave.scenario.errors.PendingErrorStrategy; +import org.jbehave.scenario.parser.ClasspathScenarioDefiner; +import org.jbehave.scenario.parser.ScenarioDefiner; import org.jbehave.scenario.reporters.ScenarioReporter; import org.jbehave.scenario.steps.CandidateStep; import org.jbehave.scenario.steps.CandidateSteps; @@ -33,9 +36,9 @@ @Test public void shouldRunStepsInScenariosAndReportResultsToReporter() throws Throwable { - ScenarioDefinition scenarioDefinition1 = new ScenarioDefinition("my title 1", "failingStep", "successfulStep"); - ScenarioDefinition scenarioDefinition2 = new ScenarioDefinition("my title 2", "successfulStep"); - ScenarioDefinition scenarioDefinition3 = new ScenarioDefinition("my title 3", "successfulStep", "pendingStep"); + ScenarioDefinition scenarioDefinition1 = new ScenarioDefinition("my title 1", asList("failingStep", "successfulStep")); + ScenarioDefinition scenarioDefinition2 = new ScenarioDefinition("my title 2", asList("successfulStep")); + ScenarioDefinition scenarioDefinition3 = new ScenarioDefinition("my title 3", asList("successfulStep", "pendingStep")); StoryDefinition storyDefinition = new StoryDefinition(new Blurb("my blurb"), scenarioDefinition1, scenarioDefinition2, scenarioDefinition3); // Given @@ -85,6 +88,49 @@ inOrder.verify(errorStrategy).handleError(anException); } + + @Test + public void shouldRunGivenScenariosBeforeSteps() throws Throwable { + ScenarioDefinition scenarioDefinition1 = new ScenarioDefinition("scenario 1", asList("successfulStep")); + ScenarioDefinition scenarioDefinition2 = new ScenarioDefinition("scenario 2", asList("/path/to/given/scenario1"), asList("anotherSuccessfulStep")); + StoryDefinition storyDefinition1 = new StoryDefinition(new Blurb("story 1"), scenarioDefinition1); + StoryDefinition storyDefinition2 = new StoryDefinition(new Blurb("story 2"), scenarioDefinition2); + + // Given + CandidateStep[] someCandidateSteps = new CandidateStep[0]; + Step step = mock(Step.class); + StepResult result = mock(StepResult.class); + stub(step.perform()).toReturn(result); + + ScenarioDefiner scenarioDefiner = mock(ScenarioDefiner.class); + ScenarioReporter reporter = mock(ScenarioReporter.class); + StepCreator creator = mock(StepCreator.class); + CandidateSteps mySteps = mock(Steps.class); + stub(mySteps.getSteps()).toReturn(someCandidateSteps); + Step successfulStep = mock(Step.class); + stub(successfulStep.perform()).toReturn(StepResult.success("successfulStep")); + Step anotherSuccessfulStep = mock(Step.class); + stub(anotherSuccessfulStep.perform()).toReturn(StepResult.success("anotherSuccessfulStep")); + + stub(creator.createStepsFrom(scenarioDefinition1, tableValues, mySteps)).toReturn(new Step[] {successfulStep}); + stub(creator.createStepsFrom(scenarioDefinition2, tableValues, mySteps)).toReturn(new Step[] {anotherSuccessfulStep}); + + stub(scenarioDefiner.loadScenarioDefinitionsFor("/path/to/given/scenario1")).toReturn(storyDefinition1); + + ErrorStrategy errorStrategy = mock(ErrorStrategy.class); + + ScenarioRunner runner = new ScenarioRunner(); + + runner.run(storyDefinition2, configurationWith(scenarioDefiner, reporter, creator, errorStrategy), mySteps); + + InOrder inOrder = inOrder(reporter); + inOrder.verify(reporter).beforeStory(storyDefinition2.getBlurb()); + inOrder.verify(reporter).givenScenario("/path/to/given/scenario1"); + inOrder.verify(reporter).successful("successfulStep"); + inOrder.verify(reporter).successful("anotherSuccessfulStep"); + inOrder.verify(reporter).afterStory(); + } + @Test public void shouldNotPerformStepsAfterStepsWhichShouldNotContinue() throws Throwable { // Given @@ -161,7 +207,7 @@ inOrder.verify(errorStrategy).handleError(failure.getThrowable()); } - @Test + @Test public void shouldResetStateForEachSetOfSteps() throws Throwable { ScenarioReporter reporter = mock(ScenarioReporter.class); @@ -211,28 +257,34 @@ } private Configuration configurationWithPendingStrategy(StepCreator creator, - ScenarioReporter reporter, PendingErrorStrategy strategy) { - - return configurationWith(reporter, creator, new ErrorStrategyInWhichWeTrustTheReporter(), strategy); + ScenarioReporter reporter, PendingErrorStrategy strategy) { + return configurationWith(new ClasspathScenarioDefiner(), reporter, creator, new ErrorStrategyInWhichWeTrustTheReporter(), strategy); } private Configuration configurationWith(final ScenarioReporter reporter, final StepCreator creator) { return configurationWith(reporter, creator, new ErrorStrategyInWhichWeTrustTheReporter()); } - private Configuration configurationWith(final ScenarioReporter reporter, - final StepCreator creator, final ErrorStrategy errorStrategy) { - return configurationWith(reporter, creator, errorStrategy, PendingErrorStrategy.PASSING); + private Configuration configurationWith(ScenarioReporter reporter, + StepCreator creator, ErrorStrategy errorStrategy) { + return configurationWith(new ClasspathScenarioDefiner(), reporter, creator, errorStrategy); + } + + private Configuration configurationWith(ScenarioDefiner definer, + final ScenarioReporter reporter, final StepCreator creator, final ErrorStrategy errorStrategy) { + return configurationWith(definer, reporter, creator, errorStrategy, PendingErrorStrategy.PASSING); } private Configuration configurationWith( - final ScenarioReporter reporter, + final ScenarioDefiner definer, final ScenarioReporter reporter, final StepCreator creator, final ErrorStrategy errorStrategy, final PendingErrorStrategy pendingStrategy) { - return new PropertyBasedConfiguration() { + return new PropertyBasedConfiguration() { @Override + public ScenarioDefiner forDefiningScenarios() { return definer; } + @Override public StepCreator forCreatingSteps() { return creator; } @Override public ScenarioReporter forReportingScenarios() { return reporter; }
Modified: trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/parser/PatternScenarioParserBehaviour.java (1221 => 1222)
--- trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/parser/PatternScenarioParserBehaviour.java 2009-09-05 11:25:34 UTC (rev 1221) +++ trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/parser/PatternScenarioParserBehaviour.java 2009-09-05 15:11:22 UTC (rev 1222) @@ -122,6 +122,7 @@ "So that I can see what we're not delivering")); ensureThat(story.getScenarios().get(0).getTitle(), equalTo("A pending scenario")); + ensureThat(story.getScenarios().get(0).getGivenScenarios().size(), equalTo(0)); ensureThat(story.getScenarios().get(0).getSteps(), equalTo(asList( "Given a step that's pending", "When I run the scenario", @@ -129,6 +130,7 @@ ))); ensureThat(story.getScenarios().get(1).getTitle(), equalTo("A passing scenario")); + ensureThat(story.getScenarios().get(1).getGivenScenarios().size(), equalTo(0)); ensureThat(story.getScenarios().get(1).getSteps(), equalTo(asList( "Given I'm not reporting passing scenarios", "When I run the scenario", @@ -136,6 +138,7 @@ ))); ensureThat(story.getScenarios().get(2).getTitle(), equalTo("A failing scenario")); + ensureThat(story.getScenarios().get(2).getGivenScenarios().size(), equalTo(0)); ensureThat(story.getScenarios().get(2).getSteps(), equalTo(asList( "Given a step that fails", "When I run the scenario", @@ -209,7 +212,8 @@ ScenarioDefinition scenario = story.getScenarios().get(0); ensureThat(scenario.getTitle(), equalTo("A template scenario with table values")); - ensureThat(scenario.getSteps(), equalTo(asList( + ensureThat(scenario.getGivenScenarios().size(), equalTo(0)); + ensureThat(scenario.getSteps(), equalTo(asList( "Given a step with a <one>", "When I run the scenario of name <two>", "Then I should see <three> in the output" @@ -229,5 +233,29 @@ ensureThat(table.getRow(1).get("two"), equalTo("e")); ensureThat(table.getRow(1).get("three"), equalTo("f")); } + + @Test + public void canParseStoryWithGivenScenarios() { + String wholeStory = + "Scenario: A scenario with given scenarios" + NL + NL + + "GivenScenarios: path/to/one,path/to/two" + NL + NL + + "Given a step with a <one>" + NL + + "When I run the scenario of name <two>" + NL + + "Then I should see <three> in the output"; + + StoryDefinition story = new PatternScenarioParser(new PropertyBasedConfiguration()).defineStoryFrom(wholeStory); + + ScenarioDefinition scenario = story.getScenarios().get(0); + ensureThat(scenario.getTitle(), equalTo("A scenario with given scenarios")); + ensureThat(scenario.getGivenScenarios(), equalTo(asList( + "path/to/one", + "path/to/two"))); + ensureThat(scenario.getSteps(), equalTo(asList( + "Given a step with a <one>", + "When I run the scenario of name <two>", + "Then I should see <three> in the output" + ))); + + } }
Modified: trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/steps/UnmatchedToPendingStepCreatorBehaviour.java (1221 => 1222)
--- trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/steps/UnmatchedToPendingStepCreatorBehaviour.java 2009-09-05 11:25:34 UTC (rev 1221) +++ trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/steps/UnmatchedToPendingStepCreatorBehaviour.java 2009-09-05 15:11:22 UTC (rev 1222) @@ -1,5 +1,6 @@ package org.jbehave.scenario.steps; +import static java.util.Arrays.asList; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.collection.IsArray.array; import static org.jbehave.Ensure.ensureThat; @@ -31,7 +32,7 @@ stub(steps.getSteps()).toReturn(new CandidateStep[] {candidate}); // When - Step[] executableSteps = creator.createStepsFrom(new ScenarioDefinition("", "my step"), tableValues, steps); + Step[] executableSteps = creator.createStepsFrom(new ScenarioDefinition("", asList("my step")), tableValues, steps); // Then ensureThat(executableSteps.length, equalTo(1)); @@ -50,8 +51,7 @@ stub(steps.getSteps()).toReturn(new CandidateStep[] {candidate}); // When - Step[] executableSteps = creator.createStepsFrom(new ScenarioDefinition("", "my step"), tableValues, steps); - + Step[] executableSteps = creator.createStepsFrom(new ScenarioDefinition("", asList("my step")), tableValues, steps); // Then ensureThat(executableSteps.length, equalTo(1)); StepResult result = executableSteps[0].perform(); @@ -85,7 +85,7 @@ // When we create the series of steps for the scenario UnmatchedToPendingStepCreator creator = new UnmatchedToPendingStepCreator(); - Step[] executableSteps = creator.createStepsFrom(new ScenarioDefinition("", "my step"), tableValues, steps1, steps2); + Step[] executableSteps = creator.createStepsFrom(new ScenarioDefinition("", asList("my step")), tableValues, steps1, steps2); // Then all before and after steps should be added ensureThat(executableSteps, array(equalTo(stepBefore2), equalTo(stepBefore1), equalTo(normalStep), equalTo(stepAfter1), equalTo(stepAfter2)));
Modified: trunk/core/jbehave-core/src/java/org/jbehave/scenario/ScenarioRunner.java (1221 => 1222)
--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/ScenarioRunner.java 2009-09-05 11:25:34 UTC (rev 1221) +++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/ScenarioRunner.java 2009-09-05 15:11:22 UTC (rev 1222) @@ -19,6 +19,8 @@ * and describes the results to the {...@link ScenarioReporter}. * * @author Elizabeth Keogh + * @author Mauro Talevi + * @author Paul Hammant */ public class ScenarioRunner { @@ -29,9 +31,16 @@ private ErrorStrategy errorStrategy; private Throwable throwable; - public ScenarioRunner() { + public void run(Class<? extends RunnableScenario> scenarioClass, Configuration configuration, CandidateSteps... candidateSteps) throws Throwable { + StoryDefinition story = configuration.forDefiningScenarios().loadScenarioDefinitionsFor(scenarioClass); + run(story, configuration, candidateSteps); } + public void run(String scenarioPath, Configuration configuration, CandidateSteps... candidateSteps) throws Throwable { + StoryDefinition story = configuration.forDefiningScenarios().loadScenarioDefinitionsFor(scenarioPath); + run(story, configuration, candidateSteps); + } + public void run(StoryDefinition story, Configuration configuration, CandidateSteps... candidateSteps) throws Throwable { reporter = configuration.forReportingScenarios(); pendingStepStrategy = configuration.forPendingSteps(); @@ -41,12 +50,10 @@ reporter.beforeStory(story.getBlurb()); for (ScenarioDefinition scenario : story.getScenarios()) { - Table table = scenario.getTable(); - if ( table != null && table.getRowCount() > 0 ){ - for (Map<String,String> tableValues : table.getRows() ) { - runScenario(configuration, scenario, tableValues, candidateSteps); - } - } else { + runGivenScenarios(configuration, scenario, candidateSteps); // first run any given scenarios, if any + if ( isTemplateScenario(scenario.getTable()) ){ // run template scenario + runTemplateScenario(configuration, scenario, scenario.getTable(), candidateSteps); + } else { // run plain old scenario runScenario(configuration, scenario, new HashMap<String, String>(), candidateSteps); } } @@ -54,6 +61,29 @@ currentStrategy.handleError(throwable); } + private boolean isTemplateScenario(Table table) { + return table != null && table.getRowCount() > 0; + } + + private void runTemplateScenario(Configuration configuration, + ScenarioDefinition scenario, Table table, + CandidateSteps... candidateSteps) { + for (Map<String,String> tableValues : table.getRows() ) { + runScenario(configuration, scenario, tableValues, candidateSteps); + } + } + + private void runGivenScenarios(Configuration configuration, + ScenarioDefinition scenario, CandidateSteps... candidateSteps) + throws Throwable { + if ( scenario.getGivenScenarios().size() > 0 ){ + for ( String scenarioPath : scenario.getGivenScenarios() ){ + reporter.givenScenario(scenarioPath); + run(scenarioPath, configuration, candidateSteps); + } + } + } + private void runScenario(Configuration configuration, ScenarioDefinition scenario, Map<String, String> tableValues, CandidateSteps... candidateSteps) { Step[] steps = configuration.forCreatingSteps().createStepsFrom(scenario, tableValues, candidateSteps);
Modified: trunk/core/jbehave-core/src/java/org/jbehave/scenario/definition/KeyWords.java (1221 => 1222)
--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/definition/KeyWords.java 2009-09-05 11:25:34 UTC (rev 1221) +++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/definition/KeyWords.java 2009-09-05 15:11:22 UTC (rev 1222) @@ -8,15 +8,17 @@ public class KeyWords { private final String scenario; + private final String givenScenarios; private final String given; private final String when; private final String then; private final String table; private final String[] others; - public KeyWords(String scenario, String given, String when, String then, - String table, String... others) { + public KeyWords(String scenario, String givenScenarios, String given, String when, + String then, String table, String... others) { this.scenario = scenario; + this.givenScenarios = givenScenarios; this.given = given; this.when = when; this.then = then; @@ -28,6 +30,10 @@ return scenario; } + public String givenScenarios() { + return givenScenarios; + } + public String table() { return table; } @@ -48,4 +54,5 @@ return others; } + }
Modified: trunk/core/jbehave-core/src/java/org/jbehave/scenario/definition/ScenarioDefinition.java (1221 => 1222)
--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/definition/ScenarioDefinition.java 2009-09-05 11:25:34 UTC (rev 1221) +++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/definition/ScenarioDefinition.java 2009-09-05 15:11:22 UTC (rev 1222) @@ -1,37 +1,49 @@ package org.jbehave.scenario.definition; -import java.util.Arrays; +import static java.util.Arrays.asList; + +import java.util.ArrayList; import java.util.List; public class ScenarioDefinition { + private final String title; + private final List<String> givenScenarios; private final List<String> steps; - private final String title; private final Table table; - public ScenarioDefinition(List<String> steps) { + public ScenarioDefinition(String title) { + this("", new ArrayList<String>()); + } + + public ScenarioDefinition(List<String> steps) { this("", steps); } public ScenarioDefinition(String title, List<String> steps) { - this(title, new Table(""), steps); + this(title, new ArrayList<String>(), new Table(""), steps); } - public ScenarioDefinition(String title, String... steps) { - this(title, Arrays.asList(steps)); + public ScenarioDefinition(String title, List<String> givenScenarios, List<String> steps) { + this(title, givenScenarios, new Table(""), steps); } - - public ScenarioDefinition(String title, Table table, List<String> steps) { + + public ScenarioDefinition(String title, List<String> givenScenarios, Table table, List<String> steps) { this.title = title; + this.givenScenarios = givenScenarios; this.steps = steps; this.table = table; } public ScenarioDefinition(String title, Table table, String... steps) { - this(title, table, Arrays.asList(steps)); + this(title, new ArrayList<String>(), table, asList(steps)); } + + public List<String> getGivenScenarios() { + return givenScenarios; + } - public List<String> getSteps() { + public List<String> getSteps() { return steps; }
Modified: trunk/core/jbehave-core/src/java/org/jbehave/scenario/definition/ScenarioGivenWhenThenAnd.java (1221 => 1222)
--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/definition/ScenarioGivenWhenThenAnd.java 2009-09-05 11:25:34 UTC (rev 1221) +++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/definition/ScenarioGivenWhenThenAnd.java 2009-09-05 15:11:22 UTC (rev 1222) @@ -3,7 +3,7 @@ public class ScenarioGivenWhenThenAnd extends KeyWords { public ScenarioGivenWhenThenAnd() { - super("Scenario:", "Given", "When", "Then", "Examples:", "And"); + super("Scenario:", "GivenScenarios:", "Given", "When", "Then", "Examples:", "And"); } }
Modified: trunk/core/jbehave-core/src/java/org/jbehave/scenario/parser/ClasspathScenarioDefiner.java (1221 => 1222)
--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/parser/ClasspathScenarioDefiner.java 2009-09-05 11:25:34 UTC (rev 1221) +++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/parser/ClasspathScenarioDefiner.java 2009-09-05 15:11:22 UTC (rev 1222) @@ -48,15 +48,23 @@ return parser.defineStoryFrom(wholeFileAsString); } + public StoryDefinition loadScenarioDefinitionsFor(String scenarioPath) { + String wholeFileAsString = asString(loadInputStreamFor(scenarioPath)); + return parser.defineStoryFrom(wholeFileAsString); + } + private InputStream loadInputStreamFor(Class<? extends RunnableScenario> scenarioClass) { - String scenarioName = resolver.resolve(scenarioClass); - InputStream stream = classLoader.getResourceAsStream(scenarioName); + return loadInputStreamFor(resolver.resolve(scenarioClass)); + } + + private InputStream loadInputStreamFor(String scenarioPath) { + InputStream stream = classLoader.getResourceAsStream(scenarioPath); if (stream == null) { - throw new ScenarioNotFoundException("Scenario " + scenarioName + " could not be found by classloader " + throw new ScenarioNotFoundException("Scenario " + scenarioPath + " could not be found by classloader " + classLoader); } return stream; - } + } private String asString(InputStream stream) { try { @@ -69,4 +77,5 @@ throw new InvalidScenarioResourceException("Failed to convert scenario resource to string", e); } } + }
Modified: trunk/core/jbehave-core/src/java/org/jbehave/scenario/parser/PatternScenarioParser.java (1221 => 1222)
--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/parser/PatternScenarioParser.java 2009-09-05 11:25:34 UTC (rev 1221) +++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/parser/PatternScenarioParser.java 2009-09-05 15:11:22 UTC (rev 1222) @@ -46,15 +46,16 @@ for (String scenario : scenarios) { String title = findTitle(scenario); Table table = findTable(scenario); + List<String> givenScenarios = findGivenScenarios(scenario); List<String> steps = findSteps(scenario); scenarioDefinitions - .add(new ScenarioDefinition(title, table, steps)); + .add(new ScenarioDefinition(title, givenScenarios, table, steps)); } return scenarioDefinitions; } private String findTitle(String scenario) { - Matcher findingTitle = patternToPullScenarioTitlesIntoGroupOne() + Matcher findingTitle = patternToPullScenarioTitleIntoGroupOne() .matcher(scenario); return findingTitle.find() ? findingTitle.group(1).trim() : ""; } @@ -66,6 +67,20 @@ return new Table(table); } + private List<String> findGivenScenarios(String scenario) { + Matcher findingGivenScenarios = patternToPullGivenScenariosIntoGroupOne() + .matcher(scenario); + String givenScenariosAsCSV = findingGivenScenarios.find() ? findingGivenScenarios.group(1).trim() : ""; + List<String> givenScenarios = new ArrayList<String>(); + for ( String givenScenario : givenScenariosAsCSV.split(",") ){ + String trimmed = givenScenario.trim(); + if ( trimmed.length() > 0 ) { + givenScenarios.add(trimmed); + } + } + return givenScenarios; + } + private List<String> findSteps(String scenarioAsString) { Matcher matcher = patternToPullOutSteps().matcher(scenarioAsString); List<String> steps = new ArrayList<String>(); @@ -153,17 +168,25 @@ + ").*", DOTALL); } + private Pattern patternToPullGivenScenariosIntoGroupOne() { + KeyWords keywords = configuration.keywords(); + String givenScenarios = keywords.givenScenarios(); + String concatenatedKeywords = concatenateWithOr(keywords.given(), + keywords.when(), keywords.then(), keywords.others()); + return compile(".*"+givenScenarios+"(.*?)\\s*(" + concatenatedKeywords + ").*"); + } + private Pattern patternToPullScenarioTableIntoGroupOne() { KeyWords keywords = configuration.keywords(); String table = keywords.table(); return compile(".*"+table+"\\s*((.|\\s)*)"); } - private Pattern patternToPullScenarioTitlesIntoGroupOne() { + private Pattern patternToPullScenarioTitleIntoGroupOne() { KeyWords keywords = configuration.keywords(); + String scenario = keywords.scenario(); String concatenatedKeywords = concatenateWithOr(keywords.given(), keywords.when(), keywords.then(), keywords.others()); - String scenario = keywords.scenario(); return compile(scenario + "(.*?)\\s*(" + concatenatedKeywords + ").*"); }
Modified: trunk/core/jbehave-core/src/java/org/jbehave/scenario/parser/ScenarioDefiner.java (1221 => 1222)
--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/parser/ScenarioDefiner.java 2009-09-05 11:25:34 UTC (rev 1221) +++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/parser/ScenarioDefiner.java 2009-09-05 15:11:22 UTC (rev 1222) @@ -12,4 +12,6 @@ StoryDefinition loadScenarioDefinitionsFor(Class<? extends RunnableScenario> scenarioClass); + StoryDefinition loadScenarioDefinitionsFor(String scenarioPath); + }
Modified: trunk/core/jbehave-core/src/java/org/jbehave/scenario/reporters/PassSilentlyDecorator.java (1221 => 1222)
--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/reporters/PassSilentlyDecorator.java 2009-09-05 11:25:34 UTC (rev 1221) +++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/reporters/PassSilentlyDecorator.java 2009-09-05 15:11:22 UTC (rev 1222) @@ -116,4 +116,12 @@ void report(); } + + public void givenScenario(final String scenarioPath) { +// currentScenario.add(new Todo() { +// public void doNow() { +// delegate.givenScenario(scenarioPath); +// } +// }); + } }
Modified: trunk/core/jbehave-core/src/java/org/jbehave/scenario/reporters/PrintStreamScenarioReporter.java (1221 => 1222)
--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/reporters/PrintStreamScenarioReporter.java 2009-09-05 11:25:34 UTC (rev 1221) +++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/reporters/PrintStreamScenarioReporter.java 2009-09-05 15:11:22 UTC (rev 1222) @@ -68,4 +68,8 @@ output.println(); } + public void givenScenario(String scenarioPath) { + output.println("GivenScenario: "+scenarioPath); + } + }
Modified: trunk/core/jbehave-core/src/java/org/jbehave/scenario/reporters/ScenarioReporter.java (1221 => 1222)
--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/reporters/ScenarioReporter.java 2009-09-05 11:25:34 UTC (rev 1221) +++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/reporters/ScenarioReporter.java 2009-09-05 15:11:22 UTC (rev 1222) @@ -25,5 +25,6 @@ void afterStory(); + void givenScenario(String scenarioPath); }
Modified: trunk/core/jbehave-core/src/java/org/jbehave/scenario/reporters/StepFailureScenarioReporterDecorator.java (1221 => 1222)
--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/reporters/StepFailureScenarioReporterDecorator.java 2009-09-05 11:25:34 UTC (rev 1221) +++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/reporters/StepFailureScenarioReporterDecorator.java 2009-09-05 15:11:22 UTC (rev 1222) @@ -59,4 +59,8 @@ delegate.successful(step); } + public void givenScenario(String scenarioPath) { + delegate.givenScenario(scenarioPath); + } + }
To unsubscribe from this list please visit:
