mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-19 23:02:24 +00:00
Refactor move tests from ebean-core to ebean-test
This commit is contained in:
1 parent
0b00a14fd3
commit
eab56d323b
1650 files changed
+185
-147
No files matched your search
@@ -0,0 +1,51 @@
|
||||
package org.tests.delete;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
@Entity
|
||||
public class DcDetail {
|
||||
|
||||
@Id
|
||||
long id;
|
||||
|
||||
@ManyToOne
|
||||
DcMaster master;
|
||||
|
||||
String description;
|
||||
|
||||
long version;
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public DcMaster getMaster() {
|
||||
return master;
|
||||
}
|
||||
|
||||
public void setMaster(DcMaster master) {
|
||||
this.master = master;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(long version) {
|
||||
this.version = version;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package org.tests.delete;
|
||||
|
||||
import io.ebean.event.AbstractBeanPersistListener;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class DcListener extends AbstractBeanPersistListener {
|
||||
|
||||
static List<Object> deleted = Collections.synchronizedList(new ArrayList<>());
|
||||
|
||||
@Override
|
||||
public boolean isRegisterFor(Class<?> cls) {
|
||||
return cls.equals(DcDetail.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleted(Object bean) {
|
||||
deleted.add(bean);
|
||||
}
|
||||
|
||||
static List<Object> deletedBeans() {
|
||||
List<Object> copy = new ArrayList<>(deleted);
|
||||
deleted.clear();
|
||||
return copy;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package org.tests.delete;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Version;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class DcMaster {
|
||||
|
||||
@Id
|
||||
long id;
|
||||
|
||||
String name;
|
||||
|
||||
@OneToMany(mappedBy = "master", cascade = CascadeType.ALL)
|
||||
List<DcDetail> details;
|
||||
|
||||
@Version
|
||||
long version;
|
||||
|
||||
public DcMaster(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<DcDetail> getDetails() {
|
||||
return details;
|
||||
}
|
||||
|
||||
public void setDetails(List<DcDetail> details) {
|
||||
this.details = details;
|
||||
}
|
||||
|
||||
public long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(long version) {
|
||||
this.version = version;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package org.tests.delete;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import org.tests.model.onetoone.Account;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
public class TestDeleteByIdUsingModel extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
Account account = new Account();
|
||||
account.setName("GoingToDeleteThis");
|
||||
account.save();
|
||||
|
||||
Account account1 = Account.find.byId(account.getId());
|
||||
assertEquals(account.getName(), account1.getName());
|
||||
|
||||
Account.find.deleteById(account.getId());
|
||||
|
||||
Account account2 = Account.find.byId(account.getId());
|
||||
assertNull(account2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package org.tests.delete;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import io.ebean.Transaction;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Product;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
public class TestDeleteByIdWithPersistenceContext extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
DB.delete(Product.class, 100);
|
||||
DB.delete(Product.class, 101);
|
||||
|
||||
Database server = DB.getDefault();
|
||||
Product prod1 = createProduct(100, "apples");
|
||||
server.insert(prod1);
|
||||
Product prod2 = createProduct(101, "bananas");
|
||||
server.insert(prod2);
|
||||
|
||||
try (Transaction txn = server.beginTransaction()) {
|
||||
// effectively load these into the persistence context
|
||||
server.find(Product.class, prod1.getId());
|
||||
server.find(Product.class, prod2.getId());
|
||||
|
||||
server.deleteAll(Product.class, Arrays.asList(prod1.getId(), prod2.getId()));
|
||||
|
||||
// are these found in the persistence context?
|
||||
Product shadow1 = server.find(Product.class, prod1.getId());
|
||||
Product shadow2 = server.find(Product.class, prod2.getId());
|
||||
|
||||
assertNull(shadow1);
|
||||
assertNull(shadow2);
|
||||
}
|
||||
|
||||
// cleanup
|
||||
DB.delete(Product.class, 100);
|
||||
DB.delete(Product.class, 101);
|
||||
}
|
||||
|
||||
private Product createProduct(Integer id, String name) {
|
||||
Product prod = new Product();
|
||||
prod.setId(id);
|
||||
prod.setName(name);
|
||||
return prod;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,267 @@
|
||||
package org.tests.delete;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import io.ebean.Query;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.annotation.IgnorePlatform;
|
||||
import io.ebean.annotation.Platform;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.BBookmarkUser;
|
||||
import org.tests.model.basic.Contact;
|
||||
import org.tests.model.basic.Country;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestDeleteByQuery extends BaseTestCase {
|
||||
|
||||
private void createUser(String name) {
|
||||
BBookmarkUser u1 = new BBookmarkUser(name);
|
||||
DB.save(u1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@IgnorePlatform({Platform.MYSQL, Platform.MARIADB})
|
||||
public void deleteWithLimit() {
|
||||
createUser("deleteWithLimit1");
|
||||
createUser("deleteWithLimit2");
|
||||
createUser("deleteWithLimit3");
|
||||
createUser("deleteWithLimit4");
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
final int rows = DB.find(BBookmarkUser.class)
|
||||
.where().startsWith("name", "deleteWithLimit")
|
||||
.setMaxRows(3)
|
||||
.delete();
|
||||
|
||||
assertThat(rows).isEqualTo(3);
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
if (isSqlServer()) {
|
||||
assertSql(sql.get(0)).contains("delete from bbookmark_user where id in (select top 3 t0.id from bbookmark_user t0 where t0.name like ");
|
||||
} else {
|
||||
assertSql(sql.get(0)).contains("delete from bbookmark_user where id in (select t0.id from bbookmark_user t0 where t0.name like");
|
||||
if (isH2() || isPostgres()) {
|
||||
assertSql(sql.get(0)).contains("limit 3");
|
||||
}
|
||||
}
|
||||
|
||||
final int rowsAfter = DB.find(BBookmarkUser.class)
|
||||
.where().startsWith("name", "deleteWithLimit")
|
||||
.setMaxRows(3)
|
||||
.delete();
|
||||
|
||||
assertThat(rowsAfter).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@IgnorePlatform(Platform.MYSQL)
|
||||
// FIXME: MySql does not the sub query selecting from the delete table
|
||||
public void deleteWithSubquery() {
|
||||
|
||||
Database server = DB.getDefault();
|
||||
|
||||
BBookmarkUser u1 = new BBookmarkUser("u1");
|
||||
DB.save(u1);
|
||||
|
||||
Query<BBookmarkUser> query = server.find(BBookmarkUser.class)
|
||||
.where().eq("org.name", "NahYeahMaybe")
|
||||
.query();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
query.delete();
|
||||
|
||||
List<String> loggedSql = LoggedSqlCollector.stop();
|
||||
assertThat(loggedSql).hasSize(1);
|
||||
assertThat(trimSql(loggedSql.get(0), 1)).contains("delete from bbookmark_user where id in (select t0.id from bbookmark_user t0 left join bbookmark_org t1 on t1.id = t0.org_id where t1.name");
|
||||
|
||||
Query<BBookmarkUser> query2 = server.find(BBookmarkUser.class)
|
||||
.where().eq("name", "NotARealFirstName").query();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
query2.delete();
|
||||
|
||||
loggedSql = LoggedSqlCollector.stop();
|
||||
assertThat(loggedSql).hasSize(1);
|
||||
if (isPlatformSupportsDeleteTableAlias()) {
|
||||
assertThat(loggedSql.get(0)).contains("delete from bbookmark_user t0 where t0.name =");
|
||||
} else if (isMySql()){
|
||||
assertThat(loggedSql.get(0)).contains("delete t0 from bbookmark_user t0 where t0.name =");
|
||||
} else {
|
||||
assertThat(loggedSql.get(0)).contains("delete from bbookmark_user where name =");
|
||||
}
|
||||
|
||||
|
||||
server.find(BBookmarkUser.class).select("id").where().eq("name", "NotARealFirstName").delete();
|
||||
server.find(BBookmarkUser.class).select("id").where().eq("name", "TwoAlsoNotRealFirstName").query().delete();
|
||||
|
||||
List<BBookmarkUser> list = server.find(BBookmarkUser.class).select("id").where().eq("name", "NotARealFirstName").findList();
|
||||
assertThat(list).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
@IgnorePlatform(Platform.MYSQL)
|
||||
// FIXME: MySql does not the sub query selecting from the delete table
|
||||
public void deleteWithSubquery_withEscalation() {
|
||||
|
||||
Database server = DB.getDefault();
|
||||
|
||||
Query<Contact> query = server.find(Contact.class).where().eq("group.name", "NahYeahMaybe").query();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
query.delete();
|
||||
|
||||
List<String> loggedSql = LoggedSqlCollector.stop();
|
||||
assertThat(loggedSql).hasSize(1);
|
||||
assertThat(trimSql(loggedSql.get(0), 1)).contains("select t0.id from contact t0 left join contact_group t1 on t1.id = t0.group_id where t1.name = ?");
|
||||
|
||||
Query<Contact> query2 = server.find(Contact.class).where().eq("firstName", "NotARealFirstName").query();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
query2.delete();
|
||||
|
||||
loggedSql = LoggedSqlCollector.stop();
|
||||
assertThat(loggedSql).hasSize(1);
|
||||
assertThat(loggedSql.get(0)).contains("select t0.id from contact t0 where t0.first_name = ?");
|
||||
|
||||
|
||||
server.find(Contact.class).select("id").where().eq("firstName", "NotARealFirstName").delete();
|
||||
server.find(Contact.class).select("id").where().eq("firstName", "TwoAlsoNotRealFirstName").query().delete();
|
||||
|
||||
List<Contact> list = server.find(Contact.class).select("id").where().eq("firstName", "NotARealFirstName").findList();
|
||||
assertThat(list).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryByIdDelete() {
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
DB.find(BBookmarkUser.class).where().eq("id", 7000).delete();
|
||||
DB.find(BBookmarkUser.class).setId(7000).delete();
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
if (isPlatformSupportsDeleteTableAlias()) {
|
||||
assertSql(sql.get(0)).contains("delete from bbookmark_user t0 where t0.id = ?");
|
||||
assertSql(sql.get(1)).contains("delete from bbookmark_user t0 where t0.id = ?");
|
||||
} else if (!isMySql()) {
|
||||
assertSql(sql.get(0)).contains("delete from bbookmark_user where id = ?");
|
||||
assertSql(sql.get(1)).contains("delete from bbookmark_user where id = ?");
|
||||
}
|
||||
|
||||
// and note this is the easiest option
|
||||
DB.delete(BBookmarkUser.class, 7000);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryByIdDelete_withEscalation() {
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
DB.find(Contact.class).where().eq("id", 7000).delete();
|
||||
DB.find(Contact.class).setId(7000).delete();
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
// escalate to fetch ids then delete ... but no rows found
|
||||
assertSql(sql.get(0)).contains("select t0.id from contact t0 where t0.id = ?");
|
||||
assertSql(sql.get(1)).contains("select t0.id from contact t0 where t0.id = ?");
|
||||
|
||||
// and note this is the easiest option
|
||||
DB.delete(Contact.class, 7000);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryDelete_withTransactionNoCascade() {
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
try (Transaction transaction = DB.beginTransaction()) {
|
||||
transaction.setPersistCascade(false);
|
||||
|
||||
DB.find(Contact.class).where().eq("id", 7001).delete();
|
||||
|
||||
transaction.commit();
|
||||
}
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
if (isPlatformSupportsDeleteTableAlias()) {
|
||||
assertSql(sql.get(0)).contains("delete from contact t0 where t0.id = ?");
|
||||
} else if (!isMySql()){
|
||||
assertSql(sql.get(0)).contains("delete from contact where id = ?");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithForUpdate() {
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
DB.find(Customer.class)
|
||||
.where().eq("name", "Don Roberto")
|
||||
.query().forUpdate()
|
||||
.delete();
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(1);
|
||||
if (isSqlServer()) {
|
||||
assertSql(sql.get(0)).contains("select t0.id from o_customer t0 with (updlock) where t0.name = ?");
|
||||
} else {
|
||||
assertSql(sql.get(0)).contains("select t0.id from o_customer t0 where t0.name = ?");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteByPredicate() {
|
||||
|
||||
BBookmarkUser ud = new BBookmarkUser("deleteQueryByPredicate");
|
||||
DB.save(ud);
|
||||
|
||||
DB.find(BBookmarkUser.class).where().eq("name", "deleteQueryByPredicate").delete();
|
||||
|
||||
BBookmarkUser found = DB.find(BBookmarkUser.class, ud.getId());
|
||||
assertThat(found).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteByPredicate_withEscalation() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Customer> all = Customer.find.all();
|
||||
Contact contact = new Contact();
|
||||
contact.setFirstName("DelByQueryFirstName");
|
||||
contact.setLastName("deleteMe");
|
||||
contact.setCustomer(all.get(0));
|
||||
|
||||
DB.save(contact);
|
||||
|
||||
DB.find(Contact.class).where().eq("firstName", "DelByQueryFirstName").delete();
|
||||
|
||||
Contact contactFind = DB.find(Contact.class, contact.getId());
|
||||
assertThat(contactFind).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteByPredicateCached() {
|
||||
|
||||
Country country = new Country();
|
||||
country.setCode("XX");
|
||||
country.setName("SecretName");
|
||||
DB.save(country);
|
||||
Query<Country> query = DB.find(Country.class).where().eq("name", "SecretName").setUseQueryCache(true);
|
||||
|
||||
assertThat(query.findList()).hasSize(1);
|
||||
assertThat(query.findCount()).isEqualTo(1);
|
||||
|
||||
DB.find(Country.class).where().eq("name", "SecretName").delete();
|
||||
//DB.getDefault().getPluginApi().getBeanType(Country.class).clearQueryCache();
|
||||
assertThat(query.findList()).hasSize(0);
|
||||
assertThat(query.findCount()).isEqualTo(0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package org.tests.delete;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestDeleteCascadeWithListener extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
DcMaster m0 = new DcMaster("m0");
|
||||
m0.getDetails().add(new DcDetail());
|
||||
m0.getDetails().add(new DcDetail());
|
||||
m0.getDetails().add(new DcDetail());
|
||||
|
||||
DB.save(m0);
|
||||
|
||||
DcMaster found = DB.find(DcMaster.class, m0.getId());
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
DB.delete(found);
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
if (isPersistBatchOnCascade()) {
|
||||
assertThat(sql).hasSize(6);
|
||||
assertSql(sql.get(0)).contains("select t0.id from dc_detail t0 where master_id=?");
|
||||
assertSql(sql.get(1)).contains("delete from dc_detail where id=?");
|
||||
assertSqlBind(sql, 2, 4);
|
||||
assertThat(sql.get(5)).contains("delete from dc_master where id=? and version=?");
|
||||
}
|
||||
awaitListenerPropagation();
|
||||
|
||||
List<Object> beans = DcListener.deletedBeans();
|
||||
assertThat(beans).hasSize(3);
|
||||
}
|
||||
|
||||
private void awaitListenerPropagation() {
|
||||
try {
|
||||
Thread.sleep(200);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package org.tests.delete;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import io.ebean.Transaction;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Contact;
|
||||
import org.tests.model.basic.EBasicVer;
|
||||
import org.tests.model.converstation.Group;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestDeleteWithoutOptimisticLocking extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testSimpleBeanDelete_missingBean_returnsFalse() {
|
||||
|
||||
// delete by by without version loaded ... should not throw OptimisticLockException
|
||||
Contact ref = DB.reference(Contact.class, 999999);
|
||||
assertThat(DB.delete(ref)).isFalse();
|
||||
|
||||
assertThat(DB.delete(Contact.class, 999999)).isEqualTo(0);
|
||||
|
||||
// same as above but using Model.delete()
|
||||
Group modelRef = DB.reference(Group.class, 999999);
|
||||
assertThat(modelRef.delete()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleBeanDelete_existingBean_returnsTrue() {
|
||||
|
||||
EBasicVer basic = new EBasicVer("DelTest");
|
||||
DB.save(basic);
|
||||
|
||||
EBasicVer basicRef = DB.reference(EBasicVer.class, basic.getId());
|
||||
assertThat(DB.delete(basicRef)).isTrue();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testSimpleBeanDelete_existingBeanWithJdbcBatch_returnsTrue() {
|
||||
|
||||
EBasicVer basic = new EBasicVer("DelTestBatch");
|
||||
DB.save(basic);
|
||||
|
||||
Database server = DB.getDefault();
|
||||
Transaction transaction = server.beginTransaction();
|
||||
try {
|
||||
transaction.setBatchMode(true);
|
||||
|
||||
// returns true even though the delete has not occurred yet
|
||||
assertThat(server.delete(DB.reference(EBasicVer.class, basic.getId()), transaction)).isTrue();
|
||||
|
||||
// returns true even though the bean does not exist
|
||||
assertThat(server.delete(DB.reference(EBasicVer.class, 999999), transaction)).isTrue();
|
||||
|
||||
transaction.commit();
|
||||
|
||||
assertThat(DB.find(EBasicVer.class, basic.getId())).isNull();
|
||||
|
||||
} finally {
|
||||
transaction.end();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user