More Unit tests

Change-Id: I32dd3851e490979621c4a5205c6e041dee900244
This commit is contained in:
Madan Jampani 2015-07-23 15:37:07 -07:00 committed by Gerrit Code Review
parent bd1eb3fba4
commit 92c64ebea2
6 changed files with 85 additions and 7 deletions

View File

@ -108,12 +108,13 @@ public final class Match<T> {
return Objects.hash(matchAny, value); return Objects.hash(matchAny, value);
} }
@SuppressWarnings("unchecked")
@Override @Override
public boolean equals(Object other) { public boolean equals(Object other) {
if (!(other instanceof Match)) { if (!(other instanceof Match)) {
return false; return false;
} }
Match<T> that = (Match) other; Match<T> that = (Match<T>) other;
return Objects.equals(this.matchAny, that.matchAny) && return Objects.equals(this.matchAny, that.matchAny) &&
Objects.equals(this.value, that.value); Objects.equals(this.value, that.value);
} }

View File

@ -15,6 +15,10 @@
*/ */
package org.onosproject.store.consistent.impl; package org.onosproject.store.consistent.impl;
import static com.google.common.base.MoreObjects.toStringHelper;
import java.util.Objects;
/** /**
* Result of a database update operation. * Result of a database update operation.
* *
@ -74,6 +78,7 @@ public final class Result<V> {
/** /**
* Returns the status of database update operation. * Returns the status of database update operation.
*
* @return database update status * @return database update status
*/ */
public Status status() { public Status status() {
@ -82,10 +87,35 @@ public final class Result<V> {
/** /**
* Returns the return value for the update. * Returns the return value for the update.
*
* @return value returned by database update. If the status is another * @return value returned by database update. If the status is another
* other than Status.OK, this returns a null * other than Status.OK, this returns a null
*/ */
public V value() { public V value() {
return value; return value;
} }
@Override
public int hashCode() {
return Objects.hash(value, status);
}
@SuppressWarnings("unchecked")
@Override
public boolean equals(Object other) {
if (!(other instanceof Result)) {
return false;
}
Result<V> that = (Result<V>) other;
return Objects.equals(this.value, that.value) &&
Objects.equals(this.status, that.status);
}
@Override
public String toString() {
return toStringHelper(this)
.add("status", status)
.add("value", value)
.toString();
}
} }

View File

@ -653,7 +653,7 @@ public class EventuallyConsistentMapImpl<K, V>
public void processItems(List<UpdateEntry<K, V>> items) { public void processItems(List<UpdateEntry<K, V>> items) {
Map<K, UpdateEntry<K, V>> map = Maps.newHashMap(); Map<K, UpdateEntry<K, V>> map = Maps.newHashMap();
items.forEach(item -> map.compute(item.key(), (key, existing) -> items.forEach(item -> map.compute(item.key(), (key, existing) ->
existing == null || item.compareTo(existing) > 0 ? item : existing)); item.isNewerThan(existing) ? item : existing));
communicationExecutor.submit(() -> { communicationExecutor.submit(() -> {
clusterCommunicator.unicast(ImmutableList.copyOf(map.values()), clusterCommunicator.unicast(ImmutableList.copyOf(map.values()),
updateMessageSubject, updateMessageSubject,

View File

@ -83,10 +83,11 @@ public class MapValue<V> implements Comparable<MapValue<V>> {
return Objects.hashCode(timestamp, value); return Objects.hashCode(timestamp, value);
} }
@SuppressWarnings("unchecked")
@Override @Override
public boolean equals(Object other) { public boolean equals(Object other) {
if (other instanceof MapValue) { if (other instanceof MapValue) {
MapValue<V> that = (MapValue) other; MapValue<V> that = (MapValue<V>) other;
return Objects.equal(this.timestamp, that.timestamp) && return Objects.equal(this.timestamp, that.timestamp) &&
Objects.equal(this.value, that.value); Objects.equal(this.value, that.value);
} }

View File

@ -22,7 +22,7 @@ import com.google.common.base.MoreObjects;
/** /**
* Describes a single update event in an EventuallyConsistentMap. * Describes a single update event in an EventuallyConsistentMap.
*/ */
final class UpdateEntry<K, V> implements Comparable<UpdateEntry<K, V>> { final class UpdateEntry<K, V> {
private final K key; private final K key;
private final MapValue<V> value; private final MapValue<V> value;
@ -55,9 +55,13 @@ final class UpdateEntry<K, V> implements Comparable<UpdateEntry<K, V>> {
return value; return value;
} }
@Override /**
public int compareTo(UpdateEntry<K, V> o) { * Returns if this entry is newer than other entry.
return this.value.timestamp().compareTo(o.value.timestamp()); * @param other other entry
* @return true if this entry is newer; false otherwise
*/
public boolean isNewerThan(UpdateEntry<K, V> other) {
return other == null || value.isNewerThan(other.value);
} }
@Override @Override

View File

@ -0,0 +1,42 @@
package org.onosproject.store.consistent.impl;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertNull;
import static junit.framework.TestCase.assertTrue;
import org.junit.Test;
/**
* Unit tests for Result.
*/
public class ResultTest {
@Test
public void testLocked() {
Result<String> r = Result.locked();
assertFalse(r.success());
assertNull(r.value());
assertEquals(Result.Status.LOCKED, r.status());
}
@Test
public void testOk() {
Result<String> r = Result.ok("foo");
assertTrue(r.success());
assertEquals("foo", r.value());
assertEquals(Result.Status.OK, r.status());
}
@Test
public void testEquality() {
Result<String> r1 = Result.ok("foo");
Result<String> r2 = Result.locked();
Result<String> r3 = Result.ok("bar");
Result<String> r4 = Result.ok("foo");
assertTrue(r1.equals(r4));
assertFalse(r1.equals(r2));
assertFalse(r1.equals(r3));
assertFalse(r2.equals(r3));
}
}