Skip to content

Commit

Permalink
Merge pull request #94 from TripleNail/master
Browse files Browse the repository at this point in the history
Enhanced spring support.
  • Loading branch information
janschaefer committed Aug 1, 2015
2 parents ceba19a + 28cda20 commit 4ab9fd0
Show file tree
Hide file tree
Showing 27 changed files with 1,111 additions and 501 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# v0.8.0

## Enhanced spring support

* when used together with the spring framework JGiven can use a specialized executor so that JGiven stages can be managed via spring
* Introduced `@JGivenStage` to ease writing spring beans that act as JGiven stage

# v0.7.4

## Fixed Issues
Expand Down
39 changes: 28 additions & 11 deletions jgiven-core/src/main/java/com/tngtech/jgiven/impl/Scenario.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,23 @@
*/
public class Scenario<GIVEN, WHEN, THEN> extends ScenarioBase {

private final GIVEN givenStage;
private final WHEN whenStage;
private final THEN thenStage;
private GIVEN givenStage;
private WHEN whenStage;
private THEN thenStage;
private final Class<GIVEN> givenClass;
private final Class<WHEN> whenClass;
private final Class<THEN> thenClass;

@SuppressWarnings( "unchecked" )
private Scenario( Class<?> stageClass ) {
givenStage = (GIVEN) executor.addStage( stageClass );
whenStage = (WHEN) givenStage;
thenStage = (THEN) givenStage;
private Scenario( Class<GIVEN> stageClass ) {
this.givenClass = stageClass;
this.whenClass = null;
this.thenClass = null;
}

public Scenario( Class<GIVEN> givenClass, Class<WHEN> whenClass, Class<THEN> thenClass ) {
givenStage = executor.addStage( givenClass );
whenStage = executor.addStage( whenClass );
thenStage = executor.addStage( thenClass );
this.givenClass = givenClass;
this.whenClass = whenClass;
this.thenClass = thenClass;
}

public GIVEN getGivenStage() {
Expand Down Expand Up @@ -84,6 +86,21 @@ public Scenario<GIVEN, WHEN, THEN> startScenario( String description ) {

}

@Override
@SuppressWarnings("unchecked")
protected void initialize() {
super.initialize();
if (whenClass == null) {
givenStage = (GIVEN) executor.addStage( givenClass );
whenStage = (WHEN) givenStage;
thenStage = (THEN) givenStage;
} else {
givenStage = executor.addStage( givenClass );
whenStage = executor.addStage( whenClass );
thenStage = executor.addStage( thenClass );
}
}

/**
* Alias for {@link #startScenario(String)}.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
package com.tngtech.jgiven.impl;

import java.lang.reflect.Method;
import java.util.List;

import com.tngtech.jgiven.impl.util.AssertionUtil;
import com.tngtech.jgiven.integration.CanWire;
import com.tngtech.jgiven.report.model.NamedArgument;
import com.tngtech.jgiven.report.model.ReportModel;
import com.tngtech.jgiven.report.model.ReportModelBuilder;

/**
* Base class for a Scenario.
* <p>
* Before a Scenario can be used it must be properly configured. After the configuration phase
* {@link #startScenario} must be called in order to execute the scenario. Once started a scenario
* cannot be reconfigured.
* <p>
* {@link #initialize} should be overridden by subclasses to apply their own configuration to the scenario.
*
*/
public class ScenarioBase {
protected final ScenarioExecutor executor = new ScenarioExecutor();
protected ScenarioExecutor executor = new StandaloneScenarioExecutor();
protected final ReportModelBuilder modelBuilder = new ReportModelBuilder();
private boolean initialized = false;

public ScenarioBase() {
executor.setListener( modelBuilder );
}

public void setModel( ReportModel scenarioCollectionModel ) {
assertNotInitialized();
modelBuilder.setReportModel( scenarioCollectionModel );
}

Expand All @@ -26,7 +42,7 @@ public <T> T addStage( Class<T> stepsClass ) {

/**
* Finishes the scenario.
*
*
* @throws Throwable in case some exception has been thrown during the execution of the scenario
*/
public void finished() throws Throwable {
Expand All @@ -37,6 +53,11 @@ public ScenarioExecutor getExecutor() {
return executor;
}

public void setExecutor(ScenarioExecutor executor) {
assertNotInitialized();
this.executor = executor;
}

public void wireSteps( CanWire canWire ) {
executor.wireSteps( canWire );
}
Expand All @@ -45,9 +66,35 @@ public ReportModelBuilder getModelBuilder() {
return modelBuilder;
}

public ScenarioBase startScenario(Method method, List<NamedArgument> arguments) {
performInitialization();
executor.startScenario(method, arguments);
return this;
}

public ScenarioBase startScenario( String description ) {
performInitialization();
executor.startScenario( description );
return this;
}

private void performInitialization() {
if (modelBuilder == null) {
throw new IllegalStateException("modelBuilder must be set before Scenario can be initalized.");
}
if (!initialized) {
executor.setListener( modelBuilder );
initialize();
initialized = true;
}
}

protected void initialize() {
// extension point for two phase initialization
}

protected void assertNotInitialized() {
AssertionUtil.assertTrue(!initialized, "Scenario is already initialized");
}

}
Loading

0 comments on commit 4ab9fd0

Please sign in to comment.