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:56:02 UTC

[logging-log4j2] branch release-2.x 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 release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/release-2.x by this push:
     new 4c49101  Expand SortedArrayStringMap test suite for equals/hashcode/serialization. (#688)
4c49101 is described below

commit 4c491018a3d2e6f09eb8d11587cf24379d2051ce
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       | 28 ++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)

diff --git a/log4j-api/src/test/java/org/apache/logging/log4j/util/SortedArrayStringMapTest.java b/log4j-api/src/test/java/org/apache/logging/log4j/util/SortedArrayStringMapTest.java
index 82db1a1..d845a6f 100644
--- a/log4j-api/src/test/java/org/apache/logging/log4j/util/SortedArrayStringMapTest.java
+++ b/log4j-api/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");
@@ -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