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

Property equality fix #344

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 25 additions & 0 deletions DaoCore/src/main/java/de/greenrobot/dao/Property.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,29 @@ public WhereCondition isNotNull() {
return new PropertyCondition(this, " IS NOT NULL");
}

@Override
public boolean equals(Object object) {
if (this == object) {
return true;
}
if(object instanceof Property) {
Property property = (Property)object;
return ordinal == property.ordinal
&& type.equals(property.type)
&& name.equals(property.name)
&& primaryKey == property.primaryKey
&& columnName.equals(property.columnName);
}
return false;
}

@Override
public int hashCode() {
return ordinal
^ type.hashCode()
^ name.hashCode()
^ (primaryKey ? 1:0)
^ columnName.hashCode();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void checkProperty(Property property) {
Property[] properties = dao.getProperties();
boolean found = false;
for (Property property2 : properties) {
if (property == property2) {
if (property.equals(property2)) {
found = true;
break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package de.greenrobot.dao.query;

import de.greenrobot.dao.unittest.*;

import android.database.sqlite.SQLiteDatabase;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;

import de.greenrobot.dao.Property;
import de.greenrobot.dao.query.WhereCollector;
import de.greenrobot.daotest.dummyapp.BuildConfig;

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 16)
public class WhereCollectorTest {

private DaoSession daoSession;
private MinimalEntityDao minimalEntityDao;

@Before
public void setUp() {
DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(RuntimeEnvironment.application, null, null);
SQLiteDatabase db = openHelper.getWritableDatabase();
daoSession = new DaoMaster(db).newSession();
minimalEntityDao = daoSession.getMinimalEntityDao();
}

@Test
public void testCheckProperty() {
WhereCollector whereCollector = new WhereCollector(minimalEntityDao, "T");
final Property Id2 = new Property(0, Long.class, "id", true, "_id");

whereCollector.checkProperty(Id2);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import java.util.concurrent.CountDownLatch;

import de.greenrobot.dao.Property;
import de.greenrobot.dao.query.Query;
import de.greenrobot.daotest.dummyapp.BuildConfig;

Expand Down Expand Up @@ -47,6 +48,12 @@ public void testBasics() {
assertNull(minimalEntityDao.load(entity.getId()));
}

@Test
public void testQueryBuilderEquality() {
final Property Id2 = new Property(0, Long.class, "id", true, "_id");
minimalEntityDao.queryBuilder().where(Id2.eq(0)).list();
}

@Test
// Testing the work around for Process.myTid() being always 0 in Robolectric
public void testQueryForCurrentThread() throws InterruptedException {
Expand Down
41 changes: 41 additions & 0 deletions DaoTest/src/test/java/de/greenrobot/dao/unittest/PropertyTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package de.greenrobot.dao.unittest;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;

import de.greenrobot.dao.Property;
import de.greenrobot.daotest.dummyapp.BuildConfig;

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 16)
public class PropertyTest {

@Test
public void testEquality() {
final Property a1Property = new Property(0, Long.class, "a", true, "_a");
final Property a2Property = new Property(0, Long.class, "a", true, "_a");

final Property bProperty = new Property(1, Long.class, "a", true, "_a");
final Property cProperty = new Property(0, Integer.class, "a", true, "_a");
final Property dProperty = new Property(0, Long.class, "b", true, "_a");
final Property eProperty = new Property(0, Long.class, "a", false, "_a");
final Property fProperty = new Property(0, Long.class, "a", true, "_b");

assertEquals(a1Property, a2Property);
assertNotSame(a1Property, a2Property);

assertFalse(a1Property.equals(bProperty));
assertFalse(a1Property.equals(cProperty));
assertFalse(a1Property.equals(dProperty));
assertFalse(a1Property.equals(eProperty));
assertFalse(a1Property.equals(fProperty));
}


}