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 10, 2021
1 parent a00d378 commit 9382958
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 4 deletions.
7 changes: 4 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ subprojects {
testngVersion = '7.4.0'
assertjVersion = '3.20.2'
slf4jVersion = '1.7.31'
log4jVersion = '2.14.1'
bytebuddyVersion = '1.11.6'
paranamerVersion = '2.8'
jansiVersion = '1.18'
Expand Down Expand Up @@ -154,10 +155,11 @@ configure(subprojects.findAll { !it.name.contains("android") }) {
sourceCompatibility = targetCompatibility = 1.8

dependencies {
implementation "org.apache.logging.log4j:log4j-core:$log4jVersion"
implementation "org.apache.logging.log4j:log4j-api:$log4jVersion"
implementation "org.slf4j:slf4j-api:$slf4jVersion"

testImplementation "org.slf4j:jcl-over-slf4j:$slf4jVersion"
testImplementation "org.slf4j:slf4j-simple:$slf4jVersion"
testImplementation "org.slf4j:slf4j-log4j12:$slf4jVersion"
testImplementation "junit:junit:$junitVersion"
testImplementation "org.assertj:assertj-core:$assertjVersion"
testImplementation "com.tngtech.java:junit-dataprovider:$junitDataproviderVersion"
Expand All @@ -176,7 +178,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
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
45 changes: 45 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 @@ -11,6 +11,7 @@
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Level;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
Expand All @@ -33,6 +34,50 @@ public void setupPropertiesFile() throws Exception {
setSystemProperty("jgiven.report.dir", null);
}

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

JGivenAppenderImpl.resetEvents();
Config.logReportEnabled();

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

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

JGivenAppenderImpl.resetEvents();
Config.logReportEnabled();

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

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

JGivenAppenderImpl.resetEvents();
Config.logDryRunEnabled();

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

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

JGivenAppenderImpl.resetEvents();
Config.logDryRunEnabled();

assertThat(JGivenAppenderImpl.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,34 @@
package com.tngtech.jgiven.impl;

import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Level;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.logging.log4j.core.config.plugins.Plugin;

@Plugin(name = "JGivenAppender", category="Core", elementType="appender", printObject=true)
public final class JGivenAppenderImpl extends AppenderSkeleton {
private static final List<LoggingEvent> loggingEvents = new ArrayList<>();

@Override
protected void append(LoggingEvent event) {
loggingEvents.add(event);
}

@Override
public void close() { }

@Override
public boolean requiresLayout() {
return false;
}

static void resetEvents() {
loggingEvents.clear();
}

static boolean containsLoggingEvent(String logMessage, Level logLevel) {
return loggingEvents.stream().anyMatch(loggingEvent -> loggingEvent.getMessage().equals(logMessage) && loggingEvent.getLevel().equals(logLevel));
}
}
6 changes: 5 additions & 1 deletion jgiven-core/src/test/resources/log4j.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
log4j.rootLogger=ERROR, stdout
packages=com.tngtech.jgiven.impl
log4j.rootLogger=ERROR, stdout, jgivenAppender
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.jgivenAppender=com.tngtech.jgiven.impl.JGivenAppenderImpl
log4j.appender.jgivenAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.jgivenAppender.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.logger.com.tngtech.jgiven=INFO
log4j.logger.com.tngtech.jgiven.impl.ScenarioExecutor=OFF

0 comments on commit 9382958

Please sign in to comment.