From ce036bd90c37fe3ed14b3c4b429404f0e8f205e1 Mon Sep 17 00:00:00 2001 From: greenrobot Team Date: Tue, 16 Aug 2016 13:43:40 +0200 Subject: [PATCH 1/2] Add NotNullThing and basic test. --- .../entityannotation/NotNullThingTest.java | 76 +++++++++++++++++++ .../test/entityannotation/NotNullThing.java | 65 ++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 tests/DaoTestEntityAnnotation/src/androidTest/Java/org/greenrobot/greendao/test/entityannotation/NotNullThingTest.java create mode 100644 tests/DaoTestEntityAnnotation/src/main/java/org/greenrobot/greendao/test/entityannotation/NotNullThing.java diff --git a/tests/DaoTestEntityAnnotation/src/androidTest/Java/org/greenrobot/greendao/test/entityannotation/NotNullThingTest.java b/tests/DaoTestEntityAnnotation/src/androidTest/Java/org/greenrobot/greendao/test/entityannotation/NotNullThingTest.java new file mode 100644 index 000000000..7e766bcc7 --- /dev/null +++ b/tests/DaoTestEntityAnnotation/src/androidTest/Java/org/greenrobot/greendao/test/entityannotation/NotNullThingTest.java @@ -0,0 +1,76 @@ +package org.greenrobot.greendao.test.entityannotation; + +import org.greenrobot.greendao.test.AbstractDaoTestLongPk; + +public class NotNullThingTest extends AbstractDaoTestLongPk { + + public NotNullThingTest() { + super(NotNullThingDao.class); + } + + @Override + protected NotNullThing createEntity(Long key) { + NotNullThing thing = new NotNullThing(); + thing.setId(key); + thing.setNotNullBoolean(true); + thing.setNotNullInteger(42); + thing.setNullableBoolean(true); + thing.setNullableInteger(42); + return thing; + } + + public void testInsertNotNullProperties() { + NotNullThing thing = createEntity(1L); + thing.setNotNullBoolean(null); + thing.setNotNullInteger(null); + try { + dao.insert(thing); + fail(); + } catch (NullPointerException ignored) { + } + } + + public void testInsertNullableProperties() { + NotNullThing thing = createEntity(1L); + thing.setNullableBoolean(null); + thing.setNullableInteger(null); + dao.insert(thing); + + loadAndAssertNullableProperties(thing); + } + + public void testUpdateNotNullProperties() { + NotNullThing thing = insertEntity(); + + thing.setNotNullBoolean(null); + thing.setNotNullInteger(null); + try { + dao.update(thing); + fail(); + } catch (NullPointerException ignored) { + } + } + + public void testUpdateNullableProperties() { + NotNullThing thing = insertEntity(); + + thing.setNullableBoolean(null); + thing.setNullableInteger(null); + dao.update(thing); + + loadAndAssertNullableProperties(thing); + } + + private NotNullThing insertEntity() { + NotNullThing thing = createEntity(1L); + dao.insert(thing); + return thing; + } + + private void loadAndAssertNullableProperties(NotNullThing thing) { + NotNullThing loaded = dao.load(thing.getId()); + assertNull(loaded.getNullableBoolean()); + assertNull(loaded.getNullableInteger()); + } + +} diff --git a/tests/DaoTestEntityAnnotation/src/main/java/org/greenrobot/greendao/test/entityannotation/NotNullThing.java b/tests/DaoTestEntityAnnotation/src/main/java/org/greenrobot/greendao/test/entityannotation/NotNullThing.java new file mode 100644 index 000000000..f073a89a1 --- /dev/null +++ b/tests/DaoTestEntityAnnotation/src/main/java/org/greenrobot/greendao/test/entityannotation/NotNullThing.java @@ -0,0 +1,65 @@ +package org.greenrobot.greendao.test.entityannotation; + +import org.greenrobot.greendao.annotation.Entity; +import org.greenrobot.greendao.annotation.Id; +import org.greenrobot.greendao.annotation.NotNull; +import org.greenrobot.greendao.annotation.Generated; + +@Entity +public class NotNullThing { + + @Id + private Long id; + + Boolean nullableBoolean; + Integer nullableInteger; + + @NotNull + Boolean notNullBoolean; + @NotNull + Integer notNullInteger; + + public Integer getNotNullInteger() { + return this.notNullInteger; + } + public void setNotNullInteger(Integer notNullInteger) { + this.notNullInteger = notNullInteger; + } + public Boolean getNotNullBoolean() { + return this.notNullBoolean; + } + public void setNotNullBoolean(Boolean notNullBoolean) { + this.notNullBoolean = notNullBoolean; + } + public Integer getNullableInteger() { + return this.nullableInteger; + } + public void setNullableInteger(Integer nullableInteger) { + this.nullableInteger = nullableInteger; + } + public Boolean getNullableBoolean() { + return this.nullableBoolean; + } + public void setNullableBoolean(Boolean nullableBoolean) { + this.nullableBoolean = nullableBoolean; + } + public Long getId() { + return this.id; + } + public void setId(Long id) { + this.id = id; + } + @Generated(hash = 2048873927) + public NotNullThing(Long id, Boolean nullableBoolean, Integer nullableInteger, + @NotNull Boolean notNullBoolean, @NotNull Integer notNullInteger) { + this.id = id; + this.nullableBoolean = nullableBoolean; + this.nullableInteger = nullableInteger; + this.notNullBoolean = notNullBoolean; + this.notNullInteger = notNullInteger; + } + @Generated(hash = 521031743) + public NotNullThing() { + } + +} From b160a37d6ecf3e944b4ce00441f03941055823f1 Mon Sep 17 00:00:00 2001 From: greenrobot Team Date: Tue, 23 Aug 2016 10:58:04 +0200 Subject: [PATCH 2/2] Add primitive types with @NotNull annotation. --- .../entityannotation/NotNullThingTest.java | 32 ++++-- .../test/entityannotation/NotNullThing.java | 99 +++++++++++++------ 2 files changed, 90 insertions(+), 41 deletions(-) diff --git a/tests/DaoTestEntityAnnotation/src/androidTest/Java/org/greenrobot/greendao/test/entityannotation/NotNullThingTest.java b/tests/DaoTestEntityAnnotation/src/androidTest/Java/org/greenrobot/greendao/test/entityannotation/NotNullThingTest.java index 7e766bcc7..b2ab60ccc 100644 --- a/tests/DaoTestEntityAnnotation/src/androidTest/Java/org/greenrobot/greendao/test/entityannotation/NotNullThingTest.java +++ b/tests/DaoTestEntityAnnotation/src/androidTest/Java/org/greenrobot/greendao/test/entityannotation/NotNullThingTest.java @@ -14,15 +14,19 @@ protected NotNullThing createEntity(Long key) { thing.setId(key); thing.setNotNullBoolean(true); thing.setNotNullInteger(42); + thing.setNotNullWrappedBoolean(true); + thing.setNotNullWrappedInteger(42); thing.setNullableBoolean(true); thing.setNullableInteger(42); + thing.setNullableWrappedBoolean(true); + thing.setNullableWrappedInteger(42); return thing; } public void testInsertNotNullProperties() { NotNullThing thing = createEntity(1L); - thing.setNotNullBoolean(null); - thing.setNotNullInteger(null); + thing.setNotNullWrappedBoolean(null); + thing.setNotNullWrappedInteger(null); try { dao.insert(thing); fail(); @@ -32,8 +36,8 @@ public void testInsertNotNullProperties() { public void testInsertNullableProperties() { NotNullThing thing = createEntity(1L); - thing.setNullableBoolean(null); - thing.setNullableInteger(null); + thing.setNullableWrappedBoolean(null); + thing.setNullableWrappedInteger(null); dao.insert(thing); loadAndAssertNullableProperties(thing); @@ -42,8 +46,8 @@ public void testInsertNullableProperties() { public void testUpdateNotNullProperties() { NotNullThing thing = insertEntity(); - thing.setNotNullBoolean(null); - thing.setNotNullInteger(null); + thing.setNotNullWrappedBoolean(null); + thing.setNotNullWrappedInteger(null); try { dao.update(thing); fail(); @@ -54,8 +58,8 @@ public void testUpdateNotNullProperties() { public void testUpdateNullableProperties() { NotNullThing thing = insertEntity(); - thing.setNullableBoolean(null); - thing.setNullableInteger(null); + thing.setNullableWrappedBoolean(null); + thing.setNullableWrappedInteger(null); dao.update(thing); loadAndAssertNullableProperties(thing); @@ -69,8 +73,16 @@ private NotNullThing insertEntity() { private void loadAndAssertNullableProperties(NotNullThing thing) { NotNullThing loaded = dao.load(thing.getId()); - assertNull(loaded.getNullableBoolean()); - assertNull(loaded.getNullableInteger()); + assertTrue(loaded.getNullableBoolean()); + assertEquals(42, loaded.getNullableInteger()); + assertTrue(loaded.getNotNullBoolean()); + assertEquals(42, loaded.getNotNullInteger()); + + assertNull(loaded.getNullableWrappedBoolean()); + assertNull(loaded.getNullableWrappedInteger()); + + assertNotNull(loaded.getNotNullWrappedBoolean()); + assertNotNull(loaded.getNotNullWrappedInteger()); } } diff --git a/tests/DaoTestEntityAnnotation/src/main/java/org/greenrobot/greendao/test/entityannotation/NotNullThing.java b/tests/DaoTestEntityAnnotation/src/main/java/org/greenrobot/greendao/test/entityannotation/NotNullThing.java index f073a89a1..19aa486aa 100644 --- a/tests/DaoTestEntityAnnotation/src/main/java/org/greenrobot/greendao/test/entityannotation/NotNullThing.java +++ b/tests/DaoTestEntityAnnotation/src/main/java/org/greenrobot/greendao/test/entityannotation/NotNullThing.java @@ -1,9 +1,9 @@ package org.greenrobot.greendao.test.entityannotation; import org.greenrobot.greendao.annotation.Entity; +import org.greenrobot.greendao.annotation.Generated; import org.greenrobot.greendao.annotation.Id; import org.greenrobot.greendao.annotation.NotNull; -import org.greenrobot.greendao.annotation.Generated; @Entity public class NotNullThing { @@ -11,55 +11,92 @@ public class NotNullThing { @Id private Long id; - Boolean nullableBoolean; - Integer nullableInteger; + boolean nullableBoolean; + int nullableInteger; + Boolean nullableWrappedBoolean; + Integer nullableWrappedInteger; @NotNull - Boolean notNullBoolean; + boolean notNullBoolean; + @NotNull + int notNullInteger; @NotNull - Integer notNullInteger; + Boolean notNullWrappedBoolean; + @NotNull + Integer notNullWrappedInteger; - public Integer getNotNullInteger() { - return this.notNullInteger; - } - public void setNotNullInteger(Integer notNullInteger) { + @Generated(hash = 1109392169) + public NotNullThing(Long id, boolean nullableBoolean, int nullableInteger, + Boolean nullableWrappedBoolean, Integer nullableWrappedInteger, + boolean notNullBoolean, int notNullInteger, + @NotNull Boolean notNullWrappedBoolean, + @NotNull Integer notNullWrappedInteger) { + this.id = id; + this.nullableBoolean = nullableBoolean; + this.nullableInteger = nullableInteger; + this.nullableWrappedBoolean = nullableWrappedBoolean; + this.nullableWrappedInteger = nullableWrappedInteger; + this.notNullBoolean = notNullBoolean; this.notNullInteger = notNullInteger; + this.notNullWrappedBoolean = notNullWrappedBoolean; + this.notNullWrappedInteger = notNullWrappedInteger; } - public Boolean getNotNullBoolean() { - return this.notNullBoolean; + @Generated(hash = 521031743) + public NotNullThing() { } - public void setNotNullBoolean(Boolean notNullBoolean) { - this.notNullBoolean = notNullBoolean; + public Long getId() { + return this.id; + } + public void setId(Long id) { + this.id = id; + } + public boolean getNullableBoolean() { + return this.nullableBoolean; } - public Integer getNullableInteger() { + public void setNullableBoolean(boolean nullableBoolean) { + this.nullableBoolean = nullableBoolean; + } + public int getNullableInteger() { return this.nullableInteger; } - public void setNullableInteger(Integer nullableInteger) { + public void setNullableInteger(int nullableInteger) { this.nullableInteger = nullableInteger; } - public Boolean getNullableBoolean() { - return this.nullableBoolean; + public Boolean getNullableWrappedBoolean() { + return this.nullableWrappedBoolean; } - public void setNullableBoolean(Boolean nullableBoolean) { - this.nullableBoolean = nullableBoolean; + public void setNullableWrappedBoolean(Boolean nullableWrappedBoolean) { + this.nullableWrappedBoolean = nullableWrappedBoolean; } - public Long getId() { - return this.id; + public Integer getNullableWrappedInteger() { + return this.nullableWrappedInteger; } - public void setId(Long id) { - this.id = id; + public void setNullableWrappedInteger(Integer nullableWrappedInteger) { + this.nullableWrappedInteger = nullableWrappedInteger; } - @Generated(hash = 2048873927) - public NotNullThing(Long id, Boolean nullableBoolean, Integer nullableInteger, - @NotNull Boolean notNullBoolean, @NotNull Integer notNullInteger) { - this.id = id; - this.nullableBoolean = nullableBoolean; - this.nullableInteger = nullableInteger; + public boolean getNotNullBoolean() { + return this.notNullBoolean; + } + public void setNotNullBoolean(boolean notNullBoolean) { this.notNullBoolean = notNullBoolean; + } + public int getNotNullInteger() { + return this.notNullInteger; + } + public void setNotNullInteger(int notNullInteger) { this.notNullInteger = notNullInteger; } - @Generated(hash = 521031743) - public NotNullThing() { + public Boolean getNotNullWrappedBoolean() { + return this.notNullWrappedBoolean; + } + public void setNotNullWrappedBoolean(Boolean notNullWrappedBoolean) { + this.notNullWrappedBoolean = notNullWrappedBoolean; + } + public Integer getNotNullWrappedInteger() { + return this.notNullWrappedInteger; + } + public void setNotNullWrappedInteger(Integer notNullWrappedInteger) { + this.notNullWrappedInteger = notNullWrappedInteger; } }