Expand SortedArrayStringMap test suite for equals/hashcode/serialization.
Some corner cases of the equals and serialization methods of the
SortedArrayStringMap class were not (unit) tested,
and its hashCode method was not (unit) tested at all.
This commit provides small changes to the test suite to address this.
@@ -72,7 +72,7 @@ public class SortedArrayStringMapTest {
public void testSerialization() throws Exception {
final SortedArrayStringMap original = new SortedArrayStringMap();
original.putValue("a", "avalue");
- original.putValue("B", "Bvalue");
+ original.putValue("B", null); // null may be treated differently
original.putValue("3", "3value");
final byte[] binary = serialize(original);
@@ -80,6 +80,14 @@ public class SortedArrayStringMapTest {
assertEquals(original, copy);
}
+ @Test
+ public void testSerializationOfEmptyMap() throws Exception {
+ final SortedArrayStringMap original = new SortedArrayStringMap();
+ final byte[] binary = serialize(original);
+ final SortedArrayStringMap copy = deserialize(binary);
+ assertEquals(original, copy);
+ }
+
@Test
public void testSerializationOfNonSerializableValue() throws Exception {
final SortedArrayStringMap original = new SortedArrayStringMap();
@@ -170,8 +178,7 @@ public class SortedArrayStringMapTest {
private SortedArrayStringMap deserialize(final byte[] binary) throws IOException, ClassNotFoundException {
final ByteArrayInputStream inArr = new ByteArrayInputStream(binary);
try (final ObjectInputStream in = new FilteredObjectInputStream(inArr)) {
- final SortedArrayStringMap result = (SortedArrayStringMap) in.readObject();
- return result;
+ return (SortedArrayStringMap) in.readObject();
}
}
@@ -393,12 +400,17 @@ public class SortedArrayStringMapTest {
@Test
public void testEquals() {
final SortedArrayStringMap original = new SortedArrayStringMap();
+ final SortedArrayStringMap other = new SortedArrayStringMap();
+
+ assertEquals(other, original, "Empty maps are equal");
+ assertEquals(other.hashCode(), original.hashCode(), "Empty maps have equal hashcode");
+ assertNotEquals(original, "Object other than SortedArrayStringMap");
+
original.putValue("a", "avalue");
original.putValue("B", "Bvalue");
original.putValue("3", "3value");
assertEquals(original, original); // equal to itself
- final SortedArrayStringMap other = new SortedArrayStringMap();
other.putValue("a", "avalue");
assertNotEquals(original, other);
@@ -407,15 +419,23 @@ public class SortedArrayStringMapTest {
other.putValue("3", "3value");
assertEquals(original, other);
+ assertEquals(original.hashCode(), other.hashCode());
other.putValue("3", "otherValue");
assertNotEquals(original, other);
other.putValue("3", null);
assertNotEquals(original, other);
+ assertNotEquals(original.hashCode(), other.hashCode());
other.putValue("3", "3value");
assertEquals(original, other);
+ assertEquals(other, original); // symmetry
+
+ original.putValue("key not in other", "4value");
+ other.putValue("key not in original", "4value");
+ assertNotEquals(original, other);
+ assertNotEquals(other, original); // symmetry
}
@Test
