TSK-950: Test pojo implementation of equals and hashcode

This commit is contained in:
Benjamin Eckstein 2019-11-19 16:04:22 +01:00 committed by Mustapha Zorgati
parent 5b74398e90
commit 0877e946a9
15 changed files with 124 additions and 12 deletions

View File

@ -102,6 +102,12 @@
<version>${version.log4j}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
<version>${version.equalsverifier}</version>
<scope>test</scope>
</dependency>
</dependencies>
<!-- this repository is needed to fetch com.ibm.db2.jcc -->

View File

@ -56,7 +56,7 @@ public class KeyDomain {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
if (!getClass().isAssignableFrom(obj.getClass())) {
return false;
}
KeyDomain other = (KeyDomain) obj;

View File

@ -90,7 +90,7 @@ public class ObjectReference {
if (other == null) {
return false;
}
if (other.getClass() != getClass()) {
if (!getClass().isAssignableFrom(other.getClass())) {
return false;
}
ObjectReference o = (ObjectReference) other;

View File

@ -73,7 +73,7 @@ public class TimeInterval {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
if (!getClass().isAssignableFrom(obj.getClass())) {
return false;
}
TimeInterval other = (TimeInterval) obj;

View File

@ -166,7 +166,7 @@ public class AttachmentImpl implements Attachment {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
if (!getClass().isAssignableFrom(obj.getClass())) {
return false;
}
AttachmentImpl other = (AttachmentImpl) obj;

View File

@ -161,7 +161,7 @@ public class AttachmentSummaryImpl implements AttachmentSummary {
if (obj == null) {
return false;
}
if (!(obj instanceof AttachmentSummaryImpl)) {
if (!getClass().isAssignableFrom(obj.getClass())) {
return false;
}
AttachmentSummaryImpl other = (AttachmentSummaryImpl) obj;

View File

@ -134,13 +134,23 @@ public class ClassificationImpl extends ClassificationSummaryImpl implements Cla
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
if (o == null) {
return false;
}
if (!getClass().isAssignableFrom(o.getClass())) {
return false;
}
if (!super.equals(o)) {
return false;
}
ClassificationImpl that = (ClassificationImpl) o;
if (!that.canEqual(this)) {
return false;
}
return Objects.equals(isValidInDomain, that.isValidInDomain)
&& Objects.equals(created, that.created)
&& Objects.equals(modified, that.modified)
@ -148,6 +158,10 @@ public class ClassificationImpl extends ClassificationSummaryImpl implements Cla
&& Objects.equals(applicationEntryPoint, that.applicationEntryPoint);
}
protected boolean canEqual(Object other) {
return (other instanceof ClassificationImpl);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), isValidInDomain, created, modified, description, applicationEntryPoint);

View File

@ -198,10 +198,20 @@ public class ClassificationSummaryImpl implements ClassificationSummary {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
if (o == null) {
return false;
}
if (!getClass().isAssignableFrom(o.getClass())) {
return false;
}
ClassificationSummaryImpl that = (ClassificationSummaryImpl) o;
if (!that.canEqual(this)) {
return false;
}
return priority == that.priority
&& Objects.equals(id, that.id)
&& Objects.equals(key, that.key)
@ -222,6 +232,10 @@ public class ClassificationSummaryImpl implements ClassificationSummary {
&& Objects.equals(custom8, that.custom8);
}
protected boolean canEqual(Object other) {
return (other instanceof ClassificationSummaryImpl);
}
@Override
public int hashCode() {
return Objects.hash(id, key, category, type, domain, name, parentId, parentKey, priority, serviceLevel, custom1,

View File

@ -774,7 +774,7 @@ public class TaskImpl implements Task {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
if (!getClass().isAssignableFrom(obj.getClass())) {
return false;
}
TaskImpl other = (TaskImpl) obj;

View File

@ -552,7 +552,7 @@ public class TaskSummaryImpl implements TaskSummary {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
if (!getClass().isAssignableFrom(obj.getClass())) {
return false;
}
TaskSummaryImpl other = (TaskSummaryImpl) obj;

View File

@ -461,6 +461,8 @@ public class WorkbasketAccessItemImpl implements WorkbasketAccessItem {
result = prime * result + (permRead ? 1231 : 1237);
result = prime * result + (permTransfer ? 1231 : 1237);
result = prime * result + ((workbasketId == null) ? 0 : workbasketId.hashCode());
result = prime * result + ((workbasketKey == null) ? 0 : workbasketKey.hashCode());
result = prime * result + ((accessName == null) ? 0 : accessName.hashCode());
return result;
}
@ -472,7 +474,7 @@ public class WorkbasketAccessItemImpl implements WorkbasketAccessItem {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
if (!getClass().isAssignableFrom(obj.getClass())) {
return false;
}
WorkbasketAccessItemImpl other = (WorkbasketAccessItemImpl) obj;
@ -548,6 +550,20 @@ public class WorkbasketAccessItemImpl implements WorkbasketAccessItem {
} else if (!workbasketId.equals(other.workbasketId)) {
return false;
}
if (workbasketKey == null) {
if (other.workbasketKey != null) {
return false;
}
} else if (!workbasketKey.equals(other.workbasketKey)) {
return false;
}
if (accessName == null) {
if (other.accessName != null) {
return false;
}
} else if (!accessName.equals(other.accessName)) {
return false;
}
return true;
}

View File

@ -1,6 +1,7 @@
package pro.taskana.impl;
import java.time.Instant;
import pro.taskana.Workbasket;
import pro.taskana.WorkbasketSummary;
import pro.taskana.WorkbasketType;
@ -262,7 +263,8 @@ public class WorkbasketImpl implements Workbasket {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
if (!getClass().isAssignableFrom(obj.getClass())) {
return false;
}
WorkbasketImpl other = (WorkbasketImpl) obj;

View File

@ -263,7 +263,7 @@ public class WorkbasketSummaryImpl implements WorkbasketSummary {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
if (!getClass().isAssignableFrom(obj.getClass())) {
return false;
}
WorkbasketSummaryImpl other = (WorkbasketSummaryImpl) obj;

View File

@ -0,0 +1,59 @@
package pro.taskana;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;
import pro.taskana.impl.AttachmentImpl;
import pro.taskana.impl.AttachmentSummaryImpl;
import pro.taskana.impl.ClassificationImpl;
import pro.taskana.impl.ClassificationSummaryImpl;
import pro.taskana.impl.TaskImpl;
import pro.taskana.impl.TaskSummaryImpl;
import pro.taskana.impl.WorkbasketAccessItemImpl;
import pro.taskana.impl.WorkbasketImpl;
import pro.taskana.impl.WorkbasketSummaryImpl;
/**
* check classes with a custom equals and hashcode implementation for correctness.
*/
class EqualsAndHashCodeTest {
@TestFactory
Collection<DynamicTest> equalsContract() {
return
getPojoClasses().stream()
.map(cl -> DynamicTest.dynamicTest("Check Hash and Equals for " + cl.getSimpleName(),
() -> {
EqualsVerifier.forClass(cl)
.suppress(Warning.NONFINAL_FIELDS, Warning.STRICT_INHERITANCE)
.withRedefinedSuperclass()
.verify();
}))
.collect(Collectors.toList());
}
//TODO find a way to dynamically create a list with custom implemented equals or hash methods.
private List<Class<?>> getPojoClasses() {
return Arrays.asList(
KeyDomain.class,
ObjectReference.class,
TimeInterval.class,
AttachmentImpl.class,
AttachmentSummaryImpl.class,
ClassificationImpl.class,
ClassificationSummaryImpl.class,
TaskImpl.class,
TaskSummaryImpl.class,
WorkbasketAccessItemImpl.class,
WorkbasketImpl.class,
WorkbasketSummaryImpl.class
);
}
}

View File

@ -69,6 +69,7 @@
<version.mockito>2.8.47</version.mockito>
<version.powermock>1.7.1</version.powermock>
<version.hamcrest>1.3</version.hamcrest>
<version.equalsverifier>3.1.10</version.equalsverifier>
<!-- database driver versions -->
<version.h2>1.4.197</version.h2>