- Revision
- 884
- Author
- sirenian
- Date
- 2008-08-20 07:15:55 -0500 (Wed, 20 Aug 2008)
Log Message
[Liz] Passing scenarios are now silent, can configure reporter to output all by setting system property org.jbehave.outputall
Modified Paths
- trunk/examples/noughtsandcrosses/src/scenario/com/lunivore/noughtsandcrosses/players_can_take_turns
- trunk/examples/noughtsandcrosses/src/scenario/com/lunivore/noughtsandcrosses/steps/GridSteps.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/ScenarioRunnerBehaviour.java
- trunk/jbehave-core/src/java/org/jbehave/scenario/PropertyBasedConfiguration.java
- trunk/jbehave-core/src/java/org/jbehave/scenario/ScenarioReporter.java
- trunk/jbehave-core/src/java/org/jbehave/scenario/ScenarioRunner.java
- trunk/jbehave-core/src/java/org/jbehave/scenario/reporters/PrintStreamScenarioReporter.java
Added Paths
Diff
Modified: trunk/examples/noughtsandcrosses/src/scenario/com/lunivore/noughtsandcrosses/players_can_take_turns (883 => 884)
--- trunk/examples/noughtsandcrosses/src/scenario/com/lunivore/noughtsandcrosses/players_can_take_turns 2008-08-18 18:34:12 UTC (rev 883) +++ trunk/examples/noughtsandcrosses/src/scenario/com/lunivore/noughtsandcrosses/players_can_take_turns 2008-08-20 12:15:55 UTC (rev 884) @@ -17,3 +17,4 @@ Then the grid should look like XO. ... +...
Modified: trunk/examples/noughtsandcrosses/src/scenario/com/lunivore/noughtsandcrosses/steps/GridSteps.java (883 => 884)
--- trunk/examples/noughtsandcrosses/src/scenario/com/lunivore/noughtsandcrosses/steps/GridSteps.java 2008-08-18 18:34:12 UTC (rev 883) +++ trunk/examples/noughtsandcrosses/src/scenario/com/lunivore/noughtsandcrosses/steps/GridSteps.java 2008-08-20 12:15:55 UTC (rev 884) @@ -13,7 +13,6 @@ import org.jbehave.scenario.annotations.Given; import org.jbehave.scenario.annotations.Then; import org.jbehave.scenario.annotations.When; -import org.jbehave.scenario.steps.PendingError; import org.jbehave.scenario.steps.Steps; import org.lunivore.tyburn.WindowControl;
Modified: trunk/jbehave-core/src/behaviour/org/jbehave/scenario/PropertyBasedConfigurationBehaviour.java (883 => 884)
--- trunk/jbehave-core/src/behaviour/org/jbehave/scenario/PropertyBasedConfigurationBehaviour.java 2008-08-18 18:34:12 UTC (rev 883) +++ trunk/jbehave-core/src/behaviour/org/jbehave/scenario/PropertyBasedConfigurationBehaviour.java 2008-08-20 12:15:55 UTC (rev 884) @@ -3,6 +3,8 @@ import static org.hamcrest.CoreMatchers.is; import static org.jbehave.Ensure.ensureThat; +import org.jbehave.scenario.reporters.PassSilentlyDecorator; +import org.jbehave.scenario.reporters.PrintStreamScenarioReporter; import org.jbehave.scenario.steps.PendingStepStrategy; import org.junit.After; import org.junit.Before; @@ -37,4 +39,16 @@ System.setProperty(PropertyBasedConfiguration.FAIL_ON_PENDING, "true"); ensureThat(new PropertyBasedConfiguration().forPendingSteps(), is(PendingStepStrategy.FAILING)); } + + @Test + public void shouldSwallowOutputFromPassingScenariosByDefault() { + System.clearProperty(PropertyBasedConfiguration.OUTPUT_ALL); + ensureThat(new PropertyBasedConfiguration().forReportingScenarios(), is(PassSilentlyDecorator.class)); + } + + @Test + public void shouldOutputAllWhenConfiguredToDoSo() { + System.setProperty(PropertyBasedConfiguration.OUTPUT_ALL, "true"); + ensureThat(new PropertyBasedConfiguration().forReportingScenarios(), is(PrintStreamScenarioReporter.class)); + } }
Modified: trunk/jbehave-core/src/behaviour/org/jbehave/scenario/ScenarioBehaviour.java (883 => 884)
--- trunk/jbehave-core/src/behaviour/org/jbehave/scenario/ScenarioBehaviour.java 2008-08-18 18:34:12 UTC (rev 883) +++ trunk/jbehave-core/src/behaviour/org/jbehave/scenario/ScenarioBehaviour.java 2008-08-20 12:15:55 UTC (rev 884) @@ -38,10 +38,14 @@ private static final String NL = System.getProperty("line.separator"); private String originalFailOnPending; + private String originalPassSilently; @Before - public void captureExistingEnvironment() { + public void captureExistingEnvironmentAndMakeTheseExamplesWork() { originalFailOnPending = System.getProperty(PropertyBasedConfiguration.FAIL_ON_PENDING); + originalPassSilently = System.getProperty(PropertyBasedConfiguration.OUTPUT_ALL); + System.clearProperty(PropertyBasedConfiguration.FAIL_ON_PENDING); + System.clearProperty(PropertyBasedConfiguration.OUTPUT_ALL); } @After @@ -51,6 +55,11 @@ } else { System.clearProperty(PropertyBasedConfiguration.FAIL_ON_PENDING); } + if (originalPassSilently != null) { + System.setProperty(PropertyBasedConfiguration.OUTPUT_ALL, originalPassSilently); + } else { + System.clearProperty(PropertyBasedConfiguration.OUTPUT_ALL); + } } @Test @@ -65,17 +74,17 @@ stub(fileLoader.loadStepsFor(MyScenario.class)).toReturn(Arrays.asList( new ScenarioDefinition(stepParser, "my_scenario"))); stub(stepParser.findSteps("my_scenario")).toReturn(Arrays.asList(new String[] { - "Given I have 2 cows", - "When I leave them over the winter", - "Then I should have 2 cows"})); + "Given I have 2 scenarios", + "When I do something unexpected", + "Then I should have 2 scenarios"})); new MyScenario(fileLoader, stepParser, reporter, steps).runUsingSteps(); - ensureThat(steps.numberOfCows, equalTo(2)); + ensureThat(steps.numberOfScenarios, equalTo(2)); ensureThat(output.toString(), equalTo( - "Given I have 2 cows" + NL + - "When I leave them over the winter (PENDING)" + NL + - "Then I should have 2 cows (NOT PERFORMED)" + NL)); + "Given I have 2 scenarios" + NL + + "When I do something unexpected (PENDING)" + NL + + "Then I should have 2 scenarios (NOT PERFORMED)" + NL)); } @Test @@ -90,17 +99,17 @@ stub(fileLoader.loadStepsFor(MyScenario.class)).toReturn(Arrays.asList( new ScenarioDefinition(stepParser, "my_scenario"))); stub(stepParser.findSteps("my_scenario")).toReturn(Arrays.asList(new String[] { - "Given I have 2 cows", - "When I leave them over the winter", - "Then I should have 2 cows"})); + "Given I have 2 scenarios", + "When I do something unexpected", + "Then I should have 2 scenarios"})); new MyScenario(fileLoader, stepParser, reporter, steps).runUsingSteps(); - ensureThat(steps.numberOfCows, equalTo(2)); + ensureThat(steps.numberOfScenarios, equalTo(2)); ensureThat(buffer.toString(), equalTo( - "Given I have 2 cows" + NL + - "When I leave them over the winter (PENDING)" + NL + - "Then I should have 2 cows (NOT PERFORMED)" + NL)); + "Given I have 2 scenarios" + NL + + "When I do something unexpected (PENDING)" + NL + + "Then I should have 2 scenarios (NOT PERFORMED)" + NL)); } @Test @@ -127,6 +136,16 @@ } @Test + public void shouldAllowPassingScenariosToBeSilent() throws Throwable { + System.setProperty(PropertyBasedConfiguration.OUTPUT_ALL, "true"); + + // The only way to test this would be to replace the reporter in the configuration. + // As a JBehave user, I want to change reporters so that the reporter I specify + // is the one that's used. Doing anything which would allow this to be testable + // would conflict with that intuitive use! So, + } + + @Test public void shouldAllowPartlyDefinedStepsToExplicitlyThrowPendingErrors() throws Throwable { ScenarioDefiner fileLoader = mock(ScenarioDefiner.class); StepParser stepParser = mock(PatternStepParser.class); @@ -137,17 +156,17 @@ stub(fileLoader.loadStepsFor(MyScenario.class)).toReturn(Arrays.asList( new ScenarioDefinition(stepParser, "my_scenario"))); stub(stepParser.findSteps("my_scenario")).toReturn(Arrays.asList(new String[] { - "Given I have 2 cows", - "When I put them in a field", - "Then my cows should still be waiting for the spring"})); + "Given I have 2 scenarios", + "When I read my scenarios", + "Then my scenario should be pending"})); new MyScenario(fileLoader, stepParser, reporter, steps).runUsingSteps(); - ensureThat(steps.numberOfCows, equalTo(2)); + ensureThat(steps.numberOfScenarios, equalTo(2)); ensureThat(buffer.toString(), equalTo( - "Given I have 2 cows" + NL + - "When I put them in a field" + NL + - "Then my cows should still be waiting for the spring (PENDING)" + NL)); + "Given I have 2 scenarios" + NL + + "When I read my scenarios" + NL + + "Then my scenario should be pending (PENDING)" + NL)); } @Test @@ -162,10 +181,10 @@ stub(scenarioDefiner.loadStepsFor(MyScenario.class)).toReturn(Arrays.asList( new ScenarioDefinition(stepParser, "my_scenario"))); stub(stepParser.findSteps("my_scenario")).toReturn(Arrays.asList(new String[] { - "Given I have 2 cows", - "When I put them in a field", - "Then my cows should not die", - "Then I should have 2 cows"})); + "Given I have 2 scenarios", + "When I read my scenarios", + "Then my scenario should fail", + "Then I should have 2 scenarios"})); try { new MyScenario(scenarioDefiner, stepParser, reporter, steps).runUsingSteps(); @@ -175,10 +194,10 @@ } ensureThat(output.toString(), equalTo( - "Given I have 2 cows" + NL + - "When I put them in a field" + NL + - "Then my cows should not die (FAILED)" + NL + - "Then I should have 2 cows (NOT PERFORMED)" + NL)); + "Given I have 2 scenarios" + NL + + "When I read my scenarios" + NL + + "Then my scenario should fail (FAILED)" + NL + + "Then I should have 2 scenarios (NOT PERFORMED)" + NL)); } @@ -198,30 +217,30 @@ public static class MySteps extends Steps { - private int numberOfCows; + private int numberOfScenarios; private IllegalAccessError error; - @Given("I have $n cows") - public void makeCows(int numberOfCows) { - this.numberOfCows = numberOfCows; + @Given("I have $n scenarios") + public void makeSomeScenarios(int numberOfScenarios) { + this.numberOfScenarios = numberOfScenarios; } - @When("I put them in a field") - public void ignoreCows() {} + @When("I read my scenarios") + public void readScenarios() {} - @Then("I should have $n cows") - public void checkCows(int numberOfCows) { - ensureThat(this.numberOfCows, equalTo(numberOfCows)); + @Then("I should have $n scenarios") + public void checkNumberOfScenarios(int numberOfCows) { + ensureThat(this.numberOfScenarios, equalTo(numberOfCows)); } - @Then("my cows should not die") - public void keepCowsAlive() { - error = new IllegalAccessError("Leave my cows alone!"); + @Then("my scenario should fail") + public void makeTheScenarioFail() { + error = new IllegalAccessError("Die, Scenario, Die!"); throw error; } - @Then("my cows should still be waiting for the spring") - public void keepCowsWaiting() { + @Then("my scenario should be pending") + public void pending() { throw new PendingError("Cows are waiting"); } } @@ -249,6 +268,16 @@ public void successful(String step) { buffer.append(step+NL); } + + public void afterScenario() { + // TODO Auto-generated method stub + + } + + public void beforeScenario(String blurb) { + // TODO Auto-generated method stub + + } } }
Modified: trunk/jbehave-core/src/behaviour/org/jbehave/scenario/ScenarioRunnerBehaviour.java (883 => 884)
--- trunk/jbehave-core/src/behaviour/org/jbehave/scenario/ScenarioRunnerBehaviour.java 2008-08-18 18:34:12 UTC (rev 883) +++ trunk/jbehave-core/src/behaviour/org/jbehave/scenario/ScenarioRunnerBehaviour.java 2008-08-20 12:15:55 UTC (rev 884) @@ -7,11 +7,13 @@ import static org.mockito.Mockito.never; import static org.mockito.Mockito.stub; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.inOrder; import org.jbehave.scenario.steps.PendingStepStrategy; import org.jbehave.scenario.steps.Step; import org.jbehave.scenario.steps.StepResult; import org.junit.Test; +import org.mockito.InOrder; public class ScenarioRunnerBehaviour { @@ -114,6 +116,41 @@ } @Test + public void shouldInformReporterOfChangeInScenarioForEachSetOfSteps() throws Throwable { + IllegalArgumentException anException = new IllegalArgumentException(); + + ScenarioReporter reporter = mock(ScenarioReporter.class); + Step pendingStep = mock(Step.class); + Step secondStep = mock(Step.class); + Step failingStep = mock(Step.class); + stub(pendingStep.perform()).toReturn(StepResult.pending("pendingStep")); + stub(secondStep.perform()).toReturn(StepResult.success("secondStep")); + stub(failingStep.perform()).toReturn(StepResult.failure("failingStep", anException)); + + ScenarioRunner runner = new ScenarioRunner(reporter, PendingStepStrategy.PASSING); + + runner.run("header for pending scenario", pendingStep); + runner.run("header for second scenario", secondStep); + try { + runner.run("header for failing scenario", failingStep); + } catch (IllegalArgumentException e) { + // expected + } + + InOrder inOrder = inOrder(reporter); + + inOrder.verify(reporter).beforeScenario("header for pending scenario"); + inOrder.verify(reporter).pending("pendingStep"); + inOrder.verify(reporter).afterScenario(); + inOrder.verify(reporter).beforeScenario("header for second scenario"); + inOrder.verify(reporter).successful("secondStep"); + inOrder.verify(reporter).afterScenario(); + inOrder.verify(reporter).beforeScenario("header for failing scenario"); + inOrder.verify(reporter).failed("failingStep", anException); + inOrder.verify(reporter).afterScenario(); + } + + @Test public void shouldHandlePendingStepsAccordingToStrategy() throws Throwable { ScenarioReporter reporter = mock(ScenarioReporter.class); Step pendingStep = mock(Step.class);
Added: trunk/jbehave-core/src/behaviour/org/jbehave/scenario/reporters/PassSilentlyDecoratorBehaviour.java (0 => 884)
--- trunk/jbehave-core/src/behaviour/org/jbehave/scenario/reporters/PassSilentlyDecoratorBehaviour.java (rev 0) +++ trunk/jbehave-core/src/behaviour/org/jbehave/scenario/reporters/PassSilentlyDecoratorBehaviour.java 2008-08-20 12:15:55 UTC (rev 884) @@ -0,0 +1,69 @@ +package org.jbehave.scenario.reporters; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.inOrder; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; + +import org.jbehave.scenario.ScenarioReporter; +import org.junit.Test; +import org.mockito.InOrder; + +public class PassSilentlyDecoratorBehaviour { + + @Test + public void shouldSwallowOutputFromPassingScenarios() { + ScenarioReporter delegate = mock(ScenarioReporter.class); + PassSilentlyDecorator decorator = new PassSilentlyDecorator(delegate); + IllegalArgumentException anException = new IllegalArgumentException(); + + decorator.beforeScenario("My scenario 1"); + decorator.successful("Given step 1.1"); + decorator.successful("When step 1.2"); + decorator.successful("Then step 1.3"); + decorator.afterScenario(); + + decorator.beforeScenario("My scenario 2"); + decorator.successful("Given step 2.1"); + decorator.pending("When step 2.2"); + decorator.notPerformed("Then step 2.3"); + decorator.afterScenario(); + + decorator.beforeScenario("My scenario 3"); + decorator.successful("Given step 3.1"); + decorator.successful("When step 3.2"); + decorator.failed("Then step 3.3", anException); + decorator.afterScenario(); + + decorator.beforeScenario("My scenario 4"); + decorator.successful("Given step 4.1"); + decorator.successful("When step 4.2"); + decorator.successful("Then step 4.3"); + decorator.afterScenario(); + + InOrder inOrder = inOrder(delegate); + + verify(delegate, never()).beforeScenario("My scenario 1"); + verify(delegate, never()).successful("Given step 1.1"); + verify(delegate, never()).successful("When step 1.2"); + verify(delegate, never()).successful("Then step 1.3"); + + verify(delegate, never()).beforeScenario("My scenario 4"); + verify(delegate, never()).successful("Given step 4.1"); + verify(delegate, never()).successful("When step 4.2"); + verify(delegate, never()).successful("Then step 4.3"); + + inOrder.verify(delegate).beforeScenario("My scenario 2"); + inOrder.verify(delegate).successful("Given step 2.1"); + inOrder.verify(delegate).pending("When step 2.2"); + inOrder.verify(delegate).notPerformed("Then step 2.3"); + inOrder.verify(delegate).afterScenario(); + + inOrder.verify(delegate).beforeScenario("My scenario 3"); + inOrder.verify(delegate).successful("Given step 3.1"); + inOrder.verify(delegate).successful("When step 3.2"); + inOrder.verify(delegate).failed("Then step 3.3", anException); + inOrder.verify(delegate).afterScenario(); + + } +}
Modified: trunk/jbehave-core/src/java/org/jbehave/scenario/PropertyBasedConfiguration.java (883 => 884)
--- trunk/jbehave-core/src/java/org/jbehave/scenario/PropertyBasedConfiguration.java 2008-08-18 18:34:12 UTC (rev 883) +++ trunk/jbehave-core/src/java/org/jbehave/scenario/PropertyBasedConfiguration.java 2008-08-20 12:15:55 UTC (rev 884) @@ -5,15 +5,21 @@ 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.steps.PendingStepStrategy; public class PropertyBasedConfiguration implements Configuration { public static final String FAIL_ON_PENDING = "org.jbehave.failonpending"; + public static String OUTPUT_ALL = "org.jbehave.outputall"; public ScenarioReporter forReportingScenarios() { - return new PrintStreamScenarioReporter(); + if (System.getProperty(OUTPUT_ALL) == null) { + return new PassSilentlyDecorator(new PrintStreamScenarioReporter()); + } else { + return new PrintStreamScenarioReporter(); + } } public ScenarioDefiner forDefiningScenarios() {
Modified: trunk/jbehave-core/src/java/org/jbehave/scenario/ScenarioReporter.java (883 => 884)
--- trunk/jbehave-core/src/java/org/jbehave/scenario/ScenarioReporter.java 2008-08-18 18:34:12 UTC (rev 883) +++ trunk/jbehave-core/src/java/org/jbehave/scenario/ScenarioReporter.java 2008-08-20 12:15:55 UTC (rev 884) @@ -7,6 +7,10 @@ */ public interface ScenarioReporter { + void beforeScenario(String title); + + void afterScenario(); + void successful(String step); void pending(String step); @@ -15,4 +19,5 @@ void failed(String step, Throwable e); + }
Modified: trunk/jbehave-core/src/java/org/jbehave/scenario/ScenarioRunner.java (883 => 884)
--- trunk/jbehave-core/src/java/org/jbehave/scenario/ScenarioRunner.java 2008-08-18 18:34:12 UTC (rev 883) +++ trunk/jbehave-core/src/java/org/jbehave/scenario/ScenarioRunner.java 2008-08-20 12:15:55 UTC (rev 884) @@ -32,12 +32,18 @@ } public void run(Step... steps) throws Throwable { + run("", steps); + } + + public void run(String title, Step... steps) throws Throwable { + reporter.beforeScenario(title); state = new FineSoFar(); for (Step step : steps) { state.run(step); } + reporter.afterScenario(); currentStrategy.handleError(throwable); - } + }; private class SomethingHappened extends State { @Override @@ -74,5 +80,5 @@ private abstract class State { protected abstract void run(Step step); - }; + } }
Added: trunk/jbehave-core/src/java/org/jbehave/scenario/reporters/PassSilentlyDecorator.java (0 => 884)
--- trunk/jbehave-core/src/java/org/jbehave/scenario/reporters/PassSilentlyDecorator.java (rev 0) +++ trunk/jbehave-core/src/java/org/jbehave/scenario/reporters/PassSilentlyDecorator.java 2008-08-20 12:15:55 UTC (rev 884) @@ -0,0 +1,97 @@ +package org.jbehave.scenario.reporters; + +import java.util.ArrayList; +import java.util.List; + +import org.jbehave.scenario.ScenarioReporter; + +/** + * Swallows the reports from all scenarios that pass, providing output + * only for failing or pending scenarios. + */ +public class PassSilentlyDecorator implements ScenarioReporter { + + + + private final ScenarioReporter delegate; + private List<Todo> currentScenario; + private State state = State.SILENT; + + public PassSilentlyDecorator(ScenarioReporter delegate) { + this.delegate = delegate; + } + + public void failed(final String step, final Throwable e) { + currentScenario.add(new Todo() { + public void doNow() { + delegate.failed(step, e); + } + }); + setStateToNoisy(); + } + + public void notPerformed(final String step) { + currentScenario.add(new Todo() { + public void doNow() { + delegate.notPerformed(step); + } + }); + } + + public void pending(final String step) { + currentScenario.add(new Todo() { + public void doNow() { + delegate.pending(step); + } + }); + setStateToNoisy(); + } + + private void setStateToNoisy() { + state = new State(){ + public void report() { + for (Todo todo : currentScenario) { + todo.doNow(); + } + }}; + } + + public void successful(final String step) { + currentScenario.add(new Todo() { + public void doNow() { + delegate.successful(step); + } + }); + } + + public void afterScenario() { + currentScenario.add(new Todo() { + public void doNow() { + delegate.afterScenario(); + } + }); + state.report(); + state = State.SILENT; + } + + public void beforeScenario(final String title) { + currentScenario = new ArrayList<Todo>(); + currentScenario.add(new Todo() { + public void doNow() { + delegate.beforeScenario(title); + } + }); + } + + + private static interface Todo { + void doNow(); + } + + + private interface State { + State SILENT = new State(){public void report() {}}; + + void report(); + }; +}
Modified: trunk/jbehave-core/src/java/org/jbehave/scenario/reporters/PrintStreamScenarioReporter.java (883 => 884)
--- trunk/jbehave-core/src/java/org/jbehave/scenario/reporters/PrintStreamScenarioReporter.java 2008-08-18 18:34:12 UTC (rev 883) +++ trunk/jbehave-core/src/java/org/jbehave/scenario/reporters/PrintStreamScenarioReporter.java 2008-08-20 12:15:55 UTC (rev 884) @@ -32,4 +32,14 @@ output.println(step + " (FAILED)"); } + public void afterScenario() { + // TODO Auto-generated method stub + + } + + public void beforeScenario(String blurb) { + // TODO Auto-generated method stub + + } + }
To unsubscribe from this list please visit:
