Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a merge method for FilteredLogger #216

Merged
merged 5 commits into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions etc/assertj-templates/assertions_entry_point_class_template.txt
Original file line number Diff line number Diff line change
@@ -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 <code>{@link Assertions}</code>.
*/
protected Assertions() {
// empty
}
}
23 changes: 23 additions & 0 deletions etc/assertj-templates/has_assertion_template.txt
Original file line number Diff line number Diff line change
@@ -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};
}
Original file line number Diff line number Diff line change
@@ -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}
}
43 changes: 42 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>edu.hm.hafner</groupId>
<artifactId>codingstyle</artifactId>
<version>1.4.1-SNAPSHOT</version>
<version>1.5.0-SNAPSHOT</version>

<packaging>jar</packaging>

Expand Down Expand Up @@ -99,6 +99,7 @@
<nexus-staging-maven-plugin.version>1.6.8</nexus-staging-maven-plugin.version>
<maven-gpg-plugin.version>1.6</maven-gpg-plugin.version>
<maven-release-plugin.version>2.5.3</maven-release-plugin.version>
<assertj-assertions-generator-maven-plugin.version>2.2.0</assertj-assertions-generator-maven-plugin.version>

<!-- Maven Surefire ArgLine -->
<argLine>-Djava.util.logging.config.file=logging.properties</argLine>
Expand Down Expand Up @@ -251,6 +252,7 @@
</annotationProcessorPaths>
<compilerArgs>
<arg>-Xlint:all</arg>
<arg>-XepExcludedPaths:.*/generated-test-sources/.*</arg>
<arg>-XepAllDisabledChecksAsWarnings</arg>
<arg>-XepIgnoreUnknownCheckNames</arg>
<arg>-XepDisableWarningsInGeneratedCode</arg>
Expand All @@ -270,6 +272,7 @@
<arg>-Xep:WildcardImport:OFF</arg>
<arg>-Xep:ThrowsUncheckedException:OFF</arg>
<arg>-Xep:NullableDereference:OFF</arg>

</compilerArgs>
<compilerId>javac-with-errorprone</compilerId>
<forceJavacCompilerUse>true</forceJavacCompilerUse>
Expand Down Expand Up @@ -357,6 +360,44 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.assertj</groupId>
<artifactId>assertj-assertions-generator-maven-plugin</artifactId>
<version>${assertj-assertions-generator-maven-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>generate-assertions</goal>
</goals>
</execution>
</executions>
<configuration>
<quiet>true</quiet>
<cleanTargetDir>true</cleanTargetDir>
<hierarchical>false</hierarchical>
<generateBddAssertions>false</generateBddAssertions>
<generateJUnitSoftAssertions>false</generateJUnitSoftAssertions>
<generateSoftAssertions>true</generateSoftAssertions>
<templates>
<templatesDirectory>${project.basedir}/etc/assertj-templates/</templatesDirectory>
<assertionsEntryPointClass>assertions_entry_point_class_template.txt</assertionsEntryPointClass>
<softEntryPointAssertionClass>soft_assertions_entry_point_class_template.txt</softEntryPointAssertionClass>
<objectAssertion>has_assertion_template.txt</objectAssertion>
</templates>
<packages>
<package>edu.hm.hafner.util</package>
</packages>
<entryPointClassPackage>edu.hm.hafner.util.assertions</entryPointClassPackage>
</configuration>
<dependencies>
<!-- Without this dependency mvn test will fail due to a class not found exception -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/edu/hm/hafner/util/FilteredLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,15 @@ public List<String> getInfoMessages() {
public List<String> 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);
}
}
32 changes: 24 additions & 8 deletions src/test/java/edu/hm/hafner/util/FilteredLogTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");

Expand Down