Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions jgiven-asciidoc-report/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# AsciiDoc Report

For text-based report output
Comment thread
hvennekate marked this conversation as resolved.
Outdated
9 changes: 9 additions & 0 deletions jgiven-asciidoc-report/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
plugins {
id 'java-library'
}

dependencies {
implementation libs.guava
implementation libs.slf4j.api
implementation project(':jgiven-core')
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import com.tngtech.jgiven.report.model.ReportStatistics;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -82,7 +80,7 @@ public void generate() {

}

private boolean prepareDirectories(final File targetDir) {
private boolean prepareDirectories(final File targetDir) { // TODO possibly move to parent
this.targetDir = targetDir;
if (this.targetDir == null) {
log.error("Target directory was not configured");
Expand All @@ -101,7 +99,7 @@ private void writeFeatureFiles() {
completeReportModel.getAllReportModels().stream()
.sorted(Comparator.comparing(AsciiDocReportGenerator::byFeatureName))
.forEach(reportModelFile -> {
final String featureFileName = Files.getNameWithoutExtension(
final var featureFileName = Files.getNameWithoutExtension(
reportModelFile.file().getName()) + ".asciidoc";
writeAsciiDocBlocksToFile(new File(featuresDir, featureFileName),
collectReportBlocks(reportModelFile, featureFileName));
Expand All @@ -111,7 +109,7 @@ private void writeFeatureFiles() {
private List<String> collectReportBlocks(final ReportModelFile reportModelFile, final String featureFileName) {
featureFiles.add(featureFileName);

final ReportStatistics statistics = completeReportModel.getStatistics(reportModelFile);
final var statistics = completeReportModel.getStatistics(reportModelFile);
if (statistics.numFailedScenarios > 0) {
failedScenarioFiles.add(featureFileName);
}
Expand All @@ -122,14 +120,14 @@ private List<String> collectReportBlocks(final ReportModelFile reportModelFile,
abortedScenarioFiles.add(featureFileName);
}

final AsciiDocReportModelVisitor visitor = new AsciiDocReportModelVisitor(blockConverter, statistics);
final var visitor = new AsciiDocReportModelVisitor(blockConverter, statistics);
reportModelFile.model().accept(visitor);

return visitor.getResult();
}

private void writeIndexFileForAllScenarios() {
final AsciiDocSnippetGenerator snippetGenerator = new AsciiDocSnippetGenerator(
final var snippetGenerator = new AsciiDocSnippetGenerator(
"All Scenarios", "scenarios in total", this.featureFiles, "",
this.completeReportModel.getTotalStatistics().numScenarios);

Expand All @@ -138,8 +136,8 @@ private void writeIndexFileForAllScenarios() {
}

private void writeIndexFileForFailedScenarios() {
final String scenarioKind = "failed";
final AsciiDocSnippetGenerator snippetGenerator = new AsciiDocSnippetGenerator(
final var scenarioKind = "failed";
final var snippetGenerator = new AsciiDocSnippetGenerator(
"Failed Scenarios", "failed scenarios", this.failedScenarioFiles, scenarioKind,
this.completeReportModel.getTotalStatistics().numFailedScenarios);

Expand All @@ -148,8 +146,8 @@ private void writeIndexFileForFailedScenarios() {
}

private void writeIndexFileForPendingScenarios() {
final String scenarioKind = "pending";
final AsciiDocSnippetGenerator snippetGenerator = new AsciiDocSnippetGenerator(
final var scenarioKind = "pending";
final var snippetGenerator = new AsciiDocSnippetGenerator(
"Pending Scenarios", "pending scenarios", this.pendingScenarioFiles, scenarioKind,
this.completeReportModel.getTotalStatistics().numPendingScenarios);

Expand All @@ -158,8 +156,8 @@ private void writeIndexFileForPendingScenarios() {
}

private void writeIndexFileForAbortedScenarios() {
final String scenarioKind = "aborted";
final AsciiDocSnippetGenerator snippetGenerator = new AsciiDocSnippetGenerator(
final var scenarioKind = "aborted";
final var snippetGenerator = new AsciiDocSnippetGenerator(
"Aborted Scenarios", "aborted scenarios", this.abortedScenarioFiles, scenarioKind,
this.completeReportModel.getTotalStatistics().numAbortedScenarios);

Expand All @@ -176,19 +174,19 @@ private void writeTotalStatisticsFile() {
completeReportModel::getStatistics,
MultimapBuilder.hashKeys().arrayListValues()::build));

final String statisticsBlock = blockConverter.convertStatisticsBlock(
final var statisticsBlock = blockConverter.convertStatisticsBlock(
featureStatistics, completeReportModel.getTotalStatistics());

writeAsciiDocBlocksToFile(new File(targetDir, "totalStatistics.asciidoc"),
Collections.singletonList(statisticsBlock));
}

private void writeIndexFileForFullReport(final String reportTitle) {
final URL resourceUrl = Resources.getResource(this.getClass(), "index.asciidoc");
final var resourceUrl = Resources.getResource(this.getClass(), "index.asciidoc");
try {
final List<String> indexLines = Resources.readLines(resourceUrl, Charset.defaultCharset());
final var indexLines = Resources.readLines(resourceUrl, Charset.defaultCharset());

try (PrintWriter writer = PrintWriterUtil.getPrintWriter(new File(targetDir, "index.asciidoc"))) {
try (var writer = PrintWriterUtil.getPrintWriter(new File(targetDir, "index.asciidoc"))) {
writer.println("= " + reportTitle);
indexLines.forEach(writer::println);
}
Expand All @@ -212,7 +210,7 @@ private static String byFeatureName(ReportModelFile modelFile) {
}

private static void writeAsciiDocBlocksToFile(final File file, final List<String> asciiDocBlocks) {
try (final PrintWriter writer = PrintWriterUtil.getPrintWriter(file)) {
try (final var writer = PrintWriterUtil.getPrintWriter(file)) {
for (final String block : asciiDocBlocks) {
writer.println(block);
writer.println();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

import com.tngtech.jgiven.exception.JGivenInstallationException;
import com.tngtech.jgiven.exception.JGivenInternalDefectException;
import com.tngtech.jgiven.report.asciidoc.AsciiDocReportGenerator;
import com.tngtech.jgiven.report.config.ConfigOptionParser;
import com.tngtech.jgiven.report.text.PlainTextReportGenerator;

import java.util.Arrays;

/**
* This is an interface to create a report based on command line flags
*/
public class ReportGenerator {
/**
private static final String ASCIIDOC_GENERATOR_FQCN = "com.tngtech.jgiven.report.asciidoc.AsciiDocReportGenerator";
private static final String HTML5_REPORT_GENERATOR_FQCN = "com.tngtech.jgiven.report.html5.Html5ReportGenerator";
Comment thread
hvennekate marked this conversation as resolved.
Outdated

/**
* to create a custom report, extend this enum with the name of your choice
*/
public enum Format {
Expand Down Expand Up @@ -43,39 +44,39 @@ public String formatName() {
* Starts the respective report (default is HTML5)
*/
public void generate( String... args ) {
Format format = ConfigOptionParser.getFormat( args );
var format = ConfigOptionParser.getFormat( args );
switch( format ) {
case ASCIIDOC:
new AsciiDocReportGenerator().generateFromCommandLine( args );
loadReportGenerator( ASCIIDOC_GENERATOR_FQCN ).generateFromCommandLine( args );
break;
case TEXT:
new PlainTextReportGenerator().generateFromCommandLine( args );
break;
case HTML:
case HTML5:
case HTML, HTML5:
default:
ReportGenerator.generateHtml5Report().generateFromCommandLine( args );
ReportGenerator.loadReportGenerator( HTML5_REPORT_GENERATOR_FQCN ).generateFromCommandLine( args );
break;
}
}

/**
* Searches the Html5ReportGenerator in Java path and instantiates the report
*/
public static AbstractReportGenerator generateHtml5Report() {
AbstractReportGenerator report;
try {
Class<?> aClass = ReportGenerator.class.getClassLoader()
.loadClass( "com.tngtech.jgiven.report.html5.Html5ReportGenerator" );
report = (AbstractReportGenerator) aClass.getDeclaredConstructor().newInstance();
* Searches the Html5ReportGenerator in Java path and instantiates it
Comment thread
hvennekate marked this conversation as resolved.
Outdated
*/
public static AbstractReportGenerator loadHtml5ReportGenerator() {
Comment thread
hvennekate marked this conversation as resolved.
Outdated
return loadReportGenerator( HTML5_REPORT_GENERATOR_FQCN );
}

private static AbstractReportGenerator loadReportGenerator(String fqcn) {
try {
Class<?> aClass = ReportGenerator.class.getClassLoader().loadClass(fqcn);
return (AbstractReportGenerator) aClass.getDeclaredConstructor().newInstance();
} catch( ClassNotFoundException e ) {
throw new JGivenInstallationException( "The JGiven HTML5 Report Generator seems not to be on the classpath.\n"
+ "Ensure that you have a dependency to jgiven-html5-report." );
} catch( Exception e ) {
throw new JGivenInternalDefectException( "The HTML5 Report Generator could not be instantiated.", e );
}
return report;
}
}

public static void main( String... args ) {
new ReportGenerator().generate( args );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.tngtech.jgiven.report;

import com.tngtech.jgiven.report.config.ConfigOption;
import java.io.File;
import java.util.List;
import java.util.Map;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class ReportConfigArgumentTest {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the former ReportGeneratorArgumentTest (next file; deleted). Like the test further down after that, it does not really require the asciidoc reporter, so I replaced it with anonymous classes. It might be worthwhile extracting those or moving/renaming the tests to signify that they are really testing the AbstractReportConfig and AbstractReportGenerator, respectively.


@Test
public void testArgumentParsing() {
var testConfig = new AbstractReportConfig("--sourceDir=source/dir", "--targetDir=target/dir") {

@Override
public void useConfigMap(Map<String, Object> configMap) {
}

@Override
public void additionalConfigOptions(List<ConfigOption> configOptions) {
}
};

assertThat(testConfig.getSourceDir()).isEqualTo(new File("source/dir"));
assertThat(testConfig.getTargetDir()).isEqualTo(new File("target/dir"));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package com.tngtech.jgiven.report;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

import com.google.common.base.Charsets;
import com.google.common.io.Files;
import com.tngtech.jgiven.exception.JGivenWrongUsageException;
import com.tngtech.jgiven.report.asciidoc.AsciiDocReportConfig;
import com.tngtech.jgiven.report.asciidoc.AsciiDocReportGenerator;
import org.assertj.core.api.Assertions;
import org.junit.ClassRule;
import com.tngtech.jgiven.report.config.ConfigOption;
import java.io.File;
import java.util.List;
import java.util.Map;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
Expand All @@ -22,18 +18,36 @@ public class ReportGeneratorTest {

@Test( expected = JGivenWrongUsageException.class )
Comment thread
hvennekate marked this conversation as resolved.
Outdated
public void wrong_json_files_are_handled_gracefully() throws Exception {
File folder = tmpFolder.newFolder();
var folder = tmpFolder.newFolder();

Files.asCharSink( new File( folder, "wrong.json" ), Charsets.UTF_8 ).write( "no json");

AsciiDocReportGenerator asciiReport = new AsciiDocReportGenerator();
var reportGenerator = new AbstractReportGenerator() {

@Override
public void generate() throws Exception {
}

@Override
public AbstractReportConfig createReportConfig(String... args) {
return null;
}
};

var config = new AbstractReportConfig() {
Comment thread
hvennekate marked this conversation as resolved.
Outdated

@Override
public void useConfigMap(Map<String, Object> configMap) {
}

AsciiDocReportConfig config = new AsciiDocReportConfig();
@Override
public void additionalConfigOptions(List<ConfigOption> configOptions) {
}
};
config.setSourceDir( tmpFolder.getRoot() );
config.setTargetDir( tmpFolder.getRoot() );

asciiReport.setConfig( config );
asciiReport.loadReportModel();
asciiReport.generate();
reportGenerator.setConfig(config);
reportGenerator.loadReportModel();
}
}
1 change: 1 addition & 0 deletions jgiven-gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ plugins {
dependencies {
implementation localGroovy() //we're a gradle plugin, we want to use the same groovy version as gradle
implementation project(':jgiven-core')
implementation project(':jgiven-asciidoc-report')
implementation project(':jgiven-html5-report')
implementation(platform(libs.junit.bom))
implementation(libs.jakarta.annotation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.tngtech.jgiven.report.text.PlainTextReportGenerator;
import groovy.lang.Closure;
import jakarta.annotation.Nullable;
import java.io.File;
import org.gradle.api.NonNullApi;
import org.gradle.api.Task;
import org.gradle.api.file.DirectoryProperty;
Expand All @@ -19,8 +20,6 @@
import org.gradle.api.tasks.Internal;
import org.gradle.util.internal.ConfigureUtil;

import java.io.File;

@NonNullApi
public abstract class AbstractJGivenReportImpl implements JGivenReport {

Expand All @@ -40,6 +39,7 @@ public AbstractJGivenReportImpl( String name, Task task, String relativeEntryPat
this.getRequired().convention(false);
}

@Override
public AbstractReportGenerator createGenerator() {
AbstractReportConfig conf;
AbstractReportGenerator generator;
Expand All @@ -55,7 +55,7 @@ public AbstractReportGenerator createGenerator() {
case HTML:
case HTML5:
default:
Html5ReportConfig customConf = new Html5ReportConfig();
var customConf = new Html5ReportConfig();
customConf.setShowThumbnails( isThumbnailsAreShown() );
if (getCustomCssFile() != null) {
customConf.setCustomCss( getCustomCssFile() );
Expand All @@ -64,7 +64,7 @@ public AbstractReportGenerator createGenerator() {
customConf.setCustomJs( getCustomJsFile() );
}
conf = customConf;
generator = ReportGenerator.generateHtml5Report();
generator = ReportGenerator.loadHtml5ReportGenerator();
Comment thread
hvennekate marked this conversation as resolved.
Outdated
break;
}
if( getTitle() != null ) {
Expand Down
1 change: 1 addition & 0 deletions jgiven-maven-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ description = 'JGiven Maven Mojo'

dependencies {
implementation project(':jgiven-core')
implementation project(':jgiven-asciidoc-report')
implementation project(':jgiven-html5-report')
implementation "org.apache.maven.plugin-tools:maven-plugin-tools-api:3.15.1"
implementation "org.apache.maven.plugin-tools:maven-plugin-annotations:3.15.1"
Expand Down
Loading