Skip to content

Commit

Permalink
Merge pull request #679 from TNG/feature/configuration-file
Browse files Browse the repository at this point in the history
Feature/configuration file
  • Loading branch information
fudler committed Aug 17, 2021
2 parents b2eaec2 + 7f2bb16 commit 9a94853
Show file tree
Hide file tree
Showing 11 changed files with 410 additions and 64 deletions.
4 changes: 1 addition & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,7 @@ configure(subprojects.findAll { !it.name.contains("android") }) {
dependencies {
implementation "org.slf4j:slf4j-api:$slf4jVersion"

testImplementation "org.slf4j:jcl-over-slf4j:$slf4jVersion"
testImplementation "org.slf4j:slf4j-simple:$slf4jVersion"
testImplementation "org.slf4j:slf4j-jdk14:$slf4jVersion"
testImplementation "junit:junit:$junitVersion"
testImplementation "org.assertj:assertj-core:$assertjVersion"
testImplementation "com.tngtech.java:junit-dataprovider:$junitDataproviderVersion"
Expand All @@ -177,7 +176,6 @@ configure(subprojects.findAll { !it.name.contains("android") }) {
test {
systemProperty 'jgiven.report.dir', 'build/reports/jgiven/json'
systemProperty 'jgiven.report.text', 'false'
systemProperty 'org.slf4j.simpleLogger.defaultLogLevel', 'warn'

if (jacocoEnabled) {
jacoco {
Expand Down
26 changes: 24 additions & 2 deletions docs/report_generation.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ Now run:
$ mvn verify
----

HTML reports are then generated into the `target/jgiven-reports/html` directory.
HTML reports are then generated into the `target/jgiven-reports/html` directory. Note that the plugin relies on the existence of the JSON output, so if the property `jgiven.reports.enabled` was set to `false`, no output will be generated.

==== Gradle

Expand Down Expand Up @@ -171,6 +171,7 @@ buildscript {
}
apply plugin: "com.tngtech.jgiven.gradle-plugin"
----

Now run:
Expand All @@ -180,7 +181,7 @@ Now run:
$ gradle test jgivenTestReport
----

HTML reports are then generated into the `build/reports/jgiven/test/html/` directory.
HTML reports are then generated into the `build/reports/jgiven/test/html/` directory. Note that the plugin relies on the existence of the JSON output, so if the property `jgiven.reports.enabled` was set to `false`, no output will be generated.

If you want that the HTML report is always generated after the tests
have been executed, you can configure the `test` task in your Gradle
Expand All @@ -193,3 +194,24 @@ test.finalizedBy jgivenTestReport

For additional information about the Gradle plugin refer to
https://plugins.gradle.org/plugin/com.tngtech.jgiven.gradle-plugin

=== Configuration File

JGiven will optionally load a configuration properties file, defaulting to:
`jgiven.properties`. The path to the configuration can be customized with the system property:
----
jgiven.config.path
----
The encoding for the file is assumed to be `UTF-8`, but can be customized with the system property:
----
jgiven.config.charset
----
The following can be defined in the properties file:
----
jgiven.report.enabled=false
jgiven.report.dir=<targetDir>
jgiven.report.text=false
jgiven.report.text.color
jgiven.report.filterStackTrace=true
----
Configuration defined via Java system properties will take precedence over values in the configuration file.
4 changes: 0 additions & 4 deletions example-projects/spock/src/test/resources/log4j.properties

This file was deleted.

119 changes: 84 additions & 35 deletions jgiven-core/src/main/java/com/tngtech/jgiven/impl/Config.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package com.tngtech.jgiven.impl;

import com.tngtech.jgiven.config.ConfigValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Optional;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Helper class to access all system properties to configure JGiven.
*/
public class Config {
private static final Logger log = LoggerFactory.getLogger( Config.class );
private static final Logger log = LoggerFactory.getLogger(Config.class);
private static final Config INSTANCE = new Config();

private static final String TRUE = "true";
Expand All @@ -23,69 +28,113 @@ public class Config {
private static final String JGIVEN_REPORT_TEXT_COLOR = "jgiven.report.text.color";
private static final String JGIVEN_FILTER_STACK_TRACE = "jgiven.report.filterStackTrace";
private static final String JGIVEN_REPORT_DRY_RUN = "jgiven.report.dry-run";
private static final String JGIVEN_CONFIG_PATH = "jgiven.config.path";
private static final String JGIVEN_CONFIG_CHARSET = "jgiven.config.charset";

public static Config config(){
private final Properties configFileProperties = loadConfigFileProperties();

public static Config config() {
return INSTANCE;
}

static{
if( INSTANCE.dryRun() ) {
log.info( "Dry Run enabled." );
static {
logDryRunEnabled();
logReportEnabled();
}

static void logDryRunEnabled() {
if (INSTANCE.dryRun()) {
log.info("Dry Run enabled.");
}
}

static void logReportEnabled() {
if (!INSTANCE.isReportEnabled()) {
log.info("Please note that the report generation is turned off.");
}
}

private Config() {
}

private static Properties loadConfigFileProperties() {
String path = System.getProperty(JGIVEN_CONFIG_PATH, "jgiven.properties");
String charset = System.getProperty(JGIVEN_CONFIG_CHARSET, "UTF-8");
Properties properties = new Properties();
try (Reader reader = Files.newBufferedReader(Paths.get(path), Charset.forName(charset))) {
properties.load(reader);
} catch (IOException e) {
log.debug("config file " + path + " not loaded: " + e.getMessage());
}
return properties;
}

private String resolveProperty(String name) {
return resolveProperty(name, null);
}

private String resolveProperty(String name, String defaultValue) {
return System.getProperty(name, configFileProperties.getProperty(name, defaultValue));
}

public Optional<File> getReportDir(){
String reportDirName = System.getProperty( JGIVEN_REPORT_DIR );
if( reportDirName == null ) {
if( System.getProperty( "surefire.test.class.path" ) != null ) {
/**
* Returns the directory set either via a configuration file or a system property.
* If no value is specified and the surefire test classpath is set, the default maven directory will be used,
* otherwise a default is returned.
*/
public Optional<File> getReportDir() {
String reportDirName = resolveProperty(JGIVEN_REPORT_DIR);
if (reportDirName == null) {
if (resolveProperty("surefire.test.class.path") != null) {
reportDirName = "target/jgiven-reports/json";
log.info( JGIVEN_REPORT_DIR + " not set, but detected surefire plugin, generating reports to " + reportDirName );
log.info(JGIVEN_REPORT_DIR + " not set, but detected surefire plugin, generating reports to "
+ reportDirName);
} else {
reportDirName = "jgiven-reports";
log.debug( JGIVEN_REPORT_DIR + " not set, using default value jgiven-reports" );
log.debug(JGIVEN_REPORT_DIR + " not set, using default value jgiven-reports");
}
}

File reportDir = new File( reportDirName );
if( reportDir.exists() && !reportDir.isDirectory() ) {
log.warn( reportDirName + " exists but is not a directory. Will not generate JGiven reports." );
File reportDir = new File(reportDirName);
if (reportDir.exists() && !reportDir.isDirectory()) {
log.warn(reportDirName + " exists but is not a directory. Will not generate JGiven reports.");
return Optional.empty();
}

log.debug( "Using folder " + reportDirName + " to store JGiven reports" );
log.debug("Using folder " + reportDirName + " to store JGiven reports");

return Optional.of( reportDir );
return Optional.of(reportDir);
}

public boolean isReportEnabled(){
return TRUE.equalsIgnoreCase( System.getProperty( JGIVEN_REPORT_ENABLED, TRUE ) );
public boolean isReportEnabled() {
return TRUE.equalsIgnoreCase(resolveProperty(JGIVEN_REPORT_ENABLED, TRUE));
}

public void setReportEnabled( boolean enabled ){
System.setProperty( JGIVEN_REPORT_ENABLED, "" + enabled );
public void setReportEnabled(boolean enabled) {
System.setProperty(JGIVEN_REPORT_ENABLED, "" + enabled);
}

public ConfigValue textColorEnabled(){
return ConfigValue.fromString( System.getProperty( JGIVEN_REPORT_TEXT_COLOR, AUTO ) );
public ConfigValue textColorEnabled() {
return ConfigValue.fromString(resolveProperty(JGIVEN_REPORT_TEXT_COLOR, AUTO));
}

public boolean textReport(){
return TRUE.equalsIgnoreCase( System.getProperty( JGIVEN_REPORT_TEXT, TRUE ) );
public boolean textReport() {
return TRUE.equalsIgnoreCase(resolveProperty(JGIVEN_REPORT_TEXT, TRUE));
}

public void setTextReport( boolean b ){
System.setProperty( JGIVEN_REPORT_TEXT, "" + b );
public void setTextReport(boolean b) {
System.setProperty(JGIVEN_REPORT_TEXT, "" + b);
}

public boolean filterStackTrace(){
return TRUE.equalsIgnoreCase( System.getProperty( JGIVEN_FILTER_STACK_TRACE, TRUE ) );
public boolean filterStackTrace() {
return TRUE.equalsIgnoreCase(resolveProperty(JGIVEN_FILTER_STACK_TRACE, TRUE));
}

public void setReportDir( File reportDir ){
System.setProperty( JGIVEN_REPORT_DIR, reportDir.getAbsolutePath() );
public void setReportDir(File reportDir) {
System.setProperty(JGIVEN_REPORT_DIR, reportDir.getAbsolutePath());
}

public boolean dryRun(){
return TRUE.equals( System.getProperty( JGIVEN_REPORT_DRY_RUN, FALSE ) );
public boolean dryRun() {
return TRUE.equals(System.getProperty(JGIVEN_REPORT_DRY_RUN, FALSE));
}
}
Loading

0 comments on commit 9a94853

Please sign in to comment.