From fca80ac649fa97873235f8a195989137a6a53b41 Mon Sep 17 00:00:00 2001 From: Robin Bygrave Date: Fri, 28 Aug 2015 20:32:40 +1200 Subject: [PATCH] #399 - ENH: Add new Finder ... to effectively replace Model.Finder --- src/main/java/com/avaje/ebean/Finder.java | 24 +------ .../com/avaje/tests/model/basic/Customer.java | 3 + .../model/basic/finder/CustomerFinder.java | 28 ++++++++ .../query/finder/TestCustomerFinder.java | 64 +++++++++++++++++++ 4 files changed, 98 insertions(+), 21 deletions(-) create mode 100644 src/test/java/com/avaje/tests/model/basic/finder/CustomerFinder.java create mode 100644 src/test/java/com/avaje/tests/query/finder/TestCustomerFinder.java diff --git a/src/main/java/com/avaje/ebean/Finder.java b/src/main/java/com/avaje/ebean/Finder.java index 52dcfa86b..beacbea49 100644 --- a/src/main/java/com/avaje/ebean/Finder.java +++ b/src/main/java/com/avaje/ebean/Finder.java @@ -82,16 +82,16 @@ public class Finder { * } */ public Finder(Class type) { - this.serverName = null; this.type = type; + this.serverName = null; } /** * Create with the type of the entity bean and specific server name. */ - public Finder(String serverName, Class type) { - this.serverName = serverName; + public Finder(Class type, String serverName) { this.type = type; + this.serverName = serverName; } /** @@ -164,22 +164,4 @@ public class Finder { return db().find(type); } -// /** -// * Creates a Finder for the named EbeanServer. -// * -// *

-// * Create and return a new Finder for a different server. -// */ -// public Finder on(String server) { -// return new Finder(server, type); -// } - -// /** -// * Create and return a new query using the OQL. -// *

-// * Equivalent to {@link EbeanServer#createQuery(Class, String)} -// */ -// public Query createQuery(String oql) { -// return db().createQuery(type, oql); -// } } diff --git a/src/test/java/com/avaje/tests/model/basic/Customer.java b/src/test/java/com/avaje/tests/model/basic/Customer.java index 2c45ed610..b3bfaa918 100644 --- a/src/test/java/com/avaje/tests/model/basic/Customer.java +++ b/src/test/java/com/avaje/tests/model/basic/Customer.java @@ -5,6 +5,7 @@ import com.avaje.ebean.annotation.ChangeLogInsertMode; import com.avaje.ebean.annotation.EnumValue; import com.avaje.ebean.annotation.JsonIgnore; import com.avaje.ebean.annotation.Where; +import com.avaje.tests.model.basic.finder.CustomerFinder; import javax.persistence.CascadeType; import javax.persistence.Entity; @@ -29,6 +30,8 @@ public class Customer extends BasicDomain { private static final long serialVersionUID = 1L; + public static final CustomerFinder find = new CustomerFinder(); + /** * EnumValue is an Ebean specific mapping for enums. */ diff --git a/src/test/java/com/avaje/tests/model/basic/finder/CustomerFinder.java b/src/test/java/com/avaje/tests/model/basic/finder/CustomerFinder.java new file mode 100644 index 000000000..12894a023 --- /dev/null +++ b/src/test/java/com/avaje/tests/model/basic/finder/CustomerFinder.java @@ -0,0 +1,28 @@ +package com.avaje.tests.model.basic.finder; + +import com.avaje.ebean.Finder; +import com.avaje.tests.model.basic.Customer; +import org.jetbrains.annotations.Nullable; + +/** + * Finder methods for Customer. + */ +public class CustomerFinder extends Finder { + + public CustomerFinder() { + super(Customer.class); + } + + public CustomerFinder(String serverName) { + super(Customer.class, serverName); + } + + /** + * Find customer by unique name. + */ + @Nullable + public Customer byName(String name) { + + return query().where().eq("name", name).findUnique(); + } +} diff --git a/src/test/java/com/avaje/tests/query/finder/TestCustomerFinder.java b/src/test/java/com/avaje/tests/query/finder/TestCustomerFinder.java new file mode 100644 index 000000000..41943da62 --- /dev/null +++ b/src/test/java/com/avaje/tests/query/finder/TestCustomerFinder.java @@ -0,0 +1,64 @@ +package com.avaje.tests.query.finder; + +import com.avaje.ebean.BaseTestCase; +import com.avaje.ebean.Ebean; +import com.avaje.tests.model.basic.Customer; +import com.avaje.tests.model.basic.ResetBasicData; +import org.junit.Test; + +import java.util.List; + +import static org.assertj.core.api.StrictAssertions.assertThat; + +public class TestCustomerFinder extends BaseTestCase { + + @Test + public void test_ref() { + + ResetBasicData.reset(); + + Customer customer = Customer.find.ref(1); + assertThat(customer.getId()).isEqualTo(1); + } + + @Test + public void test_all_byId_byName() { + + ResetBasicData.reset(); + + List all = Customer.find.all(); + List list = Ebean.find(Customer.class).findList(); + + assertThat(all.size()).isEqualTo(list.size()); + + Customer customer = all.get(0); + + Customer customer1 = Customer.find.byId(customer.getId()); + + assertThat(customer.getId()).isEqualTo(customer1.getId()); + assertThat(customer.getName()).isEqualTo(customer1.getName()); + + Customer customer2 = Customer.find.byName(customer.getName()); + assertThat(customer.getId()).isEqualTo(customer2.getId()); + assertThat(customer.getName()).isEqualTo(customer2.getName()); + + assertThat(Customer.find.db().getName()).isEqualTo(Ebean.getDefaultServer().getName()); + + } + + @Test + public void test_deleteById() { + + Customer customer = new Customer(); + customer.setName("Newbie"); + + Ebean.save(customer); + assertThat(customer.getId()).isNotNull(); + + Customer.find.deleteById(customer.getId()); + + Customer notThere = Customer.find.byId(customer.getId()); + assertThat(notThere).isNull(); + } + +}