Skip to content

Commit

Permalink
[FIXED JENKINS-31337] Update to JGiven 0.9.4 and support --exclude-em…
Browse files Browse the repository at this point in the history
…pty-scenarios option
  • Loading branch information
wolfs committed Nov 4, 2015
1 parent 62d623a commit 9ae7a3a
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 19 deletions.
16 changes: 14 additions & 2 deletions pom.xml
Expand Up @@ -2,7 +2,7 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<properties>
<jgiven.version>0.9.1</jgiven.version>
<jgiven.version>0.9.4</jgiven.version>
</properties>

<parent>
Expand All @@ -13,7 +13,7 @@
</parent>

<artifactId>jgiven</artifactId>
<version>0.9.2-SNAPSHOT</version>
<version>0.9.4-SNAPSHOT</version>
<packaging>hpi</packaging>

<name>JGiven Jenkins Plugin</name>
Expand Down Expand Up @@ -168,6 +168,18 @@
<version>1.5.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jvnet.hudson</groupId>
<artifactId>xstream</artifactId>
Expand Down
Expand Up @@ -33,6 +33,7 @@ public class JgivenReportGenerator extends Recorder implements SimpleBuildStep {

public static final String REPORTS_DIR = "jgiven-reports";
private List<ReportConfig> reportConfigs;
private boolean excludeEmptyScenarios;

@Override
public BuildStepMonitor getRequiredMonitorService() {
Expand Down Expand Up @@ -81,9 +82,11 @@ public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath workspace, @Nonnul
}
}

private void generateReport(File reportRootDir, File JgivenJsons, ReportConfig reportConfig) throws IOException, InterruptedException {
private void generateReport(File reportRootDir, File sourceDir, ReportConfig reportConfig) throws IOException, InterruptedException {
try {
reportConfig.reportGenerator(JgivenJsons, reportRootDir).generate();
ReportGenerator reportGenerator = new ReportGenerator();
configureReportGenerator(reportRootDir, sourceDir, reportConfig, reportGenerator);
reportGenerator.generate();
} catch (IOException e) {
throw e;
} catch (RuntimeException e) {
Expand All @@ -95,6 +98,16 @@ private void generateReport(File reportRootDir, File JgivenJsons, ReportConfig r
}
}

void configureReportGenerator(File reportRootDir, File sourceDir, ReportConfig reportConfig, ReportGenerator reportGenerator) {
reportGenerator.setSourceDirectory(sourceDir);
reportGenerator.setFormat(reportConfig.getFormat());
reportGenerator.setTargetDirectory(new File(reportRootDir, reportConfig.getReportDirectory()));

ReportGenerator.Config jgivenConfig = reportConfig.getJgivenConfig();
jgivenConfig.setExcludeEmptyScenarios(excludeEmptyScenarios);
reportGenerator.setConfig(jgivenConfig);
}

private File reportRootDir(Run<?, ?> run) {
return new File(run.getRootDir(), REPORTS_DIR);
}
Expand All @@ -112,6 +125,15 @@ private static String jgivenResultsFromString(String jgivenResults) {
return StringUtils.isBlank(jgivenResults) ? "**/json/*.json" : jgivenResults;
}

@DataBoundSetter
public void setExcludeEmptyScenarios(boolean excludeEmptyScenarios) {
this.excludeEmptyScenarios = excludeEmptyScenarios;
}

public boolean isExcludeEmptyScenarios() {
return excludeEmptyScenarios;
}

@Extension
public static class DescriptorImpl extends BuildStepDescriptor<Publisher> {

Expand Down Expand Up @@ -156,12 +178,8 @@ public String getReportUrl() {

abstract String getReportName();

public ReportGenerator reportGenerator(File sourceDir, File reportRootDir) {
ReportGenerator reportGenerator = new ReportGenerator();
reportGenerator.setSourceDirectory(sourceDir);
reportGenerator.setFormat(getFormat());
reportGenerator.setTargetDirectory(new File(reportRootDir, getReportDirectory()));
return reportGenerator;
public ReportGenerator.Config getJgivenConfig() {
return new ReportGenerator.Config();
}
}

Expand Down Expand Up @@ -220,18 +238,18 @@ public void setTitle(String title) {
}

@Override
public ReportGenerator reportGenerator(File sourceDir, File reportRootDir) {
ReportGenerator reportGenerator = super.reportGenerator(sourceDir, reportRootDir);
public ReportGenerator.Config getJgivenConfig() {
ReportGenerator.Config jgivenConfig = super.getJgivenConfig();
if (StringUtils.isNotBlank(customCssFile)) {
reportGenerator.getConfig().setCustomCssFile(new File(customCssFile));
jgivenConfig.setCustomCssFile(new File(customCssFile));
}
if (StringUtils.isNotBlank(customJsFile)) {
reportGenerator.getConfig().setCustomJsFile(new File(customJsFile));
jgivenConfig.setCustomJsFile(new File(customJsFile));
}
if (StringUtils.isNotBlank(title)) {
reportGenerator.getConfig().setTitle(title);
jgivenConfig.setTitle(title);
}
return reportGenerator;
return jgivenConfig;
}

@Extension
Expand Down
Expand Up @@ -8,6 +8,10 @@ f.entry(title: _('jgiven.json.files'), field: 'jgivenResults') {
f.textbox()
}

f.entry(title: _('exclude.empty.scenarios'), field: 'excludeEmptyScenarios') {
f.checkbox()
}

f.section(title: _('reports.to.generate')) {
f.block {
f.repeatableHeteroProperty(field: 'reportConfigs', hasHeader: true, oneEach: true)
Expand Down
@@ -1,2 +1,3 @@
jgiven.json.files=JGiven result json files
reports.to.generate=Reports to generate
reports.to.generate=Reports to generate
exclude.empty.scenarios=Exclude empty Scenarios from reports
@@ -1,2 +1,3 @@
reports.to.generate=zu erzeugende Berichte
jgiven.json.files=JGiven Json-Dateien
jgiven.json.files=JGiven Json-Dateien
exclude.empty.scenarios=Leere Szenarios aus den Berichten ausschlie\u00DFen
@@ -1,17 +1,41 @@
package org.jenkinsci.plugins.jgiven;

import com.google.common.collect.ImmutableList;
import com.tngtech.jgiven.report.ReportGenerator;
import com.tngtech.jgiven.report.ReportGenerator.Config;
import org.jenkinsci.plugins.jgiven.JgivenReportGenerator.ReportConfig;
import org.junit.Test;

import java.io.File;
import java.util.ArrayList;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.mock;

public class JgivenReportGeneratorTest {
@Test
public void when_no_report_is_configured_then_html_is_added_by_default() {
JgivenReportGenerator jgivenReportGenerator = new JgivenReportGenerator(new ArrayList<JgivenReportGenerator.ReportConfig>());
JgivenReportGenerator jgivenReportGenerator = new JgivenReportGenerator(new ArrayList<ReportConfig>());

assertThat(jgivenReportGenerator.getReportConfigs()).hasSize(1);
assertThat(jgivenReportGenerator.getReportConfigs().iterator().next()).isInstanceOf(JgivenReportGenerator.HtmlReportConfig.class);
}

@Test
public void excludeEmptyScenarios_is_set_into_the_Configuration() throws Exception {
ReportConfig config = mock(ReportConfig.class);
given(config.getReportDirectory()).willReturn("testDir");
Config jgivenConfig = mock(Config.class);
given(config.getJgivenConfig()).willReturn(jgivenConfig);
ReportGenerator reportGenerator = mock(ReportGenerator.class);

JgivenReportGenerator jgivenReportGenerator = new JgivenReportGenerator(ImmutableList.of(config));
jgivenReportGenerator.setExcludeEmptyScenarios(true);
jgivenReportGenerator.configureReportGenerator(new File("."), new File("."), config, reportGenerator);

then(reportGenerator).should().setConfig(jgivenConfig);
then(jgivenConfig).should().setExcludeEmptyScenarios(true);
}
}

0 comments on commit 9ae7a3a

Please sign in to comment.