Fix for #159 - Refactor - cleanup clode when closing query only transaction + change default option

This commit is contained in:
Rob Bygrave
2014-07-05 22:10:53 +12:00
parent 1ddc103582
commit cceb7c035c
11 changed files with 162 additions and 144 deletions
@@ -21,7 +21,6 @@ public class TestErrorBindLog extends BaseTestCase {
} catch (PersistenceException e) {
String msg = e.getMessage();
e.printStackTrace();
Assert.assertTrue(msg.contains("Bind values:"));
}
}
@@ -1,5 +1,7 @@
package com.avaje.tests.query;
import javax.persistence.PersistenceException;
import org.junit.Assert;
import org.junit.Test;
@@ -40,4 +42,50 @@ public class TestQueryFindIterate extends BaseTestCase {
Assert.assertEquals(2, count);
}
@Test(expected=PersistenceException.class)
public void testWithExceptionInQuery() {
ResetBasicData.reset();
EbeanServer server = Ebean.getServer(null);
// intentionally a query with incorrect type binding
Query<Customer> query = server.find(Customer.class)
.setAutofetch(false)
.where().gt("id","JUNK_NOT_A_LONG")
.setMaxRows(2);
// this throws an exception immediately
QueryIterator<Customer> it = query.findIterate();
it.hashCode();
Assert.assertTrue("Never get here as exception thrown", false);
}
@Test(expected=IllegalStateException.class)
public void testWithExceptionInLoop() {
ResetBasicData.reset();
EbeanServer server = Ebean.getServer(null);
Query<Customer> query = server.find(Customer.class)
.setAutofetch(false)
.where().gt("id", 0)
.setMaxRows(2);
QueryIterator<Customer> it = query.findIterate();
try {
while (it.hasNext()) {
Customer customer = it.next();
if (customer != null) {
throw new IllegalStateException("cause an exception");
}
}
} finally {
it.close();
}
}
}
@@ -39,4 +39,34 @@ public class TestQueryFindVisit extends BaseTestCase {
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.findVisit(new QueryResultVisitor<Customer>() {
public boolean accept(Customer bean) {
counter.incrementAndGet();
if (counter.intValue() > 0) {
throw new IllegalStateException("cause a failure");
}
return true;
}
});
Assert.assertFalse("Never get here - exception thrown", true);
}
}