Skip to content

Commit

Permalink
Fix several smaller issues
Browse files Browse the repository at this point in the history
- encode parameters in HTML report (Issue-#9)
- fix issue where last step was always marked as failed
  eventhought it should be skipped
- fix issue where some test runners (e.g. Eclipse) did not mark
  a test as failed
- deprecate Schritte-class and added Stufe-class instead
  • Loading branch information
Jan Schäfer committed Jun 29, 2014
1 parent 3608f02 commit f80df9a
Show file tree
Hide file tree
Showing 16 changed files with 131 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public enum State {

/**
* Measures the stack depth of methods called on the step definition object.
* Only the top-level method calls are used for reporting.
* Only the top-level method calls are used for reporting.
*/
private final AtomicInteger stackDepth = new AtomicInteger();

Expand Down Expand Up @@ -95,15 +95,17 @@ static class StageState {
class MethodHandler implements StepMethodHandler {
@Override
public void handleMethod( Object stageInstance, Method paramMethod, Object[] arguments, InvocationMode mode ) throws Throwable {
if( paramMethod.isSynthetic() )
if( paramMethod.isSynthetic() ) {
return;
}

if( paramMethod.isAnnotationPresent( AfterStage.class ) ||
paramMethod.isAnnotationPresent( BeforeStage.class ) ||
paramMethod.isAnnotationPresent( BeforeScenario.class ) ||
paramMethod.isAnnotationPresent( AfterScenario.class ) ||
paramMethod.isAnnotationPresent( Hidden.class ) )
paramMethod.isAnnotationPresent( Hidden.class ) ) {
return;
}

update( stageInstance );

Expand All @@ -116,15 +118,17 @@ public void handleMethod( Object stageInstance, Method paramMethod, Object[] arg

@Override
public void handleThrowable( Throwable t ) throws Throwable {
listener.stepMethodFailed( t );
failed( t );
}

}

@SuppressWarnings( "unchecked" )
public <T> T addStage( Class<T> stepsClass ) {
if( stages.containsKey( stepsClass ) )
if( stages.containsKey( stepsClass ) ) {
return (T) stages.get( stepsClass ).instance;
}

T result = setupCglibProxy( stepsClass );

Expand Down Expand Up @@ -186,8 +190,9 @@ private <T> T update( T t ) throws Throwable {

private void executeAfterStageMethods( Object stage ) throws Throwable {
StageState stageState = getStageState( stage );
if( stageState.afterStageCalled )
if( stageState.afterStageCalled ) {
return;
}
stageState.afterStageCalled = true;
executeAnnotatedMethods( stage, AfterStage.class );
}
Expand All @@ -197,8 +202,9 @@ StageState getStageState( Object stage ) {
}

private void ensureBeforeStepsAreExecuted() throws Throwable {
if( state != State.INIT )
if( state != State.INIT ) {
return;
}
state = State.STARTED;
methodInterceptor.enableMethodHandling( false );

Expand Down Expand Up @@ -274,10 +280,12 @@ public void wireSteps( CanWire canWire ) {
* Has to be called when the scenario is finished in order to execute after methods
*/
public void finished() throws Throwable {
if( state == FINISHED )
if( state == FINISHED ) {
return;
if( state != STARTED )
}
if( state != STARTED ) {
throw new IllegalStateException( "The Scenario must be in state STARTED in order to finish it, but it is in state " + state );
}
state = FINISHED;
methodInterceptor.enableMethodHandling( false );

Expand Down Expand Up @@ -350,7 +358,7 @@ public void failed( Throwable e ) {

/**
* Starts a scenario with the given description.
*
*
* @param description the description of the scenario
*/
public void startScenario( String description ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ public void stepMethodInvoked( Method paramMethod, List<Object> arguments, Invoc

@Override
public void introWordAdded( String word ) {}

@Override
public void stepMethodFailed( Throwable t ) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ public interface ScenarioListener {

void introWordAdded( String word );

void stepMethodFailed( Throwable t );

}
40 changes: 3 additions & 37 deletions jgiven-core/src/main/java/com/tngtech/jgiven/lang/de/Schritte.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,7 @@
package com.tngtech.jgiven.lang.de;

import com.tngtech.jgiven.annotation.IntroWord;
import com.tngtech.jgiven.base.StageBase;

/**
* A German version for step definitions
* @deprecated will be removed with version 0.3.0. Use {@link Stufe} instead
*/
public class Schritte<SELF extends Schritte<?>> extends StageBase<SELF> {

@IntroWord
public SELF gegeben() {
return self();
}

@IntroWord
public SELF wenn() {
return self();
}

@IntroWord
public SELF dann() {
return self();
}

@IntroWord
public SELF und() {
return self();
}

@IntroWord
public SELF aber() {
return self();
}

@IntroWord
public SELF mit() {
return self();
}

}
@Deprecated
public class Schritte<SELF extends Schritte<?>> extends Stufe<SELF> {}
43 changes: 43 additions & 0 deletions jgiven-core/src/main/java/com/tngtech/jgiven/lang/de/Stufe.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.tngtech.jgiven.lang.de;

import com.tngtech.jgiven.Stage;
import com.tngtech.jgiven.annotation.IntroWord;
import com.tngtech.jgiven.base.StageBase;

/**
* Eine deutsche Version der {@link Stage}-Klasse
*
*/
public class Stufe<SELF extends Stufe<?>> extends StageBase<SELF> {

@IntroWord
public SELF gegeben() {
return self();
}

@IntroWord
public SELF wenn() {
return self();
}

@IntroWord
public SELF dann() {
return self();
}

@IntroWord
public SELF und() {
return self();
}

@IntroWord
public SELF aber() {
return self();
}

@IntroWord
public SELF mit() {
return self();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.io.PrintWriter;

import com.google.common.html.HtmlEscapers;
import com.tngtech.jgiven.impl.util.WordUtil;
import com.tngtech.jgiven.report.model.ReportModelVisitor;
import com.tngtech.jgiven.report.model.ScenarioCaseModel;
Expand Down Expand Up @@ -108,7 +109,7 @@ public void visit( StepModel stepModel ) {
if( !firstWord ) {
writer.print( ' ' );
}
String text = word.value;
String text = HtmlEscapers.htmlEscaper().escape( word.value );

if( firstWord && word.isIntroWord ) {
writer.print( format( "<span class='introWord'>%s</span>", WordUtil.capitalize( text ) ) );
Expand All @@ -128,7 +129,7 @@ public void visit( StepModel stepModel ) {
}

private void printArg( Word word ) {
String value = word.getArgumentInfo().isCaseArg() ? formatCaseArgument( word ) : word.value;
String value = word.getArgumentInfo().isCaseArg() ? formatCaseArgument( word ) : HtmlEscapers.htmlEscaper().escape( word.value );
value = escapeToHtml( value );
String multiLine = value.contains( "<br />" ) ? "multiline" : "";
String caseClass = word.getArgumentInfo().isCaseArg() ? "caseArgument" : "argument";
Expand All @@ -140,6 +141,6 @@ private String escapeToHtml( String value ) {
}

String formatCaseArgument( Word word ) {
return word.value;
return HtmlEscapers.htmlEscaper().escape( word.value );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,16 @@ private ScenarioCaseModel getCurrentScenarioCase() {

@Override
public void stepMethodInvoked( Method paramMethod, List<Object> arguments, InvocationMode mode ) {
if( !isStepMethod( paramMethod ) )
if( !isStepMethod( paramMethod ) ) {
return;
}
addStepMethod( paramMethod, arguments, mode );
}

public boolean isStepMethod( Method paramMethod ) {
if( !Modifier.isPublic( paramMethod.getModifiers() ) )
if( !Modifier.isPublic( paramMethod.getModifiers() ) ) {
return false;
}

return true;
}
Expand Down Expand Up @@ -205,11 +207,15 @@ public ReportModel getScenarioCollectionModel() {
}

@Override
public void scenarioFailed( Throwable e ) {
public void stepMethodFailed( Throwable t ) {
if( !currentScenarioCase.steps.isEmpty() ) {
currentScenarioCase.steps.get( currentScenarioCase.steps.size() - 1 )
.setStatus( StepStatus.FAILED );
}
}

@Override
public void scenarioFailed( Throwable e ) {
setSuccess( false );
setErrorMessage( e.getMessage() );
}
Expand Down Expand Up @@ -357,4 +363,5 @@ private static List<Tag> getExplodedTags( Tag originalTag, String[] stringArray
}
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public void evaluate() throws Throwable {
throw e;
} catch( Throwable t ) {
failed( t );
throw t;
}
}
};
Expand Down Expand Up @@ -126,11 +127,11 @@ private static List<Object> getArgumentsFrom( Object object, String fieldName )

} catch( NoSuchFieldException e ) {
log.warn( format( "Could not find field containing test method arguments in '%s'. "
+ "Probably the internal representation has changed. Consider writing a bug report.",
+ "Probably the internal representation has changed. Consider writing a bug report.",
methodClass.getSimpleName() ), e );
} catch( IllegalAccessException e ) {
log.warn( format( "Not able to access field containing test method arguments in '%s'. "
+ "Probably the internal representation has changed. Consider writing a bug report.",
+ "Probably the internal representation has changed. Consider writing a bug report.",
methodClass.getSimpleName() ), e );
}
return Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ private static List<TestScenario> setupTestScenarios() {
.numberOfSteps( 2 )
.failingStep( 1 );

addTestScenario( result, "failing_test_with_three_steps" )
.numberOfSteps( 3 )
.failingStep( 1 );

addTestScenario( result, "failing_test_with_two_steps_and_second_step_fails" )
.numberOfSteps( 2 )
.failingStep( 2 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ public void failing_test_with_two_steps() {
when().something_happens();
}

@Test
@org.testng.annotations.Test
public void failing_test_with_three_steps() {
given().an_exception_is_thrown();
when().something_happens();
then().something_happened();
}

@Test
@org.testng.annotations.Test
public void failing_test_with_two_steps_and_second_step_fails() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

public class ThenTestStage extends Stage<ThenTestStage> {

public void something_happened() {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ public void failing_tests_annotated_with_NotImplementedYet_with_executeSteps_set

@Test
public void steps_following_failing_steps_are_reported_as_skipped() {
given().a_failing_test_with_$_steps( 2 )
given().a_failing_test_with_$_steps( 3 )
.and().step_$_fails( 1 );
when().the_test_is_executed_with_JUnit();
then().step_$_is_reported_as_failed( 1 )
.and().step_$_is_reported_as_skipped( 2 );
.and().step_$_is_reported_as_skipped( 2 )
.and().step_$_is_reported_as_skipped( 3 );
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import org.junit.Test;

import com.tngtech.jgiven.junit.de.DeSzenarioTest.DeutscheTestSchritte;
import com.tngtech.jgiven.junit.de.DeSzenarioTest.DeutscheTestStufe;
import com.tngtech.jgiven.lang.de.Schritte;
import com.tngtech.jgiven.lang.de.Stufe;
import com.tngtech.jgiven.tags.FeatureGerman;

@FeatureGerman
public class DeSzenarioTest extends SzenarioTest<DeutscheTestSchritte, DeutscheTestSchritte, DeutscheTestSchritte> {
public class DeSzenarioTest extends SzenarioTest<DeutscheTestStufe, DeutscheTestStufe, DeutscheTestStufe> {

@Test
public void Szenarien_können_in_deutsch_geschrieben_werden() {
Expand All @@ -17,21 +18,21 @@ public class DeSzenarioTest extends SzenarioTest<DeutscheTestSchritte, DeutscheT
dann().generiert_JGiven_deutsche_Berichte();
}

static class DeutscheTestSchritte extends Schritte<DeutscheTestSchritte> {
static class DeutscheTestStufe extends Stufe<DeutscheTestStufe> {

public DeutscheTestSchritte ein_deutsches_Projekt() {
public DeutscheTestStufe ein_deutsches_Projekt() {
return self();
}

public DeutscheTestSchritte generiert_JGiven_deutsche_Berichte() {
public DeutscheTestStufe generiert_JGiven_deutsche_Berichte() {
return self();
}

public DeutscheTestSchritte die_Szenarien_in_deutsch_geschrieben_werden() {
public DeutscheTestStufe die_Szenarien_in_deutsch_geschrieben_werden() {
return self();
}

public DeutscheTestSchritte JGiven_verwendet_wird() {
public DeutscheTestStufe JGiven_verwendet_wird() {
return self();
}

Expand Down
Loading

0 comments on commit f80df9a

Please sign in to comment.