From 699edc8d1b5b522494934ebd9f816c324d5fa99a Mon Sep 17 00:00:00 2001 From: Ulli Hafner Date: Thu, 27 Aug 2020 22:34:36 +0200 Subject: [PATCH 1/5] Add a merge method for FilteredLogger. --- .../assertions_entry_point_class_template.txt | 17 ++++++++ .../has_assertion_template.txt | 23 +++++++++++ ..._assertions_entry_point_class_template.txt | 10 +++++ pom.xml | 39 +++++++++++++++++++ .../java/edu/hm/hafner/util/FilteredLog.java | 11 ++++++ .../edu/hm/hafner/util/FilteredLogTest.java | 32 +++++++++++---- 6 files changed, 124 insertions(+), 8 deletions(-) create mode 100644 etc/assertj-templates/assertions_entry_point_class_template.txt create mode 100644 etc/assertj-templates/has_assertion_template.txt create mode 100644 etc/assertj-templates/soft_assertions_entry_point_class_template.txt diff --git a/etc/assertj-templates/assertions_entry_point_class_template.txt b/etc/assertj-templates/assertions_entry_point_class_template.txt new file mode 100644 index 00000000..f5204a15 --- /dev/null +++ b/etc/assertj-templates/assertions_entry_point_class_template.txt @@ -0,0 +1,17 @@ +package ${package}; + +/** + * Entry point for assertions of different data types. Each method in this class is a static factory for the + * type-specific assertion objects. + */ +@edu.umd.cs.findbugs.annotations.SuppressFBWarnings("NM") +@javax.annotation.Generated(value="assertj-assertions-generator") +public class Assertions extends org.assertj.core.api.Assertions { +${all_assertions_entry_points} + /** + * Creates a new {@link Assertions}. + */ + protected Assertions() { + // empty + } +} diff --git a/etc/assertj-templates/has_assertion_template.txt b/etc/assertj-templates/has_assertion_template.txt new file mode 100644 index 00000000..e3493927 --- /dev/null +++ b/etc/assertj-templates/has_assertion_template.txt @@ -0,0 +1,23 @@ + + /** + * Verifies that the actual ${class_to_assert}'s ${property} is equal to the given one. + * @param ${property_safe} the given ${property} to compare the actual ${class_to_assert}'s ${property} to. + * @return this assertion object. + * @throws AssertionError - if the actual ${class_to_assert}'s ${property} is not equal to the given one.${throws_javadoc} + */ + public ${self_type} has${Property}(${propertyType} ${property_safe}) ${throws}{ + // check that actual ${class_to_assert} we want to make assertions on is not null. + isNotNull(); + + // overrides the default error message with a more explicit one + String assertjErrorMessage = "\nExpecting ${property} of:\n <%s>\nto be:\n <%s>\nbut was:\n <%s>"; + + // null safe check + ${propertyType} actual${Property} = actual.${getter}(); + if (!java.util.Objects.deepEquals(actual${Property}, ${property_safe})) { + failWithMessage(assertjErrorMessage, actual, ${property_safe}, actual${Property}); + } + + // return the current assertion for method chaining + return ${myself}; + } diff --git a/etc/assertj-templates/soft_assertions_entry_point_class_template.txt b/etc/assertj-templates/soft_assertions_entry_point_class_template.txt new file mode 100644 index 00000000..c7485e42 --- /dev/null +++ b/etc/assertj-templates/soft_assertions_entry_point_class_template.txt @@ -0,0 +1,10 @@ +package ${package}; + +/** + * Entry point for soft assertions of different data types. + */ +@edu.umd.cs.findbugs.annotations.SuppressFBWarnings("NM") +@javax.annotation.Generated(value="assertj-assertions-generator") +public class SoftAssertions extends org.assertj.core.api.AutoCloseableSoftAssertions { +${all_assertions_entry_points} +} diff --git a/pom.xml b/pom.xml index 50378e1c..d51b1ac3 100644 --- a/pom.xml +++ b/pom.xml @@ -99,6 +99,7 @@ 1.6.8 1.6 2.5.3 + 2.2.0 -Djava.util.logging.config.file=logging.properties @@ -357,6 +358,44 @@ + + org.assertj + assertj-assertions-generator-maven-plugin + ${assertj-assertions-generator-maven-plugin.version} + + + + generate-assertions + + + + + true + true + false + false + false + true + + ${project.basedir}/etc/assertj-templates/ + assertions_entry_point_class_template.txt + soft_assertions_entry_point_class_template.txt + has_assertion_template.txt + + + edu.hm.hafner.util + + edu.hm.hafner.util.assertions + + + + + junit + junit + 4.13 + + + org.apache.maven.plugins maven-pmd-plugin diff --git a/src/main/java/edu/hm/hafner/util/FilteredLog.java b/src/main/java/edu/hm/hafner/util/FilteredLog.java index 7d219031..457f8fdf 100644 --- a/src/main/java/edu/hm/hafner/util/FilteredLog.java +++ b/src/main/java/edu/hm/hafner/util/FilteredLog.java @@ -152,4 +152,15 @@ public List getInfoMessages() { public List getErrorMessages() { return Collections.unmodifiableList(errorMessages); } + + /** + * Merges the info and error messages of the other log. + * + * @param other + * the log to merge + */ + public void merge(final FilteredLog other) { + infoMessages.addAll(other.infoMessages); + errorMessages.addAll(other.errorMessages); + } } diff --git a/src/test/java/edu/hm/hafner/util/FilteredLogTest.java b/src/test/java/edu/hm/hafner/util/FilteredLogTest.java index f5ed6da3..55678965 100644 --- a/src/test/java/edu/hm/hafner/util/FilteredLogTest.java +++ b/src/test/java/edu/hm/hafner/util/FilteredLogTest.java @@ -2,7 +2,7 @@ import org.junit.jupiter.api.Test; -import static org.assertj.core.api.Assertions.*; +import static edu.hm.hafner.util.assertions.Assertions.*; /** * Tests the class {@link FilteredLog}. @@ -16,9 +16,9 @@ class FilteredLogTest { void shouldLogNothing() { FilteredLog filteredLog = new FilteredLog(TITLE, 5); - assertThat(filteredLog.getErrorMessages()).isEmpty(); + assertThat(filteredLog).hasNoErrorMessages(); filteredLog.logSummary(); - assertThat(filteredLog.getErrorMessages()).isEmpty(); + assertThat(filteredLog).hasNoErrorMessages(); } @Test @@ -55,13 +55,30 @@ void shouldSkipAdditionalErrors() { filteredLog.logSummary(); - assertThat(filteredLog.getErrorMessages()).containsExactly(TITLE, "1", "2", "3", "4", "5", + assertThat(filteredLog).hasOnlyErrorMessages(TITLE, "1", "2", "3", "4", "5", " ... skipped logging of 2 additional errors ..."); assertThat(filteredLog.size()).isEqualTo(7); } private void assertThatExactly5MessagesAreLogged(final FilteredLog filteredLog) { - assertThat(filteredLog.getErrorMessages()).containsExactly(TITLE, "1", "2", "3", "4", "5"); + assertThat(filteredLog).hasOnlyErrorMessages(TITLE, "1", "2", "3", "4", "5"); + } + + @Test + void shouldMergeLogger() { + FilteredLog parent = new FilteredLog("Parent Errors"); + + parent.logInfo("parent Info 1"); + parent.logError("parent Error 1"); + + FilteredLog child = new FilteredLog("Child Errors"); + child.logInfo("child Info 1"); + child.logError("child Error 1"); + + parent.merge(child); + + assertThat(parent).hasOnlyInfoMessages("parent Info 1", "child Info 1"); + assertThat(parent).hasOnlyErrorMessages("Parent Errors", "parent Error 1", "Child Errors", "child Error 1"); } @Test @@ -71,9 +88,8 @@ void shouldLogExceptions() { filteredLog.logException(new IllegalArgumentException("Cause"), "Message"); filteredLog.logException(new IllegalArgumentException(""), "Message"); - assertThat(filteredLog.getErrorMessages()).contains(TITLE, - "Message", "java.lang.IllegalArgumentException: Cause", - "\tat edu.hm.hafner.util.FilteredLogTest.shouldLogExceptions(FilteredLogTest.java:71)"); + assertThat(filteredLog).hasErrorMessages(TITLE, + "Message", "java.lang.IllegalArgumentException: Cause"); } @Test From fb9f0f5b450bb87f31d1a62cd507fb089ff096a5 Mon Sep 17 00:00:00 2001 From: Ulli Hafner Date: Thu, 27 Aug 2020 22:36:15 +0200 Subject: [PATCH 2/5] Bump version to 1.5.0. --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index d51b1ac3..7f0a1b0d 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 edu.hm.hafner codingstyle - 1.4.1-SNAPSHOT + 1.5.0-SNAPSHOT jar From 6f436c1d962eae5ee7bf1ff35f89ebba10ea241e Mon Sep 17 00:00:00 2001 From: Ulli Hafner Date: Thu, 27 Aug 2020 23:10:56 +0200 Subject: [PATCH 3/5] Exclude generated test files from ErrorProne. --- pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/pom.xml b/pom.xml index 7f0a1b0d..9c55650e 100644 --- a/pom.xml +++ b/pom.xml @@ -252,6 +252,7 @@ -Xlint:all + -XepExcludedPaths:.*/generated-test-sources/.* -XepAllDisabledChecksAsWarnings -XepIgnoreUnknownCheckNames -XepDisableWarningsInGeneratedCode From ae85f69038b2d993d25f4b007c3fa91823287c61 Mon Sep 17 00:00:00 2001 From: Ulli Hafner Date: Thu, 27 Aug 2020 23:11:20 +0200 Subject: [PATCH 4/5] Do not scan tests when checking the architecture. --- src/test/java/edu/hm/hafner/util/PackageArchitectureTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/edu/hm/hafner/util/PackageArchitectureTest.java b/src/test/java/edu/hm/hafner/util/PackageArchitectureTest.java index a05e6689..c00e644a 100644 --- a/src/test/java/edu/hm/hafner/util/PackageArchitectureTest.java +++ b/src/test/java/edu/hm/hafner/util/PackageArchitectureTest.java @@ -2,6 +2,7 @@ import java.net.URL; +import com.tngtech.archunit.core.importer.ImportOption.DoNotIncludeTests; import com.tngtech.archunit.junit.AnalyzeClasses; import com.tngtech.archunit.junit.ArchTest; import com.tngtech.archunit.lang.ArchRule; @@ -16,7 +17,7 @@ * @author Ullrich Hafner */ @SuppressWarnings("hideutilityclassconstructor") -@AnalyzeClasses(packages = "edu.hm.hafner..") +@AnalyzeClasses(packages = "edu.hm.hafner..", importOptions = DoNotIncludeTests.class) class PackageArchitectureTest { private static final URL PACKAGE_DESIGN = PackageArchitectureTest.class.getResource("/design.puml"); From f3816f950f17979c66f6d181caaa9037b0246cb3 Mon Sep 17 00:00:00 2001 From: Ulli Hafner Date: Fri, 28 Aug 2020 10:42:34 +0200 Subject: [PATCH 5/5] New line. --- pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/pom.xml b/pom.xml index 9c55650e..d188a7e0 100644 --- a/pom.xml +++ b/pom.xml @@ -272,6 +272,7 @@ -Xep:WildcardImport:OFF -Xep:ThrowsUncheckedException:OFF -Xep:NullableDereference:OFF + javac-with-errorprone true