Unit test for default partition class
Also added javadocs to DefaultPartition constructors Change-Id: I4ba37800d61b82fabc60fd5a226850d3a3536913
Showing
2 changed files
with
112 additions
and
2 deletions
| ... | @@ -32,16 +32,30 @@ public class DefaultPartition implements Partition { | ... | @@ -32,16 +32,30 @@ public class DefaultPartition implements Partition { |
| 32 | private final PartitionId id; | 32 | private final PartitionId id; |
| 33 | private final Collection<NodeId> members; | 33 | private final Collection<NodeId> members; |
| 34 | 34 | ||
| 35 | - private DefaultPartition() { | 35 | + /** |
| 36 | + * Constructs an empty partition for the serializer. | ||
| 37 | + */ | ||
| 38 | + protected DefaultPartition() { | ||
| 36 | id = null; | 39 | id = null; |
| 37 | members = null; | 40 | members = null; |
| 38 | } | 41 | } |
| 39 | 42 | ||
| 43 | + /** | ||
| 44 | + * Constructs a partition. | ||
| 45 | + * | ||
| 46 | + * @param id partition identifier | ||
| 47 | + * @param members partition member nodes | ||
| 48 | + */ | ||
| 40 | public DefaultPartition(PartitionId id, Collection<NodeId> members) { | 49 | public DefaultPartition(PartitionId id, Collection<NodeId> members) { |
| 41 | this.id = checkNotNull(id); | 50 | this.id = checkNotNull(id); |
| 42 | this.members = ImmutableSet.copyOf(members); | 51 | this.members = ImmutableSet.copyOf(members); |
| 43 | } | 52 | } |
| 44 | 53 | ||
| 54 | + /** | ||
| 55 | + * Constructs a partition that is a copy of another. | ||
| 56 | + * | ||
| 57 | + * @param other partition to copy | ||
| 58 | + */ | ||
| 45 | public DefaultPartition(Partition other) { | 59 | public DefaultPartition(Partition other) { |
| 46 | this.id = checkNotNull(other.getId()); | 60 | this.id = checkNotNull(other.getId()); |
| 47 | this.members = ImmutableSet.copyOf(other.getMembers()); | 61 | this.members = ImmutableSet.copyOf(other.getMembers()); |
| ... | @@ -79,4 +93,4 @@ public class DefaultPartition implements Partition { | ... | @@ -79,4 +93,4 @@ public class DefaultPartition implements Partition { |
| 79 | return this.getId().equals(that.getId()) && | 93 | return this.getId().equals(that.getId()) && |
| 80 | Sets.symmetricDifference(Sets.newHashSet(this.members), Sets.newHashSet(that.members)).isEmpty(); | 94 | Sets.symmetricDifference(Sets.newHashSet(this.members), Sets.newHashSet(that.members)).isEmpty(); |
| 81 | } | 95 | } |
| 82 | -} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| 96 | +} | ... | ... |
| 1 | +/* | ||
| 2 | + * Copyright 2016 Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | +package org.onosproject.cluster; | ||
| 17 | + | ||
| 18 | +import java.util.Collection; | ||
| 19 | + | ||
| 20 | +import org.junit.Test; | ||
| 21 | + | ||
| 22 | +import com.google.common.collect.ImmutableSet; | ||
| 23 | +import com.google.common.testing.EqualsTester; | ||
| 24 | + | ||
| 25 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
| 26 | +import static org.hamcrest.Matchers.contains; | ||
| 27 | +import static org.hamcrest.Matchers.hasSize; | ||
| 28 | +import static org.hamcrest.Matchers.is; | ||
| 29 | +import static org.hamcrest.Matchers.notNullValue; | ||
| 30 | +import static org.hamcrest.Matchers.nullValue; | ||
| 31 | +import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass; | ||
| 32 | + | ||
| 33 | +/** | ||
| 34 | + * Unit tests for the default partition implementation. | ||
| 35 | + */ | ||
| 36 | +public class DefaultPartitionTest { | ||
| 37 | + | ||
| 38 | + NodeId id1 = new NodeId("1"); | ||
| 39 | + NodeId id2 = new NodeId("2"); | ||
| 40 | + NodeId id3 = new NodeId("3"); | ||
| 41 | + | ||
| 42 | + PartitionId pid1 = new PartitionId(1); | ||
| 43 | + PartitionId pid2 = new PartitionId(2); | ||
| 44 | + PartitionId pid3 = new PartitionId(3); | ||
| 45 | + | ||
| 46 | + DefaultPartition partition1 = new DefaultPartition(pid1, ImmutableSet.of(id1)); | ||
| 47 | + DefaultPartition sameAsPartition1 = new DefaultPartition(pid1, ImmutableSet.of(id1)); | ||
| 48 | + | ||
| 49 | + DefaultPartition partition2 = new DefaultPartition(pid2, ImmutableSet.of(id2)); | ||
| 50 | + DefaultPartition copyOfPartition2 = new DefaultPartition(partition2); | ||
| 51 | + | ||
| 52 | + DefaultPartition partition3 = new DefaultPartition(pid3, ImmutableSet.of(id1, id2, id3)); | ||
| 53 | + | ||
| 54 | + /** | ||
| 55 | + * Checks that the default partition implementation is an immutable | ||
| 56 | + * base class. | ||
| 57 | + */ | ||
| 58 | + @Test | ||
| 59 | + public void checkImmutability() { | ||
| 60 | + assertThatClassIsImmutableBaseClass(DefaultPartition.class); | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + /** | ||
| 64 | + * Tests operation of the equals(), hashCode(), and toString() methods. | ||
| 65 | + */ | ||
| 66 | + @Test | ||
| 67 | + public void testEquals() { | ||
| 68 | + new EqualsTester() | ||
| 69 | + .addEqualityGroup(partition1, sameAsPartition1) | ||
| 70 | + .addEqualityGroup(partition2, copyOfPartition2) | ||
| 71 | + .addEqualityGroup(partition3) | ||
| 72 | + .testEquals(); | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | + /** | ||
| 76 | + * Tests that default partition objects are properly constructed. | ||
| 77 | + */ | ||
| 78 | + @Test | ||
| 79 | + public void testConstruction() { | ||
| 80 | + Collection<NodeId> members = partition3.getMembers(); | ||
| 81 | + assertThat(members, notNullValue()); | ||
| 82 | + assertThat(members, hasSize(3)); | ||
| 83 | + assertThat(members, contains(id1, id2, id3)); | ||
| 84 | + assertThat(partition3.getId(), is(pid3)); | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + /** | ||
| 88 | + * Tests the empty defaut partition constructor. | ||
| 89 | + */ | ||
| 90 | + @Test | ||
| 91 | + public void testEmptyConstructor() { | ||
| 92 | + DefaultPartition empty = new DefaultPartition(); | ||
| 93 | + assertThat(empty.getId(), nullValue()); | ||
| 94 | + assertThat(empty.getMembers(), nullValue()); | ||
| 95 | + } | ||
| 96 | +} |
-
Please register or login to post a comment