From 9a0c2226c1bc24bcd7f6affdf17722331d02120d Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Wed, 8 Mar 2023 12:57:24 +0100 Subject: [PATCH] fix hashCode and equals in account --- .../android/database/model/Account.java | 36 ++++++++++--------- .../android/xmpp/AccountTest.java | 22 ++++++++++++ 2 files changed, 41 insertions(+), 17 deletions(-) create mode 100644 app/src/test/java/im/conversations/android/xmpp/AccountTest.java diff --git a/app/src/main/java/im/conversations/android/database/model/Account.java b/app/src/main/java/im/conversations/android/database/model/Account.java index ad0c38ba6..b0f1a3bb0 100644 --- a/app/src/main/java/im/conversations/android/database/model/Account.java +++ b/app/src/main/java/im/conversations/android/database/model/Account.java @@ -1,7 +1,6 @@ package im.conversations.android.database.model; import androidx.annotation.NonNull; -import com.google.common.base.Objects; import com.google.common.base.Preconditions; import com.google.common.hash.Hashing; import com.google.common.io.BaseEncoding; @@ -10,11 +9,12 @@ import com.google.common.primitives.Ints; import im.conversations.android.IDs; import java.io.IOException; import java.util.Arrays; +import java.util.Objects; import java.util.UUID; import org.jxmpp.jid.BareJid; import org.jxmpp.jid.parts.Resourcepart; -public class Account { +public final class Account { public final long id; @NonNull public final BareJid address; @@ -30,21 +30,6 @@ public class Account { this.randomSeed = randomSeed; } - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - if (!super.equals(o)) return false; - Account account = (Account) o; - return Arrays.equals(randomSeed, account.randomSeed); - } - - @Override - public int hashCode() { - // careful with hashCode and equals for byte arrays - return Objects.hashCode(super.hashCode(), Arrays.hashCode(randomSeed)); - } - public boolean isOnion() { final String domain = address.getDomain().toString(); return domain.endsWith(".onion"); @@ -85,4 +70,21 @@ public class Account { throw new RuntimeException(e); } } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Account account = (Account) o; + return id == account.id + && address.equals(account.address) + && Arrays.equals(randomSeed, account.randomSeed); + } + + @Override + public int hashCode() { + int result = Objects.hash(id, address); + result = 31 * result + Arrays.hashCode(randomSeed); + return result; + } } diff --git a/app/src/test/java/im/conversations/android/xmpp/AccountTest.java b/app/src/test/java/im/conversations/android/xmpp/AccountTest.java new file mode 100644 index 000000000..0e71768ba --- /dev/null +++ b/app/src/test/java/im/conversations/android/xmpp/AccountTest.java @@ -0,0 +1,22 @@ +package im.conversations.android.xmpp; + +import im.conversations.android.IDs; +import im.conversations.android.database.model.Account; +import org.junit.Assert; +import org.junit.Test; +import org.jxmpp.jid.impl.JidCreate; +import org.jxmpp.stringprep.XmppStringprepException; + +public class AccountTest { + + @Test + public void testEquals() throws XmppStringprepException { + final var seed = IDs.seed(); + final var accountOne = new Account(1L, JidCreate.bareFrom("test@example.com"), seed); + final var seedCopy = new byte[seed.length]; + System.arraycopy(seed, 0, seedCopy, 0, seedCopy.length); + final var accountTwo = new Account(1L, JidCreate.bareFrom("test@example.com"), seedCopy); + Assert.assertEquals(accountOne, accountTwo); + Assert.assertEquals(accountOne.hashCode(), accountTwo.hashCode()); + } +}