mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Refactor move tests from ebean-core to ebean-test
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.Database;
|
||||
import io.ebean.DatabaseFactory;
|
||||
import io.ebean.Query;
|
||||
import io.ebean.SqlRow;
|
||||
import io.ebean.config.DatabaseConfig;
|
||||
import io.ebean.config.dbplatform.postgres.PostgresPlatform;
|
||||
import io.ebean.datasource.DataSourceConfig;
|
||||
import org.tests.model.basic.TOne;
|
||||
import org.tests.model.basic.TSDetail;
|
||||
import org.tests.model.basic.TSMaster;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* Used to run some tests manually on a specific Database type.
|
||||
*/
|
||||
public class MainDbBoolean {
|
||||
|
||||
public static void main(String[] args) {
|
||||
MainDbBoolean me = new MainDbBoolean();
|
||||
|
||||
Database server = me.createEbeanServer();
|
||||
me.simpleCheck(server);
|
||||
|
||||
Database oraServer = me.createOracleEbeanServer();
|
||||
me.simpleCheck(oraServer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a server for running small oracle specific tests manually.
|
||||
* DDL generation etc.
|
||||
*/
|
||||
private Database createOracleEbeanServer() {
|
||||
DatabaseConfig c = new DatabaseConfig();
|
||||
c.setName("ora");
|
||||
c.setDdlExtra(false);
|
||||
|
||||
// requires oracle driver in class path
|
||||
DataSourceConfig oraDb = new DataSourceConfig();
|
||||
oraDb.setDriver("oracle.jdbc.driver.OracleDriver");
|
||||
oraDb.setUsername("junk");
|
||||
oraDb.setPassword("junk");
|
||||
oraDb.setUrl("jdbc:oracle:thin:junk/junk@localhost:1521:XE");
|
||||
oraDb.setHeartbeatSql("select count(*) from dual");
|
||||
|
||||
|
||||
c.loadFromProperties();
|
||||
c.setDdlGenerate(true);
|
||||
c.setDdlRun(true);
|
||||
c.setDefaultServer(false);
|
||||
c.setRegister(false);
|
||||
c.setDataSourceConfig(oraDb);
|
||||
|
||||
//c.setDatabaseBooleanTrue("1");
|
||||
//c.setDatabaseBooleanFalse("0");
|
||||
c.setDatabaseBooleanTrue("T");
|
||||
c.setDatabaseBooleanFalse("F");
|
||||
|
||||
c.addClass(TOne.class);
|
||||
c.addClass(TSMaster.class);
|
||||
c.addClass(TSDetail.class);
|
||||
|
||||
return DatabaseFactory.create(c);
|
||||
}
|
||||
|
||||
private Database createEbeanServer() {
|
||||
DatabaseConfig c = new DatabaseConfig();
|
||||
c.setName("pgtest");
|
||||
c.setDdlExtra(false);
|
||||
|
||||
// requires postgres driver in class path
|
||||
DataSourceConfig postgresDb = new DataSourceConfig();
|
||||
postgresDb.setDriver("org.postgresql.Driver");
|
||||
postgresDb.setUsername("test");
|
||||
postgresDb.setPassword("test");
|
||||
postgresDb.setUrl("jdbc:postgresql://127.0.0.1:5432/test");
|
||||
postgresDb.setHeartbeatSql("select count(*) from t_one");
|
||||
|
||||
c.loadFromProperties();
|
||||
c.setDdlGenerate(true);
|
||||
c.setDdlRun(true);
|
||||
c.setDefaultServer(false);
|
||||
c.setRegister(false);
|
||||
c.setDataSourceConfig(postgresDb);
|
||||
|
||||
//c.setDatabaseBooleanTrue("1");
|
||||
//c.setDatabaseBooleanFalse("0");
|
||||
c.setDatabaseBooleanTrue("T");
|
||||
c.setDatabaseBooleanFalse("F");
|
||||
|
||||
c.setDatabasePlatform(new PostgresPlatform());
|
||||
c.addClass(TOne.class);
|
||||
return DatabaseFactory.create(c);
|
||||
}
|
||||
|
||||
private void simpleCheck(Database server) {
|
||||
TOne o = new TOne();
|
||||
o.setName("banan");
|
||||
o.setDescription("this one is true");
|
||||
o.setActive(true);
|
||||
|
||||
server.save(o);
|
||||
|
||||
TOne o2 = new TOne();
|
||||
o2.setName("banan");
|
||||
o2.setDescription("this one is false");
|
||||
o2.setActive(false);
|
||||
|
||||
server.save(o2);
|
||||
|
||||
|
||||
List<TOne> list = server.find(TOne.class)
|
||||
.setAutoTune(false)
|
||||
.order("id")
|
||||
.findList();
|
||||
|
||||
assertThat(list).hasSize(2);
|
||||
assertTrue(list.get(0).isActive());
|
||||
|
||||
String sql = "select id, name, active from t_oneb order by id";
|
||||
List<SqlRow> sqlRows = server.sqlQuery(sql).findList();
|
||||
assertThat(sqlRows).hasSize(2);
|
||||
Object active0 = sqlRows.get(0).get("active");
|
||||
Object active1 = sqlRows.get(1).get("active");
|
||||
|
||||
assertEquals("T", active0);
|
||||
assertEquals("F", active1);
|
||||
|
||||
Query<TOne> query = server.find(TOne.class)
|
||||
.setAutoTune(false)
|
||||
.order("id");
|
||||
|
||||
int rc = query.findCount();
|
||||
assertThat(rc).isGreaterThan(0);
|
||||
|
||||
System.out.println("done");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.datasource.DataSourcePoolListener;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
public class MyTestDataSourcePoolListener implements DataSourcePoolListener {
|
||||
public static int SLEEP_AFTER_BORROW = 0;
|
||||
|
||||
@Override
|
||||
public void onAfterBorrowConnection(Connection c) {
|
||||
if (SLEEP_AFTER_BORROW > 0) {
|
||||
try {
|
||||
Thread.sleep(SLEEP_AFTER_BORROW);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBeforeReturnConnection(Connection c) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,299 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.annotation.Index;
|
||||
import io.ebean.annotation.Platform;
|
||||
import io.ebean.annotation.Where;
|
||||
import io.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import io.ebeaninternal.server.deploy.BeanProperty;
|
||||
import io.ebeaninternal.server.deploy.IndexDefinition;
|
||||
import io.ebeaninternal.server.deploy.meta.DeployBeanProperty;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.ValidationGroupSomething;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.MappedSuperclass;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Size;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import static io.ebean.annotation.Platform.H2;
|
||||
import static io.ebean.annotation.Platform.POSTGRES;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
|
||||
public class TestAnnotationBase extends BaseTestCase {
|
||||
|
||||
private final Set<Class<?>> metaAnnotationsFilter = new HashSet<>();
|
||||
|
||||
public TestAnnotationBase() {
|
||||
metaAnnotationsFilter.add(Where.class);
|
||||
metaAnnotationsFilter.add(Where.List.class);
|
||||
}
|
||||
|
||||
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Where(clause = "SELECT 'mysql' from 1", platforms = Platform.MYSQL)
|
||||
@Where(clause = "SELECT 'h2' from 1", platforms = H2)
|
||||
@Where(clause = "SELECT 'other' from 1")
|
||||
public @interface MetaTest {
|
||||
|
||||
}
|
||||
|
||||
@Index(name = "ano_1", columnNames = "direct", platforms = {H2, POSTGRES})
|
||||
@Index(name = "ano_2", columnNames = "direct", platforms = {H2, POSTGRES})
|
||||
@MappedSuperclass
|
||||
public static class MappedBaseEntity {
|
||||
|
||||
}
|
||||
|
||||
@Index(name = "ano_3", columnNames = "direct")
|
||||
@Entity
|
||||
public static class TestAnnotationBaseEntity extends MappedBaseEntity {
|
||||
|
||||
@Where(clause = "SELECT 'mysql' from 1", platforms = Platform.MYSQL)
|
||||
@Where(clause = "SELECT 'h2' from 1", platforms = H2)
|
||||
@Where(clause = "SELECT 'other' from 1")
|
||||
private String direct;
|
||||
|
||||
@MetaTest
|
||||
private String meta;
|
||||
|
||||
@MetaTest
|
||||
@Where(clause = "SELECT 'oracle' from 1", platforms = Platform.ORACLE)
|
||||
private String mixed;
|
||||
|
||||
@Size.List({
|
||||
@Size(max = 10, message = "max length for you is 10"),
|
||||
@Size(min = 1),
|
||||
@Size(max = 40, message = "max value for you is 40", groups = ValidationGroupSomething.class)
|
||||
})
|
||||
private String constraintAnnotation;
|
||||
|
||||
@NotNull
|
||||
private String null1;
|
||||
|
||||
|
||||
@NotNull(groups = ValidationGroupSomething.class)
|
||||
private String null2;
|
||||
|
||||
private String null3;
|
||||
|
||||
public String getDirect() {
|
||||
return direct;
|
||||
}
|
||||
|
||||
public void setDirect(String direct) {
|
||||
this.direct = direct;
|
||||
}
|
||||
|
||||
public String getMeta() {
|
||||
return meta;
|
||||
}
|
||||
|
||||
public void setMeta(String meta) {
|
||||
this.meta = meta;
|
||||
}
|
||||
|
||||
public String getMixed() {
|
||||
return mixed;
|
||||
}
|
||||
|
||||
public void setMixed(String mixed) {
|
||||
this.mixed = mixed;
|
||||
}
|
||||
|
||||
public String getConstraintAnnotation() {
|
||||
return constraintAnnotation;
|
||||
}
|
||||
|
||||
public void setConstraintAnnotation(String constraintAnnotation) {
|
||||
this.constraintAnnotation = constraintAnnotation;
|
||||
}
|
||||
|
||||
public String getNull1() {
|
||||
return null1;
|
||||
}
|
||||
|
||||
public void setNull1(String null1) {
|
||||
this.null1 = null1;
|
||||
}
|
||||
|
||||
public String getNull2() {
|
||||
return null2;
|
||||
}
|
||||
|
||||
public void setNull2(String null2) {
|
||||
this.null2 = null2;
|
||||
}
|
||||
|
||||
public String getNull3() {
|
||||
return null3;
|
||||
}
|
||||
|
||||
public void setNull3(String null3) {
|
||||
this.null3 = null3;
|
||||
}
|
||||
}
|
||||
|
||||
@Entity
|
||||
public static class TestJakartaAnnotationBaseEntity extends MappedBaseEntity {
|
||||
@Where(clause = "SELECT 'mysql' from 1", platforms = Platform.MYSQL)
|
||||
@Where(clause = "SELECT 'h2' from 1", platforms = H2)
|
||||
@Where(clause = "SELECT 'other' from 1")
|
||||
private String direct;
|
||||
|
||||
@MetaTest
|
||||
private String meta;
|
||||
|
||||
@MetaTest
|
||||
@Where(clause = "SELECT 'oracle' from 1", platforms = Platform.ORACLE)
|
||||
private String mixed;
|
||||
|
||||
@jakarta.validation.constraints.Size.List({
|
||||
@jakarta.validation.constraints.Size(max = 10, message = "max length for you is 10"),
|
||||
@jakarta.validation.constraints.Size(min = 1),
|
||||
@jakarta.validation.constraints.Size(max = 40, message = "max value for you is 40", groups = ValidationGroupSomething.class)
|
||||
})
|
||||
private String constraintAnnotation;
|
||||
|
||||
@NotNull
|
||||
private String null1;
|
||||
|
||||
|
||||
@NotNull(groups = ValidationGroupSomething.class)
|
||||
private String null2;
|
||||
|
||||
private String null3;
|
||||
|
||||
public String getConstraintAnnotation() {
|
||||
return constraintAnnotation;
|
||||
}
|
||||
|
||||
public void setConstraintAnnotation(String constraintAnnotation) {
|
||||
this.constraintAnnotation = constraintAnnotation;
|
||||
}
|
||||
|
||||
public String getNull1() {
|
||||
return null1;
|
||||
}
|
||||
|
||||
public void setNull1(String null1) {
|
||||
this.null1 = null1;
|
||||
}
|
||||
|
||||
public String getNull2() {
|
||||
return null2;
|
||||
}
|
||||
|
||||
public void setNull2(String null2) {
|
||||
this.null2 = null2;
|
||||
}
|
||||
|
||||
public String getNull3() {
|
||||
return null3;
|
||||
}
|
||||
|
||||
public void setNull3(String null3) {
|
||||
this.null3 = null3;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testFindMaxSize() throws SecurityException {
|
||||
BeanDescriptor<TestAnnotationBaseEntity> descriptor = spiEbeanServer().descriptor(TestAnnotationBaseEntity.class);
|
||||
BeanProperty bp = descriptor.findProperty("constraintAnnotation");
|
||||
assertEquals(40, bp.dbLength());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindJakartaMaxSize() throws SecurityException {
|
||||
BeanDescriptor<TestJakartaAnnotationBaseEntity> descriptor = spiEbeanServer().descriptor(TestJakartaAnnotationBaseEntity.class);
|
||||
BeanProperty bp = descriptor.findProperty("constraintAnnotation");
|
||||
assertEquals(40, bp.dbLength());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void annotationClassIndexes() throws SecurityException {
|
||||
BeanDescriptor<TestAnnotationBaseEntity> descriptor = spiEbeanServer().descriptor(TestAnnotationBaseEntity.class);
|
||||
final IndexDefinition[] indexDefinitions = descriptor.indexDefinitions();
|
||||
assertEquals(3, indexDefinitions.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNotNullWithGroup() throws SecurityException {
|
||||
BeanDescriptor<TestAnnotationBaseEntity> descriptor = spiEbeanServer().descriptor(TestAnnotationBaseEntity.class);
|
||||
|
||||
BeanProperty bp = descriptor.findProperty("null1");
|
||||
assertFalse(bp.isNullable());
|
||||
|
||||
bp = descriptor.findProperty("null2");
|
||||
assertTrue(bp.isNullable());
|
||||
|
||||
bp = descriptor.findProperty("null3");
|
||||
assertTrue(bp.isNullable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJakartaNotNullWithGroup() throws SecurityException {
|
||||
BeanDescriptor<TestJakartaAnnotationBaseEntity> descriptor = spiEbeanServer().descriptor(TestJakartaAnnotationBaseEntity.class);
|
||||
|
||||
BeanProperty bp = descriptor.findProperty("null1");
|
||||
assertFalse(bp.isNullable());
|
||||
|
||||
bp = descriptor.findProperty("null2");
|
||||
assertTrue(bp.isNullable());
|
||||
|
||||
bp = descriptor.findProperty("null3");
|
||||
assertTrue(bp.isNullable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindAnnotation() throws NoSuchFieldException, SecurityException {
|
||||
|
||||
Field directFld = TestAnnotationBaseEntity.class.getDeclaredField("direct");
|
||||
final DeployBeanProperty direct = createProperty(directFld);
|
||||
|
||||
assertEquals("SELECT 'mysql' from 1", where(direct, Platform.MYSQL));
|
||||
assertEquals("SELECT 'h2' from 1", where(direct, H2));
|
||||
assertEquals("SELECT 'other' from 1", where(direct, Platform.POSTGRES));
|
||||
|
||||
// meta
|
||||
Field metaFld = TestAnnotationBaseEntity.class.getDeclaredField("meta");
|
||||
final DeployBeanProperty meta = createProperty(metaFld);
|
||||
|
||||
assertEquals("SELECT 'mysql' from 1", where(meta, Platform.MYSQL));
|
||||
assertEquals("SELECT 'h2' from 1", where(meta, H2));
|
||||
assertEquals("SELECT 'other' from 1", where(meta, Platform.POSTGRES));
|
||||
|
||||
// mixed
|
||||
Field mixedFld = TestAnnotationBaseEntity.class.getDeclaredField("mixed");
|
||||
final DeployBeanProperty mixed = createProperty(mixedFld);
|
||||
|
||||
assertEquals("SELECT 'mysql' from 1", where(mixed, Platform.MYSQL));
|
||||
assertEquals("SELECT 'h2' from 1", where(mixed, H2));
|
||||
assertEquals("SELECT 'other' from 1", where(mixed, Platform.POSTGRES));
|
||||
assertEquals("SELECT 'oracle' from 1", where(mixed, Platform.ORACLE));
|
||||
}
|
||||
|
||||
private String where(DeployBeanProperty property, Platform platform) {
|
||||
return property.getMetaAnnotationWhere(platform).clause();
|
||||
}
|
||||
|
||||
private DeployBeanProperty createProperty(Field fld) {
|
||||
DeployBeanProperty directProperty = new DeployBeanProperty(null, null, null);
|
||||
directProperty.setField(fld);
|
||||
directProperty.initMetaAnnotations(metaAnnotationsFilter);
|
||||
return directProperty;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import org.tests.model.basic.Address;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TestAssocOneEqExpression extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Customer c = new Customer();
|
||||
c.setId(1);
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.where().eq("customer", c)
|
||||
.query();
|
||||
|
||||
query.findList();
|
||||
String sql = query.getGeneratedSql();
|
||||
assertTrue(sql.contains("where t0.kcustomer_id = ?"));
|
||||
|
||||
Address b = new Address();
|
||||
b.setId(1);
|
||||
|
||||
Query<Order> q2 = DB.find(Order.class)
|
||||
.where().eq("customer.billingAddress", b)
|
||||
.query();
|
||||
|
||||
q2.findList();
|
||||
sql = q2.getGeneratedSql();
|
||||
assertTrue(sql.contains("where t1.billing_address_id = ?"));
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.OrderDetail;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class TestBatchLazy extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testMe() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Order> query = DB.find(Order.class);
|
||||
List<Order> list = query.findList();
|
||||
|
||||
|
||||
for (Order order : list) {
|
||||
Customer customer = order.getCustomer();
|
||||
customer.getName();
|
||||
|
||||
List<OrderDetail> details = order.getDetails();
|
||||
for (OrderDetail orderDetail : details) {
|
||||
orderDetail.getProduct().getSku();
|
||||
}
|
||||
}
|
||||
|
||||
DB.getDefault().autoTune().collectProfiling();
|
||||
DB.getDefault().autoTune().reportProfiling();
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TestBatchLazyMany extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testMe() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Order order2 = DB.reference(Order.class, 1);
|
||||
order2.getOrderDate();
|
||||
|
||||
// List<Order> list = DB.find(Order.class)
|
||||
// //.join("details")
|
||||
// //.join("details", "+fetchquery")
|
||||
// .findList();
|
||||
//
|
||||
// Order order = list.get(0);
|
||||
// //List<OrderDetail> details = order.getDetails();
|
||||
// //details.size();
|
||||
//
|
||||
// Customer customer = order.getCustomer();
|
||||
// customer.getName();
|
||||
// System.out.println("done");
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.cache.ServerCache;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.Order.Status;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.Date;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestBeanReferenceRefresh extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testMe() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
ServerCache beanCache = DB.cacheManager().beanCache(Order.class);
|
||||
beanCache.clear();
|
||||
|
||||
Order order = DB.reference(Order.class, 1);
|
||||
|
||||
assertTrue(DB.beanState(order).isReference());
|
||||
|
||||
// invoke lazy loading
|
||||
Date orderDate = order.getOrderDate();
|
||||
assertNotNull(orderDate);
|
||||
|
||||
Customer customer = order.getCustomer();
|
||||
assertNotNull(customer);
|
||||
|
||||
assertFalse(DB.beanState(order).isReference());
|
||||
assertNotNull(order.getStatus());
|
||||
assertNotNull(order.getDetails());
|
||||
assertNull(DB.beanState(order).loadedProps());
|
||||
|
||||
Status status = order.getStatus();
|
||||
assertNotSame(status, Status.SHIPPED);
|
||||
order.setStatus(Order.Status.SHIPPED);
|
||||
DB.refresh(order);
|
||||
|
||||
Status statusRefresh = order.getStatus();
|
||||
assertEquals(status, statusRefresh);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.annotation.IgnorePlatform;
|
||||
import io.ebean.annotation.Platform;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.EDefaultProp;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class TestCreateEntityBean extends BaseTestCase {
|
||||
|
||||
@IgnorePlatform(Platform.SQLSERVER)
|
||||
@Test
|
||||
public void testDefaultRelation() {
|
||||
|
||||
// Use `new` to construct EntityBean
|
||||
final EDefaultProp beanNew = new EDefaultProp();
|
||||
DB.save(beanNew);
|
||||
|
||||
final EDefaultProp beanNewDb = DB.find(EDefaultProp.class, beanNew.getId());
|
||||
assertNotNull(beanNewDb);
|
||||
assertEquals("defaultName", beanNew.getName());
|
||||
assertNotNull(beanNewDb.geteSimple());
|
||||
assertEquals("Default prop eSimple", beanNewDb.geteSimple().getName());
|
||||
|
||||
// Use `createEntityBean` to construct EntityBean and check if it behaves the same as `new`
|
||||
final EDefaultProp beanCreateEntityBean = DB.getDefault().createEntityBean(EDefaultProp.class);
|
||||
DB.save(beanCreateEntityBean);
|
||||
|
||||
final EDefaultProp beanCreateEntityBeanDb = DB.find(EDefaultProp.class, beanCreateEntityBean.getId());
|
||||
assertNotNull(beanCreateEntityBeanDb);
|
||||
assertEquals("defaultName", beanCreateEntityBeanDb.getName());
|
||||
assertNotNull(beanCreateEntityBeanDb.geteSimple());
|
||||
assertEquals("Default prop eSimple", beanCreateEntityBeanDb.geteSimple().getName());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.TransactionalTestCase;
|
||||
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
public class TestDeleteByIdCollection extends TransactionalTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Customer c0 = ResetBasicData.createCustomer("del1", "del1 ship", "del1 bill", 1);
|
||||
Customer c1 = ResetBasicData.createCustomer("del2", "del2 ship", "del2 bill", 2);
|
||||
|
||||
DB.save(c0);
|
||||
DB.save(c1);
|
||||
|
||||
Customer c0Back = DB.find(Customer.class, c0.getId());
|
||||
Customer c1Back = DB.find(Customer.class, "" + c1.getId());
|
||||
|
||||
assertNotNull(c0Back);
|
||||
assertNotNull(c1Back);
|
||||
|
||||
List<String> ids = new ArrayList<>();
|
||||
// also test id type conversion
|
||||
ids.add("" + c0.getId());
|
||||
ids.add("" + c1.getId());
|
||||
|
||||
|
||||
DB.deleteAll(Customer.class, ids);
|
||||
awaitL2Cache();
|
||||
|
||||
c0Back = DB.find(Customer.class, c0.getId());
|
||||
c1Back = DB.find(Customer.class, "" + c1.getId());
|
||||
|
||||
assertNull(c0Back);
|
||||
assertNull(c1Back);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDelByStatement() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Order order0 = ResetBasicData.createOrderCustAndOrder("delBySql 0");
|
||||
Order order1 = ResetBasicData.createOrderCustAndOrder("delBySql 1");
|
||||
|
||||
Order o0Back = DB.find(Order.class, order0.getId());
|
||||
Order o1Back = DB.find(Order.class, order1.getId());
|
||||
|
||||
assertNotNull(o0Back);
|
||||
assertNotNull(o1Back);
|
||||
|
||||
|
||||
List<Object> ids = new ArrayList<>();
|
||||
// also test id type conversion
|
||||
ids.add(order0.getId());
|
||||
ids.add(order1.getId());
|
||||
|
||||
DB.deleteAll(Order.class, ids);
|
||||
awaitL2Cache();
|
||||
|
||||
o0Back = DB.find(Order.class, order0.getId());
|
||||
o1Back = DB.find(Order.class, order1.getId());
|
||||
|
||||
assertNull(o0Back);
|
||||
assertNull(o1Back);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Article;
|
||||
import org.tests.model.basic.Section;
|
||||
import org.tests.model.basic.SubSection;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestDeleteCascadingOneToMany extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testDeleteCascadingOneToMany() {
|
||||
Section s0 = new Section("some content");
|
||||
Article a0 = new Article("art1", "auth1");
|
||||
a0.addSection(s0);
|
||||
|
||||
DB.save(a0);
|
||||
DB.delete(a0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void l2cache_updateBeanOnly_expect_doesNotUpdateManyIds() {
|
||||
|
||||
// setup, create our graph - Bean -> OneToMany -> OneToMany
|
||||
Section s0 = new Section("c0");
|
||||
s0.getSubSections().add(new SubSection("sub0"));
|
||||
Article a0 = new Article("a2", "a2");
|
||||
a0.getSections().add(s0);
|
||||
DB.save(a0);
|
||||
|
||||
// load into l2 bean cache
|
||||
final Article bean0 = DB.find(Article.class, a0.getId());
|
||||
for (Section section : bean0.getSections()) {
|
||||
section.getSubSections().size(); // ensure the collections are loaded into l2 cache
|
||||
}
|
||||
|
||||
// mutate the manyIds associated with the bean
|
||||
final Article bean1 = DB.find(Article.class, a0.getId());
|
||||
final Section section1 = bean1.getSections().get(0);
|
||||
section1.getSubSections().clear(); // orphan remove will delete sub0 - this is cause of EntityNotFoundException: Bean not found during lazy load or refresh
|
||||
section1.getSubSections().add(new SubSection("sub-replacement"));
|
||||
DB.save(bean1);
|
||||
|
||||
bean0.setName("a2mod");
|
||||
// save bean but we have not mutated the collections
|
||||
// should NOT update the l2 manyIds (Bug will PUT the original manyIds into l2 COLL cache)
|
||||
DB.save(bean0);
|
||||
|
||||
// fetch again hitting l2 cache
|
||||
final Article beanLast = DB.find(Article.class, a0.getId());
|
||||
|
||||
// invoke lazy loading - hits l2 cache and get the expected result
|
||||
for (Section section : beanLast.getSections()) {
|
||||
for (SubSection subSection : section.getSubSections()) {
|
||||
assertThat(subSection.getTitle()).isEqualTo("sub-replacement");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.PFile;
|
||||
import org.tests.model.basic.PFileContent;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
public class TestDeleteImportedPartial extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
PFile persistentFile = new PFile("test.txt", new PFileContent("test".getBytes()));
|
||||
|
||||
DB.save(persistentFile);
|
||||
Integer id = persistentFile.getId();
|
||||
Integer contentId = persistentFile.getFileContent().getId();
|
||||
|
||||
PFile partialPfile = DB.find(PFile.class).select("id").where().idEq(persistentFile.getId())
|
||||
.findOne();
|
||||
|
||||
// should delete file and fileContent
|
||||
DB.delete(partialPfile);
|
||||
|
||||
PFile file1 = DB.find(PFile.class, id);
|
||||
PFileContent content1 = DB.find(PFileContent.class, contentId);
|
||||
|
||||
assertNull(file1);
|
||||
assertNull(content1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.PersistentFile;
|
||||
import org.tests.model.basic.PersistentFileContent;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
public class TestDeleteOneToOne extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testCreateDeletePersistentFile() {
|
||||
|
||||
PersistentFile persistentFile = new PersistentFile("test.txt", new PersistentFileContent(
|
||||
"test".getBytes()));
|
||||
|
||||
DB.save(persistentFile);
|
||||
Integer id = persistentFile.getId();
|
||||
Integer contentId = persistentFile.getPersistentFileContent().getId();
|
||||
|
||||
// should delete file and fileContent
|
||||
DB.delete(PersistentFile.class, id);
|
||||
|
||||
PersistentFile file1 = DB.find(PersistentFile.class, id);
|
||||
PersistentFileContent content1 = DB.find(PersistentFileContent.class, contentId);
|
||||
|
||||
assertNull(file1);
|
||||
assertNull(content1);
|
||||
}
|
||||
|
||||
// public void testDeleteMany() {
|
||||
//
|
||||
// Customer c = new Customer();
|
||||
// c.setName("Fiona");
|
||||
// c.setStatus(Customer.Status.ACTIVE);
|
||||
// c.addContact(new Contact("Fiona", "Black"));
|
||||
// c.addContact(new Contact("Tracy", "Red"));
|
||||
//
|
||||
// DB.save(c);
|
||||
//
|
||||
// DB.delete(Customer.class, c.getId());
|
||||
//
|
||||
// Customer deletedCustomer = DB.find(Customer.class, c.getId());
|
||||
//
|
||||
// assertNull(deletedCustomer);
|
||||
//
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.PFile;
|
||||
import org.tests.model.basic.PFileContent;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
public class TestDeleteOneToOneMultiple extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testCreateDeletePersistentFile() {
|
||||
|
||||
PFile persistentFile = new PFile("test.txt", new PFileContent("test".getBytes()));
|
||||
// PFile persistentFile = new PFile();
|
||||
// persistentFile.setName("test.txt");
|
||||
// PFileContent content = new PFileContent();
|
||||
// content.setContent("test".getBytes());
|
||||
// persistentFile.setFileContent(content);
|
||||
|
||||
DB.save(persistentFile);
|
||||
Integer id = persistentFile.getId();
|
||||
Integer contentId = persistentFile.getFileContent().getId();
|
||||
|
||||
// should delete file and fileContent
|
||||
DB.delete(PFile.class, id);
|
||||
|
||||
PFile file1 = DB.find(PFile.class, id);
|
||||
PFileContent content1 = DB.find(PFileContent.class, contentId);
|
||||
|
||||
assertNull(file1);
|
||||
assertNull(content1);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.TMapSuperEntity;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class TestDeletePartialNoVersion extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testNoVersion() {
|
||||
|
||||
TMapSuperEntity e = new TMapSuperEntity();
|
||||
e.setName("babanaone");
|
||||
|
||||
DB.save(e);
|
||||
|
||||
// select includes a transient property
|
||||
TMapSuperEntity e2 = DB.find(TMapSuperEntity.class)
|
||||
.where().idEq(e.getId())
|
||||
.select("id, name")
|
||||
.findOne();
|
||||
|
||||
assertNotNull(e2);
|
||||
|
||||
e2.setName("banaban2");
|
||||
DB.save(e2);
|
||||
|
||||
DB.delete(e2);
|
||||
}
|
||||
|
||||
|
||||
public void testWithVersion() {
|
||||
|
||||
TMapSuperEntity e = new TMapSuperEntity();
|
||||
e.setName("babanatwo");
|
||||
|
||||
DB.save(e);
|
||||
|
||||
// select includes a transient property
|
||||
TMapSuperEntity e2 = DB.find(TMapSuperEntity.class)
|
||||
.where().idEq(e.getId())
|
||||
.select("id, name, version")
|
||||
.findOne();
|
||||
|
||||
assertNotNull(e2);
|
||||
|
||||
e2.setName("banaban2two");
|
||||
DB.save(e2);
|
||||
|
||||
DB.delete(e2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.BeanState;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import org.tests.model.embedded.EMain;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestDynamicUpdate extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testUpdate() {
|
||||
|
||||
// insert
|
||||
EMain b = new EMain();
|
||||
b.setName("aaa");
|
||||
b.getEmbeddable().setDescription("123");
|
||||
|
||||
Database server = DB.getDefault();
|
||||
|
||||
server.save(b);
|
||||
|
||||
assertNotNull(b.getId());
|
||||
|
||||
// reload object und update the name
|
||||
EMain b2 = server.find(EMain.class, b.getId());
|
||||
|
||||
b2.getEmbeddable().setDescription("ABC");
|
||||
|
||||
BeanState beanState = server.beanState(b2);
|
||||
boolean dirty = beanState.isDirty();
|
||||
assertTrue(dirty);
|
||||
|
||||
server.save(b2);
|
||||
|
||||
server.beginTransaction();
|
||||
try {
|
||||
EMain b3 = server.find(EMain.class, b.getId());
|
||||
assertEquals("ABC", b3.getEmbeddable().getDescription());
|
||||
} finally {
|
||||
server.endTransaction();
|
||||
}
|
||||
EMain b4 = server.find(EMain.class, b.getId());
|
||||
b4.setName("bbb");
|
||||
b4.getEmbeddable().setDescription("123");
|
||||
server.save(b4);
|
||||
|
||||
EMain b5 = server.find(EMain.class, b.getId());
|
||||
assertEquals("123", b5.getEmbeddable().getDescription());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.BeanState;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
|
||||
public class TestEnhancementCollectionInitialisation extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testListInitialisation() {
|
||||
Customer customer = new Customer();
|
||||
BeanState beanState = DB.beanState(customer);
|
||||
if (beanState != null) {
|
||||
List<Order> orders = customer.getOrders();
|
||||
assertNotNull(orders);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Order;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TestErrorBindLog extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
try {
|
||||
DB.find(Order.class).where().gt("id", "JUNK").findList();
|
||||
|
||||
} catch (PersistenceException e) {
|
||||
String msg = e.getMessage();
|
||||
if (isHana()) {
|
||||
assertTrue(msg.contains("Error with property[1] dt[12]data[JUNK]"));
|
||||
}
|
||||
else {
|
||||
assertTrue(msg.contains("Bind values:"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.EBasic;
|
||||
import org.tests.model.basic.MyEBasicConfigStartup;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestExplicitInsert extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void setId_when_converted() {
|
||||
|
||||
Database server = DB.getDefault();
|
||||
|
||||
Customer cust = new Customer();
|
||||
Object returnId = server.beanId(cust, "42");
|
||||
|
||||
assertThat(returnId).isEqualTo(42);
|
||||
assertThat(cust.getId()).isEqualTo(42);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setId_when_correctType() {
|
||||
|
||||
Database server = DB.getDefault();
|
||||
|
||||
Customer customer = new Customer();
|
||||
Object returnId = server.beanId(customer, 42);
|
||||
|
||||
assertThat(returnId).isEqualTo(42);
|
||||
assertThat(customer.getId()).isEqualTo(42);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() throws InterruptedException {
|
||||
|
||||
Thread.sleep(100);
|
||||
|
||||
MyEBasicConfigStartup.resetCounters();
|
||||
|
||||
EBasic b = new EBasic();
|
||||
b.setName("exp insert");
|
||||
b.setDescription("explicit insert");
|
||||
b.setStatus(EBasic.Status.ACTIVE);
|
||||
|
||||
Database server = DB.getDefault();
|
||||
server.insert(b);
|
||||
assertNotNull(b.getId());
|
||||
assertEquals(b.getId(), server.beanId(b));
|
||||
|
||||
EBasic b2 = server.find(EBasic.class, b.getId());
|
||||
b2.setId(null);
|
||||
|
||||
b2.setName("force insert");
|
||||
server.insert(b2);
|
||||
|
||||
assertNotNull(b2.getId());
|
||||
assertTrue(!b.getId().equals(b2.getId()));
|
||||
|
||||
List<EBasic> list = server.find(EBasic.class).where().in("id", b.getId(), b2.getId()).findList();
|
||||
|
||||
assertEquals(2, list.size());
|
||||
|
||||
b2.setName("do an update");
|
||||
server.save(b2);
|
||||
|
||||
server.delete(b);
|
||||
server.delete(b2);
|
||||
|
||||
// just sleep a little to allow the background thread to fire
|
||||
Thread.sleep(100);
|
||||
|
||||
assertEquals(2, MyEBasicConfigStartup.insertCount.get());
|
||||
assertEquals(1, MyEBasicConfigStartup.updateCount.get());
|
||||
assertEquals(2, MyEBasicConfigStartup.deleteCount.get());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.FutureIds;
|
||||
import io.ebean.Query;
|
||||
import io.ebeantest.LoggedSql;
|
||||
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.OrderDetail;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestFetchId extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testFetchId() throws InterruptedException, ExecutionException {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.setAutoTune(false)
|
||||
.fetch("details")
|
||||
.where().gt("id", 1)
|
||||
.gt("details.id", 0)
|
||||
.query();
|
||||
|
||||
List<Object> ids = query.findIds();
|
||||
assertThat(ids).isNotEmpty();
|
||||
|
||||
FutureIds<Order> futureIds = query.findFutureIds();
|
||||
|
||||
// wait for all the id's to be fetched
|
||||
List<Object> idList = futureIds.get();
|
||||
assertThat(idList).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFetchIdWithExists() throws InterruptedException, ExecutionException {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<OrderDetail> subQuery = DB.find(OrderDetail.class)
|
||||
.alias("sq")
|
||||
.where().raw("details.id = sq.id").query();
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.where().exists(subQuery)
|
||||
.orderBy("orderDate").query();
|
||||
|
||||
List<Object> ids = query.findIds();
|
||||
// TODO: assert(query.getGeneratedSql())
|
||||
assertThat(ids).isNotEmpty();
|
||||
FutureIds<Order> futureIds = query.findFutureIds();
|
||||
|
||||
// wait for all the id's to be fetched
|
||||
List<Object> idList = futureIds.get();
|
||||
assertThat(idList).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFetchIdWithOrderFormula() throws InterruptedException, ExecutionException {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Order> query = DB.find(Order.class).orderBy("totalItems");
|
||||
query.findIds();
|
||||
// TODO: assert(query.getGeneratedSql())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.EBasicVer;
|
||||
import org.tests.model.basic.UTMaster;
|
||||
|
||||
import javax.persistence.OptimisticLockException;
|
||||
import java.sql.Timestamp;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestIUDVanilla extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
EBasicVer e0 = new EBasicVer("vanilla");
|
||||
|
||||
e0.save();
|
||||
|
||||
assertNotNull(e0.getId());
|
||||
assertNotNull(e0.getLastUpdate());
|
||||
|
||||
Timestamp lastUpdate0 = e0.getLastUpdate();
|
||||
|
||||
e0.setName("modified");
|
||||
e0.save();
|
||||
|
||||
Timestamp lastUpdate1 = e0.getLastUpdate();
|
||||
assertNotNull(lastUpdate1);
|
||||
assertNotSame(lastUpdate0, lastUpdate1);
|
||||
|
||||
EBasicVer e2 = DB.getDefault().createEntityBean(EBasicVer.class);
|
||||
|
||||
e2.setId(e0.getId());
|
||||
e2.setLastUpdate(lastUpdate1);
|
||||
|
||||
e2.setName("forcedUpdate");
|
||||
e2.update();
|
||||
|
||||
EBasicVer e3 = new EBasicVer("ModNoOCC");
|
||||
e3.setId(e0.getId());
|
||||
|
||||
e3.update();
|
||||
|
||||
e3.setName("ModAgain");
|
||||
e3.setDescription("Banana");
|
||||
|
||||
e3.update();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void stateless_noOCC() {
|
||||
|
||||
EBasicVer e0 = new EBasicVer("vanilla");
|
||||
e0.save();
|
||||
|
||||
EBasicVer e3 = new EBasicVer("ModNoOCC");
|
||||
e3.setId(e0.getId());
|
||||
e3.setLastUpdate(e0.getLastUpdate());
|
||||
|
||||
e3.update();
|
||||
|
||||
e3.setName("ModAgain");
|
||||
//e3.setDescription("Banana");
|
||||
|
||||
e3.update();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void modifyVersion_expect_optimisticLock() {
|
||||
UTMaster e0 = new UTMaster("save me");
|
||||
e0.save();
|
||||
|
||||
// for this case we know 42 should throw OptimisticLockException
|
||||
e0.setVersion(42);
|
||||
assertThrows(OptimisticLockException.class, () -> e0.update());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class TestInEmpty extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test_in_empty() {
|
||||
|
||||
Query<Order> query = DB.find(Order.class).where().in("id", new Object[0]).gt("id", 0)
|
||||
.query();
|
||||
|
||||
List<Order> list = query.findList();
|
||||
assertSql(query).contains("1=0");
|
||||
assertEquals(0, list.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_isIn_empty() {
|
||||
|
||||
Query<Order> query = DB.find(Order.class).where().isIn("id", new Object[0]).gt("id", 0)
|
||||
.query();
|
||||
|
||||
List<Order> list = query.findList();
|
||||
assertSql(query).contains("1=0");
|
||||
assertEquals(0, list.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_notIn_empty() {
|
||||
|
||||
Query<Order> query = DB.find(Order.class).where().notIn("id", new Object[0]).gt("id", 0)
|
||||
.query();
|
||||
|
||||
query.findList();
|
||||
assertSql(query).contains("1=1");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.Car;
|
||||
import org.tests.model.basic.Truck;
|
||||
import org.tests.model.basic.Vehicle;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TestInheritRef extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testAssocOne() {
|
||||
|
||||
DB.beginTransaction();
|
||||
try {
|
||||
DB.createUpdate(Vehicle.class, "delete from vehicle");
|
||||
|
||||
Car c = new Car();
|
||||
c.setLicenseNumber("C6788");
|
||||
c.setDriver("CarDriver");
|
||||
DB.save(c);
|
||||
|
||||
Truck t = new Truck();
|
||||
t.setLicenseNumber("T1098BBX");
|
||||
t.setCapacity(20D);
|
||||
DB.save(t);
|
||||
|
||||
List<Vehicle> list = DB.find(Vehicle.class)
|
||||
.setAutoTune(false)
|
||||
.findList();
|
||||
|
||||
assertTrue(!list.isEmpty());
|
||||
|
||||
Truck foundTruck = null;
|
||||
int found = 0;
|
||||
|
||||
for (Vehicle vehicle : list) {
|
||||
if (vehicle instanceof Truck) {
|
||||
Truck truck = (Truck) vehicle;
|
||||
if ("T1098BBX".equals(truck.getLicenseNumber())) {
|
||||
found++;
|
||||
foundTruck = truck;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assertEquals(1, found);
|
||||
assertTrue(foundTruck.getCapacity() == 20D);
|
||||
|
||||
} finally {
|
||||
DB.rollbackTransaction();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.core.type.ScalarType;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import io.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import io.ebeaninternal.server.deploy.BeanProperty;
|
||||
import org.tests.model.basic.TJodaEntity;
|
||||
import org.joda.time.LocalTime;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class TestJodaType extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
SpiEbeanServer server = (SpiEbeanServer) DB.getDefault();
|
||||
BeanDescriptor<TJodaEntity> beanDescriptor = server.descriptor(TJodaEntity.class);
|
||||
BeanProperty beanProperty = beanDescriptor.beanProperty("localTime");
|
||||
ScalarType<?> scalarType = beanProperty.scalarType();
|
||||
|
||||
assertNotNull(scalarType);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_insert_find() {
|
||||
|
||||
LocalTime now = new LocalTime().withMillisOfSecond(0);
|
||||
|
||||
TJodaEntity bean = new TJodaEntity();
|
||||
bean.setLocalTime(now);
|
||||
DB.save(bean);
|
||||
|
||||
TJodaEntity foundBean = DB.find(TJodaEntity.class, bean.getId());
|
||||
|
||||
assertThat(foundBean.getLocalTime()).isEqualTo(bean.getLocalTime());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.Address;
|
||||
import org.tests.model.basic.Car;
|
||||
import org.tests.model.basic.Trip;
|
||||
import org.tests.model.basic.Vehicle;
|
||||
import org.tests.model.basic.VehicleDriver;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Date;
|
||||
|
||||
public class TestJoinInheritance extends BaseTestCase {
|
||||
|
||||
|
||||
/**
|
||||
* Test join hierarchy assoc one.
|
||||
* <p>
|
||||
* This test catches the problem where the discriminator column is select
|
||||
* but is not skipped if the parent bean is already in the presistence context.
|
||||
* <p>
|
||||
* Test case: We have several trips with different addresses but always the same
|
||||
* driver and car (the cars could be different). So the driver will be loaded
|
||||
* when the first trip is loaded but when the second trip is loaded the driver is
|
||||
* found in the persistence context. So the bean is not read but the index in the
|
||||
* result set is forwarded by the number of properties - however, the vehicle type
|
||||
* column was not being skipped - but the query reads happily - only the data is
|
||||
* then wrong in the address object.
|
||||
*/
|
||||
@Test
|
||||
public void testJoinHierarchyAssocOne() {
|
||||
|
||||
DB.createUpdate(Vehicle.class, "delete from vehicle");
|
||||
|
||||
|
||||
Address address = new Address();
|
||||
address.setLine1("Street");
|
||||
address.setLine2("Street");
|
||||
address.setCity("City");
|
||||
|
||||
address.setCretime(new Timestamp(new Date().getTime()));
|
||||
|
||||
DB.save(address);
|
||||
|
||||
Car c = new Car();
|
||||
c.setLicenseNumber("C6788");
|
||||
c.setDriver("CarDriver");
|
||||
c.setRegistrationDate(new Date());
|
||||
DB.save(c);
|
||||
|
||||
VehicleDriver driver = new VehicleDriver();
|
||||
|
||||
driver.setVehicle(c);
|
||||
driver.setAddress(address);
|
||||
driver.setLicenseIssuedOn(new Date());
|
||||
DB.save(driver);
|
||||
|
||||
final String line1 = "Street1";
|
||||
final String line2 = "Street2";
|
||||
final String city = "City";
|
||||
|
||||
int nrDrivers = 2;
|
||||
for (int i = 0; i < nrDrivers; i++) {
|
||||
Address address1 = new Address();
|
||||
address1.setLine1(line1);
|
||||
address1.setLine2(line2);
|
||||
address1.setCity(city);
|
||||
DB.save(address1);
|
||||
|
||||
Trip trip = new Trip();
|
||||
trip.setVehicleDriver(driver);
|
||||
trip.setAddress(address1);
|
||||
trip.setCretime(new Timestamp(new Date().getTime()));
|
||||
DB.save(trip);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Contact;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TestLazyLoadEmptyOneToMany extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Customer c = new Customer();
|
||||
c.setName("testll");
|
||||
|
||||
DB.save(c);
|
||||
|
||||
Customer c1 = DB.find(Customer.class)
|
||||
.setAutoTune(false)
|
||||
.select("id")
|
||||
.fetch("contacts", "id")
|
||||
.where().idEq(c.getId())
|
||||
.findOne();
|
||||
|
||||
List<Contact> contacts = c1.getContacts();
|
||||
int sz = contacts.size();
|
||||
|
||||
assertTrue(sz == 0);
|
||||
|
||||
// cleanup
|
||||
DB.delete(c1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.BeanState;
|
||||
import io.ebean.CacheMode;
|
||||
import io.ebean.DB;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Address;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestLazyLoadInCache extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testLoadInCache() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Map<Integer, Customer> map = DB.find(Customer.class)
|
||||
.select("id, name")
|
||||
.setBeanCacheMode(CacheMode.PUT)
|
||||
.setReadOnly(true)
|
||||
.order().asc("id")
|
||||
.findMap();
|
||||
|
||||
assertFalse(map.isEmpty());
|
||||
|
||||
Object id = map.keySet().iterator().next();
|
||||
|
||||
Customer cust1 = map.get(id);
|
||||
|
||||
Customer cust1B = DB.find(Customer.class)
|
||||
.setReadOnly(true)
|
||||
.setId(id)
|
||||
.findOne();
|
||||
|
||||
assertNotSame(cust1, cust1B);
|
||||
|
||||
Set<String> loadedProps = DB.beanState(cust1).loadedProps();
|
||||
|
||||
assertTrue(loadedProps.contains("name"));
|
||||
assertFalse(loadedProps.contains("status"));
|
||||
|
||||
cust1.getStatus();
|
||||
|
||||
// a readOnly reference
|
||||
Address billingAddress = cust1.getBillingAddress();
|
||||
BeanState billAddrState = DB.beanState(billingAddress);
|
||||
assertTrue(billAddrState.isReference());
|
||||
assertTrue(billAddrState.isReadOnly());
|
||||
|
||||
// lazy load .. no longer a reference
|
||||
billingAddress.getCity();
|
||||
assertFalse(billAddrState.isReference());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
|
||||
public class TestLimitQuery extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testLimitWithMany() {
|
||||
rob();
|
||||
rob();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMaxRowsZeroWithFirstRow() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.setAutoTune(false)
|
||||
.fetch("details")
|
||||
.where().gt("details.id", 0)
|
||||
.setMaxRows(0)
|
||||
.setFirstRow(3)
|
||||
.order().asc("orderDate");
|
||||
|
||||
query.findList();
|
||||
|
||||
String sql = query.getGeneratedSql();
|
||||
if (isH2()) {
|
||||
assertThat(sql).contains("offset 3");
|
||||
assertThat(sql).contains("limit 0");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMaxRowsWithFirstRowZero() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.setAutoTune(false)
|
||||
.fetch("details")
|
||||
.where().gt("details.id", 0)
|
||||
.setMaxRows(3)
|
||||
.setFirstRow(0).query();
|
||||
|
||||
query.findList();
|
||||
|
||||
String sql = query.getGeneratedSql();
|
||||
boolean hasLimit = sql.contains("limit 3");
|
||||
boolean hasOffset = sql.contains("offset");
|
||||
|
||||
if (isH2()) {
|
||||
assertTrue(hasLimit);
|
||||
assertFalse(hasOffset);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaults() {
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.setAutoTune(false)
|
||||
.fetch("details")
|
||||
.where().gt("details.id", 0)
|
||||
.query();
|
||||
|
||||
query.findList();
|
||||
|
||||
String sql = query.getGeneratedSql();
|
||||
boolean hasLimit = sql.contains("limit");
|
||||
boolean hasOffset = sql.contains("offset");
|
||||
|
||||
if (isH2()) {
|
||||
assertFalse(hasLimit);
|
||||
assertFalse(hasOffset);
|
||||
}
|
||||
}
|
||||
|
||||
private void rob() {
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.setAutoTune(false)
|
||||
.fetch("details")
|
||||
.where().gt("details.id", 0)
|
||||
.setMaxRows(10).query();
|
||||
//.findList();
|
||||
|
||||
List<Order> list = query.findList();
|
||||
|
||||
assertTrue(!list.isEmpty());
|
||||
|
||||
String sql = query.getGeneratedSql();
|
||||
boolean hasDetailsJoin = sql.contains("join o_order_detail");
|
||||
boolean hasLimit = sql.contains("limit 10");
|
||||
boolean hasSelectedDetails = sql.contains("od.id,");
|
||||
boolean hasDistinct = sql.contains("select distinct");
|
||||
|
||||
assertTrue(hasDetailsJoin);
|
||||
assertFalse(hasSelectedDetails);
|
||||
assertTrue(hasDistinct);
|
||||
if (isH2()) {
|
||||
assertTrue(hasLimit);
|
||||
}
|
||||
|
||||
query = DB.find(Order.class)
|
||||
.setAutoTune(false)
|
||||
.fetch("details")
|
||||
.setMaxRows(10);
|
||||
|
||||
query.findList();
|
||||
|
||||
sql = query.getGeneratedSql();
|
||||
hasDetailsJoin = sql.contains("left join o_order_detail");
|
||||
hasLimit = sql.contains("limit 10");
|
||||
hasSelectedDetails = sql.contains("od.id");
|
||||
hasDistinct = sql.contains("select distinct");
|
||||
|
||||
assertFalse(hasDetailsJoin);
|
||||
assertFalse(hasSelectedDetails);
|
||||
assertFalse(hasDistinct);
|
||||
if (isH2()) {
|
||||
assertTrue(hasLimit);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.Country;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TestLoadBeanCache extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testLoad() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Map<String, Country> map = DB.find(Country.class)
|
||||
.setLoadBeanCache(true)
|
||||
.setUseQueryCache(true)
|
||||
.setReadOnly(true)
|
||||
.order("name")
|
||||
.findMap();
|
||||
|
||||
Country loadedNz = map.get("NZ");
|
||||
|
||||
// this will hit the cache
|
||||
Country nz = DB.find(Country.class, "NZ");
|
||||
|
||||
assertTrue(loadedNz == nz);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Transaction;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.EBasicVer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TestLogTransLogOnError extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testQueryError() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
try (Transaction txn = DB.beginTransaction()) {
|
||||
|
||||
DB.find(Customer.class).findList();
|
||||
DB.find(Order.class).where().gt("id", 1).findList();
|
||||
|
||||
EBasicVer newBean = new EBasicVer("aName");
|
||||
newBean.setDescription("something");
|
||||
|
||||
// DB.save(newBean);
|
||||
|
||||
// t.log("--- next query should error");
|
||||
List<Customer> list = DB.find(Customer.class).where().eq("id", "NotAnInt!!").findList();
|
||||
|
||||
assertEquals(0, list.size());
|
||||
// Get here with mysql?
|
||||
// assertTrue(false);
|
||||
txn.commit();
|
||||
|
||||
} catch (RuntimeException e) {
|
||||
// e.printStackTrace();
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void testPersistError() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
try (Transaction txn = DB.beginTransaction()) {
|
||||
DB.find(Customer.class).findList();
|
||||
|
||||
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 ");
|
||||
|
||||
// t.log("--- next insert should error");
|
||||
DB.save(newBean);
|
||||
|
||||
// never get here
|
||||
assertTrue(false);
|
||||
txn.commit();
|
||||
|
||||
} catch (RuntimeException e) {
|
||||
// e.printStackTrace();
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import org.tests.model.basic.MRole;
|
||||
import org.tests.model.basic.MUser;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TestM2MCascadeOne extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
MUser u = new MUser();
|
||||
u.setUserName("testM2M");
|
||||
|
||||
u.getRoles();
|
||||
DB.save(u);
|
||||
|
||||
MRole r0 = new MRole();
|
||||
r0.setRoleName("rol_0");
|
||||
DB.save(r0);
|
||||
|
||||
MRole r1 = new MRole();
|
||||
r1.setRoleName("rol_1");
|
||||
|
||||
MUser u1 = DB.find(MUser.class, u.getUserid());
|
||||
|
||||
u1.addRole(r0);
|
||||
u1.addRole(r1);
|
||||
|
||||
DB.save(u1);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRawPredicate_with_ManyToManyPath() {
|
||||
|
||||
Query<MUser> query = DB.find(MUser.class)
|
||||
.select("userid")
|
||||
.where().raw("roles.roleid in (?)", 24)
|
||||
.query();
|
||||
|
||||
query.findList();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.MRole;
|
||||
import org.tests.model.basic.MUser;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestM2MVanilla extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testVanilla() {
|
||||
|
||||
DB.sqlUpdate("delete from mrole_muser").execute();
|
||||
DB.sqlUpdate("delete from mrole").execute();
|
||||
DB.sqlUpdate("delete from muser").execute();
|
||||
|
||||
MRole r1 = new MRole();
|
||||
r1.setRoleName("role1");
|
||||
DB.save(r1);
|
||||
|
||||
MRole r2 = new MRole();
|
||||
r2.setRoleName("role2");
|
||||
DB.save(r2);
|
||||
|
||||
MRole r3 = new MRole();
|
||||
r3.setRoleName("role3");
|
||||
DB.save(r3);
|
||||
|
||||
MUser u0 = new MUser();
|
||||
u0.setUserName("something");
|
||||
|
||||
DB.save(u0);
|
||||
|
||||
MUser user = DB.find(MUser.class, u0.getUserid());
|
||||
|
||||
List<MRole> roleList = new ArrayList<>();
|
||||
roleList.add(r1);
|
||||
roleList.add(r2);
|
||||
|
||||
user.setRoles(roleList);
|
||||
|
||||
DB.save(user);
|
||||
// DB.saveManyToManyAssociations(user, "roles");
|
||||
|
||||
MUser checkUser = DB.find(MUser.class, u0.getUserid());
|
||||
List<MRole> checkRoles = checkUser.getRoles();
|
||||
assertNotNull(checkRoles);
|
||||
assertEquals(2, checkRoles.size());
|
||||
|
||||
checkRoles.add(r3);
|
||||
|
||||
DB.save(checkUser);
|
||||
// DB.saveManyToManyAssociations(checkUser, "roles");
|
||||
|
||||
MUser checkUser2 = DB.find(MUser.class, u0.getUserid());
|
||||
List<MRole> checkRoles2 = checkUser2.getRoles();
|
||||
assertNotNull(checkRoles2);
|
||||
assertEquals(3, checkRoles2.size());
|
||||
|
||||
Query<MUser> rolesQuery0 = DB.find(MUser.class).where().eq("roles", r1).query();
|
||||
|
||||
rolesQuery0.findList();
|
||||
|
||||
Query<MUser> rolesQuery = DB.find(MUser.class).where().in("roles", roleList).query();
|
||||
|
||||
List<MUser> userInRolesList = rolesQuery.findList();
|
||||
assertTrue(!userInRolesList.isEmpty());
|
||||
|
||||
List<MUser> list = DB.find(MUser.class)
|
||||
.where().in("roles", roleList)
|
||||
.filterMany("roles").eq("roleName", "role1")
|
||||
.findList();
|
||||
|
||||
MUser mUser = list.get(0);
|
||||
List<MRole> roles = mUser.getRoles();
|
||||
assertEquals(1, roles.size());
|
||||
|
||||
checkRoles2.remove(0);
|
||||
checkRoles2.remove(0);
|
||||
DB.save(checkUser2);
|
||||
|
||||
checkUser2 = DB.find(MUser.class, u0.getUserid());
|
||||
checkRoles2 = checkUser2.getRoles();
|
||||
assertNotNull(checkRoles2);
|
||||
assertEquals(1, checkRoles2.size());
|
||||
|
||||
DB.delete(checkUser2);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.OrderDetail;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestManyLazyLoad extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testLazyLoadRef() throws InterruptedException {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
awaitL2Cache();
|
||||
|
||||
List<Order> list = DB.find(Order.class).order().asc("id").findList();
|
||||
assertFalse(list.isEmpty());
|
||||
|
||||
// just use the first one
|
||||
Order order = list.get(0);
|
||||
|
||||
// get it as a reference
|
||||
Order order1 = DB.reference(Order.class, order.getId());
|
||||
assertNotNull(order1);
|
||||
|
||||
Date orderDate = order1.getOrderDate();
|
||||
assertNotNull(orderDate);
|
||||
|
||||
List<OrderDetail> details = order1.getDetails();
|
||||
|
||||
// lazy load the details
|
||||
int sz = details.size();
|
||||
assertTrue(sz > 0);
|
||||
|
||||
OrderDetail orderDetail = details.get(0);
|
||||
Order o = orderDetail.getOrder();
|
||||
assertSame(o, order1);
|
||||
|
||||
// load detail into cache
|
||||
DB.find(OrderDetail.class, orderDetail.getId());
|
||||
|
||||
// change order... list before a scalar property
|
||||
Order order2 = DB.reference(Order.class, order.getId());
|
||||
assertNotNull(order2);
|
||||
|
||||
List<OrderDetail> details2 = order2.getDetails();
|
||||
|
||||
// lazy load the details
|
||||
int sz2 = details2.size();
|
||||
assertTrue(sz2 > 0);
|
||||
|
||||
Order o2 = details2.get(0).getOrder();
|
||||
assertSame(o2, order2);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.tests.model.interfaces.Address;
|
||||
import org.tests.model.interfaces.IAddress;
|
||||
import org.tests.model.interfaces.IPerson;
|
||||
import org.tests.model.interfaces.Person;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TestManyOneInterface extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
IAddress a = new Address("hello");
|
||||
|
||||
IPerson p = new Person();
|
||||
|
||||
p.setDefaultAddress(a);
|
||||
|
||||
DB.save(a);
|
||||
DB.save(p);
|
||||
|
||||
//Assert.assertTrue();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.TMapSuperEntity;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class TestMappedSuper extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
TMapSuperEntity e = new TMapSuperEntity();
|
||||
e.setName("babana");
|
||||
|
||||
DB.save(e);
|
||||
|
||||
// select includes a transient property
|
||||
TMapSuperEntity e2 = DB.find(TMapSuperEntity.class)
|
||||
.where().idEq(e.getId())
|
||||
.select("id, name, myint, someObject, bananan")
|
||||
.findOne();
|
||||
|
||||
assertNotNull(e2);
|
||||
|
||||
TMapSuperEntity eSaveDelete = new TMapSuperEntity();
|
||||
eSaveDelete.setName("babana");
|
||||
|
||||
DB.save(eSaveDelete);
|
||||
|
||||
DB.delete(eSaveDelete);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.annotation.Platform;
|
||||
import io.ebean.annotation.IgnorePlatform;
|
||||
import org.tests.model.basic.Address;
|
||||
import org.tests.model.basic.metaannotation.SizeMedium;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* Very simple test case to check if Ebean recognizes meta-annotation correctly.
|
||||
*
|
||||
* @author Roland Praml, FOCONIS AG
|
||||
*/
|
||||
public class TestMetaAnnotation extends BaseTestCase {
|
||||
|
||||
private static final String spaces100 = new String(new char[100]).replace('\0', ' ');
|
||||
private static final String spaces101 = new String(new char[101]).replace('\0', ' ');
|
||||
|
||||
/**
|
||||
* This test writes 100 spaces to "line1" which is annotated with @Size(max=100)
|
||||
*/
|
||||
@Test
|
||||
public void testWrite100SpacesToLine1() {
|
||||
|
||||
Address address = new Address();
|
||||
address.setLine1(spaces100);
|
||||
DB.save(address);
|
||||
}
|
||||
|
||||
/**
|
||||
* This test writes 100 spaces to "line1" which is meta-annotated with {@link SizeMedium}.
|
||||
*/
|
||||
@Test
|
||||
public void testWrite100SpacesToLine2() {
|
||||
|
||||
Address address = new Address();
|
||||
address.setLine2(spaces100);
|
||||
DB.save(address);
|
||||
}
|
||||
|
||||
/**
|
||||
* This test writes 101 spaces to "line1" which is annotated with @Size(max=100).
|
||||
*/
|
||||
@Test
|
||||
@IgnorePlatform({Platform.POSTGRES, Platform.SQLSERVER, Platform.MYSQL, Platform.MARIADB}) // pg & mssql does not fail if string is too long.
|
||||
public void testWrite101SpacesToLine1() {
|
||||
|
||||
Address address = new Address();
|
||||
address.setLine1(spaces101);
|
||||
try {
|
||||
DB.save(address);
|
||||
fail("Test failed, Could insert a too long string");
|
||||
} catch (PersistenceException e) {
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This test writes 101 spaces to "line1" which is meta-annotated with {@link SizeMedium}.
|
||||
*/
|
||||
@Test
|
||||
@IgnorePlatform({Platform.POSTGRES, Platform.SQLSERVER, Platform.MYSQL, Platform.MARIADB})
|
||||
public void testWrite101SpacesToLine2() {
|
||||
|
||||
Address address = new Address();
|
||||
address.setLine2(spaces101);
|
||||
try {
|
||||
DB.save(address);
|
||||
fail("Test failed, Could insert a too long string");
|
||||
} catch (PersistenceException e) {
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Transaction;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.OCar;
|
||||
import org.tests.model.basic.OEngine;
|
||||
import org.tests.model.basic.OGearBox;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
public class TestMultipleOneToOneIUD extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
OEngine engine = new OEngine();
|
||||
engine.setShortDesc("engine 1");
|
||||
|
||||
OGearBox gearBox = new OGearBox();
|
||||
gearBox.setBoxDesc("6 speed manual");
|
||||
gearBox.setSize(6);
|
||||
|
||||
OCar car = new OCar();
|
||||
car.setVin("xx4534");
|
||||
car.setName("test car");
|
||||
car.setEngine(engine);
|
||||
|
||||
try (Transaction txn = DB.beginTransaction()) {
|
||||
DB.save(gearBox);
|
||||
DB.save(car);
|
||||
|
||||
assertNotNull(car.getId());
|
||||
assertNotNull(engine.getEngineId());
|
||||
assertNotNull(gearBox.getId());
|
||||
txn.commit();
|
||||
}
|
||||
|
||||
OCar c2 = DB.find(OCar.class, car.getId());
|
||||
assertNotNull(c2);
|
||||
assertNotNull(c2.getEngine());
|
||||
// gearBox not assigned yet
|
||||
assertNull(c2.getGearBox());
|
||||
|
||||
// ok, assign gearBox
|
||||
c2.setGearBox(gearBox);
|
||||
DB.save(c2);
|
||||
|
||||
// now all should be there...
|
||||
OCar c3 = DB.find(OCar.class, car.getId());
|
||||
assertNotNull(c3);
|
||||
assertNotNull(c3.getEngine());
|
||||
assertNotNull(c3.getGearBox());
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.MNonUpdPropEntity;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TestNonUpdateProperty extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
MNonUpdPropEntity e = new MNonUpdPropEntity();
|
||||
e.setName("name");
|
||||
e.setNote("note");
|
||||
|
||||
DB.save(e);
|
||||
|
||||
MNonUpdPropEntity e2 = DB.find(MNonUpdPropEntity.class, e.getId());
|
||||
|
||||
e2.setName("mod");
|
||||
DB.update(e2);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import io.ebean.TransactionalTestCase;
|
||||
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TestOrderByAnnotation extends TransactionalTestCase {
|
||||
|
||||
@Test
|
||||
public void testOrderBy() {
|
||||
|
||||
Customer custTest = ResetBasicData.createCustAndOrder("testOrderByAnn");
|
||||
|
||||
Customer customer = DB.find(Customer.class, custTest.getId());
|
||||
List<Order> orders = customer.getOrders();
|
||||
|
||||
assertTrue(!orders.isEmpty());
|
||||
|
||||
Query<Order> q1 = DB.find(Order.class)
|
||||
.fetch("details");
|
||||
|
||||
q1.findList();
|
||||
|
||||
String s1 = q1.getGeneratedSql();
|
||||
|
||||
assertTrue(s1.contains("order by t0.id, t1.id asc, t1.order_qty asc, t1.cretime desc"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class TestOrderTotalAmountFormula extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testAsJoin() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Customer> l0 = DB.find(Customer.class)
|
||||
.select("id, name")
|
||||
.fetch("orders", "status, totalAmount")
|
||||
.where()
|
||||
.eq("orders.details.product.name", "Desk")
|
||||
.like("contacts.firstName", "Ji%")
|
||||
.findList();
|
||||
|
||||
for (Customer c0 : l0) {
|
||||
c0.getId();
|
||||
List<Order> orders = c0.getOrders();
|
||||
for (Order order : orders) {
|
||||
order.getId();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.OrderAggregate;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TestOrderTotalAmountReportBean extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
String sql =
|
||||
"select order_id, count(*) as totalItems, sum(order_qty*unit_price) as totalAmount \n" +
|
||||
"from o_order_detail \n" +
|
||||
"group by order_id";
|
||||
|
||||
RawSql rawSql = RawSqlBuilder.parse(sql).columnMapping("order_id", "order.id").create();
|
||||
|
||||
List<OrderAggregate> l0 =
|
||||
DB.find(OrderAggregate.class)
|
||||
.setRawSql(rawSql)
|
||||
.findList();
|
||||
|
||||
for (OrderAggregate r0 : l0) {
|
||||
r0.toString();
|
||||
}
|
||||
|
||||
List<OrderAggregate> l2 = DB.createQuery(OrderAggregate.class)
|
||||
.setRawSql(rawSql)
|
||||
.where().gt("order.id", 0)
|
||||
.having().lt("totalItems", 3).gt("totalAmount", 50).findList();
|
||||
|
||||
for (OrderAggregate r2 : l2) {
|
||||
assertTrue(r2.getTotalItems() < 3);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_when_aliasInUnderscore() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
String sql =
|
||||
"select order_id, count(*) as total_items, sum(order_qty*unit_price) as total_amount \n" +
|
||||
"from o_order_detail \n" +
|
||||
"group by order_id";
|
||||
|
||||
RawSql rawSql = RawSqlBuilder.parse(sql)
|
||||
.columnMapping("order_id", "order.id")
|
||||
.create();
|
||||
|
||||
Query<OrderAggregate> query = DB.find(OrderAggregate.class)
|
||||
.setRawSql(rawSql);
|
||||
|
||||
query.findList();
|
||||
|
||||
assertSql(query).contains("count(*) as total_items, sum(order_qty*unit_price) as total_amount");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_when_aliasInCamelCase() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
String sql =
|
||||
"select order_id, count(*) as totalItems, sum(order_qty*unit_price) as totalAmount \n" +
|
||||
"from o_order_detail \n" +
|
||||
"group by order_id";
|
||||
|
||||
RawSql rawSql = RawSqlBuilder.parse(sql)
|
||||
.columnMapping("order_id", "order.id")
|
||||
.create();
|
||||
|
||||
Query<OrderAggregate> query = DB.find(OrderAggregate.class)
|
||||
.setRawSql(rawSql);
|
||||
|
||||
query.findList();
|
||||
|
||||
assertSql(query).contains("count(*) as totalItems, sum(order_qty*unit_price) as totalAmount");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.SqlRow;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class TestOrderTotalAmountSql extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
String sql = "select order_id, sum(order_qty*unit_price) as total_amount from o_order_detail where order_qty > :minQty group by order_id";
|
||||
List<SqlRow> sqlRows = DB.sqlQuery(sql).setParameter("minQty", 1).findList();
|
||||
|
||||
for (SqlRow sqlRow : sqlRows) {
|
||||
Integer id = sqlRow.getInteger("order_id");
|
||||
Double amount = sqlRow.getDouble("total_amount");
|
||||
assertNotNull(id);
|
||||
assertNotNull(amount);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TestPersistenceContext extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
// implicit transaction with its own
|
||||
// persistence context
|
||||
Order oBefore = DB.find(Order.class, 1);
|
||||
|
||||
Order order = null;
|
||||
|
||||
// start a persistence context
|
||||
DB.beginTransaction();
|
||||
try {
|
||||
|
||||
order = DB.find(Order.class, 1);
|
||||
|
||||
// not the same instance ...as a different
|
||||
// persistence context
|
||||
assertTrue(order != oBefore);
|
||||
|
||||
|
||||
// finds an existing bean in the persistence context
|
||||
// ... so doesn't even execute a query
|
||||
Order o2 = DB.find(Order.class, 1);
|
||||
Order o3 = DB.reference(Order.class, 1);
|
||||
|
||||
// all the same instance
|
||||
assertTrue(order == o2);
|
||||
assertTrue(order == o3);
|
||||
|
||||
} finally {
|
||||
DB.endTransaction();
|
||||
}
|
||||
|
||||
// implicit transaction with its own
|
||||
// persistence context
|
||||
Order oAfter = DB.find(Order.class, 1);
|
||||
|
||||
assertTrue(oAfter != oBefore);
|
||||
assertTrue(oAfter != order);
|
||||
|
||||
// start a persistence context
|
||||
DB.beginTransaction();
|
||||
try {
|
||||
Order testOrder = ResetBasicData.createOrderCustAndOrder("testPC");
|
||||
Integer id = testOrder.getCustomer().getId();
|
||||
Integer orderId = testOrder.getId();
|
||||
|
||||
Customer customer = DB.find(Customer.class)
|
||||
.setUseCache(false)
|
||||
.setId(id)
|
||||
.findOne();
|
||||
|
||||
System.gc();
|
||||
Order order2 = DB.find(Order.class, orderId);
|
||||
Customer customer2 = order2.getCustomer();
|
||||
|
||||
assertEquals(customer.getId(), customer2.getId());
|
||||
assertTrue(customer == customer2);
|
||||
|
||||
} finally {
|
||||
DB.endTransaction();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.AcquireLockException;
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import io.ebean.Query;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.annotation.ForPlatform;
|
||||
import io.ebean.annotation.Platform;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.EBasic;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TestQueryForUpdate extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
@ForPlatform({Platform.H2, Platform.ORACLE, Platform.POSTGRES, Platform.SQLSERVER, Platform.MYSQL, Platform.MARIADB})
|
||||
public void testForUpdate() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query;
|
||||
try (final Transaction transaction = DB.beginTransaction()) {
|
||||
query = DB.find(Customer.class)
|
||||
.forUpdate()
|
||||
.order().desc("id");
|
||||
|
||||
query.findList();
|
||||
}
|
||||
|
||||
if (isSqlServer()) {
|
||||
assertThat(sqlOf(query)).contains("with (updlock)");
|
||||
} else if (isPostgres()) {
|
||||
assertThat(sqlOf(query)).contains("for update");
|
||||
} else {
|
||||
assertThat(sqlOf(query)).contains("for update");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@ForPlatform({ Platform.H2, Platform.ORACLE, Platform.POSTGRES, Platform.SQLSERVER, Platform.MYSQL, Platform.MARIADB})
|
||||
public void testForUpdate_when_alreadyInPC() {
|
||||
|
||||
EBasic basic = new EBasic("test PC cache");
|
||||
DB.save(basic);
|
||||
|
||||
try (Transaction transaction = DB.beginTransaction()) {
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
EBasic basic0 = DB.find(EBasic.class, basic.getId());
|
||||
assertThat(basic0).isNotNull();
|
||||
|
||||
EBasic basic1 = DB.find(EBasic.class)
|
||||
.setId( basic.getId())
|
||||
.forUpdate()
|
||||
.findOne();
|
||||
|
||||
assertThat(basic1).isNotNull();
|
||||
assertThat(basic1).isSameAs(basic0);
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(2);
|
||||
if (isH2() || isPostgres()) {
|
||||
assertSql(sql.get(0)).contains("from e_basic t0 where t0.id =");
|
||||
assertSql(sql.get(1)).contains("from e_basic t0 where t0.id =");
|
||||
if (isPostgres()) {
|
||||
assertSql(sql.get(1)).contains("for update");
|
||||
} else {
|
||||
assertSql(sql.get(1)).contains("for update");
|
||||
}
|
||||
}
|
||||
|
||||
transaction.end();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@ForPlatform({Platform.H2, Platform.POSTGRES, Platform.SQLSERVER, Platform.ORACLE})
|
||||
public void testForUpdate_noWait_noMaxRows() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = DB.find(Customer.class)
|
||||
.forUpdateNoWait()
|
||||
.order().desc("id");
|
||||
|
||||
query.findList();
|
||||
if (isOracle()) {
|
||||
assertThat(sqlOf(query)).contains("for update nowait");
|
||||
} else if (isH2()) {
|
||||
assertThat(sqlOf(query)).contains("for update");
|
||||
} else if (isSqlServer()) {
|
||||
assertThat(sqlOf(query)).contains("with (updlock,nowait)");
|
||||
} else if (isPostgres()) {
|
||||
assertThat(sqlOf(query)).contains("for update nowait");
|
||||
} else {
|
||||
assertThat(sqlOf(query)).contains("for update nowait");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@ForPlatform({Platform.H2, Platform.POSTGRES, Platform.SQLSERVER})
|
||||
public void testForUpdate_noWait() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Database server = DB.getDefault();
|
||||
|
||||
try (Transaction txn = DB.beginTransaction()) {
|
||||
Query<Customer> query = DB.find(Customer.class)
|
||||
.forUpdateNoWait()
|
||||
.setMaxRows(1)
|
||||
.order().desc("id");
|
||||
|
||||
List<Customer> list = query.findList();
|
||||
Customer first = list.get(0);
|
||||
if (isSqlServer()) {
|
||||
assertThat(sqlOf(query)).contains("with (updlock,nowait)");
|
||||
} else if (isH2()) {
|
||||
assertThat(sqlOf(query)).contains("for update");
|
||||
} else if (isPostgres()) {
|
||||
assertThat(sqlOf(query)).contains("for update nowait");
|
||||
} else {
|
||||
assertThat(sqlOf(query)).contains("for update nowait");
|
||||
}
|
||||
// create a 2nd transaction to test that the
|
||||
// row is locked and we can't acquire it
|
||||
try (Transaction txn2 = server.createTransaction()) {
|
||||
logger.info("... attempt another acquire using 2nd transaction");
|
||||
server.find(Customer.class)
|
||||
.where().idEq(first.getId())
|
||||
.forUpdateNoWait()
|
||||
.usingTransaction(txn2)
|
||||
.findOne();
|
||||
|
||||
assertTrue(false); // never get here
|
||||
} catch (AcquireLockException e) {
|
||||
logger.info("... got AcquireLockException " + e);
|
||||
}
|
||||
txn.commit();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.annotation.ForPlatform;
|
||||
import io.ebean.annotation.Platform;
|
||||
import io.ebean.annotation.Transactional;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.tests.model.basic.Article;
|
||||
import org.tests.model.basic.Section;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static io.ebean.Query.LockType.NO_KEY_UPDATE;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestQueryForUpdatePostgresLock extends BaseTestCase {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(TestQueryForUpdatePostgresLock.class);
|
||||
|
||||
private long timePreInsert;
|
||||
private long timePostInsert;
|
||||
private long timePreLock;
|
||||
private long timePostLock;
|
||||
|
||||
@Test
|
||||
@ForPlatform(Platform.POSTGRES)
|
||||
public void testForUpdatePostgresLock() throws InterruptedException {
|
||||
|
||||
Article article = new Article("lockTest", "auth");
|
||||
DB.save(article);
|
||||
final Integer id = article.getId();
|
||||
|
||||
ExecutorService exec = Executors.newFixedThreadPool(2);
|
||||
exec.submit(() -> lockArticle(id));
|
||||
exec.submit(() -> insertSection(id));
|
||||
exec.awaitTermination(2, TimeUnit.SECONDS);
|
||||
exec.shutdown();
|
||||
|
||||
// assert the lock was obtained before the insert was attempted
|
||||
assertThat(timePreLock).isLessThan(timePreInsert);
|
||||
// assert that the insert wasn't waiting on the lock to complete
|
||||
assertThat(timePostInsert).isLessThan(timePostLock);
|
||||
}
|
||||
|
||||
/**
|
||||
* This holds row lock on article for 1 second.
|
||||
* With FOR NO KEY UPDATE this does not block the insert.
|
||||
*/
|
||||
@Transactional
|
||||
private void lockArticle(Integer id) {
|
||||
timePreLock = System.currentTimeMillis();
|
||||
log.info("lock start");
|
||||
DB.find(Article.class).setId(id).withLock(NO_KEY_UPDATE).findOne();
|
||||
sleep(1000);
|
||||
timePostLock = System.currentTimeMillis();
|
||||
log.info("lock done");
|
||||
}
|
||||
|
||||
/**
|
||||
* This inserts with FK to the article that is locked.
|
||||
* With FOR NO KEY UPDATE this insert does not wait on the lock.
|
||||
*/
|
||||
@Transactional
|
||||
private void insertSection(Integer id) {
|
||||
sleep(100);
|
||||
log.info("insert start");
|
||||
timePreInsert = System.currentTimeMillis();
|
||||
Section section = new Section();
|
||||
section.setArticle(DB.reference(Article.class, id));
|
||||
DB.save(section);
|
||||
timePostInsert = System.currentTimeMillis();
|
||||
log.info("inserted");
|
||||
}
|
||||
|
||||
private void sleep(int millis) {
|
||||
try {
|
||||
Thread.sleep(millis);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.annotation.IgnorePlatform;
|
||||
import io.ebean.annotation.Platform;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Country;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class TestQueryUsingConnection extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void usingConnection() throws SQLException {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
DataSource dataSource = DB.getDefault().dataSource();
|
||||
|
||||
try (Connection connection = dataSource.getConnection()) {
|
||||
|
||||
int count = DB.find(Country.class)
|
||||
.usingConnection(connection)
|
||||
.findCount();
|
||||
|
||||
assertThat(count).isGreaterThan(0);
|
||||
}
|
||||
}
|
||||
|
||||
@IgnorePlatform(Platform.SQLSERVER)
|
||||
@Test
|
||||
public void usingTransaction() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
try (Transaction transaction = DB.createTransaction()) {
|
||||
|
||||
Transaction current = Transaction.current();
|
||||
assertThat(current).isNull();
|
||||
|
||||
Country x = new Country();
|
||||
x.setCode("xx");
|
||||
x.setName("WillRollThisBack");
|
||||
|
||||
DB.getDefault().insert(x, transaction);
|
||||
|
||||
final CountryDto dto = DB.findDto(CountryDto.class, "select code, name from o_country where code=?")
|
||||
.usingTransaction(transaction)
|
||||
.setParameter(1, "xx")
|
||||
.findOne();
|
||||
|
||||
assertThat(dto).isNotNull();
|
||||
assertEquals("xx", dto.code);
|
||||
assertEquals("WillRollThisBack", dto.name);
|
||||
|
||||
final int count = DB.find(Country.class)
|
||||
.usingTransaction(transaction)
|
||||
.findCount();
|
||||
|
||||
final int otherCount = DB.find(Country.class).findCount();
|
||||
|
||||
transaction.rollback();
|
||||
|
||||
assertThat(count).isEqualTo(otherCount + 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class CountryDto {
|
||||
final String code;
|
||||
final String name;
|
||||
|
||||
public CountryDto(String code, String name) {
|
||||
this.code = code;
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.ESimple;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestQueryUsingDatabase {
|
||||
|
||||
private static final String SOME_OTHER_DB_NAME = "someotherdb";
|
||||
public static final ESimple RECORD1 = new ESimple();
|
||||
public static final ESimple RECORD2 = new ESimple();
|
||||
|
||||
@BeforeEach
|
||||
public void setupNonDefaultDatabase() {
|
||||
DB.byName(SOME_OTHER_DB_NAME).insert(RECORD1);
|
||||
DB.byName(SOME_OTHER_DB_NAME).insert(RECORD2);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void shutdown() {
|
||||
DB.byName(SOME_OTHER_DB_NAME).shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void usingNonDefaultDatabase() {
|
||||
final Database nonDefaultDb = DB.byName(SOME_OTHER_DB_NAME);
|
||||
|
||||
List<ESimple> orderList = DB.find(ESimple.class)
|
||||
.usingDatabase(nonDefaultDb)
|
||||
.findList();
|
||||
|
||||
assertThat(orderList).size().isEqualTo(2);
|
||||
assertThat(orderList).containsExactlyInAnyOrder(RECORD1, RECORD2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TestQueryWhereBetween extends BaseTestCase {
|
||||
|
||||
long statCount;
|
||||
long statTotal;
|
||||
|
||||
@Test
|
||||
public void testCountOrderBy() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Timestamp t = new Timestamp(System.currentTimeMillis());
|
||||
|
||||
Query<Order> query = DB.find(Order.class).setAutoTune(false).where()
|
||||
.betweenProperties("cretime", "updtime", t).order().asc("orderDate").order().desc("id");
|
||||
|
||||
query.findList();
|
||||
|
||||
String sql = query.getGeneratedSql();
|
||||
assertTrue(sql.contains("between t0.cretime and t0.updtime"));
|
||||
}
|
||||
|
||||
@Disabled
|
||||
@Test
|
||||
public void doStuff() {
|
||||
|
||||
someLoop(3, true);
|
||||
|
||||
int loop = 30000;
|
||||
someLoop(loop, true);
|
||||
someLoop(loop, true);
|
||||
someLoop(loop, true);
|
||||
someLoop(loop);
|
||||
someLoop(loop);
|
||||
someLoop(loop);
|
||||
someLoop(loop);
|
||||
someLoop(loop);
|
||||
someLoop(loop);
|
||||
someLoop(loop);
|
||||
someLoop(loop);
|
||||
someLoop(loop);
|
||||
someLoop(loop);
|
||||
someLoop(loop);
|
||||
someLoop(loop);
|
||||
someLoop(loop);
|
||||
someLoop(loop);
|
||||
someLoop(loop);
|
||||
someLoop(loop);
|
||||
someLoop(loop);
|
||||
|
||||
long avg = statTotal / statCount;
|
||||
System.out.println("avg "+avg);
|
||||
}
|
||||
|
||||
private void someLoop(int loop) {
|
||||
someLoop(loop, false);
|
||||
}
|
||||
private void someLoop(int loop, boolean warm) {
|
||||
|
||||
long start = System.currentTimeMillis();
|
||||
for (int i = 0; i < loop; i++) {
|
||||
someQuery();
|
||||
}
|
||||
|
||||
long exe = System.currentTimeMillis() - start;
|
||||
System.out.println("exe: "+exe);
|
||||
|
||||
if (!warm) {
|
||||
statTotal += exe;
|
||||
statCount++;
|
||||
}
|
||||
}
|
||||
|
||||
private void someQuery() {
|
||||
|
||||
Timestamp t = new Timestamp(System.currentTimeMillis());
|
||||
|
||||
Query<Order> query = DB.find(Order.class).setAutoTune(false)
|
||||
.where()
|
||||
.le("cretime", t)
|
||||
.order().asc("orderDate")
|
||||
.order().desc("id");
|
||||
|
||||
query.findList();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestQueryWhereInRange extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void inRange() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
LocalDate today = LocalDate.now();
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.where().inRange("orderDate", today.minusDays(7), today)
|
||||
.isNotNull("id")
|
||||
.query();
|
||||
|
||||
query.findList();
|
||||
|
||||
assertThat(sqlOf(query)).contains("where (t0.order_date >= ? and t0.order_date < ?) and ");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void eqOrNull() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
LocalDate today = LocalDate.now();
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.where().eqOrNull("orderDate", today)
|
||||
.query();
|
||||
|
||||
query.findList();
|
||||
|
||||
assertSql(query).contains("where (t0.order_date = ? or t0.order_date is null)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void gtOrNull() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
LocalDate today = LocalDate.now();
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.where().gtOrNull("orderDate", today)
|
||||
.query();
|
||||
|
||||
query.findList();
|
||||
|
||||
assertSql(query).contains("where (t0.order_date > ? or t0.order_date is null)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void geOrNull() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
LocalDate today = LocalDate.now();
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.where().geOrNull("orderDate", today)
|
||||
.query();
|
||||
|
||||
query.findList();
|
||||
|
||||
assertSql(query).contains("where (t0.order_date >= ? or t0.order_date is null)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ltOrNull() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
LocalDate today = LocalDate.now();
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.where().ltOrNull("orderDate", today)
|
||||
.query();
|
||||
|
||||
query.findList();
|
||||
assertSql(query).contains(" where (t0.order_date < ? or t0.order_date is null)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void leOrNull() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
LocalDate today = LocalDate.now();
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.where().leOrNull("orderDate", today)
|
||||
.query();
|
||||
|
||||
query.findList();
|
||||
assertSql(query).contains(" where (t0.order_date <= ? or t0.order_date is null)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void inRangeWith() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
LocalDate today = LocalDate.now();
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.where().inRangeWith("orderDate", "shipDate", today)
|
||||
.query();
|
||||
|
||||
query.findList();
|
||||
|
||||
assertSql(query).contains("(t0.order_date <= ? and (t0.ship_date > ? or t0.ship_date is null))");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import io.ebean.cache.ServerCache;
|
||||
import io.ebean.cache.ServerCacheManager;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import io.ebeaninternal.server.core.CacheOptions;
|
||||
import io.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import org.tests.model.basic.Country;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestQueryWithCache extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testCountryDeploy() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
SpiEbeanServer server = (SpiEbeanServer) DB.getDefault();
|
||||
BeanDescriptor<Country> beanDescriptor = server.descriptor(Country.class);
|
||||
CacheOptions cacheOptions = beanDescriptor.cacheOptions();
|
||||
|
||||
assertNotNull(cacheOptions);
|
||||
assertTrue(cacheOptions.isReadOnly());
|
||||
assertTrue(beanDescriptor.isCacheSharableBeans());
|
||||
|
||||
ServerCacheManager serverCacheManager = server.cacheManager();
|
||||
serverCacheManager.clear(Country.class);
|
||||
|
||||
ServerCache beanCache = serverCacheManager.beanCache(Country.class);
|
||||
assertEquals(0, beanCache.size());
|
||||
|
||||
Country nz1 = DB.reference(Country.class, "NZ");
|
||||
assertEquals(0, beanCache.size());
|
||||
|
||||
// has the effect of loading the cache via lazy loading
|
||||
nz1.getName();
|
||||
assertEquals(1, beanCache.size());
|
||||
|
||||
Country nz2 = DB.reference(Country.class, "NZ");
|
||||
Country nz2b = DB.reference(Country.class, "NZ");
|
||||
|
||||
Country nz3 = DB.find(Country.class, "NZ");
|
||||
|
||||
Country nz4 = DB.find(Country.class).setId("NZ").setAutoTune(false).setUseCache(false)
|
||||
.findOne();
|
||||
|
||||
assertTrue(nz2 == nz2b);
|
||||
assertTrue(nz2 == nz3);
|
||||
assertTrue(nz3 != nz4);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSkipCache() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
DB.find(Country.class, "NZ");
|
||||
|
||||
Query<Country> query = DB.find(Country.class).setId("NZ").setUseCache(false);
|
||||
query.findOne();
|
||||
|
||||
assertSql(query).isNotNull();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.bean.BeanCollection;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Address;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.OrderDetail;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TestReadOnlyPropagation extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testReadOnly() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
DB.cacheManager().clearAll();
|
||||
|
||||
Order order = DB.find(Order.class)
|
||||
.setReadOnly(true)
|
||||
.setId(1)
|
||||
.findOne();
|
||||
|
||||
assertTrue(DB.beanState(order).isReadOnly());
|
||||
|
||||
|
||||
Customer customer = order.getCustomer();
|
||||
assertTrue(DB.beanState(customer).isReadOnly());
|
||||
|
||||
Address billingAddress = customer.getBillingAddress();
|
||||
assertNotNull(billingAddress);
|
||||
assertTrue(DB.beanState(billingAddress).isReadOnly());
|
||||
|
||||
|
||||
List<OrderDetail> details = order.getDetails();
|
||||
BeanCollection<?> bc = (BeanCollection<?>) details;
|
||||
|
||||
assertTrue(bc.isReadOnly());
|
||||
assertTrue(!bc.isPopulated());
|
||||
|
||||
bc.size();
|
||||
assertTrue(!bc.isEmpty());
|
||||
assertTrue(bc.isReadOnly());
|
||||
assertTrue(bc.isPopulated());
|
||||
try {
|
||||
details.add(new OrderDetail());
|
||||
assertTrue(false);
|
||||
} catch (IllegalStateException e) {
|
||||
assertTrue(true);
|
||||
}
|
||||
try {
|
||||
details.remove(0);
|
||||
assertTrue(false);
|
||||
} catch (IllegalStateException e) {
|
||||
assertTrue(true);
|
||||
}
|
||||
try {
|
||||
Iterator<OrderDetail> it = details.iterator();
|
||||
it.next();
|
||||
it.remove();
|
||||
assertTrue(false);
|
||||
} catch (IllegalStateException e) {
|
||||
assertTrue(true);
|
||||
}
|
||||
try {
|
||||
ListIterator<OrderDetail> it = details.listIterator();
|
||||
it.next();
|
||||
it.remove();
|
||||
assertTrue(false);
|
||||
} catch (IllegalStateException e) {
|
||||
assertTrue(true);
|
||||
}
|
||||
try {
|
||||
List<OrderDetail> subList = details.subList(0, 1);
|
||||
subList.remove(0);
|
||||
assertTrue(false);
|
||||
} catch (UnsupportedOperationException e) {
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.PersistentFile;
|
||||
import org.tests.model.basic.PersistentFileContent;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class TestSaveDeleteOneToOne extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testCreateDeletePersistentFile() {
|
||||
PersistentFile persistentFile = new PersistentFile("test.txt",
|
||||
new PersistentFileContent("test".getBytes()));
|
||||
|
||||
DB.save(persistentFile);
|
||||
DB.delete(persistentFile);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateLoadDeletePersistentFile() {
|
||||
PersistentFile persistentFile = new PersistentFile("test.txt",
|
||||
new PersistentFileContent("test".getBytes()));
|
||||
|
||||
DB.save(persistentFile);
|
||||
|
||||
persistentFile = DB.find(PersistentFile.class, persistentFile.getId());
|
||||
|
||||
PersistentFileContent persistentFileContent = persistentFile.getPersistentFileContent();
|
||||
|
||||
assertNotNull(persistentFileContent);
|
||||
assertNotNull(persistentFileContent.getContent());
|
||||
|
||||
DB.delete(persistentFile);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.PFile;
|
||||
import org.tests.model.basic.PFileContent;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class TestSaveDeleteOneToOneMultiple extends BaseTestCase {
|
||||
|
||||
// public void testCreateDeletePFile() {
|
||||
// PFile persistentFile = new PFile("test.txt",
|
||||
// new PFileContent("test".getBytes()));
|
||||
//
|
||||
// DB.save(persistentFile);
|
||||
// DB.delete(persistentFile);
|
||||
// }
|
||||
|
||||
@Test
|
||||
public void testCreateLoadDeletePFile() {
|
||||
PFile persistentFile = new PFile("test.txt",
|
||||
new PFileContent("test".getBytes()));
|
||||
|
||||
DB.save(persistentFile);
|
||||
|
||||
persistentFile = DB.find(PFile.class, persistentFile.getId());
|
||||
|
||||
PFileContent persistentFileContent = persistentFile.getFileContent();
|
||||
|
||||
assertNotNull(persistentFileContent);
|
||||
assertNotNull(persistentFileContent.getContent());
|
||||
DB.delete(persistentFile);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.bean.BeanCollection;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.OrderDetail;
|
||||
import org.tests.model.basic.Product;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestSharedInstancePropagation extends BaseTestCase {
|
||||
|
||||
|
||||
/**
|
||||
* Test that the sharedInstance status is propagated on lazy loading.
|
||||
*/
|
||||
@Test
|
||||
public void testSharedListNavigate() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
DB.cacheManager().clearAll();
|
||||
|
||||
Order order = DB.find(Order.class)
|
||||
.setAutoTune(false)
|
||||
.setReadOnly(true)
|
||||
.setId(1)
|
||||
.findOne();
|
||||
|
||||
|
||||
assertNotNull(order);
|
||||
assertTrue(DB.beanState(order).isReadOnly());
|
||||
|
||||
List<OrderDetail> details = order.getDetails();
|
||||
BeanCollection<?> bc = (BeanCollection<?>) details;
|
||||
assertTrue(bc.isReadOnly());
|
||||
assertFalse(bc.isPopulated());
|
||||
|
||||
// lazy load
|
||||
bc.size();
|
||||
|
||||
assertTrue(bc.isPopulated());
|
||||
assertTrue(!bc.isEmpty());
|
||||
OrderDetail detail = details.get(0);
|
||||
|
||||
assertTrue(DB.beanState(detail).isReadOnly());
|
||||
assertFalse(DB.beanState(detail).isReference());
|
||||
|
||||
Product product = detail.getProduct();
|
||||
|
||||
assertTrue(DB.beanState(product).isReadOnly());
|
||||
|
||||
// lazy load
|
||||
product.getName();
|
||||
assertFalse(DB.beanState(product).isReference());
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.BeanState;
|
||||
import io.ebean.DB;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
|
||||
import java.sql.Types;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TestTransient extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testTransient() {
|
||||
|
||||
Customer cnew = new Customer();
|
||||
cnew.setName("testTrans");
|
||||
|
||||
DB.save(cnew);
|
||||
Integer custId = cnew.getId();
|
||||
|
||||
Customer c = DB.find(Customer.class).setId(custId).findOne();
|
||||
|
||||
assertNotNull(c);
|
||||
|
||||
BeanState beanState = DB.beanState(c);
|
||||
assertFalse(beanState.isNewOrDirty());
|
||||
|
||||
c.getLock().tryLock();
|
||||
try {
|
||||
c.setSelected(Boolean.TRUE);
|
||||
} finally {
|
||||
c.getLock().unlock();
|
||||
}
|
||||
|
||||
Boolean selected = c.getSelected();
|
||||
assertNotNull(selected);
|
||||
|
||||
assertFalse(beanState.isNewOrDirty());
|
||||
|
||||
DB.save(c);
|
||||
|
||||
selected = c.getSelected();
|
||||
assertNotNull(selected);
|
||||
|
||||
c.setName("Modified");
|
||||
assertTrue(beanState.isNewOrDirty());
|
||||
|
||||
selected = c.getSelected();
|
||||
assertNotNull(selected);
|
||||
|
||||
DB.save(c);
|
||||
assertFalse(beanState.isNewOrDirty());
|
||||
|
||||
selected = c.getSelected();
|
||||
assertNotNull(selected);
|
||||
|
||||
String updateStmt = "update customer set smallnote = 'testTrans2' where id = :id";
|
||||
int rows = DB.createUpdate(Customer.class, updateStmt)
|
||||
.setParameter("id", custId).execute();
|
||||
assertEquals(1, rows);
|
||||
assertEquals("testTrans2", findNote(custId).get());
|
||||
|
||||
rows = DB.createUpdate(Customer.class, "update customer set smallnote = ? where id = ?")
|
||||
.setNull(1, Types.VARCHAR)
|
||||
.setParameter(2, custId).execute();
|
||||
assertEquals(1, rows);
|
||||
assertThat(findNote(custId)).isEmpty();
|
||||
|
||||
rows = DB.createUpdate(Customer.class, "update customer set smallnote = ? where id = ?")
|
||||
.setParameter(1, "Foo")
|
||||
.setParameter(2, custId).execute();
|
||||
assertEquals(1, rows);
|
||||
assertEquals("Foo", findNote(custId).get());
|
||||
|
||||
rows = DB.createUpdate(Customer.class, "update customer set smallnote = :name where id = :id")
|
||||
.setNullParameter("name", Types.VARCHAR)
|
||||
.setParameter("id", custId).execute();
|
||||
assertEquals(1, rows);
|
||||
assertThat(findNote(custId)).isEmpty();
|
||||
|
||||
// cleanup
|
||||
DB.delete(Customer.class, custId);
|
||||
}
|
||||
|
||||
private Optional<String> findNote(Integer custId) {
|
||||
return DB.sqlQuery("select smallnote from o_customer where id = ?")
|
||||
.setParameter(custId)
|
||||
.mapToScalar(String.class).findOneOrEmpty();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class TestUpdateManyToOne extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Customer> custs = DB.find(Customer.class).findList();
|
||||
|
||||
List<Order> orders = DB.find(Order.class).setMaxRows(1).findList();
|
||||
|
||||
Order order = orders.get(0);
|
||||
Customer customer = order.getCustomer();
|
||||
|
||||
Customer changeCust = null;
|
||||
for (Customer c : custs) {
|
||||
if (!customer.getId().equals(c.getId())) {
|
||||
changeCust = c;
|
||||
break;
|
||||
}
|
||||
}
|
||||
order.setCustomer(changeCust);
|
||||
DB.save(order);
|
||||
|
||||
order.setCustomer(customer);
|
||||
DB.save(order);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.TUuidEntity;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TestUuidInsert extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
TUuidEntity e = new TUuidEntity();
|
||||
e.setName("bana");
|
||||
|
||||
DB.save(e);
|
||||
|
||||
TUuidEntity e2 = DB.find(TUuidEntity.class, e.getId());
|
||||
e2.setName("apple");
|
||||
|
||||
DB.save(e2);
|
||||
|
||||
DB.delete(e2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.UUOne;
|
||||
import org.tests.model.basic.UUTwo;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class TestUuidInsertMasterDetail extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testInsert() {
|
||||
|
||||
UUTwo two = new UUTwo();
|
||||
two.setName("something");
|
||||
|
||||
ArrayList<UUTwo> list = new ArrayList<>();
|
||||
list.add(two);
|
||||
|
||||
UUOne one = new UUOne();
|
||||
one.setName("some one");
|
||||
one.setComments(list);
|
||||
|
||||
DB.save(one);
|
||||
|
||||
UUOne oneB = DB.find(UUOne.class, one.getId());
|
||||
|
||||
UUTwo twoB = new UUTwo();
|
||||
twoB.setName("another something");
|
||||
oneB.getComments().add(twoB);
|
||||
|
||||
DB.save(oneB);
|
||||
}
|
||||
|
||||
public void testNullFK() {
|
||||
|
||||
UUTwo two = new UUTwo();
|
||||
two.setName("something");
|
||||
DB.save(two);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import io.ebean.TransactionalTestCase;
|
||||
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TestWhereAnnotation extends TransactionalTestCase {
|
||||
|
||||
@Test
|
||||
public void testWhere() {
|
||||
|
||||
Customer custTest = ResetBasicData.createCustAndOrder("testWhereAnn");
|
||||
|
||||
Customer customer = DB.find(Customer.class, custTest.getId());
|
||||
List<Order> orders = customer.getOrders();
|
||||
|
||||
assertTrue(!orders.isEmpty());
|
||||
|
||||
Query<Customer> q1 = DB.find(Customer.class).setUseCache(false).fetch("orders").where()
|
||||
.idEq(1).query();
|
||||
|
||||
q1.findOne();
|
||||
String s1 = q1.getGeneratedSql();
|
||||
assertThat(s1).contains("t1.order_date is not null");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package org.tests.basic.delete;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.OrderDetail;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class TestDeleteByIdList extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
OrderDetail dummy = DB.reference(OrderDetail.class, 1);
|
||||
SpiEbeanServer server = (SpiEbeanServer) DB.getDefault();
|
||||
server.descriptor(OrderDetail.class).cacheBeanPut(dummy);
|
||||
|
||||
Customer c0 = ResetBasicData.createCustAndOrder("DelIdList-0");
|
||||
assertNotNull(c0);
|
||||
|
||||
Customer c1 = ResetBasicData.createCustAndOrder("DelIdList-1");
|
||||
|
||||
List<Object> orderIds = DB.find(Order.class).where().in("customer", c0, c1).findIds();
|
||||
|
||||
assertEquals(2, orderIds.size());
|
||||
|
||||
DB.deleteAll(Order.class, orderIds);
|
||||
DB.delete(c0);
|
||||
DB.delete(c1);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package org.tests.basic.delete;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.OrderDetail;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class TestDeleteCascadeById extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
OrderDetail dummy = DB.reference(OrderDetail.class, 1);
|
||||
SpiEbeanServer server = (SpiEbeanServer) DB.getDefault();
|
||||
server.descriptor(OrderDetail.class).cacheBeanPut(dummy);
|
||||
|
||||
Customer cust = ResetBasicData.createCustAndOrder("DelCas");
|
||||
assertNotNull(cust);
|
||||
|
||||
List<Order> orders = DB.find(Order.class).where().eq("customer", cust).findList();
|
||||
|
||||
assertEquals(1, orders.size());
|
||||
Order o = orders.get(0);
|
||||
assertNotNull(o);
|
||||
|
||||
// cleanup
|
||||
DB.delete(o);
|
||||
DB.delete(cust);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package org.tests.basic.delete;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.onetoone.OtoUser;
|
||||
import org.tests.model.onetoone.OtoUserOptional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestDeleteCascadeByQuery extends BaseTestCase {
|
||||
|
||||
private OtoUser testUser;
|
||||
private OtoUserOptional userOptional;
|
||||
private Query<OtoUserOptional> userOptionalQuery = DB.find(OtoUserOptional.class);
|
||||
private Query<OtoUser> userQuery = DB.find(OtoUser.class);
|
||||
|
||||
/**
|
||||
* Init each test. Delete all existing beans. Then create OtoUser, add OtoUserOptional and save.
|
||||
*/
|
||||
@BeforeEach
|
||||
public void init() {
|
||||
DB.deleteAll(userQuery.findList());
|
||||
DB.deleteAll(userOptionalQuery.findList());
|
||||
|
||||
userOptional = new OtoUserOptional();
|
||||
DB.save(userOptional);
|
||||
testUser = new OtoUser();
|
||||
testUser.setOptional(userOptional);
|
||||
DB.save(testUser);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that validates deleting a bean using DB.delete() respects the CascadeType.DELETE
|
||||
* setting.
|
||||
*/
|
||||
@Test
|
||||
public void testDeleteCascadeByEbeanDelete() {
|
||||
|
||||
assertThat(DB.delete(testUser)).isTrue();
|
||||
|
||||
assertThat(userOptionalQuery.findCount())
|
||||
.overridingErrorMessage("Entity OtoUserOptional found. DB.delete() on the user "
|
||||
+ "did not delete the OneToOne mapped entity as set with CascadeType.ALL")
|
||||
.isEqualTo(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that validates deleting a bean with OneToOne mapping with a query respects the
|
||||
* CascadeType.DELETE setting.
|
||||
*/
|
||||
@Test
|
||||
public void testDeleteCascadeByQuery() {
|
||||
|
||||
assertThat(userQuery.delete()).isEqualTo(1);
|
||||
|
||||
assertThat(userOptionalQuery.findCount())
|
||||
.overridingErrorMessage("Entity OtoUserOptional found. Ebean query delete() on the user "
|
||||
+ "did not delete the OneToOne mapped entity as set with CascadeType.ALL")
|
||||
.isEqualTo(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleanup - Delete all existing beans for the next test.
|
||||
*/
|
||||
@AfterEach
|
||||
public void cleanup() {
|
||||
DB.deleteAll(userQuery.findList());
|
||||
DB.deleteAll(userOptionalQuery.findList());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.tests.basic.encrypt;
|
||||
|
||||
import io.ebean.config.EncryptKey;
|
||||
|
||||
public class BasicEncryptKey implements EncryptKey {
|
||||
|
||||
private final String key;
|
||||
|
||||
public BasicEncryptKey(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getStringValue() {
|
||||
return key;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package org.tests.basic.encrypt;
|
||||
|
||||
import io.ebean.config.EncryptKey;
|
||||
import io.ebean.config.EncryptKeyManager;
|
||||
|
||||
public class BasicEncyptKeyManager implements EncryptKeyManager {
|
||||
|
||||
/**
|
||||
* Initialise the key manager.
|
||||
*/
|
||||
@Override
|
||||
public void initialise() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public EncryptKey getEncryptKey(String tableName, String columnName) {
|
||||
// Must be 16 Chars for Oracle function
|
||||
return new BasicEncryptKey("simple0123456789");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
package org.tests.basic.encrypt;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.SqlRow;
|
||||
import io.ebean.annotation.ForPlatform;
|
||||
import io.ebean.annotation.Platform;
|
||||
import io.ebean.config.dbplatform.DbEncrypt;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.EBasicEncrypt;
|
||||
import org.tests.model.basic.EBasicEncryptRelate;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class TestEncrypt extends BaseTestCase {
|
||||
|
||||
|
||||
@Test
|
||||
@ForPlatform({Platform.H2, Platform.SQLSERVER}) // only run this on H2 - PGCrypto not happy on CI server
|
||||
public void testQueryBind() {
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
DB.find(EBasicEncrypt.class)
|
||||
.where().startsWith("description", "Rob")
|
||||
.findList();
|
||||
|
||||
List<String> loggedSql = LoggedSqlCollector.stop();
|
||||
assertThat(loggedSql).hasSize(1);
|
||||
assertThat(loggedSql.get(0)).contains("; --bind(****,Rob%)");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ForPlatform({Platform.H2, Platform.SQLSERVER}) // only run this on H2 - PGCrypto not happy on CI server
|
||||
public void testQueryJoin() {
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
DB.find(EBasicEncryptRelate.class)
|
||||
.where().eq("other.description", "foo")
|
||||
.findList();
|
||||
|
||||
DB.find(EBasicEncryptRelate.class)
|
||||
.where().eq("other.description", "foo")
|
||||
.findCount();
|
||||
|
||||
List<String> loggedSql = LoggedSqlCollector.stop();
|
||||
assertThat(loggedSql).hasSize(2);
|
||||
assertThat(loggedSql.get(0)).contains("left join e_basicenc t1 on t1.id = t0.other_id");
|
||||
assertThat(loggedSql.get(1)).contains("left join e_basicenc t1 on t1.id = t0.other_id");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void asDto() {
|
||||
DB.find(EBasicEncrypt.class).delete();
|
||||
|
||||
EBasicEncrypt e = new EBasicEncrypt();
|
||||
e.setName("name2");
|
||||
e.setDescription("descEncrypted2");
|
||||
e.setDob(new Date(System.currentTimeMillis() - 100000));
|
||||
|
||||
DB.save(e);
|
||||
|
||||
final EDto dto = DB.find(EBasicEncrypt.class)
|
||||
.select("name, description")
|
||||
.asDto(EDto.class)
|
||||
.findOne();
|
||||
|
||||
assertThat(dto).isNotNull();
|
||||
assertThat(dto.getName()).isEqualTo("name2");
|
||||
assertThat(dto.getDescription()).isEqualTo("descEncrypted2");
|
||||
}
|
||||
|
||||
public static class EDto {
|
||||
String name;
|
||||
String description;
|
||||
public EDto(){
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@ForPlatform({Platform.H2, Platform.SQLSERVER})
|
||||
public void test() {
|
||||
|
||||
DB.find(EBasicEncrypt.class).delete();
|
||||
|
||||
EBasicEncrypt e = new EBasicEncrypt();
|
||||
e.setName("testname");
|
||||
e.setDescription("testdesc");
|
||||
e.setDob(new Date(System.currentTimeMillis() - 100000));
|
||||
|
||||
DB.save(e);
|
||||
|
||||
SqlRow row = DB.sqlQuery("select * from e_basicenc where id = :id")
|
||||
.setParameter("id", e.getId())
|
||||
.findOne();
|
||||
|
||||
row.getString("name");
|
||||
row.get("description");
|
||||
|
||||
EBasicEncrypt e1 = DB.find(EBasicEncrypt.class, e.getId());
|
||||
|
||||
e1.getDescription();
|
||||
|
||||
e1.setName("testmod");
|
||||
e1.setDescription("moddesc");
|
||||
e1.setStatus(EBasicEncrypt.Status.ONE);
|
||||
|
||||
DB.save(e1);
|
||||
|
||||
EBasicEncrypt e2 = DB.find(EBasicEncrypt.class, e.getId());
|
||||
|
||||
assertEquals("moddesc", e2.getDescription());
|
||||
assertEquals(EBasicEncrypt.Status.ONE, e2.getStatus());
|
||||
|
||||
SpiEbeanServer server = (SpiEbeanServer) DB.getDefault();
|
||||
DbEncrypt dbEncrypt = server.databasePlatform().getDbEncrypt();
|
||||
|
||||
if (dbEncrypt == null) {
|
||||
// can not test the where clause
|
||||
System.out.println("TestEncrypt: Not testing where clause as no DbEncrypt");
|
||||
|
||||
} else {
|
||||
|
||||
List<EBasicEncrypt> list = DB.find(EBasicEncrypt.class).where()
|
||||
.eq("description", "moddesc").findList();
|
||||
|
||||
assertEquals(1, list.size());
|
||||
|
||||
list = DB.find(EBasicEncrypt.class).where().startsWith("description", "modde").findList();
|
||||
|
||||
assertEquals(1, list.size());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package org.tests.basic.encrypt;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.SqlRow;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.EBasicEncryptBinary;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class TestEncryptBinary extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
Timestamp t0 = new Timestamp(System.currentTimeMillis());
|
||||
EBasicEncryptBinary e = new EBasicEncryptBinary();
|
||||
e.setName("test1");
|
||||
e.setDescription("testdesc1");
|
||||
e.setSomeTime(t0);
|
||||
e.setData("HelloWorld".getBytes());
|
||||
|
||||
DB.save(e);
|
||||
|
||||
SqlRow row = DB.sqlQuery("select * from e_basicenc_bin where id = :id")
|
||||
.setParameter("id", e.getId())
|
||||
.findOne();
|
||||
|
||||
row.getString("name");
|
||||
row.get("data");
|
||||
row.get("some_time");
|
||||
|
||||
EBasicEncryptBinary e1 = DB.find(EBasicEncryptBinary.class, e.getId());
|
||||
|
||||
Timestamp t1 = e1.getSomeTime();
|
||||
e1.getData();
|
||||
|
||||
e1.getDescription();
|
||||
|
||||
assertEquals(t0, t1);
|
||||
|
||||
e1.setName("testmod");
|
||||
e1.setDescription("moddesc");
|
||||
|
||||
DB.save(e1);
|
||||
|
||||
EBasicEncryptBinary e2 = DB.find(EBasicEncryptBinary.class, e.getId());
|
||||
e2.getDescription();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package org.tests.basic.encrypt;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.EBasicEncryptClient;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestEncryptClientSide extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void insertUpdate() {
|
||||
|
||||
LocalDate today = LocalDate.now();
|
||||
|
||||
EBasicEncryptClient bean = new EBasicEncryptClient();
|
||||
bean.setDescription("hello");
|
||||
bean.setStatus(EBasicEncryptClient.Status.ONE);
|
||||
bean.setDob(today);
|
||||
|
||||
DB.save(bean);
|
||||
|
||||
EBasicEncryptClient found = DB.find(EBasicEncryptClient.class)
|
||||
.where()
|
||||
.eq("description", "hello")
|
||||
.eq("status", EBasicEncryptClient.Status.ONE)
|
||||
.eq("dob", today)
|
||||
.findOne();
|
||||
|
||||
assertThat(found).isNotNull();
|
||||
assertThat(found.getDescription()).isEqualTo("hello");
|
||||
assertThat(found.getStatus()).isEqualTo(EBasicEncryptClient.Status.ONE);
|
||||
assertThat(found.getDob()).isEqualTo(today);
|
||||
|
||||
|
||||
found.setDescription("goodbye");
|
||||
found.setStatus(EBasicEncryptClient.Status.TWO);
|
||||
DB.save(found);
|
||||
|
||||
found = DB.find(EBasicEncryptClient.class)
|
||||
.where()
|
||||
.eq("description", "goodbye")
|
||||
.eq("status", EBasicEncryptClient.Status.TWO)
|
||||
.eq("dob", today)
|
||||
.findOne();
|
||||
|
||||
assertThat(found).isNotNull();
|
||||
assertThat(found.getDescription()).isEqualTo("goodbye");
|
||||
|
||||
DB.delete(found);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package org.tests.basic.event;
|
||||
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.TWithPreInsert;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class TestPreInsertValidation extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
TWithPreInsert e = new TWithPreInsert();
|
||||
e.setTitle("Mister");
|
||||
// the perInsert should populate the
|
||||
// name with should not be null
|
||||
DB.save(e);
|
||||
|
||||
// the save worked and name set in preInsert
|
||||
assertNotNull(e.getId());
|
||||
assertNotNull(e.getName());
|
||||
|
||||
TWithPreInsert e1 = DB.find(TWithPreInsert.class, e.getId());
|
||||
|
||||
e1.setTitle("Missus");
|
||||
DB.save(e1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStatelessUpdate() {
|
||||
|
||||
TWithPreInsert e = new TWithPreInsert();
|
||||
e.setName("BeanForUpdateTest");
|
||||
DB.save(e);
|
||||
|
||||
TWithPreInsert bean2 = new TWithPreInsert();
|
||||
bean2.setId(e.getId());
|
||||
bean2.setName("stateless-update-name");
|
||||
bean2.setTitle(null);
|
||||
|
||||
DB.update(bean2);
|
||||
|
||||
// title set on preUpdate
|
||||
assertNotNull(bean2.getTitle());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package org.tests.basic.event;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import io.ebean.QueryType;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.tests.model.basic.TOne;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestQueryAdapter extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testSimple() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
TOne o = new TOne();
|
||||
o.setName("something");
|
||||
|
||||
DB.save(o);
|
||||
|
||||
Query<TOne> queryFindId = DB.find(TOne.class).setId(o.getId());
|
||||
|
||||
TOne one = queryFindId.findOne();
|
||||
|
||||
assertThat(one.getId()).isEqualTo(o.getId());
|
||||
assertThat(sqlOf(queryFindId)).contains(" 1=1");
|
||||
|
||||
Query<TOne> notUsedQuery = DB.find(TOne.class);
|
||||
assertThat(notUsedQuery.getQueryType()).isEqualTo(QueryType.FIND);
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
DB.update(TOne.class)
|
||||
.set("name", "mod")
|
||||
.where().idEq(o.getId())
|
||||
.update();
|
||||
|
||||
List<String> sql = LoggedSqlCollector.current();
|
||||
assertSql(sql.get(0)).contains(" 2=2");
|
||||
|
||||
DB.find(TOne.class)
|
||||
.where().idEq(o.getId())
|
||||
.delete();
|
||||
|
||||
sql = LoggedSqlCollector.stop();
|
||||
assertSql(sql.get(0)).contains(" 3=3");
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package org.tests.basic.join;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.TransactionalTestCase;
|
||||
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.Order.Status;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class TestSecondaryJoin extends TransactionalTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Order> list = DB.find(Order.class)
|
||||
// .select("*")
|
||||
// .join("customer")
|
||||
.findList();
|
||||
|
||||
Order o0 = list.get(0);
|
||||
o0.setCustomerName("Banan");
|
||||
o0.setStatus(Status.APPROVED);
|
||||
|
||||
DB.save(o0);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package org.tests.basic.lob;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import io.ebean.Query;
|
||||
import org.tests.model.basic.EBasicClobFetchEager;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class TestBasicClobFetchEager extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
EBasicClobFetchEager entity = new EBasicClobFetchEager();
|
||||
entity.setName("test");
|
||||
entity.setDescription("initialClobValue");
|
||||
Database server = DB.getDefault();
|
||||
server.save(entity);
|
||||
|
||||
|
||||
String expectedSql = "select t0.id, t0.name, t0.title, t0.description, t0.last_update from ebasic_clob_fetch_eager t0 where t0.id = ?";
|
||||
|
||||
// Clob included in fetch as FetchType.EAGER set by annotation
|
||||
Query<EBasicClobFetchEager> defaultQuery = DB.find(EBasicClobFetchEager.class).setId(entity.getId());
|
||||
defaultQuery.findOne();
|
||||
String sql = trimSql(defaultQuery.getGeneratedSql(), 6);
|
||||
|
||||
assertThat(sql).contains(expectedSql);
|
||||
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
// Same as previous query - clob included by default based on annotation
|
||||
DB.find(EBasicClobFetchEager.class, entity.getId());
|
||||
|
||||
// Assert query same as previous ...
|
||||
List<String> loggedSql = LoggedSqlCollector.stop();
|
||||
assertEquals(1, loggedSql.size());
|
||||
assertThat(trimSql(loggedSql.get(0), 6)).contains(expectedSql);
|
||||
|
||||
|
||||
// Explicitly select * including Clob
|
||||
Query<EBasicClobFetchEager> explicitQuery = DB.find(EBasicClobFetchEager.class).setId(entity.getId()).select("*");
|
||||
|
||||
explicitQuery.findOne();
|
||||
sql = sqlOf(explicitQuery, 6);
|
||||
|
||||
assertThat(sql).contains(expectedSql);
|
||||
|
||||
// Update description to test refresh
|
||||
|
||||
EBasicClobFetchEager updateBean = new EBasicClobFetchEager();
|
||||
updateBean.setId(entity.getId());
|
||||
updateBean.setDescription("modified");
|
||||
DB.update(updateBean);
|
||||
|
||||
|
||||
// Test refresh function
|
||||
|
||||
assertEquals("initialClobValue", entity.getDescription());
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
// Refresh query includes all properties
|
||||
server.refresh(entity);
|
||||
|
||||
// Assert all properties fetched in refresh
|
||||
loggedSql = LoggedSqlCollector.stop();
|
||||
assertEquals(1, loggedSql.size());
|
||||
assertThat(trimSql(loggedSql.get(0), 6)).contains(expectedSql);
|
||||
assertEquals("modified", entity.getDescription());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package org.tests.basic.lob;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import org.tests.model.basic.EBasicClobNoVer;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestBasicClobNoVer extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
EBasicClobNoVer entity = new EBasicClobNoVer();
|
||||
entity.setName("test");
|
||||
entity.setDescription("initialClobValue");
|
||||
DB.save(entity);
|
||||
|
||||
// Clob by default is Fetch Lazy
|
||||
Query<EBasicClobNoVer> defaultQuery = DB.find(EBasicClobNoVer.class).setId(entity.getId());
|
||||
defaultQuery.findOne();
|
||||
String sql = sqlOf(defaultQuery, 2);
|
||||
|
||||
// default SQL select excludes clob
|
||||
String sqlNoClob = "select t0.id, t0.name from ebasic_clob_no_ver t0 where t0.id = ?";
|
||||
assertThat(sql).contains(sqlNoClob);
|
||||
|
||||
|
||||
// Explicitly select * including Clob
|
||||
Query<EBasicClobNoVer> explicitQuery = DB.find(EBasicClobNoVer.class).setId(entity.getId()).select("*");
|
||||
|
||||
explicitQuery.findOne();
|
||||
sql = sqlOf(explicitQuery, 2);
|
||||
|
||||
// Explicitly include Clob
|
||||
String sqlWithClob = "select t0.id, t0.name, t0.description from ebasic_clob_no_ver t0 where t0.id = ?";
|
||||
assertThat(sql).contains(sqlWithClob);
|
||||
|
||||
// Update description to test refresh
|
||||
|
||||
EBasicClobNoVer updateBean = new EBasicClobNoVer();
|
||||
updateBean.setId(entity.getId());
|
||||
updateBean.setDescription("modified");
|
||||
DB.update(updateBean);
|
||||
|
||||
|
||||
// Test refresh function
|
||||
|
||||
assertThat(entity.getDescription()).isEqualTo("initialClobValue");
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
// Refresh query includes all properties
|
||||
DB.refresh(entity);
|
||||
|
||||
// Assert all properties fetched in refresh
|
||||
List<String> loggedSql = LoggedSqlCollector.stop();
|
||||
assertThat(loggedSql).hasSize(1);
|
||||
assertThat(trimSql(loggedSql.get(0), 2)).contains(sqlWithClob);
|
||||
assertThat(entity.getDescription()).isEqualTo("modified");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package org.tests.basic.lob;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.TBytesOnly;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TestByteOnly extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
byte[] content = new byte[]{1, 1};
|
||||
TBytesOnly e = new TBytesOnly();
|
||||
e.setContent(content);
|
||||
|
||||
DB.save(e);
|
||||
|
||||
byte[] content2 = new byte[]{1, 1};
|
||||
|
||||
TBytesOnly e2 = DB.find(TBytesOnly.class, e.getId());
|
||||
e2.setContent(content2);
|
||||
|
||||
DB.save(e2);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package org.tests.basic.one2one;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Version;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Table(name = "drel_booking")
|
||||
public class Booking {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.SEQUENCE)
|
||||
private Long id;
|
||||
|
||||
@Column(unique = true)
|
||||
Long bookingUid;
|
||||
|
||||
@Version
|
||||
private int version;
|
||||
|
||||
@OneToOne(cascade = CascadeType.ALL)
|
||||
@JoinColumn(name = "agent_invoice")
|
||||
private Invoice agentInvoice;
|
||||
|
||||
@OneToOne(cascade = CascadeType.ALL)
|
||||
@JoinColumn(name = "client_invoice")
|
||||
private Invoice clientInvoice;
|
||||
|
||||
@OneToMany(mappedBy = "booking")
|
||||
private List<Invoice> invoices;
|
||||
|
||||
public Booking(Long bookingUid) {
|
||||
this.bookingUid = bookingUid;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getBookingUid() {
|
||||
return bookingUid;
|
||||
}
|
||||
|
||||
public void setBookingUid(Long bookingUid) {
|
||||
this.bookingUid = bookingUid;
|
||||
}
|
||||
|
||||
public int getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(int version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public Invoice getAgentInvoice() {
|
||||
return agentInvoice;
|
||||
}
|
||||
|
||||
public void setAgentInvoice(Invoice agentInvoice) {
|
||||
this.agentInvoice = agentInvoice;
|
||||
}
|
||||
|
||||
public Invoice getClientInvoice() {
|
||||
return clientInvoice;
|
||||
}
|
||||
|
||||
public void setClientInvoice(Invoice clientInvoice) {
|
||||
this.clientInvoice = clientInvoice;
|
||||
}
|
||||
|
||||
public List<Invoice> getInvoices() {
|
||||
return invoices;
|
||||
}
|
||||
|
||||
public void setInvoices(List<Invoice> invoices) {
|
||||
this.invoices = invoices;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package org.tests.basic.one2one;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Version;
|
||||
|
||||
@Entity
|
||||
@Table(name = "drel_invoice")
|
||||
public class Invoice {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.SEQUENCE)
|
||||
private Long id;
|
||||
|
||||
@Version
|
||||
private int version;
|
||||
|
||||
@ManyToOne(cascade = CascadeType.ALL)
|
||||
@JoinColumn(name = "booking")//_xid", referencedColumnName = "booking_uid")
|
||||
private Booking booking;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(int version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public Booking getBooking() {
|
||||
return booking;
|
||||
}
|
||||
|
||||
public void setBooking(Booking booking) {
|
||||
this.booking = booking;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package org.tests.basic.one2one;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
|
||||
public class TestOne2OneBookingInvoice extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
Booking b = new Booking(3000L);
|
||||
Invoice ai = new Invoice();
|
||||
Invoice ci = new Invoice();
|
||||
|
||||
ai.setBooking(b);
|
||||
ci.setBooking(b);
|
||||
|
||||
b.setAgentInvoice(ai);
|
||||
b.setClientInvoice(ci);
|
||||
|
||||
DB.save(b);
|
||||
|
||||
Invoice invoice = DB.find(Invoice.class, ai.getId());
|
||||
assertEquals(b.getId(), invoice.getBooking().getId());
|
||||
|
||||
Booking b1 = DB.find(Booking.class, b.getId());
|
||||
|
||||
Invoice ai1 = b1.getAgentInvoice();
|
||||
assertNotNull(ai1);
|
||||
|
||||
Booking b2 = ai1.getBooking();
|
||||
assertNotNull(b2);
|
||||
assertEquals(b1.getId(), b2.getId());
|
||||
assertSame(b1, b2);
|
||||
|
||||
Invoice ci1 = b1.getClientInvoice();
|
||||
Booking b3 = ci1.getBooking();
|
||||
assertNotNull(b3);
|
||||
assertEquals(b1.getId(), b2.getId());
|
||||
assertSame(b1, b2);
|
||||
|
||||
// cleanup
|
||||
ai.setBooking(null);
|
||||
ci.setBooking(null);
|
||||
DB.save(ai);
|
||||
DB.save(ci);
|
||||
DB.delete(b);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package org.tests.basic.one2one;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TestOneToOneWheelTire extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
Wheel w = new Wheel();
|
||||
Tire t = new Tire();
|
||||
t.setWheel(w);
|
||||
w.setTire(t);
|
||||
|
||||
DB.save(t);
|
||||
|
||||
DB.delete(t);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package org.tests.basic.one2one;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Version;
|
||||
|
||||
@Entity
|
||||
@Table(name = "tire")
|
||||
public class Tire {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.SEQUENCE)
|
||||
private Long id;
|
||||
@Version
|
||||
private int version;
|
||||
@OneToOne(cascade = CascadeType.ALL)
|
||||
@JoinColumn(name = "wheel")
|
||||
private Wheel wheel;
|
||||
|
||||
public Tire() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(int version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public Wheel getWheel() {
|
||||
return wheel;
|
||||
}
|
||||
|
||||
public void setWheel(Wheel wheel) {
|
||||
this.wheel = wheel;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package org.tests.basic.one2one;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Version;
|
||||
|
||||
@Entity
|
||||
@Table(name = "wheel")
|
||||
public class Wheel {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.SEQUENCE)
|
||||
private Long id;
|
||||
|
||||
@Version
|
||||
private int version;
|
||||
|
||||
@OneToOne(mappedBy = "wheel", cascade = CascadeType.PERSIST)
|
||||
private Tire tire;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Wheel() {
|
||||
super();
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(int version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public Tire getTire() {
|
||||
return tire;
|
||||
}
|
||||
|
||||
public void setTire(Tire tire) {
|
||||
this.tire = tire;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package org.tests.basic.type;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Transient;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Entity
|
||||
public class BSimpleWithGen {
|
||||
|
||||
@Id
|
||||
private Integer id;
|
||||
|
||||
private String name;
|
||||
|
||||
@Transient
|
||||
private Map<String, List<String>> someMap;
|
||||
|
||||
public BSimpleWithGen(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Map<String, List<String>> getSomeMap() {
|
||||
return someMap;
|
||||
}
|
||||
|
||||
public void setSomeMap(Map<String, List<String>> someMap) {
|
||||
this.someMap = someMap;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package org.tests.basic.type;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.SqlQuery;
|
||||
import io.ebean.SqlRow;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.EBasic;
|
||||
import org.tests.model.basic.EBasic.Status;
|
||||
import org.tests.model.basic.EBasicEnumId;
|
||||
import org.tests.model.basic.EBasicEnumInt;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class TestEnumValueAnnotation extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
EBasic b = new EBasic();
|
||||
b.setName("Banana");
|
||||
b.setStatus(Status.NEW);
|
||||
|
||||
DB.save(b);
|
||||
|
||||
SqlRow sqlRow = DB.sqlQuery("select * from e_basic where id = :id")
|
||||
.setParameter("id", b.getId())
|
||||
.findOne();
|
||||
|
||||
String strStatus = sqlRow.getString("status");
|
||||
assertEquals("N", strStatus);
|
||||
|
||||
EBasic b2 = new EBasic();
|
||||
b2.setName("Apple");
|
||||
b2.setStatus(Status.NEW);
|
||||
|
||||
DB.save(b2);
|
||||
|
||||
EBasic b3 = DB.find(EBasic.class, b2.getId());
|
||||
b3.setName("Orange");
|
||||
|
||||
DB.save(b3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAsId() {
|
||||
EBasicEnumId b = new EBasicEnumId();
|
||||
b.setName("Banana");
|
||||
b.setStatus(EBasicEnumId.Status.NEW);
|
||||
|
||||
DB.save(b);
|
||||
|
||||
SqlQuery q = DB.sqlQuery("select * from e_basic_enum_id where status = :status");
|
||||
q.setParameter("status", b.getStatus());
|
||||
|
||||
SqlRow sqlRow = q.findOne();
|
||||
String strStatus = sqlRow.getString("status");
|
||||
|
||||
assertEquals("N", strStatus);
|
||||
|
||||
try {
|
||||
b = DB.find(EBasicEnumId.class, b.getStatus());
|
||||
} catch (java.lang.IllegalArgumentException iae) {
|
||||
fail("The use of an enum as id should work : " + iae.getLocalizedMessage());
|
||||
}
|
||||
|
||||
assertEquals(EBasicEnumId.Status.NEW, b.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDbEnumValueInt() {
|
||||
|
||||
EBasicEnumInt b = new EBasicEnumInt();
|
||||
b.setName("Banana");
|
||||
b.setStatus(EBasicEnumInt.Status.NEW);
|
||||
|
||||
DB.save(b);
|
||||
|
||||
SqlQuery q = DB.sqlQuery("select * from e_basic_eni where id = :id");
|
||||
q.setParameter("id", b.getId());
|
||||
|
||||
Optional<SqlRow> sqlRow = q.findOneOrEmpty();
|
||||
sqlRow.ifPresent(sqlRow1 -> {
|
||||
Integer intStatus = sqlRow1.getInteger("status");
|
||||
assertEquals(Integer.valueOf(1), intStatus);
|
||||
});
|
||||
|
||||
|
||||
EBasicEnumInt b2 = DB.find(EBasicEnumInt.class)
|
||||
.where().eq("id", b.getId())
|
||||
.eq("status", EBasicEnumInt.Status.NEW)
|
||||
.findOne();
|
||||
|
||||
assertNotNull(b2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package org.tests.basic.type;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.ESomeType;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Currency;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TestExtraScalarTypes extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
Locale locale = Locale.getDefault();
|
||||
Currency currency = Currency.getInstance(locale);
|
||||
TimeZone tz = TimeZone.getDefault();
|
||||
|
||||
ESomeType e = new ESomeType();
|
||||
e.setLocale(locale);
|
||||
e.setTimeZone(tz);
|
||||
e.setCurrency(currency);
|
||||
|
||||
DB.save(e);
|
||||
|
||||
ESomeType e2 = DB.find(ESomeType.class).setAutoTune(false).setId(e.getId()).findOne();
|
||||
|
||||
assertNotNull(e2.getCurrency());
|
||||
assertNotNull(e2.getLocale());
|
||||
assertNotNull(e2.getTimeZone());
|
||||
|
||||
List<ESomeType> list = DB.find(ESomeType.class)
|
||||
.setAutoTune(false).where()
|
||||
.eq("locale", locale)
|
||||
.eq("timeZone", tz.getID())
|
||||
.eq("currency", currency)
|
||||
.findList();
|
||||
|
||||
assertTrue(!list.isEmpty());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
package org.tests.basic.type;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.TransactionalTestCase;
|
||||
import io.ebean.types.Cidr;
|
||||
import io.ebean.types.Inet;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.EWithInetAddr;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
public class TestInetAddressType extends TransactionalTestCase {
|
||||
|
||||
@Test
|
||||
public void testIp4() throws UnknownHostException {
|
||||
|
||||
insertUpdateDeleteFind("120.12.12.56", "120.12.12.56", "192.168.100.128/25", "192.168.100.128/25");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIp6() throws UnknownHostException {
|
||||
|
||||
if (isPostgres()) {
|
||||
insertUpdateDeleteFind("2001:db8:85a3:0:0:8a2e:370:7334", "2001:db8:85a3::8a2e:370:7334", "2001:4f8:3:ba::/64", "2001:4f8:3:ba::/64");
|
||||
} else {
|
||||
insertUpdateDeleteFind("2001:db8:85a3:0:0:8a2e:370:7334", "2001:db8:85a3:0:0:8a2e:370:7334", "192.168.100.128/25", "192.168.100.128/25");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_inet_queryIn() {
|
||||
|
||||
List<Inet> addrs = Inet.listOf("120.12.12.56", "120.12.12.57");
|
||||
|
||||
DB.find(EWithInetAddr.class)
|
||||
.where()
|
||||
.in("inet2", addrs)
|
||||
.findList();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_inetAdress_queryIn() throws UnknownHostException {
|
||||
|
||||
List<InetAddress> addrs = new ArrayList<>();
|
||||
addrs.add(InetAddress.getByName("120.12.12.56"));
|
||||
addrs.add(InetAddress.getByName("120.12.12.57"));
|
||||
|
||||
DB.find(EWithInetAddr.class)
|
||||
.where()
|
||||
.in("inetAddress", addrs)
|
||||
.findList();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_inet_queryEq() {
|
||||
|
||||
DB.find(EWithInetAddr.class)
|
||||
.where()
|
||||
.eq("inet2", new Inet("120.12.12.58"))
|
||||
.findList();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_inet4address_queryEq() throws UnknownHostException {
|
||||
|
||||
DB.find(EWithInetAddr.class)
|
||||
.where()
|
||||
.eq("inetAddress", InetAddress.getByName("120.12.12.58"))
|
||||
.findList();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_inet6address_queryEq() throws UnknownHostException {
|
||||
|
||||
DB.find(EWithInetAddr.class)
|
||||
.where()
|
||||
.eq("inetAddress", InetAddress.getByName("2001:db8:85a3:0:0:8a2e:370:7334"))
|
||||
.findList();
|
||||
}
|
||||
|
||||
private void insertUpdateDeleteFind(String ipAddress, String expected, String cidr, String cidrExpected) throws UnknownHostException {
|
||||
|
||||
EWithInetAddr bean1 = new EWithInetAddr();
|
||||
bean1.setName("jim");
|
||||
|
||||
InetAddress address1 = InetAddress.getByName(ipAddress);
|
||||
bean1.setInetAddress(address1);
|
||||
bean1.setInet2(new Inet(ipAddress));
|
||||
bean1.setCidr(new Cidr(cidr));
|
||||
|
||||
|
||||
DB.save(bean1);
|
||||
|
||||
EWithInetAddr bean2 = DB.find(EWithInetAddr.class, bean1.getId());
|
||||
InetAddress address2 = bean2.getInetAddress();
|
||||
assertNotNull(address2.getHostAddress());
|
||||
assertThat(address1.getHostAddress()).isEqualTo(address2.getHostAddress());
|
||||
assertThat(bean2.getInet2().getAddress()).isEqualTo(expected);
|
||||
assertThat(bean2.getCidr().getAddress()).isEqualTo(cidrExpected);
|
||||
|
||||
bean2.setName("modJim");
|
||||
bean2.setInetAddress(InetAddress.getByName("120.12.20.80"));
|
||||
bean1.setInet2(new Inet("120.12.20.80"));
|
||||
bean1.setCidr(new Cidr("120.12.20.80"));
|
||||
DB.save(bean2);
|
||||
DB.delete(bean2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIp6_ranges() {
|
||||
|
||||
insertFindDeleteRange("2001:4f8:3:ba::/64", "2001:4f8:3:ba::/64");
|
||||
insertFindDeleteRange("2001:4f8:3:ba:2e0:81ff:fe22:d1f1/64", "2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128");
|
||||
}
|
||||
|
||||
private void insertFindDeleteRange(String ipAddressRange, String cidr) {
|
||||
|
||||
EWithInetAddr bean1 = new EWithInetAddr();
|
||||
bean1.setName("withRange");
|
||||
bean1.setInet2(new Inet(ipAddressRange));
|
||||
bean1.setCidr(new Cidr(cidr));
|
||||
|
||||
DB.save(bean1);
|
||||
|
||||
EWithInetAddr bean2 = DB.find(EWithInetAddr.class, bean1.getId());
|
||||
assertNotNull(bean2.getInet2());
|
||||
assertThat(bean2.getInet2().getAddress()).isEqualTo(ipAddressRange);
|
||||
assertThat(bean2.getCidr().getAddress()).isEqualTo(cidr);
|
||||
|
||||
DB.delete(bean2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void use_null() {
|
||||
|
||||
EWithInetAddr bean1 = new EWithInetAddr();
|
||||
bean1.setName("jim");
|
||||
|
||||
DB.save(bean1);
|
||||
|
||||
EWithInetAddr bean2 = DB.find(EWithInetAddr.class, bean1.getId());
|
||||
assertNull(bean2.getInetAddress());
|
||||
assertNull(bean2.getInet2());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package org.tests.basic.type;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.TOne;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TestQueryBooleanProperty extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
// when run in MySql is test for BUG 323
|
||||
DB.find(TOne.class).where().eq("active", true).findList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package org.tests.basic.type;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.SqlQuery;
|
||||
import io.ebean.SqlRow;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.TUuidEntity;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class TestSqlRowUUID extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
TUuidEntity e = new TUuidEntity();
|
||||
e.setName("blah");
|
||||
|
||||
DB.save(e);
|
||||
|
||||
SqlQuery q = DB.sqlQuery("select * from tuuid_entity where id = :id");
|
||||
q.setParameter("id", e.getId());
|
||||
SqlRow sqlRow = q.findOne();
|
||||
|
||||
UUID id = sqlRow.getUUID("id");
|
||||
|
||||
assertNotNull(id);
|
||||
|
||||
Boolean b = sqlRow.getBoolean("name");
|
||||
assertFalse(b);
|
||||
|
||||
|
||||
SqlQuery q2 = DB.sqlQuery("select id from tuuid_entity where id = :id");
|
||||
q2.setParameter("id", e.getId());
|
||||
UUID value = q2.mapToScalar(UUID.class).findOne();
|
||||
|
||||
assertThat(value).isEqualTo(e.getId());
|
||||
|
||||
if (isPostgres()) {
|
||||
usingPostrgesAnyWithPositionedParameter_needsExplicitCast(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void usingPostrgesAnyWithPositionedParameter_needsExplicitCast(TUuidEntity e) {
|
||||
|
||||
List<UUID> ids = Arrays.asList(e.getId(), UUID.randomUUID());
|
||||
|
||||
List<SqlRow> result = DB.sqlQuery("select id from tuuid_entity where id = any(?::uuid[])")
|
||||
.setParameter(1, ids)
|
||||
.findList();
|
||||
|
||||
assertThat(result).hasSize(1);
|
||||
|
||||
List<TUuidEntity> list = DB.find(TUuidEntity.class)
|
||||
.where().rawOrEmpty("id = any(?::uuid[])", ids)
|
||||
.findList();
|
||||
|
||||
assertThat(list).hasSize(1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package org.tests.basic.type;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestTransientMap extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testMe() {
|
||||
|
||||
Map<String, List<String>> map = new HashMap<>();
|
||||
map.put("foo", new ArrayList<>());
|
||||
|
||||
BSimpleWithGen b = new BSimpleWithGen("blah");
|
||||
b.setSomeMap(map);
|
||||
DB.save(b);
|
||||
|
||||
final BSimpleWithGen found = DB.find(BSimpleWithGen.class, b.getId());
|
||||
assertThat(found.getName()).isEqualTo("blah");
|
||||
assertThat(found.getSomeMap()).isNull();
|
||||
|
||||
DB.delete(b);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user