Skip to content

Commit

Permalink
Merge pull request #822 from TNG/bugfix/Issue-821-ignore-errors-durin…
Browse files Browse the repository at this point in the history
…g-annotation-scan

Bugfix/issue 821 ignore errors during annotation scan
  • Loading branch information
l-1squared authored Feb 1, 2022
2 parents 6eabcf7 + d5b4271 commit 084678e
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 19 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# v1.2x
##New features
* The `@Tag` annotation configuration can now be set differently from the JGiven defaults. [#806](https://github.com/TNG/JGiven/issues/806)
##Fixed issues
* JGiven no longer prints errors while scanning annotations that are not tags. [#821](https://github.com/TNG/JGiven/issues/821)
##Backward incompatible changes
# v1.2
## New features
Expand Down
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ subprojects {
ext {
junitDataproviderVersion = '1.13.1'
// set default junit version if not set via command line
junitVersion = rootProject.hasProperty('junitVersion') ? rootProject.junitVersion : '4.13.1'
junitVersion = rootProject.hasProperty('junitVersion') ? rootProject.junitVersion : '4.13.2'
junit5Version = '5.7.2'
junit5PlatformRunnerVersion = '1.7.0'
junitParamsVersion = '1.1.1'
testngVersion = '7.4.0'
assertjVersion = '3.20.2'
slf4jVersion = '1.7.32'
assertjVersion = '3.22.0'
slf4jVersion = '1.7.35'
paranamerVersion = '2.8'
jansiVersion = '1.18'
gsonVersion = '2.8.8'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ private Optional<Object> getValuesFromAnnotation(Annotation annotation) {
try {
Method method = annotation.annotationType().getMethod("value");
return Optional.ofNullable(method.invoke(annotation));
} catch (NoSuchMethodException ignoreAnnotationsThatAreNotTags) {
return Optional.empty();
} catch (Exception e) {
log.error("Error while getting 'value' method of annotation " + annotation, e);
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,43 +47,39 @@ public void teardown() {
public void disabledReportsLogAMessage() {
setSystemProperty("jgiven.report.enabled", "false");

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

assertThat(JGivenLogHandler.containsLoggingEvent("Please note that the report generation is turned off.",
assertThat(handler.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.",
assertThat(handler.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.",
assertThat(handler.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.",
assertThat(handler.containsLoggingEvent("Dry Run enabled.",
Level.INFO)).isFalse();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
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<>();
private List<LogRecord> logList = new ArrayList<>();

@Override
public void publish(LogRecord record) {
Expand All @@ -16,17 +17,19 @@ public void publish(LogRecord record) {

@Override
public void flush() {
resetEvents();
logList.clear();
}

@Override
public void close() throws SecurityException {}
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 boolean containsLoggingEvent(Predicate<LogRecord> condition) {
return logList.stream().anyMatch(condition);
}
public static void resetEvents() {
logList.clear();

public boolean containsLoggingEvent(String message, Level level) {
return containsLoggingEvent(logRecord -> logRecord.getMessage().equals(message)
&& logRecord.getLevel().equals(level));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,36 @@
import static org.assertj.core.api.Assertions.assertThat;

import com.tngtech.jgiven.config.DefaultConfiguration;
import com.tngtech.jgiven.impl.TestUtil.JGivenLogHandler;
import com.tngtech.jgiven.report.model.Tag;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import org.junit.Before;
import org.junit.Test;

public class TagCreatorTest {

private final TagCreator underTest = new TagCreator(new DefaultConfiguration());
private final JGivenLogHandler interceptor = new JGivenLogHandler();

@Before
public void addLogInterceptor() {
Logger testLogger = LogManager.getLogManager().getLogger(TagCreator.class.getName());
testLogger.addHandler(interceptor);
}

@Test
public void testAnnotationParsing() {
Tag tag = getOnlyTagFor(AnnotationTestClass.class.getAnnotations()[0]);
assertThat(tag.getName()).isEqualTo("AnnotationWithoutValue");
assertThat(tag.getValues()).isEmpty();
assertThat(interceptor.containsLoggingEvent(record -> record.getLevel() == Level.SEVERE))
.as("Attempt to convert an annotation without value method results in an error log")
.isFalse();
}

@Test
Expand Down

0 comments on commit 084678e

Please sign in to comment.