Skip to content

Commit

Permalink
Add log messages if the reports are disabled or dry run enabled
Browse files Browse the repository at this point in the history
Signed-off-by: Andru Stefanescu <als209@cam.ac.uk>
  • Loading branch information
andru47 committed Aug 16, 2021
1 parent 9161982 commit bbaf5c6
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 27 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
4 changes: 0 additions & 4 deletions example-projects/spock/src/test/resources/log4j.properties

This file was deleted.

11 changes: 11 additions & 0 deletions jgiven-core/src/main/java/com/tngtech/jgiven/impl/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,22 @@ public static Config config() {
}

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() {
}

Expand Down
54 changes: 54 additions & 0 deletions jgiven-core/src/test/java/com/tngtech/jgiven/impl/ConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
import com.google.common.io.FileWriteMode;
import com.google.common.io.Files;
import com.tngtech.jgiven.config.ConfigValue;
import com.tngtech.jgiven.impl.TestUtil.JGivenLogHandler;
import java.io.File;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
Expand All @@ -23,16 +26,67 @@ public class ConfigTest {
public TemporaryFolder temporaryFolder = new TemporaryFolder();

private final Map<String, String> systemPropertiesBackup = new HashMap<>();
private final JGivenLogHandler handler = new JGivenLogHandler();
private CharSink jgivenConfig;

@Before
public void setupPropertiesFile() throws Exception {
Logger.getLogger(Config.class.getName()).addHandler(handler);
File configFile = temporaryFolder.newFile();
jgivenConfig = Files.asCharSink(configFile, Charsets.UTF_8, FileWriteMode.APPEND);
setSystemProperty("jgiven.config.path", configFile.getAbsolutePath());
setSystemProperty("jgiven.report.dir", null);
}

@After
public void teardown() {
Logger.getLogger(Config.class.getName()).removeHandler(handler);
}

@Test
public void disabledReportsLogAMessage() {
setSystemProperty("jgiven.report.enabled", "false");

JGivenLogHandler.resetEvents();
Config.logReportEnabled();

assertThat(JGivenLogHandler.containsLoggingEvent("Please note that the report generation is turned off.",
Level.INFO)).isTrue();
}

@Test
public void enabledReportsDontLogAMessage() {
setSystemProperty("jgiven.report.enabled", "true");

JGivenLogHandler.resetEvents();
Config.logReportEnabled();

assertThat(JGivenLogHandler.containsLoggingEvent("Please note that the report generation is turned off.",
Level.INFO)).isFalse();
}

@Test
public void dryRunEnabledLogsAMessage() {
setSystemProperty("jgiven.report.dry-run", "true");

JGivenLogHandler.resetEvents();
Config.logDryRunEnabled();

assertThat(JGivenLogHandler.containsLoggingEvent("Dry Run enabled.",
Level.INFO)).isTrue();
}

@Test
public void dryRunDisabledDoesntLogAMessage() {
setSystemProperty("jgiven.report.dry-run", "false");

JGivenLogHandler.resetEvents();
Config.logDryRunEnabled();

assertThat(JGivenLogHandler.containsLoggingEvent("Dry Run enabled.",
Level.INFO)).isFalse();
}

@Test
public void configValuesHaveDefaults() throws Exception {
Config underTest = createNewTestInstance();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.tngtech.jgiven.impl.TestUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;

public class JGivenLogHandler extends Handler {
static private List<LogRecord> logList = new ArrayList<>();

@Override
public void publish(LogRecord record) {
logList.add(record);
}

@Override
public void flush() {
resetEvents();
}

@Override
public void close() throws SecurityException {}

public static boolean containsLoggingEvent(String message, Level level) {
return logList.stream().anyMatch(logRecord -> logRecord.getMessage().equals(message)
&& logRecord.getLevel().equals(level));
}
public static void resetEvents() {
logList.clear();
}
}
8 changes: 0 additions & 8 deletions jgiven-core/src/test/resources/log4j.properties

This file was deleted.

4 changes: 0 additions & 4 deletions jgiven-junit/src/test/resources/log4j.properties

This file was deleted.

4 changes: 0 additions & 4 deletions jgiven-spock/src/test/resources/log4j.properties

This file was deleted.

4 changes: 0 additions & 4 deletions jgiven-tests/src/test/resources/log4j.properties

This file was deleted.

0 comments on commit bbaf5c6

Please sign in to comment.