- Revision
- 1223
- Author
- mauro
- Date
- 2009-09-05 10:41:28 -0500 (Sat, 05 Sep 2009)
Log Message
Better scenario reporting and table parsing.
Modified Paths
- trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/ScenarioRunnerBehaviour.java
- trunk/core/jbehave-core/src/java/org/jbehave/scenario/ScenarioRunner.java
- trunk/core/jbehave-core/src/java/org/jbehave/scenario/definition/Table.java
- trunk/core/jbehave-core/src/java/org/jbehave/scenario/parser/PatternScenarioParser.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/jbehave-core/src/behaviour/org/jbehave/scenario/ScenarioRunnerBehaviour.java (1222 => 1223)
--- trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/ScenarioRunnerBehaviour.java 2009-09-05 15:11:22 UTC (rev 1222) +++ trunk/core/jbehave-core/src/behaviour/org/jbehave/scenario/ScenarioRunnerBehaviour.java 2009-09-05 15:41:28 UTC (rev 1223) @@ -10,6 +10,7 @@ import static org.mockito.Mockito.verify; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.jbehave.scenario.definition.Blurb; @@ -92,7 +93,8 @@ @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")); + List<String> givenScenarios = asList("/path/to/given/scenario1"); + ScenarioDefinition scenarioDefinition2 = new ScenarioDefinition("scenario 2", givenScenarios, asList("anotherSuccessfulStep")); StoryDefinition storyDefinition1 = new StoryDefinition(new Blurb("story 1"), scenarioDefinition1); StoryDefinition storyDefinition2 = new StoryDefinition(new Blurb("story 2"), scenarioDefinition2); @@ -125,7 +127,7 @@ InOrder inOrder = inOrder(reporter); inOrder.verify(reporter).beforeStory(storyDefinition2.getBlurb()); - inOrder.verify(reporter).givenScenario("/path/to/given/scenario1"); + inOrder.verify(reporter).givenScenarios(givenScenarios); inOrder.verify(reporter).successful("successfulStep"); inOrder.verify(reporter).successful("anotherSuccessfulStep"); inOrder.verify(reporter).afterStory();
Modified: trunk/core/jbehave-core/src/java/org/jbehave/scenario/ScenarioRunner.java (1222 => 1223)
--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/ScenarioRunner.java 2009-09-05 15:11:22 UTC (rev 1222) +++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/ScenarioRunner.java 2009-09-05 15:41:28 UTC (rev 1223) @@ -1,6 +1,7 @@ package org.jbehave.scenario; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.jbehave.scenario.definition.ScenarioDefinition; @@ -51,7 +52,7 @@ reporter.beforeStory(story.getBlurb()); for (ScenarioDefinition scenario : story.getScenarios()) { runGivenScenarios(configuration, scenario, candidateSteps); // first run any given scenarios, if any - if ( isTemplateScenario(scenario.getTable()) ){ // run template scenario + if ( isTemplateScenario(scenario) ){ // run template scenario runTemplateScenario(configuration, scenario, scenario.getTable(), candidateSteps); } else { // run plain old scenario runScenario(configuration, scenario, new HashMap<String, String>(), candidateSteps); @@ -61,7 +62,8 @@ currentStrategy.handleError(throwable); } - private boolean isTemplateScenario(Table table) { + private boolean isTemplateScenario(ScenarioDefinition scenario) { + Table table = scenario.getTable(); return table != null && table.getRowCount() > 0; } @@ -76,9 +78,10 @@ private void runGivenScenarios(Configuration configuration, ScenarioDefinition scenario, CandidateSteps... candidateSteps) throws Throwable { - if ( scenario.getGivenScenarios().size() > 0 ){ - for ( String scenarioPath : scenario.getGivenScenarios() ){ - reporter.givenScenario(scenarioPath); + List<String> givenScenarios = scenario.getGivenScenarios(); + if ( givenScenarios.size() > 0 ){ + reporter.givenScenarios(givenScenarios); + for ( String scenarioPath : givenScenarios ){ run(scenarioPath, configuration, candidateSteps); } } @@ -88,6 +91,9 @@ ScenarioDefinition scenario, Map<String, String> tableValues, CandidateSteps... candidateSteps) { Step[] steps = configuration.forCreatingSteps().createStepsFrom(scenario, tableValues, candidateSteps); reporter.beforeScenario(scenario.getTitle()); + if ( !tableValues.isEmpty() ){ + reporter.usingTableValues(tableValues); + } state = new FineSoFar(); for (Step step : steps) { state.run(step);
Modified: trunk/core/jbehave-core/src/java/org/jbehave/scenario/definition/Table.java (1222 => 1223)
--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/definition/Table.java 2009-09-05 15:11:22 UTC (rev 1222) +++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/definition/Table.java 2009-09-05 15:41:28 UTC (rev 1223) @@ -5,28 +5,37 @@ import java.util.List; import java.util.Map; +/** + * Represents a tabular structure:<br/><br/> + * + * |header 1|header 2| .... |header n|<br/> + * |value 11|value 12| .... |value 1n|<br/> + * ...<br/> + * |value m1|value m2| .... |value mn|<br/> + */ public class Table { - private List<Map<String, String>> data = "" ArrayList<Map<String, String>>(); - private final String string; + private static final String COLUMN_SEPARATOR = "\\|"; + private final List<Map<String, String>> data = "" ArrayList<Map<String, String>>(); + private final String tableAsString; - public Table(String string) { - this.string = string; - parse(string); + public Table(String tableAsString) { + this.tableAsString = tableAsString; + parse(); } - private void parse(String string) { - data = "" ArrayList<Map<String, String>>(); - String[] rows = string.split("\n"); + private void parse() { + data.clear(); + String[] rows = tableAsString.split("\n"); List<String> headers = new ArrayList<String>(); - for (int i = 0; i < rows.length; i++) { - List<String> columns = columnsFor(rows[i]); - if ( i == 0 ) { + for (int row = 0; row < rows.length; row++) { + List<String> columns = columnsFor(rows[row]); + if ( row == 0 ) { headers.addAll(columns); } else { Map<String, String> map = new HashMap<String, String>(); - for ( int j = 0; j < columns.size(); j++ ){ - map.put(headers.get(j), columns.get(j)); + for ( int column = 0; column < columns.size(); column++ ){ + map.put(headers.get(column), columns.get(column)); } data.add(map); } @@ -35,7 +44,7 @@ private List<String> columnsFor(String row) { List<String> columns = new ArrayList<String>(); - for ( String column : row.split("\\|") ){ + for ( String column : row.split(COLUMN_SEPARATOR) ){ columns.add(column.trim()); } int size = columns.size(); @@ -59,7 +68,7 @@ @Override public String toString(){ - return string; + return tableAsString; } }
Modified: trunk/core/jbehave-core/src/java/org/jbehave/scenario/parser/PatternScenarioParser.java (1222 => 1223)
--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/parser/PatternScenarioParser.java 2009-09-05 15:11:22 UTC (rev 1222) +++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/parser/PatternScenarioParser.java 2009-09-05 15:41:28 UTC (rev 1223) @@ -2,7 +2,6 @@ import static java.util.regex.Pattern.DOTALL; import static java.util.regex.Pattern.compile; -import static org.jbehave.scenario.definition.Blurb.EMPTY; import java.util.ArrayList; import java.util.List; @@ -23,6 +22,8 @@ */ public class PatternScenarioParser implements ScenarioParser { + private static final String NONE = ""; + private static final String COMMA = ","; private final Configuration configuration; public PatternScenarioParser() { @@ -57,22 +58,22 @@ private String findTitle(String scenario) { Matcher findingTitle = patternToPullScenarioTitleIntoGroupOne() .matcher(scenario); - return findingTitle.find() ? findingTitle.group(1).trim() : ""; + return findingTitle.find() ? findingTitle.group(1).trim() : NONE; } private Table findTable(String scenario) { Matcher findingTable = patternToPullScenarioTableIntoGroupOne() .matcher(scenario); - String table = findingTable.find() ? findingTable.group(1).trim() : ""; + String table = findingTable.find() ? findingTable.group(1).trim() : NONE; return new Table(table); } private List<String> findGivenScenarios(String scenario) { Matcher findingGivenScenarios = patternToPullGivenScenariosIntoGroupOne() .matcher(scenario); - String givenScenariosAsCSV = findingGivenScenarios.find() ? findingGivenScenarios.group(1).trim() : ""; + String givenScenariosAsCSV = findingGivenScenarios.find() ? findingGivenScenarios.group(1).trim() : NONE; List<String> givenScenarios = new ArrayList<String>(); - for ( String givenScenario : givenScenariosAsCSV.split(",") ){ + for ( String givenScenario : givenScenariosAsCSV.split(COMMA) ){ String trimmed = givenScenario.trim(); if ( trimmed.length() > 0 ) { givenScenarios.add(trimmed); @@ -99,7 +100,7 @@ if (matcher.find()) { return new Blurb(matcher.group(1).trim()); } else { - return EMPTY; + return Blurb.EMPTY; } } @@ -116,8 +117,7 @@ protected List<String> splitScenariosWithKeyword(String allScenariosInFile) { List<String> scenarios = new ArrayList<String>(); - String scenarioKeyword = "Scenario".replace("Scenario", configuration - .keywords().scenario()); + String scenarioKeyword = configuration.keywords().scenario(); String allScenarios = null; // chomp off anything before first keyword, if found
Modified: trunk/core/jbehave-core/src/java/org/jbehave/scenario/reporters/PassSilentlyDecorator.java (1222 => 1223)
--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/reporters/PassSilentlyDecorator.java 2009-09-05 15:11:22 UTC (rev 1222) +++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/reporters/PassSilentlyDecorator.java 2009-09-05 15:41:28 UTC (rev 1223) @@ -2,6 +2,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.Map; import org.jbehave.scenario.definition.Blurb; @@ -104,6 +105,22 @@ }); } + public void givenScenarios(final List<String> givenScenarios) { + currentScenario.add(new Todo() { + public void doNow() { + delegate.givenScenarios(givenScenarios); + } + }); + } + + public void usingTableValues(final Map<String, String> tableValues) { + currentScenario.add(new Todo() { + public void doNow() { + delegate.usingTableValues(tableValues); + } + }); + } + private static interface Todo { void doNow(); } @@ -117,11 +134,4 @@ 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 (1222 => 1223)
--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/reporters/PrintStreamScenarioReporter.java 2009-09-05 15:11:22 UTC (rev 1222) +++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/reporters/PrintStreamScenarioReporter.java 2009-09-05 15:41:28 UTC (rev 1223) @@ -1,6 +1,8 @@ package org.jbehave.scenario.reporters; import java.io.PrintStream; +import java.util.List; +import java.util.Map; import org.jbehave.scenario.definition.Blurb; @@ -68,8 +70,12 @@ output.println(); } - public void givenScenario(String scenarioPath) { - output.println("GivenScenario: "+scenarioPath); + public void givenScenarios(List<String> givenScenarios) { + output.println("GivenScenarios: "+givenScenarios); } + public void usingTableValues(Map<String, String> tableValues) { + output.println("Using table values: "+tableValues); + } + }
Modified: trunk/core/jbehave-core/src/java/org/jbehave/scenario/reporters/ScenarioReporter.java (1222 => 1223)
--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/reporters/ScenarioReporter.java 2009-09-05 15:11:22 UTC (rev 1222) +++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/reporters/ScenarioReporter.java 2009-09-05 15:41:28 UTC (rev 1223) @@ -1,18 +1,30 @@ package org.jbehave.scenario.reporters; +import java.util.List; +import java.util.Map; + import org.jbehave.scenario.definition.Blurb; /** - * Allows the runner to report the state of running scenario steps. + * Allows the runner to report the state of running scenarios * * @author Elizabeth Keogh + * @author Mauro Talevi */ public interface ScenarioReporter { + void beforeStory(Blurb blurb); + + void afterStory(); + void beforeScenario(String title); void afterScenario(); + void givenScenarios(List<String> givenScenarios); + + void usingTableValues(Map<String, String> tableValues); + void successful(String step); void pending(String step); @@ -21,10 +33,4 @@ void failed(String step, Throwable e); - void beforeStory(Blurb blurb); - - void afterStory(); - - void givenScenario(String scenarioPath); - }
Modified: trunk/core/jbehave-core/src/java/org/jbehave/scenario/reporters/StepFailureScenarioReporterDecorator.java (1222 => 1223)
--- trunk/core/jbehave-core/src/java/org/jbehave/scenario/reporters/StepFailureScenarioReporterDecorator.java 2009-09-05 15:11:22 UTC (rev 1222) +++ trunk/core/jbehave-core/src/java/org/jbehave/scenario/reporters/StepFailureScenarioReporterDecorator.java 2009-09-05 15:41:28 UTC (rev 1223) @@ -1,5 +1,8 @@ package org.jbehave.scenario.reporters; +import java.util.List; +import java.util.Map; + import org.jbehave.scenario.definition.Blurb; import org.jbehave.scenario.errors.StepFailure; @@ -59,8 +62,12 @@ delegate.successful(step); } - public void givenScenario(String scenarioPath) { - delegate.givenScenario(scenarioPath); + public void givenScenarios(List<String> givenScenarios) { + delegate.givenScenarios(givenScenarios); } + public void usingTableValues(Map<String, String> tableValues) { + delegate.usingTableValues(tableValues); + } + }
To unsubscribe from this list please visit:
