Skip to content

Commit

Permalink
use one report model per class when using TestNG (fixes #115)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Schäfer committed Sep 6, 2015
1 parent df9519e commit de83c87
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public void finishReport( ReportModel model ) {

if( Config.config().textReport() ) {
new PlainTextReporter().write( model ).flush();
;
}

Optional<File> optionalReportDir = Config.config().getReportDir();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,10 @@ public synchronized void addScenarioModelOrMergeWithExistingOne( ScenarioModel s
}
}

public void setTestClass( Class<?> testClass ) {
public synchronized void setTestClass( Class<?> testClass ) {
AssertionUtil.assertTrue( className == null || testClass.getName().equals( className ),
"Test class of the same report model was set to different values. 1st value: " + className +
", 2nd value: " + testClass.getName() );
setClassName( testClass.getName() );
if( testClass.isAnnotationPresent( Description.class ) ) {
setDescription( testClass.getAnnotation( Description.class ).value() );
Expand Down
3 changes: 3 additions & 0 deletions jgiven-testng/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ description = "Module to write JGiven tests with TestNG"

dependencies {
compile project(':jgiven-core')
compile project(':jgiven-html5-report')
compile(group: 'org.testng', name: 'testng', version:'6.8.7') {
/* This dependency was originally in the Maven provided scope, but the project was not of type war.
This behavior is not yet supported by Gradle, so this dependency has been converted to a compile dependency.
Expand All @@ -10,3 +11,5 @@ dependencies {
}

test.useTestNG()
test.finalizedBy(jgivenHtml5Report)

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.testng.ITestContext;
import org.testng.ITestListener;
Expand All @@ -15,6 +17,7 @@
import com.google.common.base.Throwables;
import com.tngtech.jgiven.base.ScenarioTestBase;
import com.tngtech.jgiven.impl.ScenarioBase;
import com.tngtech.jgiven.impl.util.AssertionUtil;
import com.tngtech.jgiven.impl.util.ParameterNameUtil;
import com.tngtech.jgiven.report.impl.CommonReportHelper;
import com.tngtech.jgiven.report.model.NamedArgument;
Expand All @@ -24,15 +27,14 @@
* TestNG Test listener to enable JGiven for a test class
*/
public class ScenarioTestListener implements ITestListener {
private volatile ReportModel reportModel;

private final Map<ITestResult, ScenarioBase> scenarioMap = Collections.synchronizedMap(
new IdentityHashMap<ITestResult, ScenarioBase>() );
private volatile ConcurrentMap<String, ReportModel> reportModels;

private volatile Map<ITestResult, ScenarioBase> scenarioMap;

@Override
public void onTestStart( ITestResult paramITestResult ) {
Object instance = paramITestResult.getInstance();
reportModel.setTestClass( instance.getClass() );

ScenarioBase scenario;

Expand All @@ -45,6 +47,7 @@ public void onTestStart( ITestResult paramITestResult ) {

scenarioMap.put( paramITestResult, scenario );

ReportModel reportModel = getReportModel( instance.getClass() );
scenario.setModel( reportModel );
scenario.getExecutor().injectSteps( instance );

Expand All @@ -55,6 +58,20 @@ public void onTestStart( ITestResult paramITestResult ) {
scenario.getExecutor().readScenarioState( instance );
}

private ReportModel getReportModel( Class<?> clazz ) {
ReportModel model = reportModels.get( clazz.getName() );
if( model == null ) {
model = new ReportModel();
model.setClassName( clazz.getName() );
ReportModel previousModel = reportModels.putIfAbsent( clazz.getName(), model );
if( previousModel != null ) {
model = previousModel;
}
}
AssertionUtil.assertNotNull( model, "Report model is null" );
return model;
}

@Override
public void onTestSuccess( ITestResult paramITestResult ) {
testFinished( paramITestResult );
Expand Down Expand Up @@ -86,12 +103,15 @@ public void onTestFailedButWithinSuccessPercentage( ITestResult paramITestResult

@Override
public void onStart( ITestContext paramITestContext ) {
reportModel = new ReportModel();
reportModels = new ConcurrentHashMap<String, ReportModel>();
scenarioMap = Collections.synchronizedMap( new IdentityHashMap<ITestResult, ScenarioBase>() );
}

@Override
public void onFinish( ITestContext paramITestContext ) {
new CommonReportHelper().finishReport( reportModel );
for( ReportModel reportModel : reportModels.values() ) {
new CommonReportHelper().finishReport( reportModel );
}
}

private List<NamedArgument> getArgumentsFrom( Method method, ITestResult paramITestResult ) {
Expand Down

0 comments on commit de83c87

Please sign in to comment.