- Revision
- 900
- Author
- sirenian
- Date
- 2008-08-28 08:01:44 -0500 (Thu, 28 Aug 2008)
Log Message
[Liz] Some tidy up, internationalisation put back in again - lolcatz stories work...
Modified Paths
- trunk/examples/gameoflife/src/scenario/com/lunivore/gameoflife/ICanToggleACell.java
- trunk/examples/gameoflife/src/scenario/com/lunivore/gameoflife/TheGridStartsEmpty.java
- trunk/examples/trader/src/main/java/org/jbehave/examples/trader/converters/TraderConverter.java
- trunk/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/StatusAlertCanBeActivated.java
- trunk/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/StatusAlertIsNeverActivated.java
- trunk/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/StockSteps.java
- trunk/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/TraderSellsAllStocks.java
- trunk/jbehave-core/src/behaviour/org/jbehave/scenario/PropertyBasedConfigurationBehaviour.java
- trunk/jbehave-core/src/behaviour/org/jbehave/scenario/ScenarioBehaviour.java
- trunk/jbehave-core/src/behaviour/org/jbehave/scenario/parser/PatternScenarioParserBehaviour.java
- trunk/jbehave-core/src/behaviour/org/jbehave/scenario/parser/ScenarioFileLoaderBehaviour.java
- trunk/jbehave-core/src/java/org/jbehave/Configuration.java
- trunk/jbehave-core/src/java/org/jbehave/scenario/PropertyBasedConfiguration.java
- trunk/jbehave-core/src/java/org/jbehave/scenario/parser/PatternScenarioParser.java
- trunk/jbehave-core/src/java/org/jbehave/scenario/parser/ScenarioFileLoader.java
- trunk/jbehave-core/src/java/org/jbehave/scenario/steps/Steps.java
Added Paths
- trunk/examples/noughtsandcrosses/src/scenario/com/lunivore/noughtsandcrosses/PlayersCanHazTurns.java
- trunk/examples/noughtsandcrosses/src/scenario/com/lunivore/noughtsandcrosses/players_can_haz_turns
- trunk/examples/noughtsandcrosses/src/scenario/com/lunivore/noughtsandcrosses/steps/LolCatzSteps.java
- trunk/jbehave-core/src/java/org/jbehave/scenario/MostUsefulConfiguration.java
- trunk/jbehave-core/src/java/org/jbehave/scenario/definition/KeyWords.java
- trunk/jbehave-core/src/java/org/jbehave/scenario/definition/ScenarioGivenWhenThenAnd.java
Diff
Modified: trunk/examples/gameoflife/src/scenario/com/lunivore/gameoflife/ICanToggleACell.java (899 => 900)
--- trunk/examples/gameoflife/src/scenario/com/lunivore/gameoflife/ICanToggleACell.java 2008-08-26 13:38:55 UTC (rev 899) +++ trunk/examples/gameoflife/src/scenario/com/lunivore/gameoflife/ICanToggleACell.java 2008-08-28 13:01:44 UTC (rev 900) @@ -19,7 +19,7 @@ @Override public ScenarioFileLoader forDefiningScenarios() { return new ScenarioFileLoader(new UnderscoredCamelCaseResolver(), classLoader, - new PatternScenarioParser()); + new PatternScenarioParser(this)); } }, new GridSteps()); }
Modified: trunk/examples/gameoflife/src/scenario/com/lunivore/gameoflife/TheGridStartsEmpty.java (899 => 900)
--- trunk/examples/gameoflife/src/scenario/com/lunivore/gameoflife/TheGridStartsEmpty.java 2008-08-26 13:38:55 UTC (rev 899) +++ trunk/examples/gameoflife/src/scenario/com/lunivore/gameoflife/TheGridStartsEmpty.java 2008-08-28 13:01:44 UTC (rev 900) @@ -19,7 +19,7 @@ @Override public ScenarioFileLoader forDefiningScenarios() { return new ScenarioFileLoader(new UnderscoredCamelCaseResolver(), classLoader, - new PatternScenarioParser()); + new PatternScenarioParser(this)); } }, new GridSteps()); }
Added: trunk/examples/noughtsandcrosses/src/scenario/com/lunivore/noughtsandcrosses/PlayersCanHazTurns.java (0 => 900)
--- trunk/examples/noughtsandcrosses/src/scenario/com/lunivore/noughtsandcrosses/PlayersCanHazTurns.java (rev 0) +++ trunk/examples/noughtsandcrosses/src/scenario/com/lunivore/noughtsandcrosses/PlayersCanHazTurns.java 2008-08-28 13:01:44 UTC (rev 900) @@ -0,0 +1,22 @@ +package com.lunivore.noughtsandcrosses; + +import org.jbehave.scenario.MostUsefulConfiguration; +import org.jbehave.scenario.Scenario; +import org.jbehave.scenario.definition.KeyWords; + +import com.lunivore.noughtsandcrosses.steps.LolCatzSteps; + +/** + * Checks that we can support scenarios written in other languages, + * eg: lolcatz + */ +public class PlayersCanHazTurns extends Scenario { + + public PlayersCanHazTurns() { + super(new MostUsefulConfiguration() { + public KeyWords keywords() { + return new KeyWords("I can haz", "Gief", "Wen", "Den", "And"); + } + }, new LolCatzSteps()); + } +}
Added: trunk/examples/noughtsandcrosses/src/scenario/com/lunivore/noughtsandcrosses/players_can_haz_turns (0 => 900)
--- trunk/examples/noughtsandcrosses/src/scenario/com/lunivore/noughtsandcrosses/players_can_haz_turns (rev 0) +++ trunk/examples/noughtsandcrosses/src/scenario/com/lunivore/noughtsandcrosses/players_can_haz_turns 2008-08-28 13:01:44 UTC (rev 900) @@ -0,0 +1,21 @@ +I can haz: X takes a turn + +Gief game +Wen I clicks a1 +Den I haz grid +X.. +... +... + +I can haz: O takes a turn + +Gief game like +X.. +... +... +Wen I clicks a2 +Den I haz grid +XO. +... +... +And message sez "X's turn"
Added: trunk/examples/noughtsandcrosses/src/scenario/com/lunivore/noughtsandcrosses/steps/LolCatzSteps.java (0 => 900)
--- trunk/examples/noughtsandcrosses/src/scenario/com/lunivore/noughtsandcrosses/steps/LolCatzSteps.java (rev 0) +++ trunk/examples/noughtsandcrosses/src/scenario/com/lunivore/noughtsandcrosses/steps/LolCatzSteps.java 2008-08-28 13:01:44 UTC (rev 900) @@ -0,0 +1,86 @@ +package com.lunivore.noughtsandcrosses.steps; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.jbehave.Ensure.ensureThat; + +import java.awt.Component; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import javax.swing.JLabel; + +import org.jbehave.scenario.annotations.Given; +import org.jbehave.scenario.annotations.Then; +import org.jbehave.scenario.annotations.When; +import org.jbehave.scenario.steps.Steps; +import org.lunivore.tyburn.WindowControl; + +import com.lunivore.noughtsandcrosses.NoughtsAndCrosses; +import com.lunivore.noughtsandcrosses.view.ComponentNames; + +public class LolCatzSteps extends Steps { + public static String ROWS = "abc"; + public static String COLUMNS = "123"; + protected static final String NL = System.getProperty("line.separator"); + protected WindowControl windowControl; + + public LolCatzSteps() { + super("Gief", "Wen", "Den", "And"); + } + + @Given("game") + public void givenTheGameIsRunning() { + windowControl = new WindowControl(ComponentNames.NOUGHTSANDCROSSES); + new NoughtsAndCrosses(); + } + + @Given("game like $grid") + public void givenThatTheGridLooksLike(String grid) throws Exception { + givenTheGameIsRunning(); + ArrayList<String> oTurns = new ArrayList<String>(); + ArrayList<String> xTurns = new ArrayList<String>(); + + captureMoves(oTurns, xTurns, grid); + performMoves(oTurns, xTurns); + } + + @Then("message sez \"$message\"") + public void thenTheMessageShouldRead(String message) throws Exception { + JLabel messageLabel = (JLabel) windowControl.findComponent(ComponentNames.MESSAGE); + ensureThat(messageLabel.getText(), equalTo(message)); + } + + @Then("I haz grid $grid") + public void thenTheGridShouldLookLike(String grid) throws Exception { + Component gridPanel = windowControl.findComponent(ComponentNames.GRID); + ensureThat(gridPanel.toString(), equalTo(grid)); + } + + @When("I clicks $space") + public void whenPlayerClicksInSpace(String space) throws Exception { + windowControl.clickButton(space); + } + + private void performMoves(List<String> oTurns, List<String> xTurns) throws Exception { + while (xTurns.size() > 0) { + whenPlayerClicksInSpace(xTurns.remove(0)); + if (oTurns.size() >0) { + whenPlayerClicksInSpace(oTurns.remove(0)); + } + } + } + + private void captureMoves(List<String> oTurns, List<String> xTurns, String grid) { + + List<String> lines = Arrays.asList(grid.split(NL)); + for(int row=0;row<3;row++) { + for(int col=0;col<3;col++) { + char player = lines.get(row).charAt(col); + String spaceLabel = "" + ROWS.charAt(row) + COLUMNS.charAt(col); + if(player == 'O') {oTurns.add(spaceLabel);} + if(player == 'X') {xTurns.add(spaceLabel);} + } + } + } +}
Modified: trunk/examples/trader/src/main/java/org/jbehave/examples/trader/converters/TraderConverter.java (899 => 900)
--- trunk/examples/trader/src/main/java/org/jbehave/examples/trader/converters/TraderConverter.java 2008-08-26 13:38:55 UTC (rev 899) +++ trunk/examples/trader/src/main/java/org/jbehave/examples/trader/converters/TraderConverter.java 2008-08-28 13:01:44 UTC (rev 900) @@ -4,8 +4,8 @@ import org.jbehave.examples.trader.model.Trader; import org.jbehave.examples.trader.persistence.TraderPersister; +import org.jbehave.scenario.steps.ParameterConverters.InvalidParameterException; import org.jbehave.scenario.steps.ParameterConverters.ParameterConverter; -import org.jbehave.scenario.steps.ParameterConverters.InvalidParameterException; public class TraderConverter implements ParameterConverter { private TraderPersister persister;
Modified: trunk/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/StatusAlertCanBeActivated.java (899 => 900)
--- trunk/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/StatusAlertCanBeActivated.java 2008-08-26 13:38:55 UTC (rev 899) +++ trunk/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/StatusAlertCanBeActivated.java 2008-08-28 13:01:44 UTC (rev 900) @@ -1,5 +1,6 @@ package org.jbehave.examples.trader.scenarios; +import org.jbehave.scenario.MostUsefulConfiguration; import org.jbehave.scenario.PropertyBasedConfiguration; import org.jbehave.scenario.Scenario; import org.jbehave.scenario.parser.PatternScenarioParser; @@ -15,9 +16,9 @@ } public StatusAlertCanBeActivated(final ClassLoader classLoader) { - super(new PropertyBasedConfiguration() { + super(new MostUsefulConfiguration() { public ScenarioDefiner forDefiningScenarios() { - return new ScenarioFileLoader(new UnderscoredCamelCaseResolver(".scenario"), classLoader, new PatternScenarioParser()); + return new ScenarioFileLoader(new UnderscoredCamelCaseResolver(".scenario"), classLoader, new PatternScenarioParser(new PropertyBasedConfiguration())); } }, new StockSteps(10.0)); }
Modified: trunk/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/StatusAlertIsNeverActivated.java (899 => 900)
--- trunk/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/StatusAlertIsNeverActivated.java 2008-08-26 13:38:55 UTC (rev 899) +++ trunk/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/StatusAlertIsNeverActivated.java 2008-08-28 13:01:44 UTC (rev 900) @@ -17,7 +17,7 @@ super(new PropertyBasedConfiguration() { @Override public ScenarioFileLoader forDefiningScenarios() { - return new ScenarioFileLoader(new UnderscoredCamelCaseResolver(".scenario"), classLoader, new PatternScenarioParser()); + return new ScenarioFileLoader(new UnderscoredCamelCaseResolver(".scenario"), classLoader, new PatternScenarioParser(new PropertyBasedConfiguration())); } }, new StockSteps(100.0)); }
Modified: trunk/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/StockSteps.java (899 => 900)
--- trunk/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/StockSteps.java 2008-08-26 13:38:55 UTC (rev 899) +++ trunk/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/StockSteps.java 2008-08-28 13:01:44 UTC (rev 900) @@ -15,13 +15,13 @@ 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; public class StockSteps extends Steps { - private static final PrintStreamStepMonitor MONITOR = new PrintStreamStepMonitor(); + private static final StepMonitor MONITOR = new SilentStepMonitor(); private double threshold; private Stock stock; private Trader trader;
Modified: trunk/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/TraderSellsAllStocks.java (899 => 900)
--- trunk/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/TraderSellsAllStocks.java 2008-08-26 13:38:55 UTC (rev 899) +++ trunk/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/TraderSellsAllStocks.java 2008-08-28 13:01:44 UTC (rev 900) @@ -17,7 +17,7 @@ public TraderSellsAllStocks(final ClassLoader classLoader) { super(new PropertyBasedConfiguration() { public ScenarioDefiner forDefiningScenarios() { - return new ScenarioFileLoader(new UnderscoredCamelCaseResolver(".scenario"), classLoader, new PatternScenarioParser()); + return new ScenarioFileLoader(new UnderscoredCamelCaseResolver(".scenario"), classLoader, new PatternScenarioParser(this)); } }, new StockSteps(10.0)); }
Modified: trunk/jbehave-core/src/behaviour/org/jbehave/scenario/PropertyBasedConfigurationBehaviour.java (899 => 900)
--- trunk/jbehave-core/src/behaviour/org/jbehave/scenario/PropertyBasedConfigurationBehaviour.java 2008-08-26 13:38:55 UTC (rev 899) +++ trunk/jbehave-core/src/behaviour/org/jbehave/scenario/PropertyBasedConfigurationBehaviour.java 2008-08-28 13:01:44 UTC (rev 900) @@ -4,6 +4,7 @@ import static org.hamcrest.CoreMatchers.is; import static org.jbehave.Ensure.ensureThat; +import org.jbehave.scenario.definition.ScenarioGivenWhenThenAnd; import org.jbehave.scenario.errors.ErrorStrategy; import org.jbehave.scenario.reporters.PassSilentlyDecorator; import org.jbehave.scenario.reporters.PrintStreamScenarioReporter; @@ -65,4 +66,9 @@ public void shouldRethrowErrrors() { ensureThat(new PropertyBasedConfiguration().forHandlingErrors(), equalTo(ErrorStrategy.RETHROW)); } + + @Test + public void shouldProvideGivenWhenThenKeywordsByDefault() { + ensureThat(new PropertyBasedConfiguration().keywords(), is(ScenarioGivenWhenThenAnd.class)); + } }
Modified: trunk/jbehave-core/src/behaviour/org/jbehave/scenario/ScenarioBehaviour.java (899 => 900)
--- trunk/jbehave-core/src/behaviour/org/jbehave/scenario/ScenarioBehaviour.java 2008-08-26 13:38:55 UTC (rev 899) +++ trunk/jbehave-core/src/behaviour/org/jbehave/scenario/ScenarioBehaviour.java 2008-08-28 13:01:44 UTC (rev 900) @@ -8,6 +8,7 @@ import org.jbehave.Configuration; import org.jbehave.scenario.definition.Blurb; +import org.jbehave.scenario.definition.KeyWords; import org.jbehave.scenario.errors.ErrorStrategy; import org.jbehave.scenario.parser.ScenarioDefiner; import org.jbehave.scenario.reporters.ScenarioReporter; @@ -60,6 +61,8 @@ public PendingStepStrategy forPendingSteps() { return null; } public ScenarioReporter forReportingScenarios() { return null; } + + public KeyWords keywords() { return null; } } }
Modified: trunk/jbehave-core/src/behaviour/org/jbehave/scenario/parser/PatternScenarioParserBehaviour.java (899 => 900)
--- trunk/jbehave-core/src/behaviour/org/jbehave/scenario/parser/PatternScenarioParserBehaviour.java 2008-08-26 13:38:55 UTC (rev 899) +++ trunk/jbehave-core/src/behaviour/org/jbehave/scenario/parser/PatternScenarioParserBehaviour.java 2008-08-28 13:01:44 UTC (rev 900) @@ -6,6 +6,7 @@ import java.util.Arrays; import java.util.List; +import org.jbehave.scenario.PropertyBasedConfiguration; import org.jbehave.scenario.StoryDefinition; import org.junit.Test; @@ -16,7 +17,7 @@ @Test public void shouldExtractGivensWhensAndThensFromSimpleScenarios() { - ScenarioParser parser = new PatternScenarioParser(); + ScenarioParser parser = new PatternScenarioParser(new PropertyBasedConfiguration()); StoryDefinition story = parser.defineStoryFrom( "Given a scenario" + NL + "When I parse it" + NL + @@ -30,7 +31,7 @@ @Test public void shouldExtractGivensWhensAndThensFromMultilineScenarios() { - ScenarioParser parser = new PatternScenarioParser(); + ScenarioParser parser = new PatternScenarioParser(new PropertyBasedConfiguration()); StoryDefinition story = parser.defineStoryFrom( "Given a scenario" + NL + "with this line" + NL + @@ -58,7 +59,7 @@ "Given my scenario" + NL + NL + "Scenario: the second scenario" + NL + NL + "Given my second scenario"; - PatternScenarioParser parser = new PatternScenarioParser(); + PatternScenarioParser parser = new PatternScenarioParser(new PropertyBasedConfiguration()); StoryDefinition story = parser.defineStoryFrom(wholeStory); ensureThat(story.getScenarios().get(0).getTitle(), equalTo("the first scenario")); @@ -91,9 +92,10 @@ "Given a step that fails" + NL + "When I run the scenario" + NL + - "Then I should see this in the output" + NL; + "Then I should see this in the output" + NL + + "And I should see this in the output" + NL; - StoryDefinition story = new PatternScenarioParser().defineStoryFrom(wholeStory); + StoryDefinition story = new PatternScenarioParser(new PropertyBasedConfiguration()).defineStoryFrom(wholeStory); ensureThat(story.getBlurb().asString(), equalTo("Story: I can output narratives" + NL + NL + "As a developer" + NL + @@ -118,7 +120,8 @@ ensureThat(story.getScenarios().get(2).getSteps(), equalTo(Arrays.asList(new String[]{ "Given a step that fails", "When I run the scenario", - "Then I should see this in the output" + "Then I should see this in the output", + "And I should see this in the output" }))); }
Modified: trunk/jbehave-core/src/behaviour/org/jbehave/scenario/parser/ScenarioFileLoaderBehaviour.java (899 => 900)
--- trunk/jbehave-core/src/behaviour/org/jbehave/scenario/parser/ScenarioFileLoaderBehaviour.java 2008-08-26 13:38:55 UTC (rev 899) +++ trunk/jbehave-core/src/behaviour/org/jbehave/scenario/parser/ScenarioFileLoaderBehaviour.java 2008-08-28 13:01:44 UTC (rev 900) @@ -6,6 +6,7 @@ import java.io.IOException; import java.io.InputStream; +import org.jbehave.scenario.PropertyBasedConfiguration; import org.jbehave.scenario.Scenario; import org.jbehave.scenario.errors.InvalidScenarioResourceException; import org.jbehave.scenario.errors.ScenarioNotFoundException; @@ -38,7 +39,7 @@ @Test(expected = InvalidScenarioResourceException.class) public void cannotLoadScenarioForInvalidResource() { - ScenarioFileLoader loader = new ScenarioFileLoader(new InvalidClassLoader()); + ScenarioFileLoader loader = new ScenarioFileLoader(new UnderscoredCamelCaseResolver(), new InvalidClassLoader(), new PatternScenarioParser(new PropertyBasedConfiguration())); loader.loadScenarioDefinitionsFor(MyPendingScenario.class); }
Modified: trunk/jbehave-core/src/java/org/jbehave/Configuration.java (899 => 900)
--- trunk/jbehave-core/src/java/org/jbehave/Configuration.java 2008-08-26 13:38:55 UTC (rev 899) +++ trunk/jbehave-core/src/java/org/jbehave/Configuration.java 2008-08-28 13:01:44 UTC (rev 900) @@ -1,5 +1,6 @@ package org.jbehave; +import org.jbehave.scenario.definition.KeyWords; import org.jbehave.scenario.errors.ErrorStrategy; import org.jbehave.scenario.parser.ScenarioDefiner; import org.jbehave.scenario.reporters.ScenarioReporter; @@ -7,8 +8,17 @@ import org.jbehave.scenario.steps.StepCreator; /** - * Provides techniques for defining, parsing and reporting scenarios. + * Provides the configuration with which JBehave runs. * + * Anyone wishing to customize JBehave may wish + * to inject a new Configuration into their Scenario. + * + * NB: This class may change dynamically, so any other class + * wishing to use this should store the whole configuration, + * and use the respective parts of it at runtime, rather + * than attempting to store any part of it when the configuration + * is provided. + * * @author Elizabeth Keogh */ public interface Configuration { @@ -22,5 +32,7 @@ StepCreator forCreatingSteps(); ErrorStrategy forHandlingErrors(); + + KeyWords keywords(); }
Added: trunk/jbehave-core/src/java/org/jbehave/scenario/MostUsefulConfiguration.java (0 => 900)
--- trunk/jbehave-core/src/java/org/jbehave/scenario/MostUsefulConfiguration.java (rev 0) +++ trunk/jbehave-core/src/java/org/jbehave/scenario/MostUsefulConfiguration.java 2008-08-28 13:01:44 UTC (rev 900) @@ -0,0 +1,86 @@ +package org.jbehave.scenario; + +import org.jbehave.Configuration; +import org.jbehave.scenario.definition.KeyWords; +import org.jbehave.scenario.definition.ScenarioGivenWhenThenAnd; +import org.jbehave.scenario.errors.ErrorStrategy; +import org.jbehave.scenario.parser.PatternScenarioParser; +import org.jbehave.scenario.parser.ScenarioDefiner; +import org.jbehave.scenario.parser.ScenarioFileLoader; +import org.jbehave.scenario.reporters.PassSilentlyDecorator; +import org.jbehave.scenario.reporters.PrintStreamScenarioReporter; +import org.jbehave.scenario.reporters.ScenarioReporter; +import org.jbehave.scenario.steps.PendingStepStrategy; +import org.jbehave.scenario.steps.StepCreator; +import org.jbehave.scenario.steps.UnmatchedToPendingStepCreator; + +/** + * The default configuration for JBehave's scenario runner. Works + * for most things that users want to do. + */ +public class MostUsefulConfiguration implements Configuration { + + /** + * Provides pending steps where unmatched steps exist. + */ + public StepCreator forCreatingSteps() { + return new UnmatchedToPendingStepCreator(); + } + + /** + * Defines scenarios by looking for a file named after + * the scenario and in the same package, using + * lower-case and underscores in place + * of the camel-cased name - so MyScenario.java maps to + * my_scenario. + */ + public ScenarioDefiner forDefiningScenarios() { + return new ScenarioFileLoader(new PatternScenarioParser(this)); + } + + /** + * Handles errors by rethrowing them. + * + * <p>If there are multiple scenarios in a single story definition, + * this could cause the story to stop after the first failing scenario. + * + * <p>If you want different behaviour, you might want to look at the + * ErrorStrategyInWhichWeTrustTheReporter. + */ + public ErrorStrategy forHandlingErrors() { + return ErrorStrategy.RETHROW; + } + + /** + * Allows pending steps to pass, so that builds etc. will not fail. + * + * <p>If you want to spot pending steps, you might want to look at + * PendingStepStrategy.FAILING, or alternatively at the + * PropertyBasedConfiguration which provides a mechanism for + * altering this behaviour in different environments. + */ + public PendingStepStrategy forPendingSteps() { + return PendingStepStrategy.PASSING; + } + + /** + * Reports failing or pending scenarios to System.out, while silently + * passing scenarios. + * + * <p>If you want different behaviour, you might like to use the + * PrintStreamScenarioReporter, or look at the PropertyBasedConfiguration + * which provides a mechanism for altering this behaviour in different + * environments. + */ + public ScenarioReporter forReportingScenarios() { + return new PassSilentlyDecorator(new PrintStreamScenarioReporter()); + } + + /** + * Provides the keywords Scenario, Given, When, Then and And. + */ + public KeyWords keywords() { + return new ScenarioGivenWhenThenAnd(); + } + +}
Modified: trunk/jbehave-core/src/java/org/jbehave/scenario/PropertyBasedConfiguration.java (899 => 900)
--- trunk/jbehave-core/src/java/org/jbehave/scenario/PropertyBasedConfiguration.java 2008-08-26 13:38:55 UTC (rev 899) +++ trunk/jbehave-core/src/java/org/jbehave/scenario/PropertyBasedConfiguration.java 2008-08-28 13:01:44 UTC (rev 900) @@ -1,47 +1,92 @@ package org.jbehave.scenario; import org.jbehave.Configuration; +import org.jbehave.scenario.definition.KeyWords; import org.jbehave.scenario.errors.ErrorStrategy; -import org.jbehave.scenario.parser.PatternScenarioParser; import org.jbehave.scenario.parser.ScenarioDefiner; -import org.jbehave.scenario.parser.ScenarioFileLoader; -import org.jbehave.scenario.parser.UnderscoredCamelCaseResolver; -import org.jbehave.scenario.reporters.PassSilentlyDecorator; import org.jbehave.scenario.reporters.PrintStreamScenarioReporter; import org.jbehave.scenario.reporters.ScenarioReporter; import org.jbehave.scenario.steps.PendingStepStrategy; import org.jbehave.scenario.steps.StepCreator; -import org.jbehave.scenario.steps.UnmatchedToPendingStepCreator; +/** + * This is backed by the MostUsefulConfiguration, but has different + * behaviour if certain system properties are non-null. + */ public class PropertyBasedConfiguration implements Configuration { public static final String FAIL_ON_PENDING = "org.jbehave.failonpending"; public static String OUTPUT_ALL = "org.jbehave.outputall"; + private final Configuration defaults; + + public PropertyBasedConfiguration() { + this(new MostUsefulConfiguration()); + } + public PropertyBasedConfiguration(Configuration defaults) { + this.defaults = defaults; + } + + /** + * If the system property org.jbehave.outputall + * is set to TRUE, uses a PrintStreamScenarioReporter; + * otherwise uses the default ScenarioReporter. + * + * Setting org.jbehave.outputall will allow you + * to see the steps for all scenarios, regardless + * of whether the scenarios fail. + */ public ScenarioReporter forReportingScenarios() { if (System.getProperty(OUTPUT_ALL) == null) { - return new PassSilentlyDecorator(new PrintStreamScenarioReporter()); + return defaults.forReportingScenarios(); } else { return new PrintStreamScenarioReporter(); } } + /** + * Returns the default ScenarioDefiner. + */ public ScenarioDefiner forDefiningScenarios() { - return new ScenarioFileLoader(new UnderscoredCamelCaseResolver(), new PatternScenarioParser()); + return defaults.forDefiningScenarios(); } + /** + * If the system property org.jbehave.failonpending + * is non-null, returns PendingStepStrategy.FAILING, + * otherwise returns the defaults. + * + * <p>Setting org.jbehave.failonpending will cause + * pending steps to throw an error, + * so you can see if any steps don't match or are + * still to be implemented. + */ public PendingStepStrategy forPendingSteps() { if (System.getProperty(FAIL_ON_PENDING) == null) { - return PendingStepStrategy.PASSING; + return defaults.forPendingSteps(); } return PendingStepStrategy.FAILING; } + /** + * Returns the default StepCreator. + */ public StepCreator forCreatingSteps() { - return new UnmatchedToPendingStepCreator(); + return defaults.forCreatingSteps(); } + /** + * Returns the default ErrorStrategy for handling + * errors. + */ public ErrorStrategy forHandlingErrors() { - return ErrorStrategy.RETHROW; + return defaults.forHandlingErrors(); + } + + /** + * Returns the default keywords. + */ + public KeyWords keywords() { + return defaults.keywords(); } }
Added: trunk/jbehave-core/src/java/org/jbehave/scenario/definition/KeyWords.java (0 => 900)
--- trunk/jbehave-core/src/java/org/jbehave/scenario/definition/KeyWords.java (rev 0) +++ trunk/jbehave-core/src/java/org/jbehave/scenario/definition/KeyWords.java 2008-08-28 13:01:44 UTC (rev 900) @@ -0,0 +1,45 @@ +package org.jbehave.scenario.definition; + +/** + * Provides the keywords which allow parsers to find steps + * in scenarios and match those steps with candidates + * through the Given, When and Then annotations + */ +public class KeyWords { + + private final String scenario; + private final String given; + private final String when; + private final String then; + private final String[] others; + + public KeyWords(String scenario, String given, String when, + String then, String... others) { + this.scenario = scenario; + this.given = given; + this.when = when; + this.then = then; + this.others = others; + } + + public String scenario() { + return scenario; + } + + public String given() { + return given; + } + + public String when() { + return when; + } + + public String then() { + return then; + } + + public String[] others() { + return others; + } + +}
Added: trunk/jbehave-core/src/java/org/jbehave/scenario/definition/ScenarioGivenWhenThenAnd.java (0 => 900)
--- trunk/jbehave-core/src/java/org/jbehave/scenario/definition/ScenarioGivenWhenThenAnd.java (rev 0) +++ trunk/jbehave-core/src/java/org/jbehave/scenario/definition/ScenarioGivenWhenThenAnd.java 2008-08-28 13:01:44 UTC (rev 900) @@ -0,0 +1,9 @@ +package org.jbehave.scenario.definition; + +public class ScenarioGivenWhenThenAnd extends KeyWords { + + public ScenarioGivenWhenThenAnd() { + super("Scenario", "Given", "When", "Then", "And"); + } + +} Property changes on: trunk/jbehave-core/src/java/org/jbehave/scenario/definition/ScenarioGivenWhenThenAnd.java ___________________________________________________________________ Name: svn:mergeinfo +
Modified: trunk/jbehave-core/src/java/org/jbehave/scenario/parser/PatternScenarioParser.java (899 => 900)
--- trunk/jbehave-core/src/java/org/jbehave/scenario/parser/PatternScenarioParser.java 2008-08-26 13:38:55 UTC (rev 899) +++ trunk/jbehave-core/src/java/org/jbehave/scenario/parser/PatternScenarioParser.java 2008-08-28 13:01:44 UTC (rev 900) @@ -5,27 +5,19 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.jbehave.Configuration; import org.jbehave.scenario.ScenarioDefinition; import org.jbehave.scenario.StoryDefinition; import org.jbehave.scenario.definition.Blurb; public class PatternScenarioParser implements ScenarioParser { - private static final String PATTERN_TO_PULL_SCENARIOS_INTO_GROUP_4 = ".*?((Scenario:) (.|\\s)*?)\\s*(\\Z|Scenario:).*"; - private static final String PATTERN_TO_PULL_STORY_BLURB_INTO_GROUP_1 = "(.*?)(Scenario:|Given).*"; - private static final String PATTERN_TO_PULL_SCENARIO_TITLE_INTO_GROUP_1 = "Scenario:(.*?)\\s*(Given|When|Then).*"; - private static final String PATTERN_TO_PULL_OUT_STEPS = "((Given|When|Then) (.|\\s)*?)\\s*(\\Z|Given|When|Then|Scenario:)"; + private final Configuration configuration; - private final Pattern pattern; - - public PatternScenarioParser() { - this(PATTERN_TO_PULL_OUT_STEPS); + public PatternScenarioParser(Configuration configuration) { + this.configuration = configuration; } - public PatternScenarioParser(String parseRegex) { - this.pattern = Pattern.compile(parseRegex); - } - public StoryDefinition defineStoryFrom(String wholeStoryAsString) { Blurb blurb = parseBlurbFrom(wholeStoryAsString); List<ScenarioDefinition> scenarioDefinitions = parseScenariosFrom(wholeStoryAsString); @@ -36,14 +28,14 @@ List<ScenarioDefinition> scenarioDefinitions = new ArrayList<ScenarioDefinition>(); List<String> scenarios = splitScenarios(wholeStoryAsString); for (String scenario : scenarios) { - Matcher findingTitle = Pattern.compile(PATTERN_TO_PULL_SCENARIO_TITLE_INTO_GROUP_1, Pattern.DOTALL).matcher(scenario); + Matcher findingTitle = patternToPullScenarioTitlesIntoGroupOne().matcher(scenario); scenarioDefinitions.add(new ScenarioDefinition(findingTitle.find() ? findingTitle.group(1).trim() : "", findSteps(scenario))); } return scenarioDefinitions; } - + private List<String> findSteps(String scenarioAsString) { - Matcher matcher = pattern.matcher(scenarioAsString); + Matcher matcher = patternToPullOutSteps().matcher(scenarioAsString); List<String> steps = new ArrayList<String>(); int startAt = 0; while (matcher.find(startAt)) { @@ -55,7 +47,7 @@ } private Blurb parseBlurbFrom(String wholeStoryAsString) { - Pattern findStoryBlurb = Pattern.compile(PATTERN_TO_PULL_STORY_BLURB_INTO_GROUP_1, Pattern.DOTALL); + Pattern findStoryBlurb = Pattern.compile("(.*?)(" + configuration.keywords().scenario() + ":).*", Pattern.DOTALL); Matcher matcher = findStoryBlurb.matcher(wholeStoryAsString); if (matcher.find()) { return new Blurb(matcher.group(1).trim()); @@ -65,7 +57,7 @@ } private List<String> splitScenarios(String allScenariosInFile) { - Pattern scenarioSplitter = Pattern.compile(PATTERN_TO_PULL_SCENARIOS_INTO_GROUP_4, Pattern.DOTALL); + Pattern scenarioSplitter = patternToPullScenariosIntoGroupFour(); Matcher matcher = scenarioSplitter.matcher(allScenariosInFile); int startAt = 0; List<String> scenarios = new ArrayList<String>(); @@ -81,4 +73,40 @@ return scenarios; } + private Pattern patternToPullScenariosIntoGroupFour() { + return Pattern.compile(".*?((Scenario:) (.|\\s)*?)\\s*(\\Z|Scenario:).*".replace("Scenario", configuration.keywords().scenario()), Pattern.DOTALL); + } + + private Pattern patternToPullScenarioTitlesIntoGroupOne() { + String concatenatedKeywords = concatenateWithOr(configuration.keywords().given(), configuration.keywords().when(), configuration.keywords().then(), configuration.keywords().others()); + return Pattern.compile(configuration.keywords().scenario() + ":(.*?)\\s*(" + concatenatedKeywords + ").*"); + } + + + private String concatenateWithOr(String given, String when, String then, + String[] others) { + StringBuilder builder = new StringBuilder(); + builder.append(given).append("|"); + builder.append(when).append("|"); + builder.append(then).append("|"); + return builder.append(concatenateWithOr(others)).toString(); + } + + private String concatenateWithOr(String... keywords) { + return concatenateWithOr(new StringBuilder(), keywords); + } + + private String concatenateWithOr(StringBuilder builder, + String[] keywords) { + for (String other : keywords) { + builder.append(other).append("|"); + } + String result = builder.toString(); + return result.substring(0, result.length() - 1); // chop off the last | + } + + private Pattern patternToPullOutSteps() { + String givenWhenThen = concatenateWithOr(configuration.keywords().given(), configuration.keywords().when(), configuration.keywords().then(), configuration.keywords().others()); + return Pattern.compile("((" + givenWhenThen + ") (.|\\s)*?)\\s*(\\Z|" + givenWhenThen + "|" + configuration.keywords().scenario()+ ":)"); + } }
Modified: trunk/jbehave-core/src/java/org/jbehave/scenario/parser/ScenarioFileLoader.java (899 => 900)
--- trunk/jbehave-core/src/java/org/jbehave/scenario/parser/ScenarioFileLoader.java 2008-08-26 13:38:55 UTC (rev 899) +++ trunk/jbehave-core/src/java/org/jbehave/scenario/parser/ScenarioFileLoader.java 2008-08-28 13:01:44 UTC (rev 900) @@ -4,6 +4,7 @@ import java.io.IOException; import java.io.InputStream; +import org.jbehave.scenario.PropertyBasedConfiguration; import org.jbehave.scenario.Scenario; import org.jbehave.scenario.StoryDefinition; import org.jbehave.scenario.errors.InvalidScenarioResourceException; @@ -15,7 +16,7 @@ private final ScenarioParser stepParser; public ScenarioFileLoader() { - this(new UnderscoredCamelCaseResolver(), Thread.currentThread().getContextClassLoader(), new PatternScenarioParser()); + this(new UnderscoredCamelCaseResolver(), Thread.currentThread().getContextClassLoader(), new PatternScenarioParser(new PropertyBasedConfiguration())); } public ScenarioFileLoader(ScenarioParser stepParser) { @@ -26,14 +27,6 @@ this(converter, Thread.currentThread().getContextClassLoader(), parser); } - public ScenarioFileLoader(ClassLoader classLoader) { - this(new UnderscoredCamelCaseResolver(), classLoader, new PatternScenarioParser()); - } - - public ScenarioFileLoader(ScenarioFileNameResolver resolver, ClassLoader classLoader) { - this(resolver, classLoader, new PatternScenarioParser()); - } - public ScenarioFileLoader(ScenarioFileNameResolver resolver, ClassLoader classLoader, ScenarioParser stepParser) { this.resolver = resolver; this.classLoader = classLoader;
Modified: trunk/jbehave-core/src/java/org/jbehave/scenario/steps/Steps.java (899 => 900)
--- trunk/jbehave-core/src/java/org/jbehave/scenario/steps/Steps.java 2008-08-26 13:38:55 UTC (rev 899) +++ trunk/jbehave-core/src/java/org/jbehave/scenario/steps/Steps.java 2008-08-28 13:01:44 UTC (rev 900) @@ -21,6 +21,10 @@ "Then", "And"); } + public Steps(String... startingWords) { + this(new PrefixCapturingPatternBuilder(), new SilentStepMonitor(), new ParameterConverters(), startingWords); + } + public Steps(StepPatternBuilder patternBuilder, StepMonitor stepMonitor, ParameterConverters parameterConverters, String... startingWords) { this.patternBuilder = patternBuilder;
To unsubscribe from this list please visit:
