Skip to content

Commit

Permalink
fix(warnings): Add equals and hashcode methods to activity
Browse files Browse the repository at this point in the history
  • Loading branch information
djansen-redhat committed Mar 21, 2017
1 parent 37d9c49 commit 88c421b
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 2 deletions.
39 changes: 37 additions & 2 deletions server/zanata-model/src/main/java/org/zanata/model/Activity.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public class Activity extends ModelEntityBase implements Serializable {
private int wordCount;

public Activity(HPerson actor, IsEntityWithType context,
IsEntityWithType target, ActivityType activityType, int wordCount) {
IsEntityWithType target, ActivityType activityType, int wordCount) {
this.actor = actor;
this.contextType = context.getEntityType();
this.contextId = context.getId();
Expand All @@ -90,7 +90,7 @@ public Activity(HPerson actor, IsEntityWithType context,
}

public void updateActivity(Date currentTime, IsEntityWithType target,
int wordCount) {
int wordCount) {
this.endOffsetMillis = currentTime.getTime() - approxTime.getTime();
this.wordCount += wordCount;
this.eventCount++;
Expand Down Expand Up @@ -162,4 +162,39 @@ public int getWordCount() {
public EntityType getContextType() {
return this.contextType;
}

/**
* Business equality comparison
*
* @param other Activity
* @return other is equal
*/
@Override
public boolean equals(Object other) {
if (this == other) return true;
if (other == null || getClass() != other.getClass()) return false;
if (!super.equals(other)) return false;

Activity activity = (Activity) other;

return (actor == null ?
activity.actor == null : actor.equals(activity.actor)) &&
(eventCount == activity.eventCount) &&
(wordCount == activity.wordCount) &&
(contextType == activity.contextType) &&
(lastTargetType == activity.lastTargetType) &&
(activityType == activity.activityType);
}

@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (actor != null ? actor.hashCode() : 0);
result = 31 * result + (contextType != null ? contextType.hashCode() : 0);
result = 31 * result + (lastTargetType != null ? lastTargetType.hashCode() : 0);
result = 31 * result + (activityType != null ? activityType.hashCode() : 0);
result = 31 * result + eventCount;
result = 31 * result + wordCount;
return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.zanata.model;

import org.junit.Test;
import org.zanata.common.ActivityType;
import org.zanata.common.ProjectType;

import java.lang.reflect.Field;
import java.util.Date;
Expand All @@ -19,4 +21,101 @@ public void getApproximateTime() throws Exception {
assertThat(activity.getApproxTime())
.isEqualTo(testDate);
}

@Test
public void testEquals() {
HDocument document = new HDocument();
document.setId(555666777L);
HPerson actorA = new HPerson();
actorA.setName("lara");
actorA.setEmail("lara@test.com");
actorA.setId(321654987L);

HPerson actorB = new HPerson();
actorB.setId(987654321L);
actorB.setName("aloy");
actorB.setEmail("aloy@test.com");

HProjectIteration versionA = new HProjectIteration();
versionA.setId(1234567890L);
HProjectIteration versionB = new HProjectIteration();
versionB.setId(5678901234L);
versionB.setProjectType(ProjectType.Utf8Properties);

Activity activity = new Activity();

assertThat(activity.equals(new Activity())).isTrue();

activity = new Activity(actorA, versionA, versionA,
ActivityType.UPLOAD_SOURCE_DOCUMENT, 9000);

assertThat(activity.equals(new Activity(actorA, versionA, versionA,
ActivityType.UPLOAD_SOURCE_DOCUMENT, 9000))).isTrue();

assertEqualsFalse(activity, new Activity(actorB, versionA, versionA,
ActivityType.UPLOAD_SOURCE_DOCUMENT, 9000));
assertEqualsFalse(activity, new Activity(actorA, document, versionA,
ActivityType.UPLOAD_SOURCE_DOCUMENT, 9000));
assertEqualsFalse(activity, new Activity(actorA, versionA, document,
ActivityType.UPLOAD_SOURCE_DOCUMENT, 9000));
assertEqualsFalse(activity, new Activity(actorA, versionA, versionA,
ActivityType.REVIEWED_TRANSLATION, 9000));
assertEqualsFalse(activity, new Activity(actorA, versionA, versionA,
ActivityType.UPLOAD_SOURCE_DOCUMENT, 9001));
}

private void assertEqualsFalse(Activity a, Activity b) {
assertThat(a.equals(b)).isFalse();
}

@Test
public void hashcodeTest() {
HDocument document = new HDocument();
document.setId(555666777L);
HPerson actorA = new HPerson();
HPerson actorB = new HPerson();
actorB.setName("aloy");

HProjectIteration versionA = new HProjectIteration();
versionA.setId(1234567890L);
HProjectIteration versionB = new HProjectIteration();
versionB.setId(5678901234L);
versionB.setProjectType(ProjectType.Utf8Properties);

int hashCode1 = new Activity(actorA, versionA, versionA,
ActivityType.UPLOAD_SOURCE_DOCUMENT, 9000).hashCode();
int hashCode2 = new Activity(actorB, versionB, versionB,
ActivityType.REVIEWED_TRANSLATION, 1000).hashCode();
assertThat(hashCode1 == hashCode2).isFalse();
hashCode2 = new Activity(actorA, versionA, versionA,
ActivityType.UPLOAD_SOURCE_DOCUMENT, 9000).hashCode();
assertThat(hashCode1 == hashCode2).isTrue();
}

@Test
public void updateActivity() throws Exception {
HDocument document = new HDocument();
document.setId(555666777L);
HPerson person = new HPerson();

HProjectIteration version = new HProjectIteration();
version.setId(1234567890L);

Activity activity = new Activity(person, version, version,
ActivityType.UPLOAD_SOURCE_DOCUMENT, 9000);
activity.setCreationDate(new Date(0L));

Field f = activity.getClass().getDeclaredField("approxTime");
f.setAccessible(true);
f.set(activity, new Date(0L));

activity.updateActivity(new Date(10000L), document, 55);

//TODO: assertThat(activity.getApproxTime()).isEqualTo(new Date(?));
assertThat(activity.getActor()).isEqualTo(person);
assertThat(activity.getWordCount()).isEqualTo(9055);
assertThat(activity.getEventCount()).isEqualTo(2);
assertThat(activity.getLastTargetType()).isEqualTo(document.getEntityType());
}

}

0 comments on commit 88c421b

Please sign in to comment.