Skip to content

Commit

Permalink
Make the title of the generated report configurable (fixes #131)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Schäfer committed Sep 28, 2015
1 parent 80edcb5 commit 0ed0622
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ public abstract class AbstractReportGenerator {

protected CompleteReportModel completeReportModel;
protected File targetDirectory;
protected ReportGenerator.Config config;

public void generate( CompleteReportModel completeReportModel, File targetDirectory ) {
public void generate( CompleteReportModel completeReportModel, File targetDirectory, ReportGenerator.Config config ) {
this.completeReportModel = completeReportModel;
this.targetDirectory = targetDirectory;
this.config = config;
generate();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ public static Format fromStringOrNull( String value ) {
private File targetDirectory = new File( "." );
private File customCssFile = null;
private Format format = HTML;
private Config config = new Config();

public static class Config {
String title = "JGiven Report";

public void setTitle( String title ) {
this.title = title;
}

public String getTitle() {
return title;
}
}

public static void main( String... args ) throws Exception {
ReportGenerator generator = new ReportGenerator();
Expand All @@ -69,6 +82,8 @@ static void parseArgs( ReportGenerator generator, String... args ) {
}
} else if( arg.startsWith( "--customcss=" ) ) {
generator.setCustomCssFile( new File( arg.split( "=" )[1] ) );
} else if( arg.startsWith( "--title=" ) ) {
generator.config.title = arg.split( "=" )[1];
} else if( arg.startsWith( "--format=" ) ) {
String formatArg = arg.split( "=" )[1];
Format format = Format.fromStringOrNull( formatArg );
Expand Down Expand Up @@ -98,9 +113,9 @@ public void generate() throws Exception {
if( format == HTML5 || format == HTML ) {
generateHtml5Report( reportModel );
} else if( format == TEXT ) {
new PlainTextReportGenerator().generate( reportModel, getTargetDirectory() );
new PlainTextReportGenerator().generate( reportModel, getTargetDirectory(), config );
} else if( format == ASCIIDOC ) {
new AsciiDocReportGenerator().generate( reportModel, getTargetDirectory() );
new AsciiDocReportGenerator().generate( reportModel, getTargetDirectory(), config );
}

}
Expand All @@ -127,12 +142,13 @@ private void generateHtml5Report( CompleteReportModel reportModel ) throws IOExc
throw new JGivenInternalDefectException( "The HTML5 Report Generator could not be instantiated.", e );
}

reportGenerator.generate( reportModel, getTargetDirectory() );
reportGenerator.generate( reportModel, getTargetDirectory(), config );
copyCustomCssFile( new File( getTargetDirectory(), "css" ) );
}

private static void printUsageAndExit() {
System.err.println( "Options: [--format=<format>] [--sourceDir=<dir>] [--targetDir=<dir>] [--customcss=<cssfile>]" ); // NOSONAR
System.err
.println( "Options: [--format=<format>] [--sourceDir=<dir>] [--targetDir=<dir>] [--customcss=<cssfile>] [--title=<title>]" ); // NOSONAR
System.err.println( " <format> = html or text, default is html" );
System.exit( 1 );
}
Expand Down Expand Up @@ -161,4 +177,12 @@ public void setCustomCssFile( File customCssFile ) {
this.customCssFile = customCssFile;
}

public void setConfig( Config config ) {
this.config = config;
}

public Config getConfig() {
return this.config;
}

}
4 changes: 2 additions & 2 deletions jgiven-html5-report/src/app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>JGiven Report</title>
<title>{{ ::metaData.title }}</title>
<link rel="stylesheet" href="css/normalize.css"/>
<link rel="stylesheet" href="css/foundation.css"/>
<link rel="stylesheet" href="css/font-awesome.min.css"/>
Expand Down Expand Up @@ -71,7 +71,7 @@
<nav class="top-bar" data-topbar role="navigation" data-options="">
<ul class="title-area">
<li class="name">
<h1><a href="#/">JGiven Report</a></h1>
<h1 id="title"><a href="#/">{{ ::metaData.title }}</a></h1>
</li>
</ul>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ public class Html5ReportGenerator extends AbstractReportGenerator {

@Override
public void generate() {
log.info( "Generating HTML5 report to {}", new File( targetDirectory, "index.html" ).getAbsoluteFile() );
this.dataDirectory = new File( targetDirectory, "data" );
log.info( "Generating HTML5 report to {}", new File( this.targetDirectory, "index.html" ).getAbsoluteFile() );
this.dataDirectory = new File( this.targetDirectory, "data" );
this.metaData.title = config.getTitle();
if( !this.dataDirectory.exists() && !this.dataDirectory.mkdirs() ) {
log.error( "Could not create target directory " + this.dataDirectory );
return;
}
try {
unzipApp( targetDirectory );
unzipApp( this.targetDirectory );
createDataFiles();
generateMetaData();
generateTagFile();
Expand Down Expand Up @@ -110,6 +111,7 @@ private void createWriter() {

static class MetaData {
Date created = new Date();
String title = "JGiven Report";
List<String> data = Lists.newArrayList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public class JGivenReportMojo extends AbstractMojo {
*/
private String format;

/**
* The title of the generated report.
* @parameter expression="JGiven Report"
*/
private String title;

@Override
public void execute() throws MojoExecutionException {
try {
Expand All @@ -50,10 +56,11 @@ public void execute() throws MojoExecutionException {
}
getLog().info( "Generating HTML reports to " + outputDirectory + "..." );
ReportGenerator generator = new ReportGenerator();
generator.setTargetDirectory(outputDirectory);
generator.setSourceDirectory(sourceDirectory);
generator.setTargetDirectory( outputDirectory );
generator.setSourceDirectory( sourceDirectory );
generator.setFormat( ReportGenerator.Format.fromStringOrNull( format ) );
generator.setCustomCssFile( customCssFile );
generator.getConfig().setTitle( title );
generator.generate();
getLog().info( "-------------------------------------------------------------------" );
getLog().info( "Generated JGiven HTML reports to directory " + outputDirectory );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.tngtech.jgiven.tags;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import com.tngtech.jgiven.annotation.IsTag;

@IsTag(
description = "Tests with this tag use a browser for testing",
color = "orange" )
@Retention( RetentionPolicy.RUNTIME )
public @interface BrowserTest {

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ public class WhenReportGenerator<SELF extends WhenReportGenerator<?>> extends St
@ProvidedScenarioState
protected ReportGenerator htmlReportGenerator;

protected ReportGenerator.Config config = new ReportGenerator.Config();

@BeforeStage
public void setupTargetReportDir() throws IOException {
targetReportDir = temporaryFolderRule.newFolder( "targetReportDir" );
}

public void the_asciidoc_reporter_is_executed() throws IOException {
new AsciiDocReportGenerator().generate( getCompleteReportModel(), targetReportDir );
new AsciiDocReportGenerator().generate( getCompleteReportModel(), targetReportDir, config );
}

protected CompleteReportModel getCompleteReportModel() {
Expand All @@ -58,7 +60,7 @@ public void the_report_generator_is_executed() throws Exception {
}

public SELF the_plain_text_reporter_is_executed() {
new PlainTextReportGenerator().generate( getCompleteReportModel(), targetReportDir );
new PlainTextReportGenerator().generate( getCompleteReportModel(), targetReportDir, config );
return self();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.tngtech.jgiven.report.model.StepStatus;
import com.tngtech.jgiven.tags.*;

@BrowserTest
@FeatureHtml5Report
@Description( "Tests against the generated HTML5 App using WebDriver" )
@RunWith( DataProviderRunner.class )
Expand Down Expand Up @@ -125,6 +126,19 @@ public void attachments_appear_in_the_HTML5_report() throws Exception {

then().an_attachment_icon_exists()
.and().the_content_of_the_referenced_attachment_is( content );
}

@Test
public void the_configured_title_appears_in_the_generated_HTML_report() throws Exception {
given().a_report_model()
.and().the_report_exist_as_JSON_file();

whenReport
.and().the_HTML_Report_Generator_is_executed_with_title( "Test Title" );

when().the_index_page_is_opened();

then().the_report_title_is( "Test Title" );

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,14 @@ public void the_HTML_report_generator_creates_a_tags_file() throws Exception {
.and().a_file_$_exists_in_folder_$( "metaData.js", "data" );
}

@Test
public void the_title_of_the_HTML_report_can_be_configured() throws Exception {
given().a_report_model()
.and().the_report_exist_as_JSON_file();

when().the_HTML_Report_Generator_is_executed_with_title( "Test Title" );

then().the_metaData_file_has_title_set_to( "Test Title" );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,9 @@ public SELF the_tag_has_style( String style ) {
assertThat( span.getAttribute( "style" ) ).contains( style );
return self();
}

public SELF the_report_title_is( String title ) {
assertThat( webDriver.findElement( By.id( "title" ) ).getText() ).isEqualTo( title );
return self();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
package com.tngtech.jgiven.report.html5;

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

import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.common.base.Charsets;
import com.google.common.io.Files;
import com.google.gson.Gson;
import com.tngtech.jgiven.report.ThenReportGenerator;

public class ThenHtml5ReportGenerator<SELF extends ThenHtml5ReportGenerator<SELF>> extends ThenReportGenerator<SELF> {}
public class ThenHtml5ReportGenerator<SELF extends ThenHtml5ReportGenerator<SELF>> extends ThenReportGenerator<SELF> {

public static final String META_DATA_PATTERN = "jgivenReport.setMetaData\\((.*)\\);";

public void the_metaData_file_has_title_set_to( String title ) throws IOException {
String metaDataContent = Files.toString( new File( new File( targetReportDir, "data" ), "metaData.js" ), Charsets.UTF_8 );

Matcher matcher = Pattern.compile( META_DATA_PATTERN ).matcher( metaDataContent );
assertThat( metaDataContent ).matches( META_DATA_PATTERN );

matcher.matches();
String metaDataObject = matcher.group( 1 );

Html5ReportGenerator.MetaData metaData = new Gson()
.fromJson( metaDataObject, Html5ReportGenerator.MetaData.class );

assertThat( metaData.title ).isEqualTo( title );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ protected void unzipApp( File toDir ) throws IOException {
}
};

html5ReportGenerator.generate( getCompleteReportModel(), targetReportDir );
html5ReportGenerator.generate( getCompleteReportModel(), targetReportDir, config );
return self();
}

public SELF the_HTML_Report_Generator_is_executed_with_title( String title ) {
config.setTitle( title );
return the_HTML_Report_Generator_is_executed();
}
}

0 comments on commit 0ed0622

Please sign in to comment.