Fix Address.equals

This commit is contained in:
Philip Whitehouse 2017-01-22 13:22:41 +00:00
parent e238ee543f
commit c8eaba861c
2 changed files with 56 additions and 2 deletions

View file

@ -161,14 +161,24 @@ public class Address implements Serializable {
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (o == this)
return true;
if (o instanceof Address) {
Address other = (Address) o;
if (mPersonal != null && other.mPersonal != null && !mPersonal.equals(other.mPersonal)) {
if (mPersonal == null && other.mPersonal != null) {
return false;
} else if (!mPersonal.equals(other.mPersonal)) {
return false;
}
if (mAddress == null) {
return other.mAddress == null;
}
return mAddress.equals(other.mAddress);
}
return super.equals(o);
return false;
}
@Override

View file

@ -7,7 +7,9 @@ import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
@RunWith(RobolectricTestRunner.class)
@ -106,4 +108,46 @@ public class AddressTest {
address.hashCode();
}
@Test
public void equals_withoutAddress_matchesSame() throws Exception {
Address address = Address.parse("name only")[0];
Address address2 = Address.parse("name only")[0];
assertNull(address.getAddress());
boolean result = address.equals(address2);
assertTrue(result);
}
@Test
public void equals_withoutAddress_doesNotMatchWithAddress() throws Exception {
Address address = Address.parse("name only")[0];
Address address2 = Address.parse("name <alice.example.com>")[0];
boolean result = address.equals(address2);
assertFalse(result);
}
@Test
public void equals_withoutPersonal_matchesSame() throws Exception {
Address address = Address.parse("alice@example.org")[0];
Address address2 = Address.parse("alice@example.org")[0];
assertNull(address.getPersonal());
boolean result = address.equals(address2);
assertTrue(result);
}
@Test
public void equals_withoutPersonal_doesNotMatchWithAddress() throws Exception {
Address address = Address.parse("alice@example.org")[0];
Address address2 = Address.parse("Alice <alice@example.org>")[0];
boolean result = address.equals(address2);
assertFalse(result);
}
}