- Revision
- 1579
- Author
- mauro
- Date
- 2010-02-21 16:15:22 -0600 (Sun, 21 Feb 2010)
Log Message
JBEHAVE-162: Added optional priority attribute to step method annotations to allow the priorisation of non-greedy regex patterns.
Modified Paths
- 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_is_alerted_of_status.scenario
- trunk/core/jbehave-core/src/behaviour/java/org/jbehave/scenario/steps/CandidateStepBehaviour.java
- trunk/core/jbehave-core/src/behaviour/java/org/jbehave/scenario/steps/UnmatchedToPendingStepCreatorBehaviour.java
- trunk/core/jbehave-core/src/main/java/org/jbehave/scenario/annotations/Given.java
- trunk/core/jbehave-core/src/main/java/org/jbehave/scenario/annotations/Then.java
- trunk/core/jbehave-core/src/main/java/org/jbehave/scenario/annotations/When.java
- trunk/core/jbehave-core/src/main/java/org/jbehave/scenario/steps/CandidateStep.java
- trunk/core/jbehave-core/src/main/java/org/jbehave/scenario/steps/Steps.java
- trunk/core/jbehave-core/src/main/java/org/jbehave/scenario/steps/UnmatchedToPendingStepCreator.java
Added Paths
- trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/PriorityMatchingSteps.java
- trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/PriorityMatching.java
- trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/priority_matching.scenario
Removed Paths
Diff
Deleted: trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/OrderMatchingSteps.java (1578 => 1579)
--- trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/OrderMatchingSteps.java 2010-02-21 18:22:23 UTC (rev 1578) +++ trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/OrderMatchingSteps.java 2010-02-21 22:15:22 UTC (rev 1579) @@ -1,27 +0,0 @@ -package org.jbehave.examples.trader; - -import junit.framework.Assert; - -import org.jbehave.scenario.annotations.Given; -import org.jbehave.scenario.annotations.Then; - -public class OrderMatchingSteps { - - private String param; - - @Given("a step that has $param") - public void has(String param){ - this.param = param; - } - - @Given("a step that has exactly one $param") - public void hasExactlyOne(String param){ - this.param = param; - } - - @Then("the parameter value is \"$param\"") - public void theParamValue(String param){ - Assert.assertEquals(this.param, param); - } - -}
Copied: trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/PriorityMatchingSteps.java (from rev 1566, trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/OrderMatchingSteps.java) (0 => 1579)
--- trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/PriorityMatchingSteps.java (rev 0) +++ trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/PriorityMatchingSteps.java 2010-02-21 22:15:22 UTC (rev 1579) @@ -0,0 +1,28 @@ +package org.jbehave.examples.trader; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.jbehave.Ensure.ensureThat; + +import org.jbehave.scenario.annotations.Given; +import org.jbehave.scenario.annotations.Then; + +public class PriorityMatchingSteps { + + private String param; + + @Given("a step that has $param") + public void has(String param){ + this.param = param; + } + + @Given(value="a step that has exactly one $param", priority=1) + public void hasExactlyOne(String param){ + this.param = param; + } + + @Then("the parameter value is \"$param\"") + public void theParamValue(String param){ + ensureThat(this.param, equalTo(param)); + } + +}
Modified: trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/TraderSteps.java (1578 => 1579)
--- trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/TraderSteps.java 2010-02-21 18:22:23 UTC (rev 1578) +++ trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/TraderSteps.java 2010-02-21 22:15:22 UTC (rev 1579) @@ -105,6 +105,11 @@ ensureThat(stock.getStatus().name(), equalTo(status)); } + @Then(value="the alert status is currently %status", priority=1) // prioritise over potential match with previous method + public void theAlertStatusIsCurrently(@Named("status") String status) { + ensureThat(stock.getStatus().name(), equalTo(status)); + } + @When("the trader sells all stocks") public void theTraderSellsAllStocks() { trader.sellAllStocks();
Deleted: trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/OrderMatching.java (1578 => 1579)
--- trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/OrderMatching.java 2010-02-21 18:22:23 UTC (rev 1578) +++ trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/OrderMatching.java 2010-02-21 22:15:22 UTC (rev 1579) @@ -1,31 +0,0 @@ -package org.jbehave.examples.trader.scenarios; - -import org.jbehave.examples.trader.OrderMatchingSteps; -import org.jbehave.scenario.JUnitScenario; -import org.jbehave.scenario.MostUsefulConfiguration; -import org.jbehave.scenario.parser.ClasspathScenarioDefiner; -import org.jbehave.scenario.parser.PatternScenarioParser; -import org.jbehave.scenario.parser.PrefixCapturingPatternBuilder; -import org.jbehave.scenario.parser.ScenarioDefiner; -import org.jbehave.scenario.parser.UnderscoredCamelCaseResolver; -import org.jbehave.scenario.steps.StepsConfiguration; -import org.jbehave.scenario.steps.StepsFactory; - -public class OrderMatching extends JUnitScenario { - - public OrderMatching() { - super(new MostUsefulConfiguration() { - @Override - public ScenarioDefiner forDefiningScenarios() { - return new ClasspathScenarioDefiner(new UnderscoredCamelCaseResolver(".scenario"), - new PatternScenarioParser(keywords())); - } - }); - - StepsConfiguration configuration = new StepsConfiguration(); - configuration.usePatternBuilder(new PrefixCapturingPatternBuilder("$")); - addSteps(new StepsFactory(configuration).createCandidateSteps(new OrderMatchingSteps())); - - } - -}
Copied: trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/PriorityMatching.java (from rev 1566, trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/OrderMatching.java) (0 => 1579)
--- trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/PriorityMatching.java (rev 0) +++ trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/PriorityMatching.java 2010-02-21 22:15:22 UTC (rev 1579) @@ -0,0 +1,31 @@ +package org.jbehave.examples.trader.scenarios; + +import org.jbehave.examples.trader.PriorityMatchingSteps; +import org.jbehave.scenario.JUnitScenario; +import org.jbehave.scenario.MostUsefulConfiguration; +import org.jbehave.scenario.parser.ClasspathScenarioDefiner; +import org.jbehave.scenario.parser.PatternScenarioParser; +import org.jbehave.scenario.parser.PrefixCapturingPatternBuilder; +import org.jbehave.scenario.parser.ScenarioDefiner; +import org.jbehave.scenario.parser.UnderscoredCamelCaseResolver; +import org.jbehave.scenario.steps.StepsConfiguration; +import org.jbehave.scenario.steps.StepsFactory; + +public class PriorityMatching extends JUnitScenario { + + public PriorityMatching() { + super(new MostUsefulConfiguration() { + @Override + public ScenarioDefiner forDefiningScenarios() { + return new ClasspathScenarioDefiner(new UnderscoredCamelCaseResolver(".scenario"), + new PatternScenarioParser(keywords())); + } + }); + + StepsConfiguration configuration = new StepsConfiguration(); + configuration.usePatternBuilder(new PrefixCapturingPatternBuilder("$")); + addSteps(new StepsFactory(configuration).createCandidateSteps(new PriorityMatchingSteps())); + + } + +}
Deleted: trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/order_matching.scenario (1578 => 1579)
--- trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/order_matching.scenario 2010-02-21 18:22:23 UTC (rev 1578) +++ trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/order_matching.scenario 2010-02-21 22:15:22 UTC (rev 1579) @@ -1,4 +0,0 @@ -Scenario: A scenario that verifies order matching of steps - -Given a step that has exactly one parameter -Then the parameter value is "exactly one parameter" \ No newline at end of file
Copied: trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/priority_matching.scenario (from rev 1566, trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/order_matching.scenario) (0 => 1579)
--- trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/priority_matching.scenario (rev 0) +++ trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/priority_matching.scenario 2010-02-21 22:15:22 UTC (rev 1579) @@ -0,0 +1,4 @@ +Scenario: A scenario that verifies priority matching of steps + +Given a step that has exactly one parameter +Then the parameter value is "parameter" \ No newline at end of file
Modified: trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/trader_is_alerted_of_status.scenario (1578 => 1579)
--- trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/trader_is_alerted_of_status.scenario 2010-02-21 18:22:23 UTC (rev 1578) +++ trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/scenarios/trader_is_alerted_of_status.scenario 2010-02-21 22:15:22 UTC (rev 1579) @@ -13,7 +13,10 @@ When the stock is sold at price 11.0 Then the alert status is OFF When the stock is sold at price 16.0 +!-- The next steps show step priority in action, since both textual steps could be matched by same regex pattern, +we set a higher priority to the less-greedy pattern Then the alert status is ON +Then the alert status is currently ON Scenario:
Modified: trunk/core/jbehave-core/src/behaviour/java/org/jbehave/scenario/steps/CandidateStepBehaviour.java (1578 => 1579)
--- trunk/core/jbehave-core/src/behaviour/java/org/jbehave/scenario/steps/CandidateStepBehaviour.java 2010-02-21 18:22:23 UTC (rev 1578) +++ trunk/core/jbehave-core/src/behaviour/java/org/jbehave/scenario/steps/CandidateStepBehaviour.java 2010-02-21 22:15:22 UTC (rev 1579) @@ -36,6 +36,7 @@ private static final StepPatternBuilder PATTERN_BUILDER = new PrefixCapturingPatternBuilder(); private static final String NL = System.getProperty("line.separator"); + private static final int DEFAULT_PRIORITY = 0; private Map<String, String> tableRow = new HashMap<String, String>(); private Paranamer paranamer = new CachingParanamer(new BytecodeReadingParanamer()); private Map<StepType, String> startingWords = startingWords(); @@ -50,15 +51,15 @@ @Test public void shouldMatchASimpleString() throws Exception { - CandidateStep candidateStep = new CandidateStep("I laugh", GIVEN, SomeSteps.class.getMethod("aMethod"), - null, PATTERN_BUILDER, new ParameterConverters(), startingWords); + CandidateStep candidateStep = new CandidateStep("I laugh", DEFAULT_PRIORITY, GIVEN, + SomeSteps.class.getMethod("aMethod"), null, PATTERN_BUILDER, new ParameterConverters(), startingWords); ensureThat(candidateStep.matches("Given I laugh")); } @Test public void shouldMatchAStringWithArguments() throws Exception { - CandidateStep candidateStep = new CandidateStep("windows on the $nth floor", WHEN, SomeSteps.class - .getMethod("aMethod"), null, PATTERN_BUILDER, new ParameterConverters(), startingWords); + CandidateStep candidateStep = new CandidateStep("windows on the $nth floor", DEFAULT_PRIORITY, WHEN, SomeSteps.class + .getMethod("aMethod"), null, PATTERN_BUILDER, new ParameterConverters(), startingWords); ensureThat(candidateStep.matches("When windows on the 1st floor")); ensureThat(not(candidateStep.matches("When windows on the 1st floor are open"))); } @@ -66,8 +67,8 @@ @Test public void shouldProvideARealStepUsingTheMatchedString() throws Exception { SomeSteps someSteps = new SomeSteps(); - CandidateStep candidateStep = new CandidateStep("I live on the $nth floor", THEN, SomeSteps.class.getMethod( - "aMethodWith", String.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords); + CandidateStep candidateStep = new CandidateStep("I live on the $nth floor", DEFAULT_PRIORITY, THEN, SomeSteps.class.getMethod( + "aMethodWith", String.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords); Step step = candidateStep.createFrom(tableRow, "Then I live on the 1st floor"); step.perform(); ensureThat((String) someSteps.args, equalTo("1st")); @@ -75,31 +76,31 @@ @Test public void shouldMatchMultilineStrings() throws Exception { - CandidateStep candidateStep = new CandidateStep("the grid should look like $grid", THEN, SomeSteps.class - .getMethod("aMethod"), null, PATTERN_BUILDER, new ParameterConverters(), startingWords); + CandidateStep candidateStep = new CandidateStep("the grid should look like $grid", DEFAULT_PRIORITY, THEN, SomeSteps.class + .getMethod("aMethod"), null, PATTERN_BUILDER, new ParameterConverters(), startingWords); ensureThat(candidateStep.matches("Then the grid should look like " + NL + "...." + NL + "...." + NL)); } @Test public void shouldConvertArgsToAppropriateNumbers() throws Exception { SomeSteps someSteps = new SomeSteps(); - CandidateStep candidateStep = new CandidateStep("I should live in no. $no", THEN, SomeSteps.class.getMethod( - "aMethodWith", int.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords); + CandidateStep candidateStep = new CandidateStep("I should live in no. $no", DEFAULT_PRIORITY, THEN, SomeSteps.class.getMethod( + "aMethodWith", int.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords); candidateStep.createFrom(tableRow, "Then I should live in no. 14").perform(); ensureThat((Integer) someSteps.args, equalTo(14)); - candidateStep = new CandidateStep("I should live in no. $no", THEN, SomeSteps.class.getMethod("aMethodWith", - long.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords); + candidateStep = new CandidateStep("I should live in no. $no", DEFAULT_PRIORITY, THEN, SomeSteps.class.getMethod("aMethodWith", + long.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords); candidateStep.createFrom(tableRow, "Then I should live in no. 14").perform(); ensureThat((Long) someSteps.args, equalTo(14L)); - candidateStep = new CandidateStep("I should live in no. $no", THEN, SomeSteps.class.getMethod("aMethodWith", - double.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords); + candidateStep = new CandidateStep("I should live in no. $no", DEFAULT_PRIORITY, THEN, SomeSteps.class.getMethod("aMethodWith", + double.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords); candidateStep.createFrom(tableRow, "Then I should live in no. 14").perform(); ensureThat((Double) someSteps.args, equalTo(14.0)); - candidateStep = new CandidateStep("I should live in no. $no", THEN, SomeSteps.class.getMethod("aMethodWith", - float.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords); + candidateStep = new CandidateStep("I should live in no. $no", DEFAULT_PRIORITY, THEN, SomeSteps.class.getMethod("aMethodWith", + float.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords); candidateStep.createFrom(tableRow, "Then I should live in no. 14").perform(); ensureThat((Float) someSteps.args, equalTo(14.0f)); } @@ -108,8 +109,8 @@ public void shouldProvideAStepWithADescriptionThatMatchesTheCandidateStep() throws Exception { ScenarioReporter reporter = mock(ScenarioReporter.class); SomeSteps someSteps = new SomeSteps(); - CandidateStep candidateStep = new CandidateStep("I live on the $nth floor", THEN, SomeSteps.class.getMethod( - "aMethodWith", String.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords); + CandidateStep candidateStep = new CandidateStep("I live on the $nth floor", DEFAULT_PRIORITY, THEN, SomeSteps.class.getMethod( + "aMethodWith", String.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords); Step step = candidateStep.createFrom(tableRow, "Then I live on the 1st floor"); StepResult result = step.perform(); @@ -123,8 +124,8 @@ String unixNewline = "\n"; String systemNewline = System.getProperty("line.separator"); SomeSteps someSteps = new SomeSteps(); - CandidateStep candidateStep = new CandidateStep("the grid should look like $grid", THEN, SomeSteps.class.getMethod( - "aMethodWith", String.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords); + CandidateStep candidateStep = new CandidateStep("the grid should look like $grid", DEFAULT_PRIORITY, THEN, SomeSteps.class.getMethod( + "aMethodWith", String.class), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords); Step step = candidateStep.createFrom(tableRow, "Then the grid should look like" + windowsNewline + ".." + unixNewline + ".." + windowsNewline); step.perform(); @@ -135,22 +136,22 @@ public void shouldConvertArgsToListOfNumbers() throws Exception { SomeSteps someSteps = new SomeSteps(); CandidateStep candidateStep = new CandidateStep("windows on the $nth floors", - WHEN, SomeSteps.methodFor("aMethodWithListOfLongs"), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords); + DEFAULT_PRIORITY, WHEN, SomeSteps.methodFor("aMethodWithListOfLongs"), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords); candidateStep.createFrom(tableRow, "When windows on the 1,2,3 floors").perform(); ensureThat(((List<?>) someSteps.args).toString(), equalTo(asList(1L, 2L, 3L).toString())); - candidateStep = new CandidateStep("windows on the $nth floors", WHEN, - SomeSteps.methodFor("aMethodWithListOfIntegers"), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords); + candidateStep = new CandidateStep("windows on the $nth floors", DEFAULT_PRIORITY, + WHEN, SomeSteps.methodFor("aMethodWithListOfIntegers"), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords); candidateStep.createFrom(tableRow, "When windows on the 1,2,3 floors").perform(); ensureThat(((List<?>) someSteps.args).toString(), equalTo(asList(1, 2, 3).toString())); - candidateStep = new CandidateStep("windows on the $nth floors", WHEN, - SomeSteps.methodFor("aMethodWithListOfDoubles"), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords); + candidateStep = new CandidateStep("windows on the $nth floors", DEFAULT_PRIORITY, + WHEN, SomeSteps.methodFor("aMethodWithListOfDoubles"), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords); candidateStep.createFrom(tableRow, "When windows on the 1.1,2.2,3.3 floors").perform(); ensureThat(((List<?>) someSteps.args).toString(), equalTo(asList(1.1, 2.2, 3.3).toString())); - candidateStep = new CandidateStep("windows on the $nth floors", WHEN, - SomeSteps.methodFor("aMethodWithListOfFloats"), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords); + candidateStep = new CandidateStep("windows on the $nth floors", DEFAULT_PRIORITY, + WHEN, SomeSteps.methodFor("aMethodWithListOfFloats"), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords); candidateStep.createFrom(tableRow, "When windows on the 1.1,2.2,3.3 floors").perform(); ensureThat(((List<?>) someSteps.args).toString(), equalTo(asList(1.1f, 2.2f, 3.3f).toString())); @@ -160,7 +161,7 @@ public void shouldConvertArgsToListOfStrings() throws Exception { SomeSteps someSteps = new SomeSteps(); CandidateStep candidateStep = new CandidateStep("windows on the $nth floors", - WHEN, SomeSteps.methodFor("aMethodWithListOfStrings"), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords); + DEFAULT_PRIORITY, WHEN, SomeSteps.methodFor("aMethodWithListOfStrings"), someSteps, PATTERN_BUILDER, new ParameterConverters(), startingWords); candidateStep.createFrom(tableRow, "When windows on the 1,2,3 floors").perform(); ensureThat(((List<?>) someSteps.args).toString(), equalTo(asList("1", "2", "3").toString())); } @@ -169,7 +170,7 @@ public void shouldMatchMethodParametersByAnnotatedNamesInNaturalOrder() throws Exception { AnnotationNamedParameterSteps steps = new AnnotationNamedParameterSteps(); CandidateStep candidateStep = new CandidateStep("I live on the $ith floor but some call it the $nth", - WHEN, stepMethodFor("methodWithNamedParametersInNaturalOrder", AnnotationNamedParameterSteps.class), steps, PATTERN_BUILDER, new ParameterConverters(), startingWords); + DEFAULT_PRIORITY, WHEN, stepMethodFor("methodWithNamedParametersInNaturalOrder", AnnotationNamedParameterSteps.class), steps, PATTERN_BUILDER, new ParameterConverters(), startingWords); candidateStep.createFrom(tableRow, "When I live on the first floor but some call it the ground").perform(); ensureThat(steps.ith, equalTo("first")); ensureThat(steps.nth, equalTo("ground")); @@ -179,7 +180,7 @@ public void shouldMatchMethodParametersByAnnotatedNamesInverseOrder() throws Exception { AnnotationNamedParameterSteps steps = new AnnotationNamedParameterSteps(); CandidateStep candidateStep = new CandidateStep("I live on the $ith floor but some call it the $nth", - WHEN, stepMethodFor("methodWithNamedParametersInInverseOrder", AnnotationNamedParameterSteps.class), steps, PATTERN_BUILDER, new ParameterConverters(), startingWords); + DEFAULT_PRIORITY, WHEN, stepMethodFor("methodWithNamedParametersInInverseOrder", AnnotationNamedParameterSteps.class), steps, PATTERN_BUILDER, new ParameterConverters(), startingWords); candidateStep.createFrom(tableRow, "When I live on the first floor but some call it the ground").perform(); ensureThat(steps.ith, equalTo("first")); ensureThat(steps.nth, equalTo("ground")); @@ -191,7 +192,7 @@ tableRow.put("ith", "first"); tableRow.put("nth", "ground"); CandidateStep candidateStep = new CandidateStep("I live on the ith floor but some call it the nth", - WHEN, stepMethodFor("methodWithNamedParametersInNaturalOrder", AnnotationNamedParameterSteps.class), steps, PATTERN_BUILDER, new ParameterConverters(), startingWords); + DEFAULT_PRIORITY, WHEN, stepMethodFor("methodWithNamedParametersInNaturalOrder", AnnotationNamedParameterSteps.class), steps, PATTERN_BUILDER, new ParameterConverters(), startingWords); candidateStep.createFrom(tableRow, "When I live on the <ith> floor but some call it the <nth>").perform(); ensureThat(steps.ith, equalTo("first")); ensureThat(steps.nth, equalTo("ground")); @@ -201,7 +202,7 @@ public void shouldMatchMethodParametersByAnnotatedNamesInNaturalOrderForJsr330Named() throws Exception { Jsr330AnnotationNamedParameterSteps steps = new Jsr330AnnotationNamedParameterSteps(); CandidateStep candidateStep = new CandidateStep("I live on the $ith floor but some call it the $nth", - WHEN, stepMethodFor("methodWithNamedParametersInNaturalOrder", Jsr330AnnotationNamedParameterSteps.class), steps, PATTERN_BUILDER, new ParameterConverters(), startingWords); + DEFAULT_PRIORITY, WHEN, stepMethodFor("methodWithNamedParametersInNaturalOrder", Jsr330AnnotationNamedParameterSteps.class), steps, PATTERN_BUILDER, new ParameterConverters(), startingWords); candidateStep.createFrom(tableRow, "When I live on the first floor but some call it the ground").perform(); ensureThat(steps.ith, equalTo("first")); ensureThat(steps.nth, equalTo("ground")); @@ -211,7 +212,7 @@ public void shouldMatchMethodParametersByAnnotatedNamesInverseOrderForJsr330Named() throws Exception { Jsr330AnnotationNamedParameterSteps steps = new Jsr330AnnotationNamedParameterSteps(); CandidateStep candidateStep = new CandidateStep("I live on the $ith floor but some call it the $nth", - WHEN, stepMethodFor("methodWithNamedParametersInInverseOrder", Jsr330AnnotationNamedParameterSteps.class), steps, PATTERN_BUILDER, new ParameterConverters(), startingWords); + DEFAULT_PRIORITY, WHEN, stepMethodFor("methodWithNamedParametersInInverseOrder", Jsr330AnnotationNamedParameterSteps.class), steps, PATTERN_BUILDER, new ParameterConverters(), startingWords); candidateStep.createFrom(tableRow, "When I live on the first floor but some call it the ground").perform(); ensureThat(steps.ith, equalTo("first")); ensureThat(steps.nth, equalTo("ground")); @@ -223,7 +224,7 @@ tableRow.put("ith", "first"); tableRow.put("nth", "ground"); CandidateStep candidateStep = new CandidateStep("I live on the ith floor but some call it the nth", - WHEN, stepMethodFor("methodWithNamedParametersInNaturalOrder", Jsr330AnnotationNamedParameterSteps.class), steps, PATTERN_BUILDER, new ParameterConverters(), startingWords); + DEFAULT_PRIORITY, WHEN, stepMethodFor("methodWithNamedParametersInNaturalOrder", Jsr330AnnotationNamedParameterSteps.class), steps, PATTERN_BUILDER, new ParameterConverters(), startingWords); candidateStep.createFrom(tableRow, "When I live on the <ith> floor but some call it the <nth>").perform(); ensureThat(steps.ith, equalTo("first")); ensureThat(steps.nth, equalTo("ground")); @@ -243,7 +244,7 @@ private void shouldMatchMethodParametersByParanamerSomeOrder(String methodName) throws IntrospectionException { ParanamerNamedParameterSteps steps = new ParanamerNamedParameterSteps(); CandidateStep candidateStep = new CandidateStep("I live on the $ith floor but some call it the $nth", - WHEN, stepMethodFor(methodName, ParanamerNamedParameterSteps.class), steps, PATTERN_BUILDER, new ParameterConverters(), startingWords); + DEFAULT_PRIORITY, WHEN, stepMethodFor(methodName, ParanamerNamedParameterSteps.class), steps, PATTERN_BUILDER, new ParameterConverters(), startingWords); candidateStep.useParanamer(paranamer); candidateStep.createFrom(tableRow, "When I live on the first floor but some call it the ground").perform(); ensureThat(steps.ith, equalTo("first")); @@ -256,7 +257,7 @@ tableRow.put("ith", "first"); tableRow.put("nth", "ground"); CandidateStep candidateStep = new CandidateStep("I live on the ith floor but some call it the nth", - WHEN, stepMethodFor("methodWithNamedParametersInNaturalOrder", ParanamerNamedParameterSteps.class), steps, PATTERN_BUILDER, new ParameterConverters(), startingWords); + DEFAULT_PRIORITY, WHEN, stepMethodFor("methodWithNamedParametersInNaturalOrder", ParanamerNamedParameterSteps.class), steps, PATTERN_BUILDER, new ParameterConverters(), startingWords); candidateStep.useParanamer(paranamer); candidateStep.createFrom(tableRow, "When I live on the <ith> floor but some call it the <nth>").perform(); ensureThat(steps.ith, equalTo("first"));
Modified: trunk/core/jbehave-core/src/behaviour/java/org/jbehave/scenario/steps/UnmatchedToPendingStepCreatorBehaviour.java (1578 => 1579)
--- trunk/core/jbehave-core/src/behaviour/java/org/jbehave/scenario/steps/UnmatchedToPendingStepCreatorBehaviour.java 2010-02-21 18:22:23 UTC (rev 1578) +++ trunk/core/jbehave-core/src/behaviour/java/org/jbehave/scenario/steps/UnmatchedToPendingStepCreatorBehaviour.java 2010-02-21 22:15:22 UTC (rev 1579) @@ -126,4 +126,47 @@ ensureThat(afterSteps, array(equalTo(stepAfter1), equalTo(stepAfter2))); } + @Test + public void shouldPrioritiseAnnotatedSteps() { + // Given some Steps classes + // and some candidate steps split across them + + CandidateSteps steps1 = mock(Steps.class); + CandidateSteps steps2 = mock(Steps.class); + CandidateStep candidate1 = mock(CandidateStep.class); + CandidateStep candidate2 = mock(CandidateStep.class); + CandidateStep candidate3 = mock(CandidateStep.class); + CandidateStep candidate4 = mock(CandidateStep.class); + Step step1 = mock(Step.class); + Step step2 = mock(Step.class); + Step step3 = mock(Step.class); + Step step4 = mock(Step.class); + + when(steps1.getSteps()).thenReturn(new CandidateStep[]{candidate1, candidate2}); + when(steps2.getSteps()).thenReturn(new CandidateStep[]{candidate3, candidate4}); + + // all matching the same step string with different priorities + String stepAsString = "Given a step"; + when(candidate1.matches(stepAsString)).thenReturn(true); + when(candidate2.matches(stepAsString)).thenReturn(true); + when(candidate3.matches(stepAsString)).thenReturn(true); + when(candidate4.matches(stepAsString)).thenReturn(true); + when(candidate1.getPriority()).thenReturn(1); + when(candidate2.getPriority()).thenReturn(2); + when(candidate3.getPriority()).thenReturn(3); + when(candidate4.getPriority()).thenReturn(4); + when(candidate1.createFrom(tableRow, stepAsString)).thenReturn(step1); + when(candidate2.createFrom(tableRow, stepAsString)).thenReturn(step2); + when(candidate3.createFrom(tableRow, stepAsString)).thenReturn(step3); + when(candidate4.createFrom(tableRow, stepAsString)).thenReturn(step4); + + // When we create the series of steps for the scenario + UnmatchedToPendingStepCreator creator = new UnmatchedToPendingStepCreator(); + Step[] steps = creator.createStepsFrom(new ScenarioDefinition(asList(stepAsString)), tableRow, + steps1, steps2); + + // Then the step with highest priority is returned + ensureThat(step4, equalTo(steps[0])); + } + }
Modified: trunk/core/jbehave-core/src/main/java/org/jbehave/scenario/annotations/Given.java (1578 => 1579)
--- trunk/core/jbehave-core/src/main/java/org/jbehave/scenario/annotations/Given.java 2010-02-21 18:22:23 UTC (rev 1578) +++ trunk/core/jbehave-core/src/main/java/org/jbehave/scenario/annotations/Given.java 2010-02-21 22:15:22 UTC (rev 1579) @@ -11,4 +11,6 @@ String value(); + int priority() default 0; + }
Modified: trunk/core/jbehave-core/src/main/java/org/jbehave/scenario/annotations/Then.java (1578 => 1579)
--- trunk/core/jbehave-core/src/main/java/org/jbehave/scenario/annotations/Then.java 2010-02-21 18:22:23 UTC (rev 1578) +++ trunk/core/jbehave-core/src/main/java/org/jbehave/scenario/annotations/Then.java 2010-02-21 22:15:22 UTC (rev 1579) @@ -11,4 +11,6 @@ String value(); + int priority() default 0; + }
Modified: trunk/core/jbehave-core/src/main/java/org/jbehave/scenario/annotations/When.java (1578 => 1579)
--- trunk/core/jbehave-core/src/main/java/org/jbehave/scenario/annotations/When.java 2010-02-21 18:22:23 UTC (rev 1578) +++ trunk/core/jbehave-core/src/main/java/org/jbehave/scenario/annotations/When.java 2010-02-21 22:15:22 UTC (rev 1579) @@ -5,11 +5,12 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; - @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface When { String value(); + int priority() default 0; + }
Modified: trunk/core/jbehave-core/src/main/java/org/jbehave/scenario/steps/CandidateStep.java (1578 => 1579)
--- trunk/core/jbehave-core/src/main/java/org/jbehave/scenario/steps/CandidateStep.java 2010-02-21 18:22:23 UTC (rev 1578) +++ trunk/core/jbehave-core/src/main/java/org/jbehave/scenario/steps/CandidateStep.java 2010-02-21 22:15:22 UTC (rev 1579) @@ -32,6 +32,7 @@ public static final String PARAMETER_VALUE_START = "\uFF5F"; public static final String PARAMETER_VALUE_END = "\uFF60"; private final String patternAsString; + private final Integer priority; private final StepType stepType; private final Method method; protected final Object steps; @@ -43,16 +44,17 @@ private StepMonitor stepMonitor = new SilentStepMonitor(); private Paranamer paranamer = new NullParanamer(); - public CandidateStep(String patternAsString, StepType stepType, Method method, CandidateSteps steps, - StepPatternBuilder patternBuilder, ParameterConverters parameterConverters, - Map<StepType, String> startingWords) { - this(patternAsString, stepType, method, (Object) steps, patternBuilder, parameterConverters, startingWords); + public CandidateStep(String patternAsString, int priority, StepType stepType, Method method, + CandidateSteps steps, StepPatternBuilder patternBuilder, + ParameterConverters parameterConverters, Map<StepType, String> startingWords) { + this(patternAsString, priority, stepType, method, (Object) steps, patternBuilder, parameterConverters, startingWords); } - public CandidateStep(String patternAsString, StepType stepType, Method method, Object stepsInstance, - StepPatternBuilder patternBuilder, ParameterConverters parameterConverters, - Map<StepType, String> startingWords) { + public CandidateStep(String patternAsString, int priority, StepType stepType, Method method, + Object stepsInstance, StepPatternBuilder patternBuilder, + ParameterConverters parameterConverters, Map<StepType, String> startingWords) { this.patternAsString = patternAsString; + this.priority = priority; this.stepType = stepType; this.method = method; this.steps = stepsInstance; @@ -74,6 +76,10 @@ return paranamer; } + public Integer getPriority() { + return priority; + } + public boolean ignore(String stepAsString) { try { String ignoreWord = startingWordFor(StepType.IGNORABLE); @@ -359,4 +365,5 @@ } + }
Modified: trunk/core/jbehave-core/src/main/java/org/jbehave/scenario/steps/Steps.java (1578 => 1579)
--- trunk/core/jbehave-core/src/main/java/org/jbehave/scenario/steps/Steps.java 2010-02-21 18:22:23 UTC (rev 1578) +++ trunk/core/jbehave-core/src/main/java/org/jbehave/scenario/steps/Steps.java 2010-02-21 22:15:22 UTC (rev 1579) @@ -153,19 +153,25 @@ List<CandidateStep> steps = new ArrayList<CandidateStep>(); for (Method method : stepsClass.getMethods()) { if (method.isAnnotationPresent(Given.class)) { - String value = encode(method.getAnnotation(Given.class).value()); - createCandidateStep(steps, method, GIVEN, value); - createCandidateStepsFromAliases(steps, method, GIVEN); + Given annotation = method.getAnnotation(Given.class); + String value = encode(annotation.value()); + int priority = annotation.priority(); + createCandidateStep(steps, method, GIVEN, value, priority); + createCandidateStepsFromAliases(steps, method, GIVEN, priority); } if (method.isAnnotationPresent(When.class)) { - String value = encode(method.getAnnotation(When.class).value()); - createCandidateStep(steps, method, WHEN, value); - createCandidateStepsFromAliases(steps, method, WHEN); + When annotation = method.getAnnotation(When.class); + String value = encode(annotation.value()); + int priority = annotation.priority(); + createCandidateStep(steps, method, WHEN, value, priority); + createCandidateStepsFromAliases(steps, method, WHEN, priority); } if (method.isAnnotationPresent(Then.class)) { - String value = encode(method.getAnnotation(Then.class).value()); - createCandidateStep(steps, method, THEN, value); - createCandidateStepsFromAliases(steps, method, THEN); + Then annotation = method.getAnnotation(Then.class); + String value = encode(annotation.value()); + int priority = annotation.priority(); + createCandidateStep(steps, method, THEN, value, priority); + createCandidateStepsFromAliases(steps, method, THEN, priority); } } return steps.toArray(new CandidateStep[steps.size()]); @@ -176,18 +182,18 @@ } private void createCandidateStep(List<CandidateStep> steps, Method method, StepType stepType, - String stepPatternAsString) { + String stepPatternAsString, int priority) { checkForDuplicateCandidateSteps(steps, stepType, stepPatternAsString); - CandidateStep step = createCandidateStep(method, stepType, stepPatternAsString, configuration); + CandidateStep step = createCandidateStep(method, stepType, stepPatternAsString, priority, configuration); step.useStepMonitor(configuration.getMonitor()); step.useParanamer(configuration.getParanamer()); steps.add(step); } - protected CandidateStep createCandidateStep(Method method, StepType stepType, String stepPatternAsString, + protected CandidateStep createCandidateStep(Method method, StepType stepType, String stepPatternAsString, int priority, StepsConfiguration configuration) { - return new CandidateStep(stepPatternAsString, stepType, method, instance, configuration.getPatternBuilder(), - configuration.getParameterConverters(), configuration.getStartingWordsByType()); + return new CandidateStep(stepPatternAsString, priority, stepType, method, instance, + configuration.getPatternBuilder(), configuration.getParameterConverters(), configuration.getStartingWordsByType()); } private void checkForDuplicateCandidateSteps(List<CandidateStep> steps, StepType stepType, String patternAsString) { @@ -198,15 +204,16 @@ } } - private void createCandidateStepsFromAliases(List<CandidateStep> steps, Method method, StepType stepType) { + private void createCandidateStepsFromAliases(List<CandidateStep> steps, Method method, StepType stepType, int priority) { if (method.isAnnotationPresent(Aliases.class)) { String[] aliases = method.getAnnotation(Aliases.class).values(); for (String alias : aliases) { - createCandidateStep(steps, method, stepType, alias); + createCandidateStep(steps, method, stepType, alias, priority); } } if (method.isAnnotationPresent(Alias.class)) { - createCandidateStep(steps, method, stepType, method.getAnnotation(Alias.class).value()); + String alias = method.getAnnotation(Alias.class).value(); + createCandidateStep(steps, method, stepType, alias, priority); } }
Modified: trunk/core/jbehave-core/src/main/java/org/jbehave/scenario/steps/UnmatchedToPendingStepCreator.java (1578 => 1579)
--- trunk/core/jbehave-core/src/main/java/org/jbehave/scenario/steps/UnmatchedToPendingStepCreator.java 2010-02-21 18:22:23 UTC (rev 1578) +++ trunk/core/jbehave-core/src/main/java/org/jbehave/scenario/steps/UnmatchedToPendingStepCreator.java 2010-02-21 22:15:22 UTC (rev 1579) @@ -1,6 +1,10 @@ package org.jbehave.scenario.steps; +import static java.util.Arrays.asList; + import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; import java.util.List; import java.util.Map; @@ -51,22 +55,36 @@ private void addMatchedScenarioSteps(ScenarioDefinition scenarioDefinition, List<Step> steps, Map<String, String> tableRow, CandidateSteps... candidateSteps) { + List<CandidateStep> prioritised = prioritise(candidateSteps); for (String stringStep : scenarioDefinition.getSteps()) { Step step = new PendingStep(stringStep); - for (CandidateSteps candidates : candidateSteps) { - for (CandidateStep candidate : candidates.getSteps()) { - if (candidate.ignore(stringStep)) { // ignorable steps are added so they can be reported - step = new IgnorableStep(stringStep); - break; - } - if (candidate.matches(stringStep)) { - step = candidate.createFrom(tableRow, stringStep); - break; - } + for (CandidateStep candidate : prioritised) { + if (candidate.ignore(stringStep)) { // ignorable steps are added + // so they can be reported + step = new IgnorableStep(stringStep); + break; } + if (candidate.matches(stringStep)) { + step = candidate.createFrom(tableRow, stringStep); + break; + } } steps.add(step); } } + private List<CandidateStep> prioritise(CandidateSteps[] candidateSteps) { + List<CandidateStep> steps = new ArrayList<CandidateStep>(); + for (CandidateSteps candidates : candidateSteps) { + steps.addAll(asList(candidates.getSteps())); + } + Collections.sort(steps, new Comparator<CandidateStep>() { + public int compare(CandidateStep o1, CandidateStep o2) { + // sort by decreasing order of priority + return -1* o1.getPriority().compareTo(o2.getPriority()); + } + }); + return steps; + } + }
To unsubscribe from this list please visit: