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 414932c commit 34443ef
Show file tree
Hide file tree
Showing 8 changed files with 90 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 @@ -9,6 +9,7 @@
public class JgivenDslContext implements Context {
List<JgivenReportGenerator.ReportConfig> reportConfigs = new ArrayList<JgivenReportGenerator.ReportConfig>();
String resultFiles = "";
boolean excludeEmptyScenarios;

public void html() {
reportConfigs.add(new JgivenReportGenerator.HtmlReportConfig());
Expand All @@ -22,4 +23,12 @@ public void html(Closure<?> closure) {
public void results(String glob) {
resultFiles = glob;
}

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

public void excludeEmptyScenarios() {
excludeEmptyScenarios(true);
}
}
Expand Up @@ -32,6 +32,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 All @@ -47,6 +48,7 @@ public JgivenReportGenerator(Closure<?> configClosure) {
JgivenDslContext context = new JgivenDslContext();
executeInContext(configClosure, context);
setJgivenResults(context.resultFiles);
setExcludeEmptyScenarios(context.excludeEmptyScenarios);
reportConfigs = context.reportConfigs;
}

Expand Down Expand Up @@ -82,7 +84,9 @@ public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath workspace, @Nonnul

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

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

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

private File reportRootDir(Run<?, ?> run) {
return new File(run.getRootDir(), REPORTS_DIR);
}
Expand All @@ -111,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 @@ -155,12 +178,8 @@ public String getReportUrl() {

abstract String getReportName();

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

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

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

private File copyFileToMaster(FilePath workspace, String file) throws IOException, InterruptedException {
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
Expand Up @@ -20,14 +20,15 @@ public void html_Report_can_be_configured() throws Exception {
FreeStyleProject seedJob = j.createFreeStyleProject();

seedJob.getBuildersList().add(new ExecuteDslScripts(new ExecuteDslScripts.ScriptLocation(Boolean.TRUE.toString(), null,
"freeStyleJob('test-job') { publishers { jgivenReports { results 'some.json'; html { customCss '/some/css' } } } }"), false, RemovedJobAction.DELETE));
"freeStyleJob('test-job') { publishers { jgivenReports { results 'some.json'; excludeEmptyScenarios(); html { customCss '/some/css' } } } }"), false, RemovedJobAction.DELETE));

j.buildAndAssertSuccess(seedJob);

FreeStyleProject createdJob = (FreeStyleProject) j.getInstance().getItem("test-job");
JgivenReportGenerator jgivenReportGenerator = createdJob.getPublishersList().get(JgivenReportGenerator.class);

assertThat(jgivenReportGenerator.getJgivenResults()).isEqualTo("some.json");
assertThat(jgivenReportGenerator.isExcludeEmptyScenarios()).isTrue();

JgivenReportGenerator.HtmlReportConfig reportConfig = (JgivenReportGenerator.HtmlReportConfig) Iterables.getOnlyElement(jgivenReportGenerator.getReportConfigs());
assertThat(reportConfig.getFormat()).isEqualTo(ReportGenerator.Format.HTML);
Expand Down
@@ -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(null)).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, null);

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

0 comments on commit 34443ef

Please sign in to comment.