You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by vy...@apache.org on 2022/01/09 21:58:24 UTC

[logging-log4j2] branch master updated: Expand SortedArrayStringMap test suite for equals/hashcode/serialization. (#688)

This is an automated email from the ASF dual-hosted git repository.

vy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/master by this push:
     new 76dbbf6  Expand SortedArrayStringMap test suite for equals/hashcode/serialization. (#688)
76dbbf6 is described below

commit 76dbbf6c1522eaeef461af6ef68e044e52b9f467
Author: Arie van Deursen <av...@users.noreply.github.com>
AuthorDate: Sun Jan 9 22:55:57 2022 +0100

    Expand SortedArrayStringMap test suite for equals/hashcode/serialization. (#688)
    
    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.
---
 .../log4j/util/SortedArrayStringMapTest.java       | 25 ++++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)

diff --git a/log4j-api-test/src/test/java/org/apache/logging/log4j/util/SortedArrayStringMapTest.java b/log4j-api-test/src/test/java/org/apache/logging/log4j/util/SortedArrayStringMapTest.java
index cf5c852..6e1d9dc 100644
--- a/log4j-api-test/src/test/java/org/apache/logging/log4j/util/SortedArrayStringMapTest.java
+++ b/log4j-api-test/src/test/java/org/apache/logging/log4j/util/SortedArrayStringMapTest.java
@@ -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);
@@ -81,6 +81,14 @@ public class SortedArrayStringMapTest {
     }
 
     @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();
         original.putValue("a", "avalue");
@@ -393,12 +401,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 +420,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