Committed by
Gerrit Code Review
More Unit tests
Change-Id: I32dd3851e490979621c4a5205c6e041dee900244
Showing
6 changed files
with
85 additions
and
7 deletions
... | @@ -108,12 +108,13 @@ public final class Match<T> { | ... | @@ -108,12 +108,13 @@ public final class Match<T> { |
108 | return Objects.hash(matchAny, value); | 108 | return Objects.hash(matchAny, value); |
109 | } | 109 | } |
110 | 110 | ||
111 | + @SuppressWarnings("unchecked") | ||
111 | @Override | 112 | @Override |
112 | public boolean equals(Object other) { | 113 | public boolean equals(Object other) { |
113 | if (!(other instanceof Match)) { | 114 | if (!(other instanceof Match)) { |
114 | return false; | 115 | return false; |
115 | } | 116 | } |
116 | - Match<T> that = (Match) other; | 117 | + Match<T> that = (Match<T>) other; |
117 | return Objects.equals(this.matchAny, that.matchAny) && | 118 | return Objects.equals(this.matchAny, that.matchAny) && |
118 | Objects.equals(this.value, that.value); | 119 | Objects.equals(this.value, that.value); |
119 | } | 120 | } | ... | ... |
... | @@ -15,6 +15,10 @@ | ... | @@ -15,6 +15,10 @@ |
15 | */ | 15 | */ |
16 | package org.onosproject.store.consistent.impl; | 16 | package org.onosproject.store.consistent.impl; |
17 | 17 | ||
18 | +import static com.google.common.base.MoreObjects.toStringHelper; | ||
19 | + | ||
20 | +import java.util.Objects; | ||
21 | + | ||
18 | /** | 22 | /** |
19 | * Result of a database update operation. | 23 | * Result of a database update operation. |
20 | * | 24 | * |
... | @@ -74,6 +78,7 @@ public final class Result<V> { | ... | @@ -74,6 +78,7 @@ public final class Result<V> { |
74 | 78 | ||
75 | /** | 79 | /** |
76 | * Returns the status of database update operation. | 80 | * Returns the status of database update operation. |
81 | + * | ||
77 | * @return database update status | 82 | * @return database update status |
78 | */ | 83 | */ |
79 | public Status status() { | 84 | public Status status() { |
... | @@ -82,10 +87,35 @@ public final class Result<V> { | ... | @@ -82,10 +87,35 @@ public final class Result<V> { |
82 | 87 | ||
83 | /** | 88 | /** |
84 | * Returns the return value for the update. | 89 | * Returns the return value for the update. |
90 | + * | ||
85 | * @return value returned by database update. If the status is another | 91 | * @return value returned by database update. If the status is another |
86 | * other than Status.OK, this returns a null | 92 | * other than Status.OK, this returns a null |
87 | */ | 93 | */ |
88 | public V value() { | 94 | public V value() { |
89 | return value; | 95 | return value; |
90 | } | 96 | } |
97 | + | ||
98 | + @Override | ||
99 | + public int hashCode() { | ||
100 | + return Objects.hash(value, status); | ||
101 | + } | ||
102 | + | ||
103 | + @SuppressWarnings("unchecked") | ||
104 | + @Override | ||
105 | + public boolean equals(Object other) { | ||
106 | + if (!(other instanceof Result)) { | ||
107 | + return false; | ||
108 | + } | ||
109 | + Result<V> that = (Result<V>) other; | ||
110 | + return Objects.equals(this.value, that.value) && | ||
111 | + Objects.equals(this.status, that.status); | ||
112 | + } | ||
113 | + | ||
114 | + @Override | ||
115 | + public String toString() { | ||
116 | + return toStringHelper(this) | ||
117 | + .add("status", status) | ||
118 | + .add("value", value) | ||
119 | + .toString(); | ||
120 | + } | ||
91 | } | 121 | } |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
... | @@ -653,7 +653,7 @@ public class EventuallyConsistentMapImpl<K, V> | ... | @@ -653,7 +653,7 @@ public class EventuallyConsistentMapImpl<K, V> |
653 | public void processItems(List<UpdateEntry<K, V>> items) { | 653 | public void processItems(List<UpdateEntry<K, V>> items) { |
654 | Map<K, UpdateEntry<K, V>> map = Maps.newHashMap(); | 654 | Map<K, UpdateEntry<K, V>> map = Maps.newHashMap(); |
655 | items.forEach(item -> map.compute(item.key(), (key, existing) -> | 655 | items.forEach(item -> map.compute(item.key(), (key, existing) -> |
656 | - existing == null || item.compareTo(existing) > 0 ? item : existing)); | 656 | + item.isNewerThan(existing) ? item : existing)); |
657 | communicationExecutor.submit(() -> { | 657 | communicationExecutor.submit(() -> { |
658 | clusterCommunicator.unicast(ImmutableList.copyOf(map.values()), | 658 | clusterCommunicator.unicast(ImmutableList.copyOf(map.values()), |
659 | updateMessageSubject, | 659 | updateMessageSubject, | ... | ... |
... | @@ -83,10 +83,11 @@ public class MapValue<V> implements Comparable<MapValue<V>> { | ... | @@ -83,10 +83,11 @@ public class MapValue<V> implements Comparable<MapValue<V>> { |
83 | return Objects.hashCode(timestamp, value); | 83 | return Objects.hashCode(timestamp, value); |
84 | } | 84 | } |
85 | 85 | ||
86 | + @SuppressWarnings("unchecked") | ||
86 | @Override | 87 | @Override |
87 | public boolean equals(Object other) { | 88 | public boolean equals(Object other) { |
88 | if (other instanceof MapValue) { | 89 | if (other instanceof MapValue) { |
89 | - MapValue<V> that = (MapValue) other; | 90 | + MapValue<V> that = (MapValue<V>) other; |
90 | return Objects.equal(this.timestamp, that.timestamp) && | 91 | return Objects.equal(this.timestamp, that.timestamp) && |
91 | Objects.equal(this.value, that.value); | 92 | Objects.equal(this.value, that.value); |
92 | } | 93 | } | ... | ... |
... | @@ -22,7 +22,7 @@ import com.google.common.base.MoreObjects; | ... | @@ -22,7 +22,7 @@ import com.google.common.base.MoreObjects; |
22 | /** | 22 | /** |
23 | * Describes a single update event in an EventuallyConsistentMap. | 23 | * Describes a single update event in an EventuallyConsistentMap. |
24 | */ | 24 | */ |
25 | -final class UpdateEntry<K, V> implements Comparable<UpdateEntry<K, V>> { | 25 | +final class UpdateEntry<K, V> { |
26 | private final K key; | 26 | private final K key; |
27 | private final MapValue<V> value; | 27 | private final MapValue<V> value; |
28 | 28 | ||
... | @@ -55,9 +55,13 @@ final class UpdateEntry<K, V> implements Comparable<UpdateEntry<K, V>> { | ... | @@ -55,9 +55,13 @@ final class UpdateEntry<K, V> implements Comparable<UpdateEntry<K, V>> { |
55 | return value; | 55 | return value; |
56 | } | 56 | } |
57 | 57 | ||
58 | - @Override | 58 | + /** |
59 | - public int compareTo(UpdateEntry<K, V> o) { | 59 | + * Returns if this entry is newer than other entry. |
60 | - return this.value.timestamp().compareTo(o.value.timestamp()); | 60 | + * @param other other entry |
61 | + * @return true if this entry is newer; false otherwise | ||
62 | + */ | ||
63 | + public boolean isNewerThan(UpdateEntry<K, V> other) { | ||
64 | + return other == null || value.isNewerThan(other.value); | ||
61 | } | 65 | } |
62 | 66 | ||
63 | @Override | 67 | @Override | ... | ... |
1 | +package org.onosproject.store.consistent.impl; | ||
2 | + | ||
3 | +import static junit.framework.TestCase.assertEquals; | ||
4 | +import static junit.framework.TestCase.assertFalse; | ||
5 | +import static junit.framework.TestCase.assertNull; | ||
6 | +import static junit.framework.TestCase.assertTrue; | ||
7 | + | ||
8 | +import org.junit.Test; | ||
9 | + | ||
10 | +/** | ||
11 | + * Unit tests for Result. | ||
12 | + */ | ||
13 | +public class ResultTest { | ||
14 | + | ||
15 | + @Test | ||
16 | + public void testLocked() { | ||
17 | + Result<String> r = Result.locked(); | ||
18 | + assertFalse(r.success()); | ||
19 | + assertNull(r.value()); | ||
20 | + assertEquals(Result.Status.LOCKED, r.status()); | ||
21 | + } | ||
22 | + | ||
23 | + @Test | ||
24 | + public void testOk() { | ||
25 | + Result<String> r = Result.ok("foo"); | ||
26 | + assertTrue(r.success()); | ||
27 | + assertEquals("foo", r.value()); | ||
28 | + assertEquals(Result.Status.OK, r.status()); | ||
29 | + } | ||
30 | + | ||
31 | + @Test | ||
32 | + public void testEquality() { | ||
33 | + Result<String> r1 = Result.ok("foo"); | ||
34 | + Result<String> r2 = Result.locked(); | ||
35 | + Result<String> r3 = Result.ok("bar"); | ||
36 | + Result<String> r4 = Result.ok("foo"); | ||
37 | + assertTrue(r1.equals(r4)); | ||
38 | + assertFalse(r1.equals(r2)); | ||
39 | + assertFalse(r1.equals(r3)); | ||
40 | + assertFalse(r2.equals(r3)); | ||
41 | + } | ||
42 | +} |
-
Please register or login to post a comment