Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Equals() should be type safe, null-aware and hashcode() should be overridden #104

Merged
merged 3 commits into from
Mar 24, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/com/activeandroid/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@

@SuppressWarnings("unchecked")
public abstract class Model {

/** Prime number used for hashcode() implementation. */
private static final int HASH_PRIME = 739;

//////////////////////////////////////////////////////////////////////////////////////
// PRIVATE MEMBERS
//////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -287,9 +291,21 @@ public String toString() {

@Override
public boolean equals(Object obj) {
final Model other = (Model) obj;
if (obj instanceof Model && this.mId != null) {
final Model other = (Model) obj;

return this.mId.equals(other.mId)
&& (this.mTableInfo.getTableName().equals(other.mTableInfo.getTableName()));
} else {
return this == obj;
}
}

return this.mId != null && (this.mTableInfo.getTableName().equals(other.mTableInfo.getTableName()))
&& (this.mId.equals(other.mId));
@Override
public int hashCode() {
int hash = HASH_PRIME;
hash += HASH_PRIME * (mId == null ? super.hashCode() : mId.hashCode()); //if id is null, use Object.hashCode()
hash += HASH_PRIME * mTableInfo.getTableName().hashCode();
return hash; //To change body of generated methods, choose Tools | Templates.
}
}
129 changes: 129 additions & 0 deletions tests/src/com/activeandroid/test/ModelTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* Copyright (C) 2013 Vojtech Sigler.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.activeandroid.test;

import com.activeandroid.Model;
import com.activeandroid.annotation.Table;
import java.util.HashSet;
import java.util.Set;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;

/**
* Simple test now covering equals and hashcode methods.
*/
public class ModelTest extends ActiveAndroidTestCase {

/**
* Equals should be type-safe.
*/
public void testEqualsNonModel() {
MockModel model = new MockModel();

assertFalse(model.equals("Dummy"));
assertFalse(model.equals(null));
}

/**
* Equals should not be true for different model classes.
*/
public void testEqualsDifferentModel() {
Model model1 = new MockModel();
Model model2 = new AnotherMockModel();

assertFalse(model1.equals(model2));
}

/**
* A new object does not have PK assigned yet,
* therefore by default it is equal only to itself.
*/
public void testEqualsOnNew() {
MockModel model1 = new MockModel();
MockModel model2 = new MockModel();

assertFalse(model1.equals(model2));
assertFalse(model2.equals(model1));
assertTrue(model1.equals(model1));//equal only to itself
}

/**
* Two different rows in a table should not be equal (different ids).
*/
public void testEqualsDifferentRows() {
MockModel model1 = new MockModel();
MockModel model2 = new MockModel();
MockModel model3;

model1.save();
model2.save();
model3 = Model.load(MockModel.class, model1.getId());

assertFalse(model1.equals(model2));
assertFalse(model2.equals(model1));
assertTrue(model1.equals(model3));
assertTrue(model1.equals(model3));
assertFalse(model3.equals(model2));
assertFalse(model2.equals(model3));
}

/**
* Tests hashcode for new instances.
*/
public void testHashCode() {
Set<Model> set = new HashSet<Model>();
Model m1 = new MockModel();
Model m2 = new MockModel();
Model m3 = new AnotherMockModel();

assertFalse(m1.hashCode() == m2.hashCode()); // hashes for unsaved models must not match
set.add(m1);
set.add(m2);
assertEquals(2, set.size()); //try in a set

assertFalse(m1.hashCode() == m3.hashCode());
set.add(m3);
assertEquals(3, set.size());
}

/**
* Two rows in a table should have different hashcodes.
*/
public void testHashCodeDifferentRows() {
Set<Model> set = new HashSet<Model>();
Model m1 = new MockModel();
Model m2 = new MockModel();
Model m3;

m1.save();
m2.save();
m3 = Model.load(MockModel.class, m1.getId());

assertEquals(m1.hashCode(), m3.hashCode());
assertFalse(m1.hashCode() == m2.hashCode());
set.add(m1);
set.add(m2);
set.add(m3);
assertEquals(2, set.size());
}

/**
* Mock model as we need 2 different model classes.
*/
@Table(name = "AnotherMockTable")
public static class AnotherMockModel extends Model {}
}