Skip to content

Commit

Permalink
Merge pull request #851 from uhafner/filtered-log-equals
Browse files Browse the repository at this point in the history
Override `equals` for `FilteredLog`
  • Loading branch information
uhafner authored Nov 16, 2023
2 parents 7699a75 + b17e9ce commit 38244ac
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,7 @@
</dependency>
</dependencies>
<configuration>
<skip>true</skip>
<versionFormat>[-0-9.]*</versionFormat>
<failBuildOnProblemsFound>true</failBuildOnProblemsFound>
<checkDependencies>true</checkDependencies>
Expand Down
41 changes: 39 additions & 2 deletions src/main/java/edu/hm/hafner/util/FilteredLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.locks.ReentrantLock;

import org.apache.commons.lang3.StringUtils;
Expand All @@ -18,7 +19,7 @@
*
* @author Ullrich Hafner
*/
public class FilteredLog implements Serializable {
public final class FilteredLog implements Serializable {
private static final long serialVersionUID = -8552323621953159904L;

private static final int DEFAULT_MAX_LINES = 20;
Expand Down Expand Up @@ -69,7 +70,7 @@ public FilteredLog(final String title, final int maxLines) {
*
* @return this
*/
protected Object readResolve() {
private Object readResolve() {
lock = new ReentrantLock();

return this;
Expand Down Expand Up @@ -260,4 +261,40 @@ public void merge(final FilteredLog other) {
lock.unlock();
}
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

FilteredLog log = (FilteredLog) o;

Check warning on line 274 in src/main/java/edu/hm/hafner/util/FilteredLog.java

View workflow job for this annotation

GitHub Actions / Autograding results

Varifier

Consider using `var` here to avoid boilerplate.

if (maxLines != log.maxLines) {
return false;
}
if (lines != log.lines) {
return false;
}
if (!Objects.equals(title, log.title)) {
return false;
}
if (!infoMessages.equals(log.infoMessages)) {
return false;
}
return errorMessages.equals(log.errorMessages);
}

@Override
public int hashCode() {
int result = title != null ? title.hashCode() : 0;
result = 31 * result + maxLines;
result = 31 * result + lines;
result = 31 * result + infoMessages.hashCode();
result = 31 * result + errorMessages.hashCode();
return result;
}
}
14 changes: 14 additions & 0 deletions src/test/java/edu/hm/hafner/util/FilteredLogTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package edu.hm.hafner.util;

import java.util.concurrent.locks.ReentrantLock;

import org.apache.commons.lang3.StringUtils;
import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration;
import org.junit.jupiter.api.Test;

import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;

import static edu.hm.hafner.util.assertions.Assertions.*;

/**
Expand Down Expand Up @@ -121,6 +126,15 @@ void shouldLog20ErrorsByDefault() {
.contains("info24");
}

@Test
void shouldAdhereToEquals() {
EqualsVerifier.forClass(FilteredLog.class)
.withIgnoredFields("lock")
.withPrefabValues(ReentrantLock.class, new ReentrantLock(), new ReentrantLock())
.suppress(Warning.NONFINAL_FIELDS)
.verify();
}

@Override
protected void assertThatRestoredInstanceEqualsOriginalInstance(final FilteredLog original,
final FilteredLog restored) {
Expand Down

0 comments on commit 38244ac

Please sign in to comment.