From 0adfd9d54284889e47b0dc6e5dab56a9a209e6ca Mon Sep 17 00:00:00 2001 From: Emmet McPoland Date: Tue, 24 May 2016 18:23:29 +0100 Subject: [PATCH 1/2] applied property equality fix --- .../main/java/de/greenrobot/dao/Property.java | 25 +++++++++++++++++++ .../greenrobot/dao/query/WhereCollector.java | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/DaoCore/src/main/java/de/greenrobot/dao/Property.java b/DaoCore/src/main/java/de/greenrobot/dao/Property.java index 77923eb71..f6cc7914d 100644 --- a/DaoCore/src/main/java/de/greenrobot/dao/Property.java +++ b/DaoCore/src/main/java/de/greenrobot/dao/Property.java @@ -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(); + } + } diff --git a/DaoCore/src/main/java/de/greenrobot/dao/query/WhereCollector.java b/DaoCore/src/main/java/de/greenrobot/dao/query/WhereCollector.java index 21022d5e7..b93b74d50 100644 --- a/DaoCore/src/main/java/de/greenrobot/dao/query/WhereCollector.java +++ b/DaoCore/src/main/java/de/greenrobot/dao/query/WhereCollector.java @@ -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; } From 0559c09456442208b49831b8778c85b3b00a75f5 Mon Sep 17 00:00:00 2001 From: Emmet McPoland Date: Tue, 24 May 2016 23:20:25 +0100 Subject: [PATCH 2/2] Added test for property equality --- .../dao/query/WhereCollectorTest.java | 41 +++++++++++++++++++ .../dao/unittest/MinimalEntityTest.java | 7 ++++ .../greenrobot/dao/unittest/PropertyTest.java | 41 +++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 DaoTest/src/test/java/de/greenrobot/dao/query/WhereCollectorTest.java create mode 100644 DaoTest/src/test/java/de/greenrobot/dao/unittest/PropertyTest.java diff --git a/DaoTest/src/test/java/de/greenrobot/dao/query/WhereCollectorTest.java b/DaoTest/src/test/java/de/greenrobot/dao/query/WhereCollectorTest.java new file mode 100644 index 000000000..297238028 --- /dev/null +++ b/DaoTest/src/test/java/de/greenrobot/dao/query/WhereCollectorTest.java @@ -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); + } + +} diff --git a/DaoTest/src/test/java/de/greenrobot/dao/unittest/MinimalEntityTest.java b/DaoTest/src/test/java/de/greenrobot/dao/unittest/MinimalEntityTest.java index 702ba039e..1a64e9b0a 100644 --- a/DaoTest/src/test/java/de/greenrobot/dao/unittest/MinimalEntityTest.java +++ b/DaoTest/src/test/java/de/greenrobot/dao/unittest/MinimalEntityTest.java @@ -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; @@ -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 { diff --git a/DaoTest/src/test/java/de/greenrobot/dao/unittest/PropertyTest.java b/DaoTest/src/test/java/de/greenrobot/dao/unittest/PropertyTest.java new file mode 100644 index 000000000..e6aee1de9 --- /dev/null +++ b/DaoTest/src/test/java/de/greenrobot/dao/unittest/PropertyTest.java @@ -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)); + } + + +}