mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Merge remote-tracking branch 'ebean/master'
This commit is contained in:
@@ -13,7 +13,7 @@ public class BaseTestCase {
|
||||
|
||||
static {
|
||||
logger.debug("... preStart");
|
||||
if (!AgentLoader.loadAgentFromClasspath("avaje-ebeanorm-agent","debug=0;packages=com.avaje.tests.**,org.avaje.test.**")) {
|
||||
if (!AgentLoader.loadAgentFromClasspath("ebean-agent","debug=1;packages=com.avaje.tests,org.avaje.test")) {
|
||||
logger.info("avaje-ebeanorm-agent not found in classpath - not dynamically loaded");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,8 +114,6 @@ public class EbeanServer_deleteAllByIdTest {
|
||||
}
|
||||
|
||||
private EBasicVer bean(String name) {
|
||||
EBasicVer bean = new EBasicVer();
|
||||
bean.setName(name);
|
||||
return bean;
|
||||
return new EBasicVer(name);
|
||||
}
|
||||
}
|
||||
@@ -85,8 +85,6 @@ public class EbeanServer_deleteByIdTest {
|
||||
}
|
||||
|
||||
private EBasicVer bean(String name) {
|
||||
EBasicVer bean = new EBasicVer();
|
||||
bean.setName(name);
|
||||
return bean;
|
||||
return new EBasicVer(name);
|
||||
}
|
||||
}
|
||||
@@ -48,8 +48,6 @@ public class EbeanServer_deleteTest {
|
||||
}
|
||||
|
||||
private EBasicVer bean(String name) {
|
||||
EBasicVer bean = new EBasicVer();
|
||||
bean.setName(name);
|
||||
return bean;
|
||||
return new EBasicVer(name);
|
||||
}
|
||||
}
|
||||
@@ -123,8 +123,6 @@ public class EbeanServer_saveAllTest extends BaseTestCase {
|
||||
}
|
||||
|
||||
private EBasicVer bean(String name) {
|
||||
EBasicVer bean = new EBasicVer();
|
||||
bean.setName(name);
|
||||
return bean;
|
||||
return new EBasicVer(name);
|
||||
}
|
||||
}
|
||||
@@ -24,8 +24,7 @@ public class BeanPersistControllerTest {
|
||||
EbeanServer ebeanServer = getEbeanServer(continuePersistingAdapter);
|
||||
|
||||
|
||||
EBasicVer bean = new EBasicVer();
|
||||
bean.setName("testController");
|
||||
EBasicVer bean = new EBasicVer("testController");
|
||||
|
||||
ebeanServer.save(bean);
|
||||
assertThat(continuePersistingAdapter.methodsCalled).hasSize(2);
|
||||
@@ -49,8 +48,7 @@ public class BeanPersistControllerTest {
|
||||
|
||||
EbeanServer ebeanServer = getEbeanServer(stopPersistingAdapter);
|
||||
|
||||
EBasicVer bean = new EBasicVer();
|
||||
bean.setName("testController");
|
||||
EBasicVer bean = new EBasicVer("testController");
|
||||
|
||||
ebeanServer.save(bean);
|
||||
assertThat(stopPersistingAdapter.methodsCalled).hasSize(1);
|
||||
|
||||
@@ -24,8 +24,7 @@ public class BeanPostLoadTest extends BaseTestCase {
|
||||
|
||||
EbeanServer ebeanServer = getEbeanServer();
|
||||
|
||||
EBasicVer bean = new EBasicVer();
|
||||
bean.setName("testPostLoad");
|
||||
EBasicVer bean = new EBasicVer("testPostLoad");
|
||||
bean.setDescription("someDescription");
|
||||
bean.setOther("other");
|
||||
|
||||
|
||||
@@ -186,7 +186,7 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> List<Object> findIdsWithCopy(Query<T> query, Transaction t) {
|
||||
public <A> List<A> findIdsWithCopy(Query<?> query, Transaction t) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -455,13 +455,23 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> int findCount(Query<T> query, Transaction transaction) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> int findRowCount(Query<T> query, Transaction transaction) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> List<Object> findIds(Query<T> query, Transaction transaction) {
|
||||
public <A> List<A> findIds(Query<?> query, Transaction transaction) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> QueryIterator<T> findIterate(Query<T> query, Transaction transaction) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -480,6 +490,11 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> FutureRowCount<T> findFutureCount(Query<T> query, Transaction transaction) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> FutureRowCount<T> findFutureRowCount(Query<T> query, Transaction transaction) {
|
||||
return null;
|
||||
@@ -506,7 +521,12 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> Map<?, T> findMap(Query<T> query, Transaction transaction) {
|
||||
public <K, T> Map<K, T> findMap(Query<T> query, Transaction transaction) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <A> List<A> findSingleAttributeList(Query<?> query, Transaction transaction) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
+28
@@ -153,6 +153,20 @@ public class DefaultServer_createOrmQueryRequestTest extends BaseTestCase {
|
||||
assertThat(detail.getFetchPaths()).containsExactly("customer");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoinOrder_when_queryFetch_expect_getFetchPaths_doesNotIncludeQueryJoin_via_fetchQuery() {
|
||||
|
||||
Query<Order> query = Ebean.find(Order.class)
|
||||
.select("status, orderDate")
|
||||
.fetch("customer", "name")
|
||||
.fetchQuery("details");
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query);
|
||||
OrmQueryDetail detail = queryRequest.getQuery().getDetail();
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("customer");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoinOrder_when_lazyFetch_expect_getFetchPaths_doesNotIncludeQueryJoin() {
|
||||
|
||||
@@ -167,6 +181,20 @@ public class DefaultServer_createOrmQueryRequestTest extends BaseTestCase {
|
||||
assertThat(detail.getFetchPaths()).containsExactly("customer");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoinOrder_when_lazyFetch_expect_getFetchPaths_doesNotIncludeQueryJoin_via_fetchLazy() {
|
||||
|
||||
Query<Order> query = Ebean.find(Order.class)
|
||||
.select("status, orderDate")
|
||||
.fetch("customer", "name")
|
||||
.fetchLazy("details");
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query);
|
||||
OrmQueryDetail detail = queryRequest.getQuery().getDetail();
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("customer");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoinOrder_when_lazyFetchAndHasChildren_expect_getFetchPaths_doesNotIncludeJoinOrChild() {
|
||||
|
||||
|
||||
+1
-1
@@ -54,7 +54,7 @@ public class TestDataSourceMaxWithEntity extends BaseTestCase {
|
||||
|
||||
public void run() {
|
||||
|
||||
server.find(Customer.class).findRowCount();
|
||||
server.find(Customer.class).findCount();
|
||||
try {
|
||||
System.out.println(position+" sleep " + sleepMillis);
|
||||
Thread.sleep(sleepMillis);
|
||||
|
||||
@@ -49,6 +49,20 @@ public class DLoadContextTest extends BaseTestCase {
|
||||
assertThat(customer.secondaryBatchSize).isEqualTo(100);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void construct_when_fetchQuery_expect_100_batchSize_viaFetchQuery() {
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query().fetchQuery("customer"));
|
||||
queryRequest.initTransIfRequired();
|
||||
queryRequest.endTransIfRequired();
|
||||
|
||||
DLoadContext graphContext = (DLoadContext)queryRequest.getGraphContext();
|
||||
DLoadBeanContext customer = graphContext.getBeanContext("customer");
|
||||
|
||||
assertThat(customer.firstBatchSize).isEqualTo(100);
|
||||
assertThat(customer.secondaryBatchSize).isEqualTo(100);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void construct_when_fetchQuery50_expect_50_batchSize() {
|
||||
|
||||
@@ -77,4 +91,19 @@ public class DLoadContextTest extends BaseTestCase {
|
||||
assertThat(customer.secondaryBatchSize).isEqualTo(5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void construct_when_fetch_expect_100_100_batchSize() {
|
||||
|
||||
// the fetch is converted to a query join due to the maxRows
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query().fetch("details").setMaxRows(100)) ;
|
||||
queryRequest.initTransIfRequired();
|
||||
queryRequest.endTransIfRequired();
|
||||
|
||||
DLoadContext graphContext = (DLoadContext)queryRequest.getGraphContext();
|
||||
DLoadManyContext details = graphContext.getManyContext("details");
|
||||
|
||||
assertThat(details.firstBatchSize).isEqualTo(100);
|
||||
assertThat(details.secondaryBatchSize).isEqualTo(100);
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -29,7 +29,7 @@ public class TestFutureRowCountErrorHandling extends BaseTestCase {
|
||||
.where().eq("doesNotExist", "this will fail")
|
||||
.query();
|
||||
|
||||
FutureRowCount<Customer> futureRowCount = server.findFutureRowCount(query, null);
|
||||
FutureRowCount<Customer> futureRowCount = server.findFutureCount(query, null);
|
||||
|
||||
QueryFutureRowCount<Customer> internalRowCount = (QueryFutureRowCount<Customer>)futureRowCount;
|
||||
Transaction t = internalRowCount.getTransaction();
|
||||
|
||||
@@ -30,7 +30,7 @@ public class OrmQueryPlanKeyTest extends BaseExpressionTest {
|
||||
@Test
|
||||
public void equals_when_diffTableJoinNull() {
|
||||
|
||||
TableJoin tableJoin = tableJoin("id", "customer_id");
|
||||
TableJoin tableJoin = tableJoin("table", "id", "customer_id");
|
||||
|
||||
OrmQueryPlanKey key1 = new OrmQueryPlanKey(tableJoin, SpiQuery.Type.BEAN, null, 0, 0, false, null, false, false, null, null, null, null, null, SpiQuery.TemporalMode.CURRENT, false, null, null, null);
|
||||
OrmQueryPlanKey key2 = new OrmQueryPlanKey(null, SpiQuery.Type.BEAN, null, 0, 0, false, null, false, false, null, null, null, null, null, SpiQuery.TemporalMode.CURRENT, false, null, null, null);
|
||||
@@ -41,8 +41,8 @@ public class OrmQueryPlanKeyTest extends BaseExpressionTest {
|
||||
@Test
|
||||
public void equals_when_diffTableJoin() {
|
||||
|
||||
TableJoin tableJoin1 = tableJoin("id", "customer_id");
|
||||
TableJoin tableJoin2 = tableJoin("id", "other_customer_id");
|
||||
TableJoin tableJoin1 = tableJoin("one", "id", "customer_id");
|
||||
TableJoin tableJoin2 = tableJoin("two", "id", "customer_id");
|
||||
|
||||
OrmQueryPlanKey key1 = new OrmQueryPlanKey(tableJoin1, SpiQuery.Type.BEAN, null, 0, 0, false, null, false, false, null, null, null, null, null, SpiQuery.TemporalMode.CURRENT, false, null, null, null);
|
||||
OrmQueryPlanKey key2 = new OrmQueryPlanKey(tableJoin2, SpiQuery.Type.BEAN, null, 0, 0, false, null, false, false, null, null, null, null, null, SpiQuery.TemporalMode.CURRENT, false, null, null, null);
|
||||
@@ -53,8 +53,8 @@ public class OrmQueryPlanKeyTest extends BaseExpressionTest {
|
||||
@Test
|
||||
public void equals_when_sameTableJoin() {
|
||||
|
||||
TableJoin tableJoin1 = tableJoin("id", "customer_id");
|
||||
TableJoin tableJoin2 = tableJoin("id", "customer_id");
|
||||
TableJoin tableJoin1 = tableJoin("one", "id", "customer_id");
|
||||
TableJoin tableJoin2 = tableJoin("one", "id", "customer_id");
|
||||
|
||||
OrmQueryPlanKey key1 = new OrmQueryPlanKey(tableJoin1, SpiQuery.Type.BEAN, null, 0, 0, false, null, false, false, null, null, null, null, null, SpiQuery.TemporalMode.CURRENT, false, null, null, null);
|
||||
OrmQueryPlanKey key2 = new OrmQueryPlanKey(tableJoin2, SpiQuery.Type.BEAN, null, 0, 0, false, null, false, false, null, null, null, null, null, SpiQuery.TemporalMode.CURRENT, false, null, null, null);
|
||||
@@ -63,9 +63,9 @@ public class OrmQueryPlanKeyTest extends BaseExpressionTest {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private TableJoin tableJoin(String col1, String col2) {
|
||||
private TableJoin tableJoin(String table, String col1, String col2) {
|
||||
DeployTableJoin deploy = new DeployTableJoin();
|
||||
deploy.setTable("myTable");
|
||||
deploy.setTable(table);
|
||||
deploy.addJoinColumn(new DeployTableJoinColumn(col1, col2));
|
||||
return new TableJoin(deploy);
|
||||
}
|
||||
|
||||
@@ -147,7 +147,7 @@ public class MainDbBoolean {
|
||||
.setAutoTune(false)
|
||||
.order("id");
|
||||
|
||||
int rc = query.findRowCount();
|
||||
int rc = query.findCount();
|
||||
Assert.assertTrue(rc > 0);
|
||||
|
||||
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.FutureIds;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestFetchId extends BaseTestCase {
|
||||
|
||||
@@ -28,19 +28,12 @@ public class TestFetchId extends BaseTestCase {
|
||||
.query();
|
||||
|
||||
List<Object> ids = Ebean.getServer(null).findIds(query, null);
|
||||
assertThat(ids).isNotEmpty();
|
||||
|
||||
FutureIds<Order> futureIds = Ebean.getServer(null).findFutureIds(query,null);
|
||||
|
||||
// this list is likely empty at this point and
|
||||
// will get populated in the background
|
||||
List<Object> partial = futureIds.getPartialIds();
|
||||
|
||||
// this is likely 0 or a small number
|
||||
|
||||
// wait for all the id's to be fetched
|
||||
List<Object> idList = futureIds.get();
|
||||
Assert.assertTrue("same instance", partial == idList);
|
||||
|
||||
Assert.assertTrue("sz > 0", !ids.isEmpty());
|
||||
assertThat(idList).isNotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,8 +14,7 @@ public class TestIUDVanilla extends BaseTestCase {
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
EBasicVer e0 = new EBasicVer();
|
||||
e0.setName("vanilla");
|
||||
EBasicVer e0 = new EBasicVer("vanilla");
|
||||
|
||||
Ebean.save(e0);
|
||||
|
||||
@@ -39,9 +38,8 @@ public class TestIUDVanilla extends BaseTestCase {
|
||||
e2.setName("forcedUpdate");
|
||||
Ebean.update(e2);
|
||||
|
||||
EBasicVer e3 = new EBasicVer();
|
||||
EBasicVer e3 = new EBasicVer("ModNoOCC");
|
||||
e3.setId(e0.getId());
|
||||
e3.setName("ModNoOCC");
|
||||
|
||||
Ebean.update(e3);
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ public class TestLazyLoadInCache extends BaseTestCase {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Map<?, Customer> map = Ebean.find(Customer.class)
|
||||
Map<Integer, Customer> map = Ebean.find(Customer.class)
|
||||
.select("id, name")
|
||||
.setLoadBeanCache(true)
|
||||
.setReadOnly(true)
|
||||
|
||||
@@ -17,7 +17,7 @@ public class TestLoadBeanCache extends BaseTestCase {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Map<?, Country> map = Ebean.find(Country.class)
|
||||
Map<String, Country> map = Ebean.find(Country.class)
|
||||
.setLoadBeanCache(true)
|
||||
.setUseQueryCache(true)
|
||||
.setReadOnly(true)
|
||||
|
||||
@@ -25,9 +25,8 @@ public class TestLogTransLogOnError extends BaseTestCase {
|
||||
Ebean.find(Customer.class).findList();
|
||||
Ebean.find(Order.class).where().gt("id", 1).findList();
|
||||
|
||||
EBasicVer newBean = new EBasicVer();
|
||||
EBasicVer newBean = new EBasicVer("aName");
|
||||
newBean.setDescription("something");
|
||||
newBean.setName("aName");
|
||||
|
||||
// Ebean.save(newBean);
|
||||
|
||||
@@ -55,13 +54,12 @@ public class TestLogTransLogOnError extends BaseTestCase {
|
||||
try {
|
||||
Ebean.find(Customer.class).findList();
|
||||
|
||||
EBasicVer newBean = new EBasicVer();
|
||||
EBasicVer newBean = new EBasicVer("aName");
|
||||
newBean
|
||||
.setDescription("something sdfjksdjflsjdflsjdflksjdfkjd fsjdfkjsdkfjsdkfjskdjfskjdf"
|
||||
+ " sjdf sdjflksjdfkjsdlfkjsdkfjs ksjdfksjdlfjsldf something sdfjksdjflsjdflsjdflksjdfkjd"
|
||||
+ "fsjdfkjsdkfjsdkfjskdjfskjdf sjdf sdjflksjdfkjsdlfkjsdkfjs ksjdfksjdlfjsldf something s"
|
||||
+ "dfjksdjflsjdflsjdflksjdfkjd fsjdfkjsdkfjsdkfjskdjfskjdf sjdf sdjflksjdfkjsdlfkjsdkfjs ");
|
||||
newBean.setName("aName");
|
||||
|
||||
// t.log("--- next insert should error");
|
||||
Ebean.save(newBean);
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
package com.avaje.tests.basic.event;
|
||||
|
||||
import com.avaje.ebean.Transaction;
|
||||
import com.avaje.ebean.event.TransactionEventListener;
|
||||
|
||||
public class MyTestTransactionEventListener implements TransactionEventListener {
|
||||
private volatile static boolean doTest = false;
|
||||
|
||||
private static Transaction lastCommitted;
|
||||
private static Transaction lastRollbacked;
|
||||
|
||||
public void postTransactionCommit(Transaction tx) {
|
||||
if (!doTest) {
|
||||
return;
|
||||
}
|
||||
|
||||
lastCommitted = tx;
|
||||
}
|
||||
|
||||
public void postTransactionRollback(Transaction tx, Throwable cause) {
|
||||
if (!doTest) {
|
||||
return;
|
||||
}
|
||||
|
||||
lastRollbacked = tx;
|
||||
}
|
||||
|
||||
public static void setDoTest(boolean doTest) {
|
||||
MyTestTransactionEventListener.doTest = doTest;
|
||||
|
||||
// reset what we've recorded so far
|
||||
lastCommitted = null;
|
||||
lastRollbacked = null;
|
||||
}
|
||||
|
||||
public static Transaction getLastCommitted() {
|
||||
return lastCommitted;
|
||||
}
|
||||
|
||||
public static Transaction getLastRollbacked() {
|
||||
return lastRollbacked;
|
||||
}
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
package com.avaje.tests.basic.event;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Transaction;
|
||||
import com.avaje.tests.model.basic.TWithPreInsert;
|
||||
|
||||
public class TestTransactionEvent extends TestCase {
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
MyTestTransactionEventListener.setDoTest(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
MyTestTransactionEventListener.setDoTest(true);
|
||||
}
|
||||
|
||||
public void test() {
|
||||
|
||||
assertNull(MyTestTransactionEventListener.getLastCommitted());
|
||||
assertNull(MyTestTransactionEventListener.getLastRollbacked());
|
||||
|
||||
final Object myUserObject = new Object();
|
||||
|
||||
Transaction tx = Ebean.beginTransaction();
|
||||
tx.putUserObject("myUserObject", myUserObject);
|
||||
|
||||
TWithPreInsert e = new TWithPreInsert();
|
||||
e.setTitle("Mister Transaction1");
|
||||
Ebean.save(e);
|
||||
|
||||
tx.commit();
|
||||
|
||||
assertNotNull(MyTestTransactionEventListener.getLastCommitted());
|
||||
assertNotNull(MyTestTransactionEventListener.getLastCommitted().getUserObject("myUserObject"));
|
||||
assertSame(MyTestTransactionEventListener.getLastCommitted().getUserObject("myUserObject"), myUserObject);
|
||||
assertNull(MyTestTransactionEventListener.getLastRollbacked());
|
||||
|
||||
Transaction tx2 = Ebean.beginTransaction();
|
||||
tx2.putUserObject("myUserObject2", myUserObject);
|
||||
|
||||
TWithPreInsert e2 = new TWithPreInsert();
|
||||
e2.setTitle("Mister Transaction2");
|
||||
Ebean.save(e2);
|
||||
|
||||
tx2.rollback();
|
||||
|
||||
assertNotNull(MyTestTransactionEventListener.getLastCommitted());
|
||||
assertNotNull(MyTestTransactionEventListener.getLastRollbacked());
|
||||
|
||||
assertNotSame(MyTestTransactionEventListener.getLastCommitted(), MyTestTransactionEventListener.getLastRollbacked());
|
||||
|
||||
assertNotNull(MyTestTransactionEventListener.getLastCommitted().getUserObject("myUserObject"));
|
||||
assertSame(MyTestTransactionEventListener.getLastCommitted().getUserObject("myUserObject"), myUserObject);
|
||||
|
||||
assertNotNull(MyTestTransactionEventListener.getLastRollbacked().getUserObject("myUserObject2"));
|
||||
assertSame(MyTestTransactionEventListener.getLastRollbacked().getUserObject("myUserObject2"), myUserObject);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
package com.avaje.tests.batchinsert;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.EbeanServer;
|
||||
import com.avaje.ebean.Transaction;
|
||||
import com.avaje.ebean.annotation.Transactional;
|
||||
import com.avaje.ebean.config.PersistBatch;
|
||||
import com.avaje.tests.model.basic.EBasicVer;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class TestBatchInsertFlush extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
@Transactional(batch = PersistBatch.ALL)
|
||||
public void transactional_flushOnGetId() {
|
||||
|
||||
EbeanServer server = Ebean.getDefaultServer();
|
||||
|
||||
EBasicVer b1 = new EBasicVer("b1");
|
||||
server.save(b1);
|
||||
|
||||
EBasicVer b2 = new EBasicVer("b2");
|
||||
server.save(b2);
|
||||
|
||||
Integer id = b1.getId();
|
||||
assertNotNull(id);
|
||||
EBasicVer b3 = new EBasicVer("b3");
|
||||
server.save(b3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFlushOnGetId() {
|
||||
|
||||
EbeanServer server = Ebean.getDefaultServer();
|
||||
Transaction txn = server.beginTransaction();
|
||||
try {
|
||||
txn.setBatch(PersistBatch.ALL);
|
||||
|
||||
EBasicVer b1 = new EBasicVer("b1");
|
||||
server.save(b1, txn);
|
||||
|
||||
EBasicVer b2 = new EBasicVer("b2");
|
||||
server.save(b2, txn);
|
||||
|
||||
Integer id = b1.getId();
|
||||
assertNotNull(id);
|
||||
|
||||
EBasicVer b3 = new EBasicVer("b3");
|
||||
server.save(b3, txn);
|
||||
|
||||
txn.commit();
|
||||
|
||||
} finally {
|
||||
txn.end();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFlushOnGetProperty() {
|
||||
|
||||
EbeanServer server = Ebean.getDefaultServer();
|
||||
Transaction txn = server.beginTransaction();
|
||||
try {
|
||||
txn.setBatch(PersistBatch.ALL);
|
||||
|
||||
EBasicVer b1 = new EBasicVer("b1");
|
||||
server.save(b1, txn);
|
||||
|
||||
EBasicVer b2 = new EBasicVer("b2");
|
||||
server.save(b2, txn);
|
||||
|
||||
// flush here
|
||||
Timestamp lastUpdate = b1.getLastUpdate();
|
||||
assertNotNull(lastUpdate);
|
||||
|
||||
EBasicVer b3 = new EBasicVer("b3");
|
||||
server.save(b3, txn);
|
||||
|
||||
txn.commit();
|
||||
|
||||
} finally {
|
||||
txn.end();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFlushOnSetProperty() {
|
||||
|
||||
EbeanServer server = Ebean.getDefaultServer();
|
||||
Transaction txn = server.beginTransaction();
|
||||
try {
|
||||
txn.setBatch(PersistBatch.ALL);
|
||||
|
||||
EBasicVer b1 = new EBasicVer("b1");
|
||||
server.save(b1, txn);
|
||||
|
||||
EBasicVer b2 = new EBasicVer("b2");
|
||||
server.save(b2, txn);
|
||||
|
||||
// flush here
|
||||
b1.setDescription("modify");
|
||||
|
||||
EBasicVer b3 = new EBasicVer("b3");
|
||||
server.save(b3, txn);
|
||||
|
||||
txn.commit();
|
||||
|
||||
} finally {
|
||||
txn.end();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,8 +26,10 @@ public class TestLazyLoadEmptyCollection extends BaseTestCase {
|
||||
|
||||
Ebean.save(c);
|
||||
|
||||
List<Customer> list = Ebean.find(Customer.class).fetch("contacts", new FetchConfig().query(0))
|
||||
.fetch("contacts.notes", new FetchConfig().query(100)).findList();
|
||||
List<Customer> list = Ebean.find(Customer.class)
|
||||
.fetch("contacts", new FetchConfig().query(0))
|
||||
.fetch("contacts.notes", new FetchConfig().query(100))
|
||||
.findList();
|
||||
|
||||
for (Customer customer : list) {
|
||||
List<Contact> contacts = customer.getContacts();
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.avaje.tests.batchload;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.avaje.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -14,8 +15,60 @@ import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestSecondaryQueries extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void fetchQuery() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
Ebean.find(Order.class)
|
||||
.select("status")
|
||||
.fetchQuery("customer", "name")
|
||||
.findList();
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
assertThat(sql).hasSize(2);
|
||||
assertThat(sql.get(0)).contains("select t0.id c0, t0.status c1, t0.kcustomer_id c2 from o_order t0");
|
||||
assertThat(sql.get(1)).contains("select t0.id c0, t0.name c1 from o_customer t0 where t0.id in");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fetchLazy() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
List<Order> orders = Ebean.find(Order.class)
|
||||
.select("status")
|
||||
.fetchLazy("customer", "name")
|
||||
.setMaxRows(10)
|
||||
.setUseCache(false)
|
||||
.findList();
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
assertThat(sql).hasSize(1);
|
||||
assertThat(sql.get(0)).contains("select t0.id c0, t0.status c1, t0.kcustomer_id c2 from o_order t0");
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
// invoke lazy loading
|
||||
for (Order order : orders) {
|
||||
order.getCustomer().getName();
|
||||
}
|
||||
|
||||
sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(1);
|
||||
assertThat(sql.get(0)).contains("select t0.id c0, t0.name c1 from o_customer t0 where t0.id in");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSecQueryOneToMany() {
|
||||
|
||||
|
||||
@@ -17,12 +17,12 @@ public class TestQueryCacheInsert extends BaseTestCase {
|
||||
|
||||
EbeanServer server = Ebean.getServer(null);
|
||||
|
||||
EBasicVer account = new EBasicVer();
|
||||
EBasicVer account = new EBasicVer("junk");
|
||||
server.save(account);
|
||||
|
||||
List<EBasicVer> alist0 = server.find(EBasicVer.class).setUseQueryCache(true).findList();
|
||||
|
||||
EBasicVer a2 = new EBasicVer();
|
||||
EBasicVer a2 = new EBasicVer("junk2");
|
||||
server.save(a2);
|
||||
awaitL2Cache();
|
||||
|
||||
|
||||
@@ -31,8 +31,7 @@ public class TestDeleteWithoutOptimisticLocking extends BaseTestCase {
|
||||
@Test
|
||||
public void testSimpleBeanDelete_existingBean_returnsTrue() {
|
||||
|
||||
EBasicVer basic = new EBasicVer();
|
||||
basic.setName("DelTest");
|
||||
EBasicVer basic = new EBasicVer("DelTest");
|
||||
Ebean.save(basic);
|
||||
|
||||
EBasicVer basicRef = Ebean.getReference(EBasicVer.class, basic.getId());
|
||||
@@ -43,8 +42,7 @@ public class TestDeleteWithoutOptimisticLocking extends BaseTestCase {
|
||||
@Test
|
||||
public void testSimpleBeanDelete_existingBeanWithJdbcBatch_returnsTrue() {
|
||||
|
||||
EBasicVer basic = new EBasicVer();
|
||||
basic.setName("DelTestBatch");
|
||||
EBasicVer basic = new EBasicVer("DelTestBatch");
|
||||
Ebean.save(basic);
|
||||
|
||||
EbeanServer server = Ebean.getDefaultServer();
|
||||
|
||||
@@ -39,7 +39,7 @@ public class LinkQueryPublishTest {
|
||||
.setMaxRows(10)
|
||||
.findPagedList();
|
||||
|
||||
assertThat(pagedList.getTotalRowCount()).isEqualTo(3);
|
||||
assertThat(pagedList.getTotalCount()).isEqualTo(3);
|
||||
assertThat(pagedList.getList()).hasSize(3);
|
||||
|
||||
|
||||
|
||||
@@ -15,8 +15,7 @@ public class TestInsertQueryUpdate extends BaseTestCase {
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
EBasicVer e0 = new EBasicVer();
|
||||
e0.setName("name0");
|
||||
EBasicVer e0 = new EBasicVer("name0");
|
||||
e0.setDescription("desc0");
|
||||
Ebean.save(e0);
|
||||
|
||||
|
||||
@@ -15,8 +15,7 @@ public class TestInsertUpdateTrans extends BaseTestCase {
|
||||
Ebean.beginTransaction();
|
||||
try {
|
||||
|
||||
EBasicVer e0 = new EBasicVer();
|
||||
e0.setName("onInsert");
|
||||
EBasicVer e0 = new EBasicVer("onInsert");
|
||||
e0.setDescription("something");
|
||||
Ebean.save(e0);
|
||||
|
||||
|
||||
@@ -39,13 +39,13 @@ public class TestM2MDeleteWithCascade extends BaseTestCase {
|
||||
List<Object> roleIds = new ArrayList<Object>();
|
||||
Collections.addAll(roleIds, r0.getRoleid(), r1.getRoleid());
|
||||
|
||||
int rc = Ebean.find(MRole.class).where().idIn(roleIds).findRowCount();
|
||||
int rc = Ebean.find(MRole.class).where().idIn(roleIds).findCount();
|
||||
|
||||
Assert.assertEquals("roles not deleted", 2, rc);
|
||||
|
||||
Ebean.deleteAll(roles);
|
||||
|
||||
rc = Ebean.find(MRole.class).where().idIn(roleIds).findRowCount();
|
||||
rc = Ebean.find(MRole.class).where().idIn(roleIds).findCount();
|
||||
|
||||
Assert.assertEquals("roles deleted now", 0, rc);
|
||||
}
|
||||
|
||||
@@ -25,6 +25,10 @@ public class EBasicVer {
|
||||
@Version
|
||||
Timestamp lastUpdate;
|
||||
|
||||
public EBasicVer(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ public class ResetBasicData {
|
||||
|
||||
server.execute(new TxRunnable() {
|
||||
public void run() {
|
||||
if (server.find(Product.class).findRowCount() > 0) {
|
||||
if (server.find(Product.class).findCount() > 0) {
|
||||
// we can't really delete this base data as
|
||||
// the test rely on the products being in there
|
||||
return;
|
||||
@@ -72,7 +72,7 @@ public class ResetBasicData {
|
||||
|
||||
public void insertCountries() {
|
||||
|
||||
if (server.find(Country.class).findRowCount() > 0) {
|
||||
if (server.find(Country.class).findCount() > 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -94,7 +94,7 @@ public class ResetBasicData {
|
||||
|
||||
public void insertProducts() {
|
||||
|
||||
if (server.find(Product.class).findRowCount() > 0) {
|
||||
if (server.find(Product.class).findCount() > 0) {
|
||||
return;
|
||||
}
|
||||
server.execute(new TxRunnable() {
|
||||
|
||||
@@ -10,6 +10,7 @@ import org.junit.Test;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class TestInsertBatchThenUpdate extends BaseTestCase {
|
||||
@@ -24,6 +25,8 @@ public class TestInsertBatchThenUpdate extends BaseTestCase {
|
||||
try {
|
||||
txn.setBatch(PersistBatch.ALL);
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
EdParent parent = new EdParent();
|
||||
parent.setName("MyComputer");
|
||||
|
||||
@@ -36,21 +39,17 @@ public class TestInsertBatchThenUpdate extends BaseTestCase {
|
||||
|
||||
Ebean.save(parent);
|
||||
|
||||
// nothing flushed yet
|
||||
List<String> loggedSql0 = LoggedSqlCollector.start();
|
||||
assertEquals(0, loggedSql0.size());
|
||||
|
||||
parent.setName("MyDesk");
|
||||
Ebean.save(parent);
|
||||
|
||||
// nothing flushed yet
|
||||
assertEquals(0, LoggedSqlCollector.start().size());
|
||||
|
||||
Ebean.commitTransaction();
|
||||
|
||||
// insert statements for EdExtendedParent
|
||||
List<String> loggedSql2 = LoggedSqlCollector.start();
|
||||
assertEquals(2, loggedSql2.size());
|
||||
List<String> loggedSql = LoggedSqlCollector.stop();
|
||||
assertEquals(3, loggedSql.size());
|
||||
assertThat(loggedSql.get(0)).contains("insert into td_parent");
|
||||
assertThat(loggedSql.get(1)).contains("insert into td_child ");
|
||||
assertThat(loggedSql.get(2)).contains("update td_parent set parent_name=? where parent_id=?");
|
||||
|
||||
} finally {
|
||||
Ebean.endTransaction();
|
||||
|
||||
@@ -19,6 +19,13 @@ public class MnyB extends BaseModel {
|
||||
@ManyToMany(cascade = CascadeType.REMOVE)
|
||||
List<MnyC> cs;
|
||||
|
||||
public MnyB(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public MnyB() {
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ public class TestInsertNoIdBean extends BaseTestCase {
|
||||
|
||||
Ebean.save(bean);
|
||||
|
||||
int rowCount = Ebean.find(NoIdBean.class).findRowCount();
|
||||
int rowCount = Ebean.find(NoIdBean.class).findCount();
|
||||
|
||||
assertTrue("rowCount:"+rowCount, rowCount > 0);
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ public class TestTextJsonSelfRef extends BaseTestCase {
|
||||
Ebean.execute(new TxRunnable() {
|
||||
public void run() {
|
||||
|
||||
if (Ebean.find(SelfRefCustomer.class).findRowCount() == 0) {
|
||||
if (Ebean.find(SelfRefCustomer.class).findCount() == 0) {
|
||||
SelfRefCustomer c1 = new SelfRefCustomer();
|
||||
c1.setName("Foo");
|
||||
c1.setReferredBy(c1);
|
||||
|
||||
+1
-2
@@ -16,8 +16,7 @@ public class TestPersistenceContextQueryScope extends BaseTestCase {
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
EBasicVer bean = new EBasicVer();
|
||||
bean.setName("first");
|
||||
EBasicVer bean = new EBasicVer("first");
|
||||
Ebean.save(bean);
|
||||
|
||||
//Ebean.getServerCacheManager().setCaching(EBasicVer.class, true);
|
||||
|
||||
@@ -48,7 +48,7 @@ public class TestQueryFindFutureList extends BaseTestCase {
|
||||
// wait for it to complete
|
||||
List<Order> orders = futureList.getUnchecked();
|
||||
|
||||
assertEquals(Ebean.find(Order.class).findRowCount(), orders.size());
|
||||
assertEquals(Ebean.find(Order.class).findCount(), orders.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -61,7 +61,7 @@ public class TestQueryFindFutureList extends BaseTestCase {
|
||||
// wait for it to complete
|
||||
List<Order> orders = futureList.getUnchecked(1, TimeUnit.SECONDS);
|
||||
|
||||
assertEquals(Ebean.find(Order.class).findRowCount(), orders.size());
|
||||
assertEquals(Ebean.find(Order.class).findCount(), orders.size());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -27,6 +27,32 @@ public class TestQueryFindIterate extends BaseTestCase {
|
||||
|
||||
EbeanServer server = Ebean.getServer(null);
|
||||
|
||||
Query<Customer> query = server.find(Customer.class)
|
||||
.setMaxRows(2);
|
||||
|
||||
final AtomicInteger count = new AtomicInteger();
|
||||
|
||||
QueryIterator<Customer> it = query.findIterate();
|
||||
try {
|
||||
while (it.hasNext()) {
|
||||
Customer customer = it.next();
|
||||
customer.getName();
|
||||
count.incrementAndGet();
|
||||
}
|
||||
} finally {
|
||||
it.close();
|
||||
}
|
||||
|
||||
assertEquals(2, count.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findEach() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
EbeanServer server = Ebean.getServer(null);
|
||||
|
||||
Query<Customer> query = server.find(Customer.class)
|
||||
.setAutoTune(false)
|
||||
//.fetch("contacts", new FetchConfig().query(2)).where().gt("id", 0).orderBy("id")
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.Product;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class TestQueryFindMapTypedKey extends BaseTestCase {
|
||||
|
||||
@@ -17,9 +19,21 @@ public class TestQueryFindMapTypedKey extends BaseTestCase {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Map<String, Customer> map = Ebean.find(Customer.class).select("id, name")
|
||||
.findMap("name", String.class);
|
||||
Map<String, Product> productsBySku = Ebean.find(Product.class)
|
||||
.setMapKey("sku")
|
||||
.findMap();
|
||||
|
||||
assertThat(productsBySku).isNotEmpty();
|
||||
|
||||
Product desk = productsBySku.get("DSK1");
|
||||
assertNotNull(desk);
|
||||
|
||||
Map<String, Customer> map = Ebean.find(Customer.class)
|
||||
.select("id, name")
|
||||
.setMapKey("name")
|
||||
.findMap();
|
||||
|
||||
assertNotNull(map);
|
||||
|
||||
Assert.assertNotNull(map);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,7 +83,7 @@ public class TestQueryFindPagedList extends BaseTestCase {
|
||||
|
||||
pagedList.loadRowCount();
|
||||
List<Order> orders = pagedList.getList();
|
||||
int totalRowCount = pagedList.getTotalRowCount();
|
||||
int totalRowCount = pagedList.getTotalCount();
|
||||
|
||||
assertThat(orders.size()).isLessThan(totalRowCount);
|
||||
assertTrue(pagedList.hasNext());
|
||||
@@ -96,9 +96,9 @@ public class TestQueryFindPagedList extends BaseTestCase {
|
||||
.setMaxRows(3)
|
||||
.findPagedList();
|
||||
|
||||
pagedList2.loadRowCount();
|
||||
pagedList2.loadCount();
|
||||
List<Order> orders2 = pagedList2.getList();
|
||||
int totalRowCount2 = pagedList2.getTotalRowCount();
|
||||
int totalRowCount2 = pagedList2.getTotalCount();
|
||||
assertTrue(pagedList2.hasNext());
|
||||
assertTrue(pagedList2.hasPrev());
|
||||
|
||||
@@ -161,11 +161,11 @@ public class TestQueryFindPagedList extends BaseTestCase {
|
||||
|
||||
PagedList<Order> pagedList = Ebean.find(Order.class).setMaxRows(3).findPagedList();
|
||||
|
||||
Future<Integer> rowCount = pagedList.getFutureRowCount();
|
||||
Future<Integer> rowCount = pagedList.getFutureCount();
|
||||
List<Order> orders = pagedList.getList();
|
||||
|
||||
// these are each getting the total row count
|
||||
int totalRowCount = pagedList.getTotalRowCount();
|
||||
int totalRowCount = pagedList.getTotalCount();
|
||||
Integer totalRowCountWithTimeout = rowCount.get(30, TimeUnit.SECONDS);
|
||||
Integer totalRowCountViaFuture = rowCount.get();
|
||||
|
||||
@@ -183,9 +183,9 @@ public class TestQueryFindPagedList extends BaseTestCase {
|
||||
// fetch less that total orders (page size 3)
|
||||
PagedList<Order> pagedList = Ebean.find(Order.class).setMaxRows(3).findPagedList();
|
||||
|
||||
pagedList.loadRowCount();
|
||||
pagedList.loadCount();
|
||||
List<Order> orders = pagedList.getList();
|
||||
int totalRowCount = pagedList.getTotalRowCount();
|
||||
int totalRowCount = pagedList.getTotalCount();
|
||||
|
||||
assertThat(orders.size()).isLessThan(totalRowCount);
|
||||
}
|
||||
@@ -207,10 +207,10 @@ public class TestQueryFindPagedList extends BaseTestCase {
|
||||
try {
|
||||
|
||||
List<Order> orders = pagedList.getList();
|
||||
int totalRowCount = pagedList.getTotalRowCount();
|
||||
int totalRowCount = pagedList.getTotalCount();
|
||||
|
||||
// invoke it again but cached...
|
||||
int totalRowCountAgain = pagedList.getTotalRowCount();
|
||||
int totalRowCountAgain = pagedList.getTotalCount();
|
||||
|
||||
List<String> loggedSql = LoggedSqlCollector.stop();
|
||||
|
||||
@@ -242,7 +242,7 @@ public class TestQueryFindPagedList extends BaseTestCase {
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
pagedList.getTotalRowCount();
|
||||
pagedList.getTotalCount();
|
||||
pagedList.getList();
|
||||
|
||||
List<String> loggedSql = LoggedSqlCollector.stop();
|
||||
|
||||
@@ -23,16 +23,16 @@ public class TestQueryPlanCacheRowCount extends BaseTestCase {
|
||||
|
||||
int rc0 = query.findRowCount();
|
||||
|
||||
List<Object> ids = query.findIds();
|
||||
List<Integer> ids = query.findIds();
|
||||
Assert.assertEquals(rc0, ids.size());
|
||||
|
||||
List<Order> list0 = query.findList();
|
||||
Assert.assertEquals(rc0, list0.size());
|
||||
|
||||
int rc1 = query.findRowCount();
|
||||
int rc1 = query.findCount();
|
||||
Assert.assertEquals(rc0, rc1);
|
||||
|
||||
List<Object> ids1 = query.findIds();
|
||||
List<Integer> ids1 = query.findIds();
|
||||
Assert.assertEquals(rc0, ids1.size());
|
||||
|
||||
List<Order> list1 = query.findList();
|
||||
@@ -48,12 +48,12 @@ public class TestQueryPlanCacheRowCount extends BaseTestCase {
|
||||
Query<Order> query2 = Ebean.find(Order.class).where().eq("status", Order.Status.NEW)
|
||||
.ge("id", idGt).order().desc("id");
|
||||
|
||||
int rc2 = query2.findRowCount();
|
||||
int rc2 = query2.findCount();
|
||||
|
||||
System.out.println("Expection Not same " + rc0 + " != " + rc2);
|
||||
Assert.assertNotSame(rc0, rc2);
|
||||
|
||||
List<Object> ids2 = query2.findIds();
|
||||
List<Integer> ids2 = query2.findIds();
|
||||
Assert.assertEquals(rc2, ids2.size());
|
||||
|
||||
List<Order> list2 = query2.findList();
|
||||
|
||||
@@ -21,7 +21,7 @@ public class TestRowCount extends BaseTestCase {
|
||||
Query<Order> query = Ebean.find(Order.class).fetch("details").where().gt("id", 1)
|
||||
.gt("details.id", 1).order("id desc");
|
||||
|
||||
int rc = query.findRowCount();
|
||||
int rc = query.findCount();
|
||||
|
||||
List<Object> ids = query.findIds();
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@ public class TestDisjunctWhereOuterOnMany extends BaseTestCase {
|
||||
.query();
|
||||
|
||||
List<UUOne> list = query.findList();
|
||||
int rowCount = query.findRowCount();
|
||||
int rowCount = query.findCount();
|
||||
|
||||
// select distinct t0.id c0, t0.name c1
|
||||
// from uuone t0
|
||||
|
||||
@@ -20,8 +20,7 @@ public class TestFindIdsWithInheritance extends BaseTestCase {
|
||||
|
||||
Ebean.save(truck);
|
||||
|
||||
List<Object> ids = Ebean.find(Vehicle.class).findIds();
|
||||
|
||||
List<Integer> ids = Ebean.find(Vehicle.class).findIds();
|
||||
Assert.assertNotNull(ids);
|
||||
|
||||
Ebean.delete(truck);
|
||||
|
||||
@@ -31,7 +31,7 @@ public class TestFormulaWithFindCount extends BaseTestCase {
|
||||
}
|
||||
|
||||
ExpressionList<Order> expressionList = server.find(Order.class).where().gt("totalAmount", 1d);
|
||||
int rowCount = expressionList.findRowCount();
|
||||
int rowCount = expressionList.findCount();
|
||||
Assert.assertEquals(list.size(), rowCount);
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ public class TestObjectGraphNodeStatsCollection extends BaseTestCase {
|
||||
|
||||
MetaInfoManager infoManager = server.getMetaInfoManager();
|
||||
|
||||
server.find(Order.class).findRowCount();
|
||||
server.find(Order.class).findCount();
|
||||
|
||||
infoManager.collectNodeStatistics(true);
|
||||
infoManager.collectQueryPlanStatistics(true);
|
||||
@@ -95,7 +95,7 @@ public class TestObjectGraphNodeStatsCollection extends BaseTestCase {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Object> ids = Ebean.find(Order.class).findIds();
|
||||
List<Integer> ids = Ebean.find(Order.class).findIds();
|
||||
Assert.assertTrue(!ids.isEmpty());
|
||||
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ public class TestQueryConversationRowCount extends BaseTestCase {
|
||||
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
query.findRowCount();
|
||||
query.findCount();
|
||||
|
||||
// select count(*) from (
|
||||
// select distinct t0.id c0
|
||||
|
||||
@@ -1,10 +1,5 @@
|
||||
package com.avaje.tests.query.other;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
@@ -12,6 +7,14 @@ import com.avaje.ebean.bean.EntityBean;
|
||||
import com.avaje.ebean.bean.EntityBeanIntercept;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static junit.framework.TestCase.assertNull;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class TestQueryDistinct extends BaseTestCase {
|
||||
|
||||
@@ -27,17 +30,17 @@ public class TestQueryDistinct extends BaseTestCase {
|
||||
List<Customer> customers = query.findList();
|
||||
|
||||
String generatedSql = query.getGeneratedSql();
|
||||
Assert.assertTrue(generatedSql.contains("select distinct t0.name c0 from o_customer t0"));
|
||||
assertThat(generatedSql).contains("select distinct t0.name c0 from o_customer t0");
|
||||
|
||||
for (Customer customer : customers) {
|
||||
|
||||
EntityBeanIntercept ebi = ((EntityBean)customer)._ebean_getIntercept();
|
||||
Assert.assertTrue(ebi.isDisableLazyLoad());
|
||||
Assert.assertNull(ebi.getPersistenceContext());
|
||||
assertTrue(ebi.isDisableLazyLoad());
|
||||
assertNull(ebi.getPersistenceContext());
|
||||
|
||||
// lazy loading disabled
|
||||
Assert.assertNull(customer.getId());
|
||||
Assert.assertNull(customer.getAnniversary());
|
||||
assertNull(customer.getId());
|
||||
assertNull(customer.getAnniversary());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,7 +56,7 @@ public class TestQueryDistinct extends BaseTestCase {
|
||||
query.findList();
|
||||
|
||||
String generatedSql = query.getGeneratedSql();
|
||||
Assert.assertTrue(generatedSql.contains("select distinct t0.name c0 from o_customer t0"));
|
||||
assertThat(generatedSql).contains("select distinct t0.name c0 from o_customer t0");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -69,15 +72,33 @@ public class TestQueryDistinct extends BaseTestCase {
|
||||
List<Customer> customers = query.findList();
|
||||
|
||||
String generatedSql = query.getGeneratedSql();
|
||||
Assert.assertTrue(generatedSql.contains("select distinct t0.status c0 from o_customer t0"));
|
||||
assertThat(generatedSql).contains("select distinct t0.status c0 from o_customer t0");
|
||||
|
||||
for (Customer customer : customers) {
|
||||
|
||||
Assert.assertNotNull(customer.getStatus());
|
||||
assertNotNull(customer.getStatus());
|
||||
|
||||
// lazy loading disabled
|
||||
Assert.assertNull(customer.getId());
|
||||
Assert.assertNull(customer.getAnniversary());
|
||||
assertNull(customer.getId());
|
||||
assertNull(customer.getAnniversary());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPagingQuery_expect_doesNotAddOrderBy() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = Ebean.find(Customer.class)
|
||||
.setMaxRows(10)
|
||||
.setDistinct(true)
|
||||
.select("name");
|
||||
|
||||
query.findList();
|
||||
|
||||
if (isH2() || isPostgres()) {
|
||||
String generatedSql = query.getGeneratedSql();
|
||||
assertThat(generatedSql).contains("select distinct t0.name c0 from o_customer t0 limit 10");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.avaje.tests.query.other;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
import org.avaje.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestQueryRawExpressionMany extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Integer quantity = 1;
|
||||
|
||||
Query<Order> query = Ebean.find(Order.class)
|
||||
.where().raw("details.orderQty = ?", quantity)
|
||||
.query();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
query.findCount();
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
assertThat(sql.get(0)).contains("select count(*) from ( select distinct t0.id c0 from o_order t0 left outer join o_order_detail t1 on t1.order_id = t0.id where t1.order_qty = ?)");
|
||||
}
|
||||
}
|
||||
@@ -47,7 +47,7 @@ public class TestQueryRowCountWithMany extends BaseTestCase {
|
||||
Assert.assertTrue(generatedSql.contains(" order by t0.cretime"));
|
||||
|
||||
|
||||
int rowCount = query.findRowCount();
|
||||
int rowCount = query.findCount();
|
||||
|
||||
// select count(*) from o_order t0
|
||||
// left outer join o_order_detail t1 on t1.order_id = t0.id
|
||||
@@ -84,7 +84,7 @@ public class TestQueryRowCountWithMany extends BaseTestCase {
|
||||
.orderBy("cretime asc");
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
query.findRowCount();
|
||||
query.findCount();
|
||||
|
||||
List<String> sqlLogged = LoggedSqlCollector.stop();
|
||||
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
package com.avaje.tests.query.other;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestQuerySingleAttribute extends BaseTestCase {
|
||||
|
||||
|
||||
@Test
|
||||
public void exampleUsage() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<String> names =
|
||||
Ebean.find(Customer.class)
|
||||
.setDistinct(true)
|
||||
.select("name")
|
||||
.where().eq("status", Customer.Status.NEW)
|
||||
.orderBy().asc("name")
|
||||
.setMaxRows(100)
|
||||
.findSingleAttributeList();
|
||||
|
||||
assertThat(names).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exampleUsage_otherType() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Date> dates =
|
||||
Ebean.find(Customer.class)
|
||||
.setDistinct(true)
|
||||
.select("anniversary")
|
||||
.where().isNotNull("anniversary")
|
||||
.orderBy().asc("anniversary")
|
||||
.findSingleAttributeList();
|
||||
|
||||
assertThat(dates).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withOrderBy() {
|
||||
|
||||
Query<Customer> query =
|
||||
Ebean.find(Customer.class)
|
||||
.setDistinct(true)
|
||||
.select("name")
|
||||
.where().eq("status", Customer.Status.NEW)
|
||||
.orderBy().asc("name")
|
||||
.setMaxRows(100);
|
||||
|
||||
query.findSingleAttributeList();
|
||||
assertThat(query.getGeneratedSql()).contains("select distinct t0.name c0 from o_customer t0 where t0.status = ? order by t0.name ");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void basic() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = Ebean.find(Customer.class).select("name");
|
||||
|
||||
List<String> names = query.findSingleAttributeList();//String.class);
|
||||
|
||||
assertThat(query.getGeneratedSql()).contains("select t0.name c0 from o_customer t0");
|
||||
assertThat(names).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void distinctAndWhere() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = Ebean.find(Customer.class)
|
||||
.setDistinct(true)
|
||||
.select("name")
|
||||
.where().eq("status", Customer.Status.NEW)
|
||||
.query();
|
||||
|
||||
List<String> names = query.findSingleAttributeList();
|
||||
|
||||
assertThat(query.getGeneratedSql()).contains("select distinct t0.name c0 from o_customer t0 where t0.status = ? ");
|
||||
assertThat(names).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void distinctWhereWithJoin() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = Ebean.find(Customer.class)
|
||||
.setDistinct(true)
|
||||
.select("name")
|
||||
.where().eq("status", Customer.Status.NEW)
|
||||
.istartsWith("billingAddress.city", "auck")
|
||||
.query();
|
||||
|
||||
List<String> names = query.findSingleAttributeList();
|
||||
|
||||
assertThat(query.getGeneratedSql()).contains("select distinct t0.name c0 from o_customer t0 left outer join o_address t1 on t1.id = t0.billing_address_id where t0.status = ? and lower(t1.city) like ?");
|
||||
assertThat(names).isNotNull();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void queryPlan_expect_differentPlans() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = Ebean.find(Customer.class).select("name");
|
||||
query.findSingleAttributeList();
|
||||
assertThat(query.getGeneratedSql()).contains("select t0.name c0 from o_customer t0");
|
||||
|
||||
Query<Customer> query2 = Ebean.find(Customer.class).select("name");
|
||||
query2.findList();
|
||||
assertThat(query2.getGeneratedSql()).contains("select t0.id c0, t0.name c1 from o_customer t0");
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ public class TestSelfParent extends BaseTestCase {
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
if (Ebean.find(SelfParent.class).findRowCount() > 0) {
|
||||
if (Ebean.find(SelfParent.class).findCount() > 0) {
|
||||
// only run once
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ public class TestSoftDeletePagingList {
|
||||
.setMaxRows(10)
|
||||
.findPagedList();
|
||||
|
||||
int totalRowCount = pagedList.getTotalRowCount();
|
||||
int totalRowCount = pagedList.getTotalCount();
|
||||
List<Cover> resultList = pagedList.getList();
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
@@ -111,7 +111,7 @@ public class SqlQueryTests extends BaseTestCase {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
int expectedRows = Ebean.find(Order.class).findRowCount();
|
||||
int expectedRows = Ebean.find(Order.class).findCount();
|
||||
|
||||
final AtomicInteger count = new AtomicInteger();
|
||||
|
||||
|
||||
@@ -56,7 +56,7 @@ public class TestOrderReportTotal extends BaseTestCase {
|
||||
.where()
|
||||
.gt("order.id", 2)
|
||||
.istartsWith("order.customer.name","rob")
|
||||
.findRowCount();
|
||||
.findCount();
|
||||
|
||||
assertThat(detailsCount).isGreaterThan(0);
|
||||
}
|
||||
|
||||
@@ -78,14 +78,14 @@ public class TestRawSqlOrmQuery extends BaseTestCase {
|
||||
Query<Customer> query = Ebean.find(Customer.class);
|
||||
query.setRawSql(rawSql);
|
||||
|
||||
int initialRowCount = query.findRowCount();
|
||||
int initialRowCount = query.findCount();
|
||||
|
||||
query.setFirstRow(1);
|
||||
query.setMaxRows(2);
|
||||
List<Customer> list = query.findList();
|
||||
|
||||
int rowCount = query.findRowCount();
|
||||
FutureRowCount<Customer> futureRowCount = query.findFutureRowCount();
|
||||
int rowCount = query.findCount();
|
||||
FutureRowCount<Customer> futureRowCount = query.findFutureCount();
|
||||
|
||||
Assert.assertEquals(initialRowCount, rowCount);
|
||||
Assert.assertEquals(initialRowCount, futureRowCount.get().intValue());
|
||||
@@ -110,12 +110,12 @@ public class TestRawSqlOrmQuery extends BaseTestCase {
|
||||
Query<Customer> query = Ebean.find(Customer.class);
|
||||
query.setRawSql(rawSql);
|
||||
|
||||
int initialRowCount = query.findRowCount();
|
||||
int initialRowCount = query.findCount();
|
||||
|
||||
PagedList<Customer> page = query.setMaxRows(2).findPagedList();
|
||||
|
||||
List<Customer> list = page.getList();
|
||||
int rowCount = page.getTotalRowCount();
|
||||
int rowCount = page.getTotalCount();
|
||||
|
||||
Assert.assertEquals(2, list.size());
|
||||
Assert.assertEquals(initialRowCount, rowCount);
|
||||
@@ -186,7 +186,7 @@ public class TestRawSqlOrmQuery extends BaseTestCase {
|
||||
query.order("id desc");
|
||||
PagedList<Order> pagedList = query.findPagedList();
|
||||
pagedList.getList();
|
||||
pagedList.getTotalRowCount();
|
||||
pagedList.getTotalCount();
|
||||
|
||||
assertThat(query.getGeneratedSql()).contains("order by o.id desc limit 100");
|
||||
}
|
||||
|
||||
@@ -257,7 +257,7 @@ public class TestReadAudit extends BaseTestCase {
|
||||
|
||||
resetCounters();
|
||||
|
||||
Map<?,EBasicChangeLog> list = server.find(EBasicChangeLog.class)
|
||||
Map<Long,EBasicChangeLog> list = server.find(EBasicChangeLog.class)
|
||||
.where().startsWith("shortDescription", "readAudit")
|
||||
.findMap();
|
||||
|
||||
|
||||
@@ -71,7 +71,7 @@ public class TestSoftDeleteBasic extends BaseTestCase {
|
||||
bean.setName("two");
|
||||
Ebean.save(bean);
|
||||
|
||||
int rowCountBefore = Ebean.find(EBasicSoftDelete.class).findRowCount();
|
||||
int rowCountBefore = Ebean.find(EBasicSoftDelete.class).findCount();
|
||||
|
||||
Ebean.delete(EBasicSoftDelete.class, bean.getId());
|
||||
|
||||
@@ -79,7 +79,7 @@ public class TestSoftDeleteBasic extends BaseTestCase {
|
||||
// -- test .findRowCount()
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
int rowCountAfter = Ebean.find(EBasicSoftDelete.class).findRowCount();
|
||||
int rowCountAfter = Ebean.find(EBasicSoftDelete.class).findCount();
|
||||
|
||||
List<String> loggedSql = LoggedSqlCollector.stop();
|
||||
assertThat(loggedSql).hasSize(1);
|
||||
@@ -91,7 +91,7 @@ public class TestSoftDeleteBasic extends BaseTestCase {
|
||||
// -- test includeSoftDeletes().findRowCount()
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
int rowCountFull = Ebean.find(EBasicSoftDelete.class).setIncludeSoftDeletes().findRowCount();
|
||||
int rowCountFull = Ebean.find(EBasicSoftDelete.class).setIncludeSoftDeletes().findCount();
|
||||
assertThat(rowCountFull).isGreaterThan(rowCountAfter);
|
||||
|
||||
loggedSql = LoggedSqlCollector.stop();
|
||||
@@ -197,7 +197,7 @@ public class TestSoftDeleteBasic extends BaseTestCase {
|
||||
Ebean.find(EBasicSoftDelete.class)
|
||||
.setId(bean.getId())
|
||||
.where()
|
||||
.includeSoftDeletes()
|
||||
.setIncludeSoftDeletes()
|
||||
.findUnique();
|
||||
|
||||
assertThat(fetchAllWithLazy.getChildren()).hasSize(3);
|
||||
|
||||
@@ -41,7 +41,7 @@ public class TestJsonMap extends BaseTestCase {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Map<String, Customer> map = Ebean.find(Customer.class).findMap("id", String.class);
|
||||
Map<String, Customer> map = Ebean.find(Customer.class).findMap();
|
||||
|
||||
JsonContext jsonContext = Ebean.json();
|
||||
JsonWriteOptions options = JsonWriteOptions.parsePath("(id,status,name)");
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
package com.avaje.tests.transaction;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Transaction;
|
||||
import com.avaje.tests.model.m2m.MnyB;
|
||||
import com.avaje.tests.model.m2m.MnyC;
|
||||
import org.avaje.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class TestBeanStateReset extends BaseTestCase {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(TestBeanStateReset.class);
|
||||
|
||||
@Test
|
||||
public void resetForInsert() {
|
||||
|
||||
// setup to fail foreign key constraint
|
||||
MnyC c = new MnyC();
|
||||
c.setId(Long.MAX_VALUE);
|
||||
|
||||
MnyB b = new MnyB();
|
||||
b.getCs().add(c);
|
||||
|
||||
try {
|
||||
// inserts of b succeeds but intersection insert fails FK check on c
|
||||
b.save();
|
||||
|
||||
} catch (PersistenceException e) {
|
||||
logger.info("expected error " + e.getMessage());
|
||||
|
||||
Ebean.getBeanState(b).resetForInsert();
|
||||
b.getCs().clear();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
b.setName("mod");
|
||||
b.save();
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql.get(0)).contains("insert into mny_b (id, name, version, when_created, when_modified, a_id) values (");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void alternativeVia_persistCascadeOff_with_commitAndContinue() {
|
||||
|
||||
// setup to fail foreign key constraint when using cascade save
|
||||
MnyC c = new MnyC();
|
||||
c.setId(Long.MAX_VALUE);
|
||||
|
||||
MnyB b = new MnyB();
|
||||
b.getCs().add(c);
|
||||
|
||||
Transaction transaction = Ebean.beginTransaction();
|
||||
|
||||
try {
|
||||
// turn off cascade ...
|
||||
transaction.setPersistCascade(false);
|
||||
b.save();
|
||||
|
||||
// commit at this point
|
||||
transaction.commitAndContinue();
|
||||
try {
|
||||
|
||||
// turn on cascade ... such that the ManyToMany is persisted
|
||||
transaction.setPersistCascade(true);
|
||||
|
||||
// save b again which this time cascades to the ManyToMany
|
||||
// but this fails due to FK on ManyToMany
|
||||
b.save();
|
||||
|
||||
// we actually don't get here due to the FK error
|
||||
transaction.commit();
|
||||
|
||||
} catch (PersistenceException e) {
|
||||
// so we failed the second save but that is ok'ish
|
||||
// we handle this exception knowing b got inserted and committed
|
||||
// and that the inserts into the intersection table failed
|
||||
logger.info("The ManyToMany intersection error: " + e.getMessage());
|
||||
}
|
||||
} finally {
|
||||
// performs a rollback as the commit at line:80 does not happen
|
||||
transaction.end();
|
||||
}
|
||||
|
||||
// assert our insert prior to the commitAndContinue succeeded
|
||||
MnyB madeIt = Ebean.find(MnyB.class, b.getId());
|
||||
assertNotNull(madeIt);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
package com.avaje.tests.transaction;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.EbeanServer;
|
||||
import com.avaje.ebean.Transaction;
|
||||
import com.avaje.ebean.annotation.Transactional;
|
||||
import com.avaje.tests.model.m2m.MnyB;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
public class TestCommitAndContinue extends BaseTestCase {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger("org.avaje.ebean.TXN");
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void transactional_partialSuccess() {
|
||||
|
||||
MnyB a = new MnyB("a100");
|
||||
MnyB b = new MnyB("b200");
|
||||
|
||||
a.save();
|
||||
|
||||
// commit at this point
|
||||
Ebean.currentTransaction().commitAndContinue();
|
||||
|
||||
try {
|
||||
b.save();
|
||||
|
||||
// some error occurs
|
||||
throw new IllegalStateException();
|
||||
|
||||
} catch (IllegalStateException e) {
|
||||
// mark the transaction as rollback
|
||||
Ebean.currentTransaction().setRollbackOnly();
|
||||
|
||||
// use a different transaction to assert
|
||||
EbeanServer server = Ebean.getDefaultServer();
|
||||
Transaction anotherTxn = server.createTransaction();
|
||||
|
||||
// success prior to commitAndContinue
|
||||
assertNotNull(server.find(MnyB.class, a.getId(), anotherTxn));
|
||||
|
||||
// insert failed after commitAndContinue
|
||||
assertNull(server.find(MnyB.class, b.getId(), anotherTxn));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The @Transactional is nicer to me.
|
||||
*/
|
||||
@Test
|
||||
public void tryFinally_partialSuccess() {
|
||||
|
||||
MnyB a = new MnyB("a100");
|
||||
MnyB b = new MnyB("b200");
|
||||
|
||||
EbeanServer server = Ebean.getDefaultServer();
|
||||
Transaction txn = server.beginTransaction();
|
||||
try {
|
||||
a.save();
|
||||
// commit at this point
|
||||
txn.commitAndContinue();
|
||||
|
||||
try {
|
||||
b.save();
|
||||
|
||||
// some error occurs
|
||||
throw new IllegalStateException();
|
||||
|
||||
} catch (IllegalStateException e) {
|
||||
// mark the transaction as rollback
|
||||
txn.setRollbackOnly();
|
||||
|
||||
// use a different transaction to assert
|
||||
Transaction anotherTxn = server.createTransaction();
|
||||
// success prior to commitAndContinue
|
||||
assertNotNull(server.find(MnyB.class, a.getId(), anotherTxn));
|
||||
// insert failed after commitAndContinue
|
||||
assertNull(server.find(MnyB.class, b.getId(), anotherTxn));
|
||||
}
|
||||
|
||||
// does not commit due to the txn.setRollbackOnly();
|
||||
txn.commit();
|
||||
|
||||
} finally {
|
||||
server.endTransaction();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void transactional_partialSuccess_secondTransactionInsert() {
|
||||
|
||||
MnyB a = new MnyB("a100");
|
||||
MnyB b = new MnyB("b200");
|
||||
MnyB c = new MnyB("c300");
|
||||
|
||||
a.save();
|
||||
|
||||
// commit at this point
|
||||
Ebean.currentTransaction().commitAndContinue();
|
||||
|
||||
try {
|
||||
b.save();
|
||||
|
||||
// some error occurs
|
||||
throw new IllegalStateException();
|
||||
|
||||
} catch (IllegalStateException e) {
|
||||
// mark the transaction as rollback
|
||||
Ebean.currentTransaction().setRollbackOnly();
|
||||
|
||||
// use a different transaction to do something useful
|
||||
EbeanServer server = Ebean.getDefaultServer();
|
||||
Transaction txn2 = server.createTransaction();
|
||||
try {
|
||||
server.save(c, txn2);
|
||||
txn2.commit();
|
||||
} finally {
|
||||
txn2.end();
|
||||
}
|
||||
}
|
||||
|
||||
// asserts
|
||||
|
||||
EbeanServer server = Ebean.getDefaultServer();
|
||||
Transaction txnForAssert = server.createTransaction();
|
||||
|
||||
// success prior to commitAndContinue
|
||||
assertNotNull(server.find(MnyB.class, a.getId(), txnForAssert));
|
||||
|
||||
// insert failed after commitAndContinue
|
||||
assertNull(server.find(MnyB.class, b.getId(), txnForAssert));
|
||||
|
||||
// successful insert using txn2
|
||||
assertNotNull(server.find(MnyB.class, c.getId(), txnForAssert));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void basic() {
|
||||
|
||||
MnyB a = new MnyB("a");
|
||||
MnyB b = new MnyB("b");
|
||||
MnyB c = new MnyB("c");
|
||||
|
||||
Transaction txn = Ebean.beginTransaction();
|
||||
try {
|
||||
a.save();
|
||||
txn.commitAndContinue();
|
||||
|
||||
txn.setBatchMode(true);
|
||||
b.save();
|
||||
logger.info("... pre commitAndContinue");
|
||||
txn.commitAndContinue();
|
||||
|
||||
c.save();
|
||||
txn.commit();
|
||||
|
||||
} finally {
|
||||
txn.end();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void runTransactional() {
|
||||
|
||||
new MnyB("a100").save();
|
||||
new MnyB("a101").save();
|
||||
|
||||
Ebean.currentTransaction().commitAndContinue();
|
||||
|
||||
new MnyB("a200").save();
|
||||
new MnyB("a201").save();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,9 +18,7 @@ public class TestDeleteFromPersistenceContext extends BaseTestCase {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
EBasicVer bean = new EBasicVer();
|
||||
bean.setName("Please Delete Me");
|
||||
|
||||
EBasicVer bean = new EBasicVer("Please Delete Me");
|
||||
Ebean.save(bean);
|
||||
|
||||
SpiTransaction transaction = (SpiTransaction)Ebean.beginTransaction();
|
||||
|
||||
@@ -70,7 +70,7 @@ public class TestExplicitTransactionMode extends BaseTestCase {
|
||||
}
|
||||
|
||||
// rollback as expected
|
||||
assertEquals(0, ebeanServer.find(UTMaster.class).findRowCount());
|
||||
assertEquals(0, ebeanServer.find(UTMaster.class).findCount());
|
||||
|
||||
UTMaster bean1 = new UTMaster("one1");
|
||||
UTMaster bean2 = new UTMaster("two2");
|
||||
|
||||
@@ -25,11 +25,11 @@ public class TestNestedBeginRequired extends BaseTestCase {
|
||||
Transaction txn = server.beginTransaction(TxScope.required());
|
||||
try {
|
||||
|
||||
server.find(Country.class).findRowCount();
|
||||
server.find(Country.class).findCount();
|
||||
|
||||
someInnerMethod();
|
||||
|
||||
server.find(Product.class).findRowCount();
|
||||
server.find(Product.class).findCount();
|
||||
|
||||
txn.commit();
|
||||
|
||||
@@ -45,7 +45,7 @@ public class TestNestedBeginRequired extends BaseTestCase {
|
||||
Transaction txn = server.beginTransaction(TxScope.required());
|
||||
try {
|
||||
|
||||
server.find(Customer.class).findRowCount();
|
||||
server.find(Customer.class).findCount();
|
||||
|
||||
txn.commit();
|
||||
|
||||
|
||||
@@ -23,12 +23,12 @@ public class TestNestedBeginRequiredWithFailure extends BaseTestCase {
|
||||
Transaction txn = server.beginTransaction(TxScope.required());
|
||||
try {
|
||||
|
||||
server.find(Country.class).findRowCount();
|
||||
server.find(Country.class).findCount();
|
||||
|
||||
try {
|
||||
someInnerMethodWithFailure();
|
||||
|
||||
server.find(Product.class).findRowCount();
|
||||
server.find(Product.class).findCount();
|
||||
txn.commit();
|
||||
|
||||
} catch (RuntimeException e) {
|
||||
@@ -50,7 +50,7 @@ public class TestNestedBeginRequiredWithFailure extends BaseTestCase {
|
||||
Transaction txn = server.beginTransaction(TxScope.required());
|
||||
try {
|
||||
|
||||
server.find(Customer.class).findRowCount();
|
||||
server.find(Customer.class).findCount();
|
||||
|
||||
EBasic basic = new EBasic();
|
||||
basic.setName("ignore");
|
||||
|
||||
@@ -25,11 +25,11 @@ public class TestNestedBeginRequiresNew extends BaseTestCase {
|
||||
Transaction txn = server.beginTransaction(TxScope.requiresNew());
|
||||
try {
|
||||
|
||||
server.find(Country.class).findRowCount();
|
||||
server.find(Country.class).findCount();
|
||||
|
||||
someInnerMethod();
|
||||
|
||||
server.find(Product.class).findRowCount();
|
||||
server.find(Product.class).findCount();
|
||||
|
||||
txn.commit();
|
||||
|
||||
@@ -45,7 +45,7 @@ public class TestNestedBeginRequiresNew extends BaseTestCase {
|
||||
Transaction txn = server.beginTransaction(TxScope.requiresNew());
|
||||
try {
|
||||
|
||||
server.find(Customer.class).findRowCount();
|
||||
server.find(Customer.class).findCount();
|
||||
txn.commit();
|
||||
|
||||
} finally {
|
||||
|
||||
+3
-3
@@ -25,14 +25,14 @@ public class TestNestedBeginRequiresNewWithFailure extends BaseTestCase {
|
||||
Transaction txn = server.beginTransaction(TxScope.requiresNew());
|
||||
try {
|
||||
|
||||
server.find(Country.class).findRowCount();
|
||||
server.find(Country.class).findCount();
|
||||
|
||||
try {
|
||||
someInnerMethodWithFailure();
|
||||
} catch (RuntimeException e) {
|
||||
logger.info("Inner method failed with " + e.getMessage());
|
||||
}
|
||||
server.find(Product.class).findRowCount();
|
||||
server.find(Product.class).findCount();
|
||||
|
||||
txn.commit();
|
||||
|
||||
@@ -48,7 +48,7 @@ public class TestNestedBeginRequiresNewWithFailure extends BaseTestCase {
|
||||
Transaction txn = server.beginTransaction(TxScope.requiresNew());
|
||||
try {
|
||||
|
||||
server.find(Customer.class).findRowCount();
|
||||
server.find(Customer.class).findCount();
|
||||
|
||||
if (server != null) {
|
||||
throw new RuntimeException("barf");
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.avaje.tests.transaction;
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.EbeanServer;
|
||||
import com.avaje.ebean.Transaction;
|
||||
import com.avaje.ebean.TransactionCallbackAdapter;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -27,8 +28,9 @@ public class TestTransactionCallback extends BaseTestCase {
|
||||
public void test_commitAndRollback() {
|
||||
|
||||
|
||||
Ebean.beginTransaction();
|
||||
Transaction txn = Ebean.beginTransaction();
|
||||
Ebean.register(new MyCallback());
|
||||
txn.getConnection();
|
||||
Ebean.commitTransaction();
|
||||
|
||||
assertEquals(1, countPreCommit);
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.avaje.tests.transaction;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Transaction;
|
||||
import com.avaje.ebean.annotation.Transactional;
|
||||
import com.avaje.tests.model.basic.EBasic;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.StrictAssertions.assertThat;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class TestTransactionRollbackOnly {
|
||||
|
||||
private EBasic one;
|
||||
|
||||
private EBasic two;
|
||||
|
||||
@Test
|
||||
public void transaction_setRollbackOnly() {
|
||||
|
||||
doVia_currentTransaction();
|
||||
|
||||
assertThat(one.getId()).isNotNull();
|
||||
assertThat(Ebean.find(EBasic.class, one.getId())).isNull();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
protected void doVia_currentTransaction() {
|
||||
|
||||
one = new EBasic("WillNotSave");
|
||||
Ebean.save(one);
|
||||
|
||||
Transaction transaction = Ebean.currentTransaction();
|
||||
assertFalse(transaction.isRollbackOnly());
|
||||
|
||||
transaction.setRollbackOnly();
|
||||
assertTrue(transaction.isRollbackOnly());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_Ebean_setRollbackOnly() {
|
||||
|
||||
do_Ebean_setRollbackOnly();
|
||||
|
||||
assertThat(two.getId()).isNotNull();
|
||||
assertThat(Ebean.find(EBasic.class, two.getId())).isNull();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
protected void do_Ebean_setRollbackOnly() {
|
||||
|
||||
two = new EBasic("WillNotSave");
|
||||
Ebean.save(two);
|
||||
|
||||
Ebean.setRollbackOnly();
|
||||
}
|
||||
}
|
||||
@@ -39,8 +39,7 @@ public class TestTxTypeOnTransactional extends BaseTestCase {
|
||||
public void testOptimisticException() {
|
||||
|
||||
logger.info("-- testOptimisticException");
|
||||
EBasicVer v = new EBasicVer();
|
||||
v.setName("occ");
|
||||
EBasicVer v = new EBasicVer("occ");
|
||||
v.setDescription("blah");
|
||||
Ebean.save(v);
|
||||
|
||||
|
||||
@@ -14,9 +14,7 @@ public class TestMarkAsDirty extends BaseTestCase {
|
||||
@Test
|
||||
public void test() throws InterruptedException {
|
||||
|
||||
EBasicVer bean = new EBasicVer();
|
||||
bean.setName("markAsDirty");
|
||||
|
||||
EBasicVer bean = new EBasicVer("markAsDirty");
|
||||
Ebean.save(bean);
|
||||
|
||||
Timestamp lastUpdate = bean.getLastUpdate();
|
||||
|
||||
@@ -20,13 +20,11 @@ public class TestUpdateAllLoadedProperties extends BaseTestCase {
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
EBasicVer basic1 = new EBasicVer();
|
||||
basic1.setName("basic1");
|
||||
EBasicVer basic1 = new EBasicVer("basic1");
|
||||
basic1.setDescription("aaa");
|
||||
Ebean.save(basic1);
|
||||
|
||||
EBasicVer basic2 = new EBasicVer();
|
||||
basic1.setName("basic2");
|
||||
EBasicVer basic2 = new EBasicVer("basic2");
|
||||
basic1.setDescription("bbb");
|
||||
Ebean.save(basic2);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user