#197 - Add Query findEach() findEachWhile() ... as better method names for findVisit(). More consistent with forEach() methods

This commit is contained in:
rbygrave
2014-11-14 21:26:32 +13:00
parent 641385aab5
commit 4a882b537c
14 changed files with 461 additions and 95 deletions
@@ -0,0 +1,67 @@
package com.avaje.tests.query;
import com.avaje.ebean.*;
import com.avaje.tests.model.basic.Customer;
import com.avaje.tests.model.basic.ResetBasicData;
import org.junit.Assert;
import org.junit.Test;
import java.util.concurrent.atomic.AtomicInteger;
public class TestQueryFindEach extends BaseTestCase {
@Test
public void test() {
ResetBasicData.reset();
EbeanServer server = Ebean.getServer(null);
Query<Customer> query
= server.find(Customer.class)
.setAutofetch(false)
.fetch("contacts", new FetchConfig().query(2)).where().gt("id", 0).orderBy("id")
.setMaxRows(2);
final AtomicInteger counter = new AtomicInteger(0);
query.findEach(new QueryEachConsumer<Customer>() {
public void accept(Customer customer) {
counter.incrementAndGet();
customer.getName();
}
});
Assert.assertEquals(2, counter.get());
}
/**
* Test the behaviour when an exception is thrown inside the findVisit().
*/
@Test(expected=IllegalStateException.class)
public void testVisitThrowingException() {
ResetBasicData.reset();
EbeanServer server = Ebean.getServer(null);
Query<Customer> query = server.find(Customer.class).setAutofetch(false)
.fetch("contacts", new FetchConfig().query(2)).where().gt("id", 0).orderBy("id")
.setMaxRows(2);
final AtomicInteger counter = new AtomicInteger(0);
query.findEach(new QueryEachConsumer<Customer>() {
public void accept(Customer customer) {
counter.incrementAndGet();
if (counter.intValue() > 0) {
throw new IllegalStateException("cause a failure");
}
}
});
Assert.assertFalse("Never get here - exception thrown", true);
}
}
@@ -0,0 +1,69 @@
package com.avaje.tests.query;
import com.avaje.ebean.*;
import com.avaje.tests.model.basic.Customer;
import com.avaje.tests.model.basic.ResetBasicData;
import org.junit.Assert;
import org.junit.Test;
import java.util.concurrent.atomic.AtomicInteger;
public class TestQueryFindEachWhile extends BaseTestCase {
@Test
public void test() {
ResetBasicData.reset();
EbeanServer server = Ebean.getServer(null);
Query<Customer> query
= server.find(Customer.class)
.setAutofetch(false)
.fetch("contacts", new FetchConfig().query(2)).where().gt("id", 0).orderBy("id")
.setMaxRows(2);
final AtomicInteger counter = new AtomicInteger(0);
query.findEachWhile(new QueryEachWhileConsumer< Customer > () {
public boolean accept(Customer customer) {
counter.incrementAndGet();
customer.getName();
return true;
}
});
Assert.assertEquals(2, counter.get());
}
/**
* Test the behaviour when an exception is thrown inside the findVisit().
*/
@Test(expected=IllegalStateException.class)
public void testVisitThrowingException() {
ResetBasicData.reset();
EbeanServer server = Ebean.getServer(null);
Query<Customer> query = server.find(Customer.class).setAutofetch(false)
.fetch("contacts", new FetchConfig().query(2)).where().gt("id", 0).orderBy("id")
.setMaxRows(2);
final AtomicInteger counter = new AtomicInteger(0);
query.findEachWhile(new QueryEachWhileConsumer<Customer>() {
public boolean accept(Customer customer) {
counter.incrementAndGet();
if (counter.intValue() > 0) {
throw new IllegalStateException("cause a failure");
}
return true;
}
});
Assert.assertFalse("Never get here - exception thrown", true);
}
}