From 3ae2f6d620a894c93e509b03990c2d4ba9de7f7a Mon Sep 17 00:00:00 2001
From: Neil Wilson
Date: Wed, 26 Oct 2022 17:10:52 -0500
Subject: [PATCH] Fix a bug in SearchResultEntry.equals
Fixed a bug in the SearchResultEntry.equals method that could
prevent a SearchResultEntry object (which is a subclass of Entry)
from being considered equal to an Entry that is not a
SearchResultEntry.
---
docs/release-notes.html | 7 +++
.../unboundid/ldap/sdk/SearchResultEntry.java | 14 +++---
.../ldap/sdk/SearchResultEntryTestCase.java | 44 ++++++++++++++++---
3 files changed, 49 insertions(+), 16 deletions(-)
diff --git a/docs/release-notes.html b/docs/release-notes.html
index 192ef16a7..4061bfad5 100644
--- a/docs/release-notes.html
+++ b/docs/release-notes.html
@@ -13,6 +13,13 @@ Version 6.0.7
+ -
+ Fixed a bug in the SearchResultEntry.equals method that could prevent a
+ SearchResultEntry object (which is a subclass of Entry) from being considered
+ equal to an Entry that is not a SearchResultEntry.
+
+
+
-
Fixed an issue with the Entry.applyModifications method in which it could fail
with a NOT_ALLOWED_ON_RDN result if the provided entry was missing one or more
diff --git a/src/com/unboundid/ldap/sdk/SearchResultEntry.java b/src/com/unboundid/ldap/sdk/SearchResultEntry.java
index 3a7e59d9b..d6a22eb38 100644
--- a/src/com/unboundid/ldap/sdk/SearchResultEntry.java
+++ b/src/com/unboundid/ldap/sdk/SearchResultEntry.java
@@ -392,14 +392,8 @@ public Control getControl(@NotNull final String oid)
@Override()
public int hashCode()
{
- int hashCode = super.hashCode();
-
- for (final Control c : controls)
- {
- hashCode += c.hashCode();
- }
-
- return hashCode;
+ // We need this method to satisfy checkstyle.
+ return super.hashCode();
}
@@ -424,7 +418,9 @@ public boolean equals(@Nullable final Object o)
if (! (o instanceof SearchResultEntry))
{
- return false;
+ // When comparing a SearchResultEntry with an entry that isn't a
+ // SearchResultEntry, we'll only rely on the base entry comparison.
+ return true;
}
final SearchResultEntry e = (SearchResultEntry) o;
diff --git a/tests/unit/src/com/unboundid/ldap/sdk/SearchResultEntryTestCase.java b/tests/unit/src/com/unboundid/ldap/sdk/SearchResultEntryTestCase.java
index 3929c7af3..d5a23f05b 100644
--- a/tests/unit/src/com/unboundid/ldap/sdk/SearchResultEntryTestCase.java
+++ b/tests/unit/src/com/unboundid/ldap/sdk/SearchResultEntryTestCase.java
@@ -739,8 +739,7 @@ public void testEqualsIdentity()
/**
- * Tests the {@code equals} method with an object that isn't a search result
- * entry.
+ * Tests the {@code equals} method with an object that isn't an entry.
*/
@Test()
public void testEqualsNotEntry()
@@ -761,10 +760,41 @@ public void testEqualsNotEntry()
SearchResultEntry e = new SearchResultEntry(dn, attributes, controls);
+ final String nonEntry = "foo";
+ assertFalse(e.hashCode() == nonEntry.hashCode());
+ assertFalse(nonEntry.equals(e));
+ assertFalse(e.equals(nonEntry));
+ }
+
+
+
+ /**
+ * Tests the {@code equals} method with an object that is an equivalent entry
+ * that is not a search result entry.
+ */
+ @Test()
+ public void testEqualsEquivalentEntry()
+ {
+ String dn = "dc=example,dc=com";
+
+ Attribute[] attributes =
+ {
+ new Attribute("objectClass", "top", "domain"),
+ new Attribute("dc", "example"),
+ };
+
+ Control[] controls =
+ {
+ new Control("1.2.3.4"),
+ new Control("1.2.3.5", true, null)
+ };
+
+ SearchResultEntry e = new SearchResultEntry(dn, attributes, controls);
+
Entry e2 = new Entry(dn, attributes);
- assertFalse(e.hashCode() == e2.hashCode());
+ assertEquals(e.hashCode(), e2.hashCode());
+ assertTrue(e.equals(e2));
assertTrue(e2.equals(e));
- assertFalse(e.equals(e2));
}
@@ -774,7 +804,7 @@ public void testEqualsNotEntry()
* result entry.
*/
@Test()
- public void testEqualsEquivalentEntry()
+ public void testEqualsEquivalentSearchResultEntry()
{
String dn = "dc=example,dc=com";
@@ -825,7 +855,7 @@ public void testEqualsDifferentControlCount()
SearchResultEntry e2 = new SearchResultEntry(dn, attributes,
new Control[0]);
- assertFalse(e.hashCode() == e2.hashCode());
+ assertEquals(e.hashCode(), e2.hashCode());
assertFalse(e.equals(e2));
assertFalse(e2.equals(e));
}
@@ -864,7 +894,7 @@ public void testEqualsDifferentControlContent()
};
SearchResultEntry e2 = new SearchResultEntry(dn, attributes, controls2);
- assertFalse(e.hashCode() == e2.hashCode());
+ assertEquals(e.hashCode(), e2.hashCode());
assertFalse(e.equals(e2));
assertFalse(e2.equals(e));
}