mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-12 22:41:35 +00:00
Refactor move tests from ebean-core to ebean-test
This commit is contained in:
1 parent
0b00a14fd3
commit
eab56d323b
1650 files changed
+185
-147
No files matched your search
+123
@@ -0,0 +1,123 @@
|
||||
package io.ebeaninternal.server.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.bean.EntityBean;
|
||||
import io.ebean.bean.PersistenceContext;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import io.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import io.ebeaninternal.server.deploy.BeanPropertyAssocOne;
|
||||
import io.ebeaninternal.server.transaction.DefaultPersistenceContext;
|
||||
import org.tests.model.basic.Address;
|
||||
import org.tests.model.basic.Country;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Customer.Status;
|
||||
import org.tests.model.embedded.EAddress;
|
||||
import org.tests.model.embedded.EPerson;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class CacheBeanDataTest extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void extract_load_on_customer() {
|
||||
|
||||
SpiEbeanServer server = spiEbeanServer();
|
||||
BeanDescriptor<Customer> desc = server.descriptor(Customer.class);
|
||||
|
||||
Customer c = new Customer();
|
||||
c.setId(98989);
|
||||
c.setName("Rob");
|
||||
c.setCretime(new Timestamp(System.currentTimeMillis()));
|
||||
c.setUpdtime(new Timestamp(System.currentTimeMillis()));
|
||||
c.setStatus(Status.ACTIVE);
|
||||
c.setSmallnote("somenote");
|
||||
|
||||
Address billingAddress = new Address();
|
||||
billingAddress.setId(12);
|
||||
billingAddress.setCity("Auckland");
|
||||
billingAddress.setCountry(server.reference(Country.class, "NZ"));
|
||||
billingAddress.setLine1("92 Someplace Else");
|
||||
c.setBillingAddress(billingAddress);
|
||||
|
||||
((EntityBean) c)._ebean_getIntercept().setLoaded();
|
||||
|
||||
CachedBeanData cacheData = CachedBeanDataFromBean.extract(desc, (EntityBean) c);
|
||||
|
||||
|
||||
assertNotNull(cacheData);
|
||||
|
||||
Customer newCustomer = new Customer();
|
||||
newCustomer.setId(c.getId());
|
||||
CachedBeanDataToBean.load(desc, (EntityBean) newCustomer, cacheData, new DefaultPersistenceContext());
|
||||
|
||||
assertEquals(c.getId(), newCustomer.getId());
|
||||
assertEquals(c.getName(), newCustomer.getName());
|
||||
assertEquals(c.getStatus(), newCustomer.getStatus());
|
||||
assertEquals(c.getSmallnote(), newCustomer.getSmallnote());
|
||||
assertEquals(c.getCretime(), newCustomer.getCretime());
|
||||
assertEquals(c.getUpdtime(), newCustomer.getUpdtime());
|
||||
assertEquals(c.getBillingAddress().getId(), newCustomer.getBillingAddress().getId());
|
||||
|
||||
assertNotNull(newCustomer.getId());
|
||||
assertNotNull(newCustomer.getName());
|
||||
assertNotNull(newCustomer.getStatus());
|
||||
assertNotNull(newCustomer.getSmallnote());
|
||||
assertNotNull(newCustomer.getCretime());
|
||||
assertNotNull(newCustomer.getUpdtime());
|
||||
assertNotNull(newCustomer.getBillingAddress());
|
||||
assertNotNull(newCustomer.getBillingAddress().getId());
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void extract_load_withEmbeddedBean() {
|
||||
|
||||
SpiEbeanServer server = spiEbeanServer();
|
||||
BeanDescriptor<EPerson> desc = server.descriptor(EPerson.class);
|
||||
BeanPropertyAssocOne<?> addressBeanProperty = (BeanPropertyAssocOne<?>) desc.beanProperty("address");
|
||||
|
||||
EAddress address = new EAddress();
|
||||
address.setStreet("92 Someplace Else");
|
||||
address.setSuburb("Sandringham");
|
||||
address.setCity("Auckland");
|
||||
|
||||
EPerson person = new EPerson();
|
||||
person.setId(98989L);
|
||||
person.setName("Rob");
|
||||
person.setAddress(address);
|
||||
|
||||
CachedBeanData addressCacheData = (CachedBeanData) addressBeanProperty.getCacheDataValue((EntityBean) person);
|
||||
|
||||
PersistenceContext context = new DefaultPersistenceContext();
|
||||
|
||||
EPerson newPersonCheck = new EPerson();
|
||||
newPersonCheck.setId(98989L);
|
||||
addressBeanProperty.setCacheDataValue((EntityBean) newPersonCheck, addressCacheData, context);
|
||||
|
||||
EAddress newAddress = newPersonCheck.getAddress();
|
||||
assertEquals(address.getStreet(), newAddress.getStreet());
|
||||
assertEquals(address.getCity(), newAddress.getCity());
|
||||
assertEquals(address.getSuburb(), newAddress.getSuburb());
|
||||
|
||||
|
||||
CachedBeanData cacheData = desc.cacheEmbeddedBeanExtract((EntityBean) person);
|
||||
|
||||
assertNotNull(cacheData);
|
||||
|
||||
EPerson newPerson = (EPerson) desc.cacheEmbeddedBeanLoad(cacheData, context);
|
||||
|
||||
assertNotNull(newPerson.getId());
|
||||
assertNotNull(newPerson.getName());
|
||||
assertNotNull(newPerson.getAddress());
|
||||
|
||||
assertEquals(person.getId(), newPerson.getId());
|
||||
assertEquals(person.getName(), newPerson.getName());
|
||||
assertEquals(person.getAddress().getStreet(), newPerson.getAddress().getStreet());
|
||||
assertEquals(person.getAddress().getCity(), newPerson.getAddress().getCity());
|
||||
}
|
||||
}
|
||||
+127
@@ -0,0 +1,127 @@
|
||||
package io.ebeaninternal.server.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.bean.EntityBean;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import io.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import io.ebeaninternal.server.transaction.DefaultPersistenceContext;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Address;
|
||||
import org.tests.model.basic.Car;
|
||||
import org.tests.model.basic.Contact;
|
||||
import org.tests.model.basic.Customer;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class CachedBeanDataFromBeanTest extends BaseTestCase {
|
||||
|
||||
private final SpiEbeanServer server = spiEbeanServer();
|
||||
|
||||
@Test
|
||||
public void extract() {
|
||||
|
||||
BeanDescriptor<Customer> desc = server.descriptor(Customer.class);
|
||||
|
||||
Date largeDate = new Date(9223372036825200000L);
|
||||
|
||||
Customer customer = new Customer();
|
||||
customer.setId(42);
|
||||
customer.setName("Rob");
|
||||
customer.setAnniversary(largeDate);
|
||||
|
||||
Address billingAddress = new Address();
|
||||
billingAddress.setId(12);
|
||||
billingAddress.setCity("SomePlace");
|
||||
|
||||
customer.setBillingAddress(billingAddress);
|
||||
|
||||
CachedBeanData cacheData = CachedBeanDataFromBean.extract(desc, (EntityBean) customer);
|
||||
|
||||
assertEquals(cacheData.getData("id"), "42");
|
||||
assertEquals(cacheData.getData("name"), "Rob");
|
||||
assertEquals(cacheData.getData("billingAddress"), "12");
|
||||
assertEquals(cacheData.getData("anniversary"), "9223372036825200000");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void inheritance() {
|
||||
|
||||
Car car = new Car();
|
||||
car.setId(42);
|
||||
car.setDriver("Jimmy");
|
||||
car.setNotes("some notes");
|
||||
|
||||
BeanDescriptor<Car> carDesc = server.descriptor(Car.class);
|
||||
CachedBeanData cacheData = CachedBeanDataFromBean.extract(carDesc, (EntityBean) car);
|
||||
|
||||
Car newCar = new Car();
|
||||
EntityBean entityBean = (EntityBean) newCar;
|
||||
CachedBeanDataToBean.load(carDesc, entityBean, cacheData, new DefaultPersistenceContext());
|
||||
|
||||
assertEquals(newCar.getId(), car.getId());
|
||||
assertEquals(newCar.getDriver(), car.getDriver());
|
||||
assertEquals(newCar.getNotes(), car.getNotes());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void dirtyScalar_expect_originalValueUsed() {
|
||||
|
||||
Contact contact = new Contact();
|
||||
contact.setId(42);
|
||||
contact.setLastName("Bygrave");
|
||||
contact.setFirstName("Foo");
|
||||
contact.setEmail("rob@email.com");
|
||||
|
||||
EntityBean entityBean = (EntityBean)contact;
|
||||
entityBean._ebean_getIntercept().setLoaded();
|
||||
|
||||
// mutate, dirty
|
||||
contact.setLastName("Banana");
|
||||
|
||||
final BeanDescriptor<Contact> desc = getBeanDescriptor(Contact.class);
|
||||
CachedBeanData cacheData = CachedBeanDataFromBean.extract(desc, entityBean);
|
||||
|
||||
final Map<String, Object> data = cacheData.getData();
|
||||
assertThat(data.get("id")).isEqualTo("42");
|
||||
assertThat(data.get("lastName")).isEqualTo("Bygrave"); // ORIGINAL VALUE
|
||||
assertThat(data.get("firstName")).isEqualTo(contact.getFirstName());
|
||||
assertThat(data.get("email")).isEqualTo(contact.getEmail());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dirtyManyToOne_expect_originalValueUsed() {
|
||||
|
||||
Customer customer = new Customer();
|
||||
customer.setId(99);
|
||||
|
||||
Contact contact = new Contact();
|
||||
contact.setFirstName("Foo");
|
||||
contact.setLastName("Bygrave");
|
||||
contact.setEmail("rob@email.com");
|
||||
contact.setCustomer(customer);
|
||||
|
||||
EntityBean entityBean = (EntityBean)contact;
|
||||
entityBean._ebean_getIntercept().setLoaded();
|
||||
|
||||
// mutate, dirty
|
||||
Customer customer2 = new Customer();
|
||||
customer2.setId(108);
|
||||
contact.setCustomer(customer2);
|
||||
contact.setLastName("Banana");
|
||||
|
||||
final BeanDescriptor<Contact> desc = getBeanDescriptor(Contact.class);
|
||||
CachedBeanData cacheData = CachedBeanDataFromBean.extract(desc, entityBean);
|
||||
|
||||
final Map<String, Object> data = cacheData.getData();
|
||||
assertThat(data.get("lastName")).isEqualTo("Bygrave"); // Original value
|
||||
assertThat(data.get("customer")).isEqualTo("99"); // Original value
|
||||
assertThat(data.get("firstName")).isEqualTo(contact.getFirstName());
|
||||
assertThat(data.get("email")).isEqualTo(contact.getEmail());
|
||||
}
|
||||
}
|
||||
+138
@@ -0,0 +1,138 @@
|
||||
package io.ebeaninternal.server.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.bean.EntityBean;
|
||||
import io.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import io.ebeaninternal.server.transaction.DefaultPersistenceContext;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.tests.model.basic.TBytesOnly;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
|
||||
public class CachedBeanDataSerializeTest extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void write() throws IOException, ClassNotFoundException {
|
||||
|
||||
Map<String, Object> map = new LinkedHashMap<>();
|
||||
map.put("name", "rob");
|
||||
map.put("some", "thing");
|
||||
map.put("whenCreated", "" + System.currentTimeMillis());
|
||||
|
||||
long version = System.currentTimeMillis();
|
||||
CachedBeanData write = new CachedBeanData(null, "C", map, version);
|
||||
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(os);
|
||||
|
||||
write.writeExternal(oos);
|
||||
oos.flush();
|
||||
oos.close();
|
||||
|
||||
byte[] bytes = os.toByteArray();
|
||||
|
||||
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
|
||||
ObjectInputStream ois = new ObjectInputStream(is);
|
||||
|
||||
CachedBeanData read = new CachedBeanData();
|
||||
read.readExternal(ois);
|
||||
|
||||
assertEquals(read.getVersion(), write.getVersion());
|
||||
assertEquals(read.getWhenCreated(), write.getWhenCreated());
|
||||
assertEquals(read.getDiscValue(), write.getDiscValue());
|
||||
assertEquals(read.getData(), write.getData());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void fullBean() throws IOException, ClassNotFoundException {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Customer> customers = DB.find(Customer.class)
|
||||
.order().asc("id")
|
||||
.setMaxRows(1).findList();
|
||||
|
||||
Customer customer = customers.get(0);
|
||||
|
||||
BeanDescriptor<Customer> desc = getBeanDescriptor(Customer.class);
|
||||
CachedBeanData extract = CachedBeanDataFromBean.extract(desc, (EntityBean) customer);
|
||||
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
writeToStream(extract, os);
|
||||
|
||||
byte[] bytes = os.toByteArray();
|
||||
|
||||
CachedBeanData read = readFromStream(bytes);
|
||||
|
||||
assertEquals(read.getData(), extract.getData());
|
||||
|
||||
Customer loadCustomer = new Customer();
|
||||
CachedBeanDataToBean.load(desc, (EntityBean) loadCustomer, read, new DefaultPersistenceContext());
|
||||
|
||||
assertEquals(loadCustomer.getVersion(), customer.getVersion());
|
||||
assertEquals(loadCustomer.getId(), customer.getId());
|
||||
assertEquals(loadCustomer.getName(), customer.getName());
|
||||
assertEquals(loadCustomer.getStatus(), customer.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beanWithByteArray() throws IOException, ClassNotFoundException {
|
||||
|
||||
String stringContent = "ThisIsSome";
|
||||
|
||||
TBytesOnly bean = new TBytesOnly();
|
||||
bean.setId(42);
|
||||
bean.setContent(stringContent.getBytes("UTF-8"));
|
||||
|
||||
BeanDescriptor<TBytesOnly> desc = getBeanDescriptor(TBytesOnly.class);
|
||||
CachedBeanData extract = CachedBeanDataFromBean.extract(desc, (EntityBean) bean);
|
||||
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
writeToStream(extract, os);
|
||||
byte[] bytes = os.toByteArray();
|
||||
|
||||
CachedBeanData read = readFromStream(bytes);
|
||||
byte[] extraContent = (byte[]) extract.getData("content");
|
||||
|
||||
assertEquals(stringContent, new String(extraContent));
|
||||
assertTrue(Arrays.equals(bean.getContent(), extraContent));
|
||||
|
||||
TBytesOnly loadBean = new TBytesOnly();
|
||||
CachedBeanDataToBean.load(desc, (EntityBean) loadBean, read, new DefaultPersistenceContext());
|
||||
|
||||
assertEquals(loadBean.getId(), bean.getId());
|
||||
assertTrue(Arrays.equals(loadBean.getContent(), bean.getContent()));
|
||||
}
|
||||
|
||||
private CachedBeanData readFromStream(byte[] bytes) throws IOException, ClassNotFoundException {
|
||||
|
||||
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
|
||||
ObjectInputStream ois = new ObjectInputStream(is);
|
||||
return (CachedBeanData) ois.readObject();
|
||||
}
|
||||
|
||||
private void writeToStream(CachedBeanData extract, ByteArrayOutputStream os) throws IOException {
|
||||
|
||||
ObjectOutputStream oos = new ObjectOutputStream(os);
|
||||
oos.writeObject(extract);
|
||||
oos.flush();
|
||||
oos.close();
|
||||
}
|
||||
|
||||
}
|
||||
+137
@@ -0,0 +1,137 @@
|
||||
package io.ebeaninternal.server.cache;
|
||||
|
||||
import io.ebean.cache.ServerCacheFactory;
|
||||
import io.ebean.cache.ServerCacheOptions;
|
||||
import io.ebean.cache.ServerCacheType;
|
||||
import io.ebean.config.DatabaseConfig;
|
||||
import io.ebeaninternal.server.transaction.TableModState;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Contact;
|
||||
import org.tests.model.basic.Customer;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
public class DefaultCacheHolderTest {
|
||||
|
||||
private final ThreadLocal<String> tenantId = new ThreadLocal<>();
|
||||
|
||||
private final ServerCacheFactory cacheFactory = new DefaultServerCacheFactory();
|
||||
private final ServerCacheOptions defaultOptions = new ServerCacheOptions();
|
||||
|
||||
private CacheManagerOptions options() {
|
||||
return new CacheManagerOptions(null, new DatabaseConfig(), true)
|
||||
.with(defaultOptions, defaultOptions)
|
||||
.with(cacheFactory, new TableModState());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getCache_normal() {
|
||||
|
||||
DefaultCacheHolder holder = new DefaultCacheHolder(options());
|
||||
|
||||
DefaultServerCache cache = cache(holder, Customer.class);
|
||||
assertThat(cache.getName()).isEqualTo("org.tests.model.basic.Customer_B");
|
||||
assertThat(cache.getShortName()).isEqualTo("Customer_B");
|
||||
|
||||
DefaultServerCache cache1 = cache(holder, Customer.class);
|
||||
assertThat(cache1).isSameAs(cache);
|
||||
|
||||
DefaultServerCache cache2 = cache(holder, Contact.class);
|
||||
assertThat(cache1).isNotSameAs(cache2);
|
||||
assertThat(cache2.getName()).isEqualTo("org.tests.model.basic.Contact_B");
|
||||
assertThat(cache2.getShortName()).isEqualTo("Contact_B");
|
||||
|
||||
}
|
||||
|
||||
private DefaultServerCache cache(DefaultCacheHolder holder, Class<?> type) {
|
||||
return (DefaultServerCache) holder.getCache(type, ServerCacheType.BEAN);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getCache_multiTenant() throws Exception {
|
||||
|
||||
CacheManagerOptions builder = options().with(tenantId::get);
|
||||
|
||||
DefaultCacheHolder holder = new DefaultCacheHolder(builder);
|
||||
|
||||
tenantId.set("ten_1");
|
||||
DefaultServerCache cache = cache(holder, Customer.class);
|
||||
assertThat(cache.getName()).isEqualTo("org.tests.model.basic.Customer_B");
|
||||
assertThat(cache.getShortName()).isEqualTo("Customer_B");
|
||||
|
||||
cache.put("1", "value-for-tenant1");
|
||||
cache.put("2", "an other value-for-tenant1");
|
||||
|
||||
assertThat(cache.size()).isEqualTo(2);
|
||||
|
||||
tenantId.set("ten_2");
|
||||
|
||||
cache.put("1", "value-for-tenant2");
|
||||
cache.put("2", "an other value-for-tenant2");
|
||||
|
||||
assertThat(cache.size()).isEqualTo(4);
|
||||
|
||||
assertThat(cache.get("1")).isEqualTo("value-for-tenant2");
|
||||
assertThat(cache.get("2")).isEqualTo("an other value-for-tenant2");
|
||||
|
||||
|
||||
tenantId.set("ten_1");
|
||||
|
||||
assertThat(cache.get("1")).isEqualTo("value-for-tenant1");
|
||||
assertThat(cache.get("2")).isEqualTo("an other value-for-tenant1");
|
||||
|
||||
Exception[] exInThread = new Exception[1];
|
||||
Thread t = new Thread(() -> {
|
||||
try {
|
||||
assertThat(cache.get("1")).isNull();
|
||||
tenantId.set("ten_2");
|
||||
|
||||
cache.put("1", "value-for-tenant2");
|
||||
cache.put("2", "an other value-for-tenant2");
|
||||
|
||||
tenantId.set(null);
|
||||
|
||||
cache.clear();
|
||||
} catch (Exception e) {
|
||||
exInThread[0] = e;
|
||||
}
|
||||
});
|
||||
|
||||
// do some async work
|
||||
t.start();
|
||||
t.join();
|
||||
if (exInThread[0] != null) {
|
||||
throw exInThread[0];
|
||||
}
|
||||
assertThat(cache.size()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void clearAll() {
|
||||
|
||||
DefaultCacheHolder holder = new DefaultCacheHolder(options());
|
||||
DefaultServerCache cache = cache(holder, Customer.class);
|
||||
cache.put("foo", "foo");
|
||||
assertThat(cache.size()).isEqualTo(1);
|
||||
holder.clearAll();
|
||||
|
||||
assertThat(cache.size()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void clearAll_multiTenant() {
|
||||
|
||||
CacheManagerOptions options = options().with(tenantId::get);
|
||||
|
||||
DefaultCacheHolder holder = new DefaultCacheHolder(options);
|
||||
DefaultServerCache cache = cache(holder, Customer.class);
|
||||
cache.put("foo", "foo");
|
||||
assertThat(cache.size()).isEqualTo(1);
|
||||
|
||||
holder.clearAll();
|
||||
assertThat(cache.size()).isEqualTo(0);
|
||||
}
|
||||
|
||||
}
|
||||
Vendored
+56
@@ -0,0 +1,56 @@
|
||||
package io.ebeaninternal.server.cache;
|
||||
|
||||
import io.ebean.cache.ServerCacheOptions;
|
||||
import io.ebean.cache.ServerCacheType;
|
||||
import io.ebean.config.DatabaseConfig;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Article;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.Product;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class DefaultCacheHolder_getCacheOptions_Test {
|
||||
|
||||
private final DefaultCacheHolder cacheHolder;
|
||||
|
||||
public DefaultCacheHolder_getCacheOptions_Test() {
|
||||
|
||||
ServerCacheOptions defaultOptions = new ServerCacheOptions();
|
||||
defaultOptions.setMaxSize(10000);
|
||||
defaultOptions.setMaxSecsToLive(120);
|
||||
|
||||
CacheManagerOptions builder = new CacheManagerOptions(null, new DatabaseConfig(), true)
|
||||
.with(defaultOptions, defaultOptions);
|
||||
|
||||
this.cacheHolder = new DefaultCacheHolder(builder);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beanOptions_when_set() {
|
||||
|
||||
ServerCacheOptions options = cacheHolder.getCacheOptions(Article.class, ServerCacheType.BEAN);
|
||||
assertEquals(options.getMaxSecsToLive(), 45);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beanOptions_when_notSet_expect_default() {
|
||||
|
||||
ServerCacheOptions options = cacheHolder.getCacheOptions(Order.class, ServerCacheType.BEAN);
|
||||
assertEquals(options.getMaxSecsToLive(), 120);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryOptions_when_set() {
|
||||
|
||||
ServerCacheOptions options = cacheHolder.getCacheOptions(Product.class, ServerCacheType.QUERY);
|
||||
assertEquals(options.getMaxSecsToLive(), 15);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryOptions_when_notSet_expect_default() {
|
||||
|
||||
ServerCacheOptions options = cacheHolder.getCacheOptions(Order.class, ServerCacheType.QUERY);
|
||||
assertEquals(options.getMaxSecsToLive(), 120);
|
||||
}
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package io.ebeaninternal.server.cache;
|
||||
|
||||
import io.ebean.cache.ServerCacheConfig;
|
||||
import io.ebean.cache.ServerCacheOptions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class DefaultServerCacheConfigTest {
|
||||
|
||||
|
||||
private DefaultServerCacheConfig create(int maxSize, int maxIdleSecs, int maxSecsToLive, int trimFreq) {
|
||||
ServerCacheOptions options = new ServerCacheOptions();
|
||||
options.setMaxSize(maxSize);
|
||||
options.setMaxIdleSecs(maxIdleSecs);
|
||||
options.setMaxSecsToLive(maxSecsToLive);
|
||||
options.setTrimFrequency(trimFreq);
|
||||
|
||||
return new DefaultServerCacheConfig(new ServerCacheConfig(null, null, null, options, null, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void trimFreq_halfIdle() {
|
||||
|
||||
assertEquals(create(10000,10,20, 0).determineTrimFrequency(), 4);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void trimFreq_halfIdle_withRounding() {
|
||||
|
||||
assertEquals(create(10000,11,20, 0).determineTrimFrequency(), 4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void trimFreq_halfTTL() {
|
||||
|
||||
assertEquals(create(10000,0,20, 0).determineTrimFrequency(), 9);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void trimFreq_halfTTL_withRounding() {
|
||||
|
||||
assertEquals(create(10000,0,21, 0).determineTrimFrequency(), 9);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void trimFreq_explicit() {
|
||||
|
||||
assertEquals(create(10000,10,20, 42).determineTrimFrequency(), 42);
|
||||
}
|
||||
}
|
||||
+124
@@ -0,0 +1,124 @@
|
||||
package io.ebeaninternal.server.cache;
|
||||
|
||||
import io.ebean.config.ContainerConfig;
|
||||
import io.ebean.config.CurrentTenantProvider;
|
||||
import io.ebean.config.DatabaseConfig;
|
||||
import io.ebeaninternal.server.cluster.ClusterManager;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Contact;
|
||||
import org.tests.model.basic.Customer;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class DefaultServerCacheManagerTest {
|
||||
|
||||
private final ThreadLocal<String> tenantId = new ThreadLocal<>();
|
||||
|
||||
class TdTenPro implements CurrentTenantProvider {
|
||||
|
||||
@Override
|
||||
public Object currentId() {
|
||||
return tenantId.get();
|
||||
}
|
||||
}
|
||||
|
||||
private final ClusterManager clusterManager = new ClusterManager(new ContainerConfig());
|
||||
|
||||
private final DefaultServerCacheManager manager = new DefaultServerCacheManager(new CacheManagerOptions(clusterManager, new DatabaseConfig(), true));
|
||||
|
||||
private final DefaultServerCacheManager multiTenantManager;
|
||||
|
||||
public DefaultServerCacheManagerTest(){
|
||||
CacheManagerOptions builder = new CacheManagerOptions(clusterManager, new DatabaseConfig(), true);
|
||||
builder.with(new TdTenPro());
|
||||
this.multiTenantManager = new DefaultServerCacheManager(builder);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getCache_normal() {
|
||||
|
||||
DefaultServerCache cache = cache(manager, Customer.class);
|
||||
assertThat(cache.getName()).isEqualTo("org.tests.model.basic.Customer_B");
|
||||
assertThat(cache.getShortName()).isEqualTo("Customer_B");
|
||||
|
||||
DefaultServerCache cache1 = cache(manager, Customer.class);
|
||||
assertThat(cache1).isSameAs(cache);
|
||||
|
||||
DefaultServerCache cache2 = cache(manager, Contact.class);
|
||||
assertThat(cache1).isNotSameAs(cache2);
|
||||
assertThat(cache2.getName()).isEqualTo("org.tests.model.basic.Contact_B");
|
||||
assertThat(cache2.getShortName()).isEqualTo("Contact_B");
|
||||
|
||||
|
||||
DefaultServerCache natKeyCache = (DefaultServerCache) manager.getNaturalKeyCache(Customer.class);
|
||||
assertThat(natKeyCache.getName()).isEqualTo("org.tests.model.basic.Customer_N");
|
||||
assertThat(natKeyCache.getShortName()).isEqualTo("Customer_N");
|
||||
|
||||
DefaultServerCache queryCache = (DefaultServerCache) manager.getQueryCache(Customer.class);
|
||||
assertThat(queryCache.getName()).isEqualTo("org.tests.model.basic.Customer_Q");
|
||||
assertThat(queryCache.getShortName()).isEqualTo("Customer_Q");
|
||||
|
||||
DefaultServerCache collCache = (DefaultServerCache) manager.getCollectionIdsCache(Customer.class, "contacts");
|
||||
assertThat(collCache.getName()).isEqualTo("org.tests.model.basic.Customer.contacts_C");
|
||||
assertThat(collCache.getShortName()).isEqualTo("Customer.contacts_C");
|
||||
|
||||
cache.clearCount.reset();
|
||||
collCache.clearCount.reset();
|
||||
queryCache.clearCount.reset();
|
||||
natKeyCache.clearCount.reset();
|
||||
|
||||
manager.clear(Customer.class);
|
||||
|
||||
assertThat(cache.clearCount.get(true)).isEqualTo(1);
|
||||
assertThat(natKeyCache.clearCount.get(true)).isEqualTo(1);
|
||||
assertThat(queryCache.clearCount.get(true)).isEqualTo(1);
|
||||
assertThat(collCache.clearCount.get(true)).isEqualTo(1);
|
||||
}
|
||||
|
||||
private DefaultServerCache cache(DefaultServerCacheManager manager, Class<?> beanType) {
|
||||
return (DefaultServerCache) manager.getBeanCache(beanType);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getCache_multiTenant() {
|
||||
|
||||
tenantId.set("ten1");
|
||||
DefaultServerCache cache = cache(multiTenantManager, Customer.class);
|
||||
assertThat(cache.getName()).isEqualTo("org.tests.model.basic.Customer_B");
|
||||
cache.put("1", "tenant1");
|
||||
|
||||
tenantId.set("ten2");
|
||||
|
||||
assertThat(cache.get("1")).isNull();
|
||||
|
||||
tenantId.set("ten1");
|
||||
|
||||
assertThat(cache.get("1")).isNotNull();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getCache_singleTenant() {
|
||||
|
||||
tenantId.set("ten1");
|
||||
DefaultServerCache cache = cache(manager, Customer.class);
|
||||
assertThat(cache.getName()).isEqualTo("org.tests.model.basic.Customer_B");
|
||||
|
||||
cache.put("1", "tenant1");
|
||||
|
||||
tenantId.set("ten2");
|
||||
|
||||
assertThat(cache.get("1")).isEqualTo("tenant1");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isLocalL2Caching() {
|
||||
|
||||
assertTrue(manager.isLocalL2Caching());
|
||||
assertTrue(multiTenantManager.isLocalL2Caching());
|
||||
}
|
||||
|
||||
}
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
package io.ebeaninternal.server.cache;
|
||||
|
||||
import io.ebean.cache.ServerCacheConfig;
|
||||
import io.ebean.cache.ServerCacheOptions;
|
||||
import io.ebean.cache.ServerCacheType;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class DefaultServerCacheTest {
|
||||
|
||||
private DefaultServerCache createCache() {
|
||||
|
||||
ServerCacheOptions cacheOptions = new ServerCacheOptions();
|
||||
cacheOptions.setMaxSize(100);
|
||||
cacheOptions.setMaxIdleSecs(60);
|
||||
cacheOptions.setMaxSecsToLive(600);
|
||||
cacheOptions.setTrimFrequency(60);
|
||||
|
||||
ServerCacheConfig con = new ServerCacheConfig(ServerCacheType.BEAN, "foo", null, cacheOptions, null, null);
|
||||
DefaultServerCacheConfig config = new DefaultServerCacheConfig(con);
|
||||
return new DefaultServerCache(config);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetHitRatio() {
|
||||
|
||||
DefaultServerCache cache = createCache();
|
||||
assertEquals(0, cache.hitRatio());
|
||||
assertEquals(0, cache.getHitCount());
|
||||
assertEquals(0, cache.getMissCount());
|
||||
cache.put("A", "A");
|
||||
cache.get("A");
|
||||
assertEquals(100, cache.hitRatio());
|
||||
assertEquals(1, cache.getHitCount());
|
||||
assertEquals(0, cache.getMissCount());
|
||||
cache.get("B");
|
||||
assertEquals(50, cache.hitRatio());
|
||||
assertEquals(1, cache.getMissCount());
|
||||
cache.get("B");
|
||||
cache.get("B");
|
||||
assertEquals(25, cache.hitRatio());
|
||||
assertEquals(3, cache.getMissCount());
|
||||
assertEquals(1, cache.getHitCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSize() {
|
||||
|
||||
DefaultServerCache cache = createCache();
|
||||
assertEquals(0, cache.size());
|
||||
cache.put("A", "A");
|
||||
assertEquals(1, cache.size());
|
||||
cache.put("A", "B");
|
||||
assertEquals(1, cache.size());
|
||||
cache.put("B", "B");
|
||||
assertEquals(2, cache.size());
|
||||
|
||||
cache.remove("B");
|
||||
cache.remove("A");
|
||||
assertEquals(0, cache.size());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Vendored
+62
@@ -0,0 +1,62 @@
|
||||
package io.ebeaninternal.server.cache;
|
||||
|
||||
import io.ebean.cache.ServerCacheConfig;
|
||||
import io.ebean.cache.ServerCacheOptions;
|
||||
import io.ebean.cache.ServerCacheStatistics;
|
||||
import io.ebean.cache.ServerCacheType;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
|
||||
public class DefaultServerCache_RunEvictionTest {
|
||||
|
||||
|
||||
private DefaultServerCache createCache() {
|
||||
|
||||
ServerCacheOptions cacheOptions = new ServerCacheOptions();
|
||||
cacheOptions.setMaxSize(10000);
|
||||
cacheOptions.setMaxIdleSecs(1);
|
||||
cacheOptions.setMaxSecsToLive(2);
|
||||
cacheOptions.setTrimFrequency(1);
|
||||
|
||||
ServerCacheConfig con = new ServerCacheConfig(ServerCacheType.BEAN, "foo", "foo", cacheOptions, null, null);
|
||||
return new DefaultServerCache(new DefaultServerCacheConfig(con));
|
||||
}
|
||||
|
||||
private final DefaultServerCache cache;
|
||||
|
||||
private final Random random = new Random();
|
||||
|
||||
public DefaultServerCache_RunEvictionTest() {
|
||||
this.cache = createCache();
|
||||
}
|
||||
|
||||
@Disabled("test takes long time")
|
||||
@Test
|
||||
public void runEvict() throws InterruptedException {
|
||||
|
||||
for (int i = 0; i < 15; i++) {
|
||||
doStuff();
|
||||
cache.runEviction();
|
||||
ServerCacheStatistics statistics = cache.statistics(true);
|
||||
System.out.println(statistics);
|
||||
Thread.sleep(500);
|
||||
}
|
||||
}
|
||||
|
||||
private void doStuff() {
|
||||
|
||||
for (int i = 0; i < 500; i++) {
|
||||
String key = "" + random.nextInt(20000);
|
||||
|
||||
int mode = random.nextInt(10);
|
||||
if (mode < 8) {
|
||||
cache.get(key);
|
||||
} else {
|
||||
cache.put(key, key + "-" + System.currentTimeMillis());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package io.ebeaninternal.server.changelog;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.event.changelog.BeanChange;
|
||||
import io.ebean.event.changelog.ChangeSet;
|
||||
import io.ebean.text.json.JsonContext;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
public class ChangeJsonBuilderTest extends BaseTestCase {
|
||||
|
||||
Helper helper = new Helper();
|
||||
|
||||
@Test
|
||||
public void testToJson() throws Exception {
|
||||
|
||||
ChangeJsonBuilder builder = new ChangeJsonBuilder();
|
||||
|
||||
ChangeSet changeSet = helper.createChangeSet("ABCD", 10);
|
||||
changeSet.getUserContext().put("altUser", "role user");
|
||||
|
||||
List<BeanChange> changes = changeSet.getChanges();
|
||||
for (int i = 0; i < changes.size(); i++) {
|
||||
StringWriter buffer = new StringWriter();
|
||||
builder.writeBeanJson(buffer, changes.get(i), changeSet);
|
||||
|
||||
assertThat(buffer.toString()).contains("\"source\"");
|
||||
assertThat(buffer.toString()).contains("\"userId\"");
|
||||
assertThat(buffer.toString()).contains("\"userIpAddress\"");
|
||||
assertThat(buffer.toString()).contains("\"userContext\"");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package io.ebeaninternal.server.changelog;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import io.ebean.event.changelog.ChangeSet;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class DefaultChangeLogListenerTest extends BaseTestCase {
|
||||
|
||||
Helper helper = new Helper();
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
DefaultChangeLogListener changeLogListener = new DefaultChangeLogListener();
|
||||
|
||||
Database defaultServer = DB.getDefault();
|
||||
changeLogListener.configure(defaultServer.pluginApi());
|
||||
|
||||
ChangeSet changeSet = helper.createChangeSet("INT-001", 13);
|
||||
|
||||
changeLogListener.log(changeSet);
|
||||
}
|
||||
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package io.ebeaninternal.server.changelog;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.event.changelog.ChangeLogFilter;
|
||||
import org.tests.inheritance.model.ProductConfiguration;
|
||||
import org.tests.model.basic.Address;
|
||||
import org.tests.model.basic.Contact;
|
||||
import org.tests.model.basic.Country;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class DefaultChangeLogRegisterTest extends BaseTestCase {
|
||||
|
||||
|
||||
@Test
|
||||
public void test_defaultInsertTrue() {
|
||||
|
||||
DefaultChangeLogRegister register = new DefaultChangeLogRegister(true);
|
||||
|
||||
assertNull(register.getChangeFilter(Address.class));
|
||||
|
||||
ChangeLogFilter changeFilter = register.getChangeFilter(Customer.class);
|
||||
DefaultChangeLogRegister.UpdateFilter updateFilter = (DefaultChangeLogRegister.UpdateFilter) changeFilter;
|
||||
assertFalse(updateFilter.includeInserts);
|
||||
assertThat(updateFilter.updateProperties).containsExactly("name", "status");
|
||||
|
||||
changeFilter = register.getChangeFilter(Country.class);
|
||||
DefaultChangeLogRegister.BasicFilter countryFilter = (DefaultChangeLogRegister.BasicFilter) changeFilter;
|
||||
assertTrue(countryFilter.includeInserts);
|
||||
|
||||
// use default setting
|
||||
changeFilter = register.getChangeFilter(Contact.class);
|
||||
DefaultChangeLogRegister.BasicFilter contactFilter = (DefaultChangeLogRegister.BasicFilter) changeFilter;
|
||||
assertTrue(contactFilter.includeInserts);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_defaultInsertFalse() {
|
||||
|
||||
DefaultChangeLogRegister register = new DefaultChangeLogRegister(false);
|
||||
|
||||
assertNull(register.getChangeFilter(Address.class));
|
||||
|
||||
ChangeLogFilter changeFilter = register.getChangeFilter(Customer.class);
|
||||
DefaultChangeLogRegister.UpdateFilter updateFilter = (DefaultChangeLogRegister.UpdateFilter) changeFilter;
|
||||
assertFalse(updateFilter.includeInserts);
|
||||
assertThat(updateFilter.updateProperties).containsExactly("name", "status");
|
||||
|
||||
changeFilter = register.getChangeFilter(Country.class);
|
||||
DefaultChangeLogRegister.BasicFilter countryFilter = (DefaultChangeLogRegister.BasicFilter) changeFilter;
|
||||
assertTrue(countryFilter.includeInserts);
|
||||
|
||||
// use default setting
|
||||
changeFilter = register.getChangeFilter(Contact.class);
|
||||
DefaultChangeLogRegister.BasicFilter contactFilter = (DefaultChangeLogRegister.BasicFilter) changeFilter;
|
||||
assertFalse(contactFilter.includeInserts);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_inheritance() {
|
||||
|
||||
DefaultChangeLogRegister register = new DefaultChangeLogRegister(true);
|
||||
ChangeLogFilter changeFilter = register.getChangeFilter(ProductConfiguration.class);
|
||||
assertNotNull(changeFilter);
|
||||
DefaultChangeLogRegister.BasicFilter basicFilter = (DefaultChangeLogRegister.BasicFilter) changeFilter;
|
||||
assertTrue(basicFilter.includeInserts);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package io.ebeaninternal.server.changelog;
|
||||
|
||||
import io.ebean.event.changelog.BeanChange;
|
||||
import io.ebean.event.changelog.ChangeSet;
|
||||
import io.ebean.event.changelog.ChangeType;
|
||||
import io.ebean.event.changelog.TxnState;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class Helper {
|
||||
|
||||
public ChangeSet createChangeSet(String txnId, long startId) {
|
||||
|
||||
ChangeSet cs = new ChangeSet();
|
||||
cs.setTxnId(txnId);
|
||||
cs.setTxnState(TxnState.COMMITTED);
|
||||
cs.setTxnBatch(0);
|
||||
cs.setSource("myApp");
|
||||
cs.setUserId("user234");
|
||||
cs.setUserIpAddress("123.4.5.6");
|
||||
cs.getUserContext().put("someKey", "user defined input");
|
||||
|
||||
List<BeanChange> changes = cs.getChanges();
|
||||
|
||||
changes.add(createInsert(startId));
|
||||
changes.add(createUpdate(startId));
|
||||
changes.add(createDelete(startId));
|
||||
|
||||
return cs;
|
||||
}
|
||||
|
||||
private BeanChange createInsert(long startId) {
|
||||
|
||||
Map<String, Object> values = new LinkedHashMap<>();
|
||||
values.put("name", "rob");
|
||||
values.put("modified", new Timestamp(System.currentTimeMillis()));
|
||||
|
||||
Map<String, Object> oldValues = new LinkedHashMap<>();
|
||||
oldValues.put("name", "rob");
|
||||
oldValues.put("modified", new Timestamp(System.currentTimeMillis() - 100000));
|
||||
|
||||
return new BeanChange("mytable", null, startId + 1, ChangeType.INSERT, "");//values, oldValues);
|
||||
}
|
||||
|
||||
private BeanChange createUpdate(long startId) {
|
||||
Map<String, Object> values = new LinkedHashMap<>();
|
||||
values.put("name", "jim");
|
||||
values.put("nowHasVal", "wasNull");
|
||||
values.put("nowNull", null);
|
||||
values.put("modified", new Timestamp(System.currentTimeMillis()));
|
||||
|
||||
|
||||
Map<String, Object> oldValues = new LinkedHashMap<>();
|
||||
oldValues.put("name", "steve");
|
||||
oldValues.put("nowHasVal", null);
|
||||
oldValues.put("nowNull", "hadVal");
|
||||
oldValues.put("modified", new Timestamp(System.currentTimeMillis() - 100000));
|
||||
|
||||
|
||||
return new BeanChange("mytable", null, startId + 2, ChangeType.UPDATE, "");//values, oldValues);
|
||||
}
|
||||
|
||||
private BeanChange createDelete(long startId) {
|
||||
return new BeanChange("mytable", null, startId + 3, ChangeType.DELETE, null);
|
||||
}
|
||||
|
||||
}
|
||||
+107
@@ -0,0 +1,107 @@
|
||||
package io.ebeaninternal.server.cluster.binarymessage;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.EbeanServer;
|
||||
import io.ebeaninternal.api.TDSpiEbeanServer;
|
||||
import io.ebeaninternal.api.TransactionEventTable;
|
||||
import io.ebeaninternal.server.cache.RemoteCacheEvent;
|
||||
import io.ebeaninternal.server.cluster.BinaryTransactionEventReader;
|
||||
import io.ebeaninternal.server.cluster.ServerLookup;
|
||||
import io.ebeaninternal.server.core.PersistRequest;
|
||||
import io.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import io.ebeaninternal.server.transaction.BeanPersistIds;
|
||||
import io.ebeaninternal.server.transaction.RemoteTableMod;
|
||||
import io.ebeaninternal.server.transaction.RemoteTransactionEvent;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class BinaryTransactionEventReadWriteTest extends BaseTestCase {
|
||||
|
||||
private final BeanDescriptor<Customer> customerBeanDescriptor = getBeanDescriptor(Customer.class);
|
||||
private final TDEbeanServer mockEbeanServer = new TDEbeanServer();
|
||||
private final BinaryTransactionEventReader reader = new BinaryTransactionEventReader(new TDServerLookup());
|
||||
|
||||
@Override
|
||||
protected <T> BeanDescriptor<T> getBeanDescriptor(Class<T> cls) {
|
||||
return super.getBeanDescriptor(cls);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readWrite() throws IOException {
|
||||
|
||||
RemoteTransactionEvent event = new RemoteTransactionEvent("db");
|
||||
|
||||
Set<String> tables = new HashSet<>();
|
||||
Collections.addAll(tables, "one", "two", "three");
|
||||
|
||||
event.addRemoteTableMod(new RemoteTableMod(tables));
|
||||
event.addRemoteCacheEvent(new RemoteCacheEvent(Customer.class));
|
||||
|
||||
event.addTableIUD(new TransactionEventTable.TableIUD("foo", true, false, true));
|
||||
event.addTableIUD(new TransactionEventTable.TableIUD("bar", false, true, false));
|
||||
|
||||
|
||||
BeanPersistIds beanPersistIds = new BeanPersistIds(customerBeanDescriptor);
|
||||
beanPersistIds.addId(PersistRequest.Type.INSERT, 42);
|
||||
beanPersistIds.addId(PersistRequest.Type.INSERT, 43);
|
||||
beanPersistIds.addId(PersistRequest.Type.UPDATE, 55);
|
||||
beanPersistIds.addId(PersistRequest.Type.DELETE, 66);
|
||||
beanPersistIds.addId(PersistRequest.Type.UPDATE, 92);
|
||||
|
||||
event.addBeanPersistIds(beanPersistIds);
|
||||
|
||||
byte[] binaryMessage = event.writeBinaryAsBytes(256);
|
||||
RemoteTransactionEvent read = reader.read(binaryMessage);
|
||||
|
||||
// cache event
|
||||
RemoteCacheEvent remoteCacheEvent = read.getRemoteCacheEvent();
|
||||
assertThat(remoteCacheEvent.getClearCaches()).containsOnly(Customer.class.getName());
|
||||
|
||||
// table mod
|
||||
RemoteTableMod remoteTableMod = read.getRemoteTableMod();
|
||||
assertThat(remoteTableMod.getTables()).isEqualTo(tables);
|
||||
|
||||
// Bean persist ids
|
||||
List<BeanPersistIds> beanPersistList = read.getBeanPersistList();
|
||||
assertThat(beanPersistList).hasSize(1);
|
||||
assertThat(beanPersistList.get(0).getIds()).containsOnly(55, 66, 92);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readWrite_RemoteTableMod() throws IOException {
|
||||
RemoteTransactionEvent event = new RemoteTransactionEvent("db");
|
||||
|
||||
Set<String> tables = new HashSet<>();
|
||||
Collections.addAll(tables, "one", "two", "three");
|
||||
|
||||
event.addRemoteTableMod(new RemoteTableMod(tables));
|
||||
|
||||
byte[] binaryMessage = event.writeBinaryAsBytes(256);
|
||||
RemoteTransactionEvent read = reader.read(binaryMessage);
|
||||
|
||||
RemoteTableMod remoteTableMod = read.getRemoteTableMod();
|
||||
assertThat(remoteTableMod.getTables()).isEqualTo(tables);
|
||||
}
|
||||
|
||||
class TDServerLookup implements ServerLookup {
|
||||
@Override
|
||||
public EbeanServer getServer(String name) {
|
||||
return mockEbeanServer;
|
||||
}
|
||||
}
|
||||
|
||||
class TDEbeanServer extends TDSpiEbeanServer {
|
||||
@Override
|
||||
public BeanDescriptor<?> descriptorById(String descriptorId) {
|
||||
return customerBeanDescriptor;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package io.ebeaninternal.server.core;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class BasicTypeConverterTest {
|
||||
|
||||
@Test
|
||||
public void toBoolean_when_1_long() throws Exception {
|
||||
assertTrue(BasicTypeConverter.toBoolean(1L, "T"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toBoolean_when_1_int() throws Exception {
|
||||
assertTrue(BasicTypeConverter.toBoolean(1, "T"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toBoolean_when_0() throws Exception {
|
||||
assertFalse(BasicTypeConverter.toBoolean(0L, "T"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toBoolean_when_TrueValue() throws Exception {
|
||||
assertTrue(BasicTypeConverter.toBoolean("T", "T"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toBoolean_when_notTrueValue() throws Exception {
|
||||
assertFalse(BasicTypeConverter.toBoolean("F", "T"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package io.ebeaninternal.server.core;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class BindPaddingTest {
|
||||
|
||||
@Test
|
||||
public void padIds() {
|
||||
|
||||
final List<Object> input = asList(1, 2);
|
||||
BindPadding.padIds(input);
|
||||
assertThat(input).contains(1,2,1,1,1);
|
||||
assertThat(input).hasSize(5);
|
||||
}
|
||||
|
||||
private List<Object> asList(int... id) {
|
||||
List<Object> list = new ArrayList<>();
|
||||
for (int i : id) {
|
||||
list.add(i);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void padding() {
|
||||
assertEquals(0, BindPadding.padding(0));
|
||||
assertEquals(0, BindPadding.padding(1));
|
||||
assertEquals(3, BindPadding.padding(2));
|
||||
assertEquals(2, BindPadding.padding(3));
|
||||
assertEquals(1, BindPadding.padding(4));
|
||||
assertEquals(0, BindPadding.padding(5));
|
||||
assertEquals(4, BindPadding.padding(6));
|
||||
assertEquals(1, BindPadding.padding(9));
|
||||
assertEquals(0, BindPadding.padding(10));
|
||||
assertEquals(9, BindPadding.padding(11));
|
||||
assertEquals(0, BindPadding.padding(20));
|
||||
assertEquals(19, BindPadding.padding(21));
|
||||
assertEquals(0, BindPadding.padding(40));
|
||||
assertEquals(9, BindPadding.padding(41));
|
||||
assertEquals(0, BindPadding.padding(50));
|
||||
assertEquals(49, BindPadding.padding(51));
|
||||
assertEquals(0, BindPadding.padding(100));
|
||||
assertEquals(0, BindPadding.padding(101));
|
||||
}
|
||||
}
|
||||
+354
@@ -0,0 +1,354 @@
|
||||
package io.ebeaninternal.server.core;
|
||||
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.FetchConfig;
|
||||
import io.ebean.Query;
|
||||
import io.ebeaninternal.server.querydefn.DefaultOrmQuery;
|
||||
import io.ebeaninternal.server.querydefn.OrmQueryDetail;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Order;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class DefaultServer_createOrmQueryRequestTest extends BaseTestCase {
|
||||
|
||||
Query<Order> query() {
|
||||
return server().find(Order.class);
|
||||
}
|
||||
|
||||
OrmQueryRequest<Order> queryRequest(Query<Order> query) {
|
||||
return OrmQueryRequestTestHelper.queryRequest(query);
|
||||
}
|
||||
|
||||
OrmQueryDetail detail(Query<Order> query) {
|
||||
return queryRequest(query).query().getDetail();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_empty_then_same() {
|
||||
|
||||
assertSame(detail(query()), detail(query()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void select_all_then_same() {
|
||||
|
||||
assertSame(detail(query().select("*")), detail(query().select("*")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_propertiesInOrder_then_same() {
|
||||
|
||||
assertSame(detail(query().select("id,name")), detail(query().select("id,name")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_propertiesInDifferentOrder_then_different() {
|
||||
|
||||
assertDifferent(detail(query().select("id,name")), detail(query().select("name, id")));
|
||||
|
||||
// but same from autotune perspective
|
||||
assertThat(detail(query().select("id,name")).isAutoTuneEqual(detail(query().select("name, id")))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_additional_fetch_then_different() {
|
||||
|
||||
assertDifferent(detail(query().select("id,name")), detail(query().select("id,name").fetch("details")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_same_fetch_then_same() {
|
||||
|
||||
assertSame(detail(query().select("id,name").fetch("details")), detail(query().select("id,name").fetch("details")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_fetch_order_different_then_different() {
|
||||
|
||||
assertDifferent(detail(query().select("id,name").fetch("details").fetch("customer")),
|
||||
detail(query().select("id,name").fetch("customer").fetch("details")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_extra_queryFetchToMany_then_same() {
|
||||
|
||||
assertDifferent(detail(query().select("id,name").fetch("customer")),
|
||||
detail(query().select("id,name").fetch("customer").fetch("details", FetchConfig.ofQuery())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_extra_queryToOne_fetch_then_different() {
|
||||
|
||||
// with the fetch of customer the foreign key must be added to the root query
|
||||
assertDifferent(detail(query().select("id,name")),
|
||||
detail(query().select("id,name").fetch("customer", FetchConfig.ofQuery())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_additional_fetch_V2_then_different() {
|
||||
|
||||
assertDifferent(detail(query().select("id,name")), detail(query().select("id,name").fetch("customer", "id")));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void when_fetchConfig_then_differentPlan() throws Exception {
|
||||
|
||||
DefaultOrmQuery<Order> query1 = (DefaultOrmQuery<Order>) DB.find(Order.class)
|
||||
.select("status, shipDate")
|
||||
.fetchQuery("details", "orderQty, unitPrice")
|
||||
.fetch("details.product", "sku, name");
|
||||
|
||||
|
||||
DefaultOrmQuery<Order> query2 = (DefaultOrmQuery<Order>) DB.find(Order.class)
|
||||
.select("status, shipDate")
|
||||
.fetch("details", "orderQty, unitPrice")
|
||||
.fetch("details.product", "sku, name");
|
||||
|
||||
assertDifferent(detail(query1), detail(query2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoinOrder_when_fetchJoins_expect_detailJoinsPreserveOrder() {
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.select("status, orderDate")
|
||||
.fetch("customer", "name")
|
||||
.fetch("details");
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query);
|
||||
OrmQueryDetail detail = queryRequest.query().getDetail();
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("customer", "details");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoinOrder_when_fetchJoinsAndWhere_expect_fetchJoinsOnlyInFetchPaths() {
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.select("status, orderDate")
|
||||
.fetch("details")
|
||||
.where().eq("customer.name", "rob").query();
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query);
|
||||
OrmQueryDetail detail = queryRequest.query().getDetail();
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("details");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoinOrder_when_queryFetch_expect_getFetchPaths_doesNotIncludeQueryJoin() {
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.select("status, orderDate")
|
||||
.fetch("customer", "name")
|
||||
.fetchQuery("details");
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query);
|
||||
OrmQueryDetail detail = queryRequest.query().getDetail();
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("customer");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoinOrder_when_queryFetch_expect_getFetchPaths_doesNotIncludeQueryJoin_via_fetchQuery() {
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.select("status, orderDate")
|
||||
.fetch("customer", "name")
|
||||
.fetchQuery("details");
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query);
|
||||
OrmQueryDetail detail = queryRequest.query().getDetail();
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("customer");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoinOrder_when_lazyFetch_expect_getFetchPaths_doesNotIncludeQueryJoin() {
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.select("status, orderDate")
|
||||
.fetch("customer", "name")
|
||||
.fetch("details", FetchConfig.ofLazy());
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query);
|
||||
OrmQueryDetail detail = queryRequest.query().getDetail();
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("customer");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoinOrder_when_lazyFetch_expect_getFetchPaths_doesNotIncludeQueryJoin_via_fetchLazy() {
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.select("status, orderDate")
|
||||
.fetch("customer", "name")
|
||||
.fetchLazy("details");
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query);
|
||||
OrmQueryDetail detail = queryRequest.query().getDetail();
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("customer");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoinOrder_when_lazyFetchAndHasChildren_expect_getFetchPaths_doesNotIncludeJoinOrChild() {
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.select("status, orderDate")
|
||||
.fetch("customer", "name")
|
||||
.fetchLazy("details")
|
||||
.fetch("details.product");
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query);
|
||||
OrmQueryDetail detail = queryRequest.query().getDetail();
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("customer");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoinOrder_when_fetchMany_expect_getFetchPaths_containsAllInOrder() {
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.select("status, orderDate")
|
||||
.fetch("details")
|
||||
.fetch("details.product")
|
||||
.fetch("customer", "name");
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query);
|
||||
OrmQueryDetail detail = queryRequest.query().getDetail();
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("details", "details.product", "customer");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoinOrder_when_queryJoin_expect_getFetchPaths_excludesQueryJoinAndChildren() {
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.select("status, orderDate")
|
||||
.fetchQuery("details")
|
||||
.fetch("details.product")
|
||||
.fetch("customer", "name");
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query);
|
||||
OrmQueryDetail detail = queryRequest.query().getDetail();
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("customer");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_removeJoinToMany_when_multipleManyPaths() {
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.fetch("details")
|
||||
.fetch("details.product")
|
||||
.fetch("customer")
|
||||
.fetch("customer.contacts"); // second many path
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query);
|
||||
OrmQueryDetail detail = queryRequest.query().getDetail();
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("details", "details.product", "customer");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_removeAllJoinToMany_when_firstRow() {
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.setFirstRow(1)
|
||||
.fetch("details") // many path
|
||||
.fetch("details.product")
|
||||
.fetch("customer")
|
||||
.fetch("customer.contacts"); // many path
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query);
|
||||
OrmQueryDetail detail = queryRequest.query().getDetail();
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("customer");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_removeAllJoinToMany_when_maxRows() {
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.setMaxRows(1)
|
||||
.fetch("details") // many path
|
||||
.fetch("details.product")
|
||||
.fetch("customer")
|
||||
.fetch("customer.contacts"); // many path
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query);
|
||||
OrmQueryDetail detail = queryRequest.query().getDetail();
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("customer");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_filterMany_included() {
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.fetch("details")
|
||||
.fetch("details.product")
|
||||
.fetch("customer")
|
||||
.fetch("customer.contacts")
|
||||
.filterMany("details").eq("orderQuantity", 10)
|
||||
.query();
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query);
|
||||
OrmQueryDetail detail = queryRequest.query().getDetail();
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("details", "details.product", "customer");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_filterMany_excludedByOrdering() {
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.fetch("customer")
|
||||
.fetch("customer.contacts")
|
||||
.fetch("details")
|
||||
.fetch("details.product")
|
||||
.filterMany("details").eq("orderQuantity", 10)
|
||||
.query();
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query);
|
||||
OrmQueryDetail detail = queryRequest.query().getDetail();
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("customer", "customer.contacts");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_filterMany_excludedExplicitly() {
|
||||
|
||||
Query<Order> query = DB.find(Order.class)
|
||||
.fetchQuery("details")
|
||||
.fetch("details.product")
|
||||
.fetch("customer")
|
||||
.fetch("customer.contacts")
|
||||
.filterMany("details").eq("orderQuantity", 10)
|
||||
.query();
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query);
|
||||
OrmQueryDetail detail = queryRequest.query().getDetail();
|
||||
|
||||
assertThat(detail.getFetchPaths()).containsExactly("customer", "customer.contacts");
|
||||
}
|
||||
|
||||
private void assertSame(OrmQueryDetail detail1, OrmQueryDetail detail2) {
|
||||
assertThat(hash(detail1)).isEqualTo(hash(detail2));
|
||||
}
|
||||
|
||||
private void assertDifferent(OrmQueryDetail detail1, OrmQueryDetail detail2) {
|
||||
assertThat(hash(detail1)).isNotEqualTo(hash(detail2));
|
||||
}
|
||||
|
||||
private String hash(OrmQueryDetail detail1) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
detail1.queryPlanHash(sb);
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
package io.ebeaninternal.server.core;
|
||||
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Car;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.tests.model.basic.Vehicle;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class DefaultServer_getReferenceTest extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void getReference_noPC() {
|
||||
DB.reference(Customer.class, 42);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getReference_when_inPC_expect_getFromPC() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
DB.execute(() -> {
|
||||
Customer loaded = DB.find(Customer.class, 1);
|
||||
Customer reference = DB.reference(Customer.class, 1);
|
||||
assertThat(loaded).isSameAs(reference);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void inherit_getReference_when_noPC() {
|
||||
|
||||
Car car = new Car();
|
||||
car.setDriver("TestForRef");
|
||||
DB.save(car);
|
||||
|
||||
Vehicle reference = DB.reference(Vehicle.class, car.getId());
|
||||
|
||||
assertThat(reference).isInstanceOf(Car.class);
|
||||
assertThat(reference.getId()).isSameAs(car.getId());
|
||||
|
||||
DB.delete(car);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void inherit_getReference_when_inPC() {
|
||||
|
||||
Car car = new Car();
|
||||
car.setDriver("TestForRef");
|
||||
DB.save(car);
|
||||
|
||||
DB.execute(() -> {
|
||||
Vehicle loaded = DB.find(Vehicle.class, car.getId());
|
||||
Vehicle reference = DB.reference(Vehicle.class, car.getId());
|
||||
assertThat(reference).isSameAs(loaded);
|
||||
}
|
||||
);
|
||||
DB.delete(car);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package io.ebeaninternal.server.core;
|
||||
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class DtoQueryRequestTest {
|
||||
|
||||
@Test
|
||||
public void testParse() {
|
||||
|
||||
assertEquals("foo", DtoQueryRequest.parseColumn("foo"));
|
||||
assertEquals("bar", DtoQueryRequest.parseColumn("zx__t0_bar"));
|
||||
assertEquals("BAR", DtoQueryRequest.parseColumn("ZX__T0_BAR"));
|
||||
assertEquals("baz", DtoQueryRequest.parseColumn("zx__t42_baz"));
|
||||
assertEquals("BAZ", DtoQueryRequest.parseColumn("ZX__T42_BAZ"));
|
||||
|
||||
assertEquals("e_t42_nope", DtoQueryRequest.parseColumn("e_t42_nope"));
|
||||
assertEquals("_f_t42_nope", DtoQueryRequest.parseColumn("_f_t42_nope"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package io.ebeaninternal.server.core;
|
||||
|
||||
import io.ebean.Database;
|
||||
import io.ebean.Query;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebeaninternal.api.SpiQuery;
|
||||
|
||||
public class HelpCreateQueryRequest {
|
||||
|
||||
public static <T> OrmQueryRequest<T> create(Database server, SpiQuery.Type type, Query<T> query, Transaction t) {
|
||||
DefaultServer defaultServer = (DefaultServer) server;
|
||||
return (OrmQueryRequest<T>)defaultServer.createQueryRequest(type, query, t);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
package io.ebeaninternal.server.core;
|
||||
|
||||
import io.ebean.config.DatabaseConfig;
|
||||
import io.ebean.datasource.DataSourceConfig;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class InitDataSourceTest {
|
||||
|
||||
private DatabaseConfig newConfig(String readOnlyUrl) {
|
||||
DatabaseConfig config = new DatabaseConfig();
|
||||
DataSourceConfig roConfig = new DataSourceConfig();
|
||||
roConfig.setUrl(readOnlyUrl);
|
||||
config.setReadOnlyDataSourceConfig(roConfig);
|
||||
return config;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readOnlyConfig_nullByDefault() {
|
||||
InitDataSource init = new InitDataSource(new DatabaseConfig());
|
||||
assertNull(init.readOnlyConfig());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readOnlyConfig_null_whenSetNullExplicitly() {
|
||||
DatabaseConfig config = new DatabaseConfig();
|
||||
config.setReadOnlyDataSourceConfig(null);
|
||||
|
||||
assertNull(new InitDataSource(config).readOnlyConfig());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readOnlyConfig_null_whenSetNullExplicitly_2() {
|
||||
assertNull(new InitDataSource(newConfig(null)).readOnlyConfig());
|
||||
assertNull(new InitDataSource(newConfig("")).readOnlyConfig());
|
||||
assertNull(new InitDataSource(newConfig(" ")).readOnlyConfig());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readOnlyConfig_null_whenValueNONE() {
|
||||
assertNull(new InitDataSource(newConfig("none")).readOnlyConfig());
|
||||
assertNull(new InitDataSource(newConfig("None")).readOnlyConfig());
|
||||
assertNull(new InitDataSource(newConfig("NONE")).readOnlyConfig());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readOnlyConfig_when_autoReadOnlyDataSource() {
|
||||
DatabaseConfig config = new DatabaseConfig();
|
||||
config.setAutoReadOnlyDataSource(true);
|
||||
|
||||
assertNotNull(new InitDataSource(config).readOnlyConfig());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readOnlyConfig_when_autoReadOnlyDataSource_expect_setToNull() {
|
||||
DatabaseConfig config = newConfig("none");
|
||||
config.setAutoReadOnlyDataSource(true);
|
||||
|
||||
final DataSourceConfig readOnlyConfig = new InitDataSource(config).readOnlyConfig();
|
||||
assertNull(readOnlyConfig.getUrl());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readOnlyConfig_when_urlSet() {
|
||||
DatabaseConfig config = newConfig("foo");
|
||||
|
||||
final DataSourceConfig roConfig = new InitDataSource(config).readOnlyConfig();
|
||||
assertNotNull(roConfig);
|
||||
assertEquals("foo", roConfig.getUrl());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readOnlyConfig_when_readOnlyUrlSetOnMain() {
|
||||
DatabaseConfig config = newConfig(null);
|
||||
// alternate location to set read-only url for developer convenience
|
||||
config.getDataSourceConfig().setReadOnlyUrl("bar");
|
||||
|
||||
final DataSourceConfig roConfig = new InitDataSource(config).readOnlyConfig();
|
||||
assertNotNull(roConfig);
|
||||
assertEquals("bar", roConfig.getUrl());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readOnlyConfig_when_readOnlyUrlSetOnMain_withNone() {
|
||||
DatabaseConfig config = newConfig("None");
|
||||
// alternate location to set read-only url for developer convenience
|
||||
config.getDataSourceConfig().setReadOnlyUrl("bar");
|
||||
|
||||
final DataSourceConfig roConfig = new InitDataSource(config).readOnlyConfig();
|
||||
assertNotNull(roConfig);
|
||||
assertEquals("bar", roConfig.getUrl());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readOnlyConfig_when_bothReadOnlyUrlsSet() {
|
||||
DatabaseConfig config = newConfig("one");
|
||||
config.getDataSourceConfig().setReadOnlyUrl("two");
|
||||
|
||||
final DataSourceConfig roConfig = new InitDataSource(config).readOnlyConfig();
|
||||
assertNotNull(roConfig);
|
||||
assertEquals("one", roConfig.getUrl());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readOnlyConfig_when_readOnlyUrlSetOnMain_withNoneNone() {
|
||||
DatabaseConfig config = newConfig("none");
|
||||
// alternate location to set read-only url for developer convenience
|
||||
config.getDataSourceConfig().setReadOnlyUrl("none");
|
||||
|
||||
final DataSourceConfig roConfig = new InitDataSource(config).readOnlyConfig();
|
||||
assertNull(roConfig);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readOnlyConfig_when_urlSet_2() {
|
||||
DatabaseConfig config = new DatabaseConfig();
|
||||
config.getReadOnlyDataSourceConfig().setUrl("foo");
|
||||
|
||||
final DataSourceConfig roConfig = new InitDataSource(config).readOnlyConfig();
|
||||
assertNotNull(roConfig);
|
||||
assertEquals("foo", roConfig.getUrl());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package io.ebeaninternal.server.core;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import io.ebeaninternal.api.SpiQuery;
|
||||
|
||||
public class OrmQueryRequestTestHelper {
|
||||
|
||||
static DefaultServer defaultServer = (DefaultServer) DB.getDefault();
|
||||
|
||||
/**
|
||||
* Create and return a OrmQueryRequest for the given query.
|
||||
*/
|
||||
public static <T> OrmQueryRequest<T> queryRequest(Query<T> query) {
|
||||
return (OrmQueryRequest<T>) defaultServer.createQueryRequest(SpiQuery.Type.LIST, query, null);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
package io.ebeaninternal.server.core;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import io.ebean.ValuePair;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import io.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.Order.Status;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestDiffHelpSimple extends BaseTestCase {
|
||||
|
||||
long firstTime = System.currentTimeMillis() - 10000;
|
||||
long secondTime = System.currentTimeMillis();
|
||||
|
||||
Database server;
|
||||
BeanDescriptor<Order> orderDesc;
|
||||
|
||||
public TestDiffHelpSimple() {
|
||||
server = DB.getDefault();
|
||||
SpiEbeanServer spiServer = (SpiEbeanServer) server;
|
||||
orderDesc = spiServer.descriptor(Order.class);
|
||||
}
|
||||
|
||||
private Order createBaseOrder(Database server) {
|
||||
Order order1 = new Order();
|
||||
order1.setId(12);
|
||||
order1.setCretime(new Timestamp(firstTime));
|
||||
order1.setCustomer(server.reference(Customer.class, 1234));
|
||||
order1.setStatus(Status.NEW);
|
||||
order1.setShipDate(new Date(firstTime));
|
||||
order1.setOrderDate(new Date(firstTime));
|
||||
return order1;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBasicChanges() {
|
||||
|
||||
|
||||
Order order1 = createBaseOrder(server);
|
||||
|
||||
Order order2 = new Order();
|
||||
order2.setId(14);
|
||||
order2.setCretime(new Timestamp(secondTime));
|
||||
order2.setCustomer(server.reference(Customer.class, 2133));
|
||||
order2.setStatus(Status.COMPLETE);
|
||||
order2.setShipDate(new Date(secondTime));
|
||||
order2.setOrderDate(new Date(secondTime));
|
||||
|
||||
Map<String, ValuePair> diff = DiffHelp.diff(order1, order2, orderDesc);
|
||||
|
||||
assertEquals(5, diff.size());
|
||||
|
||||
Set<String> keySet = diff.keySet();
|
||||
assertTrue(keySet.contains("cretime"));
|
||||
assertTrue(keySet.contains("status"));
|
||||
assertTrue(keySet.contains("shipDate"));
|
||||
assertTrue(keySet.contains("orderDate"));
|
||||
assertTrue(keySet.contains("customer.id"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBasicChanges_given_flatMode() {
|
||||
|
||||
|
||||
Order order1 = createBaseOrder(server);
|
||||
|
||||
Order order2 = new Order();
|
||||
order2.setId(14);
|
||||
order2.setCretime(new Timestamp(secondTime));
|
||||
order2.setCustomer(server.reference(Customer.class, 2133));
|
||||
order2.setStatus(Status.COMPLETE);
|
||||
order2.setShipDate(new Date(secondTime));
|
||||
order2.setOrderDate(new Date(secondTime));
|
||||
|
||||
Map<String, ValuePair> diff = DiffHelp.diff(order1, order2, orderDesc);
|
||||
|
||||
assertEquals(5, diff.size());
|
||||
|
||||
Set<String> keySet = diff.keySet();
|
||||
assertTrue(keySet.contains("cretime"));
|
||||
assertTrue(keySet.contains("status"));
|
||||
assertTrue(keySet.contains("shipDate"));
|
||||
assertTrue(keySet.contains("orderDate"));
|
||||
assertTrue(keySet.contains("customer.id"));
|
||||
assertEquals(2133, diff.get("customer.id").getOldValue());
|
||||
assertEquals(1234, diff.get("customer.id").getNewValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIdIgnored() {
|
||||
|
||||
Order order1 = createBaseOrder(server);
|
||||
Order order2 = createBaseOrder(server);
|
||||
order2.setId(14);
|
||||
|
||||
Map<String, ValuePair> diff = DiffHelp.diff(order1, order2, orderDesc);
|
||||
|
||||
assertEquals(0, diff.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSecondValueNull() {
|
||||
|
||||
Order order1 = createBaseOrder(server);
|
||||
|
||||
Order order2 = createBaseOrder(server);
|
||||
order2.setCustomer(server.reference(Customer.class, 2133));
|
||||
order2.setStatus(Status.COMPLETE);
|
||||
order2.setShipDate(null);
|
||||
|
||||
Map<String, ValuePair> diff = DiffHelp.diff(order1, order2, orderDesc);
|
||||
|
||||
assertEquals(3, diff.size());
|
||||
|
||||
Set<String> keySet = diff.keySet();
|
||||
assertTrue(keySet.contains("status"));
|
||||
assertTrue(keySet.contains("customer.id"));
|
||||
assertTrue(keySet.contains("shipDate"));
|
||||
|
||||
ValuePair shipDatePair = diff.get("shipDate");
|
||||
assertEquals(order1.getShipDate(), shipDatePair.getNewValue());
|
||||
assertEquals(order2.getShipDate(), shipDatePair.getOldValue());
|
||||
assertNull(shipDatePair.getOldValue());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testFirstValueNull() {
|
||||
|
||||
Order order1 = createBaseOrder(server);
|
||||
order1.setShipDate(null);
|
||||
|
||||
Order order2 = createBaseOrder(server);
|
||||
order2.setShipDate(new Date(secondTime));
|
||||
|
||||
Map<String, ValuePair> diff = DiffHelp.diff(order1, order2, orderDesc);
|
||||
|
||||
assertEquals(1, diff.size());
|
||||
Set<String> keySet = diff.keySet();
|
||||
assertTrue(keySet.contains("shipDate"));
|
||||
|
||||
ValuePair shipDatePair = diff.get("shipDate");
|
||||
assertEquals(order1.getShipDate(), shipDatePair.getNewValue());
|
||||
assertEquals(order2.getShipDate(), shipDatePair.getOldValue());
|
||||
assertNull(shipDatePair.getNewValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBothNull() {
|
||||
|
||||
Order order1 = createBaseOrder(server);
|
||||
order1.setShipDate(null);
|
||||
|
||||
Order order2 = createBaseOrder(server);
|
||||
order2.setShipDate(null);
|
||||
|
||||
Map<String, ValuePair> diff = DiffHelp.diff(order1, order2, orderDesc);
|
||||
|
||||
assertEquals(0, diff.size());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
package io.ebeaninternal.server.core;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import io.ebean.ValuePair;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import io.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.embedded.EMain;
|
||||
import org.tests.model.embedded.Eembeddable;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestDiffHelpWithEmbedded extends BaseTestCase {
|
||||
|
||||
Database server;
|
||||
|
||||
BeanDescriptor<EMain> emainDesc;
|
||||
|
||||
public TestDiffHelpWithEmbedded() {
|
||||
server = DB.getDefault();
|
||||
SpiEbeanServer spiServer = (SpiEbeanServer) server;
|
||||
emainDesc = spiServer.descriptor(EMain.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChangeExistingEmbedded() {
|
||||
|
||||
EMain emain1 = createEMain();
|
||||
EMain emain2 = createEMain();
|
||||
|
||||
emain2.getEmbeddable().setDescription("baz");
|
||||
|
||||
Map<String, ValuePair> diff = DiffHelp.diff(emain1, emain2, emainDesc);
|
||||
assertEquals(1, diff.size());
|
||||
ValuePair valuePair = diff.get("embeddable.description");
|
||||
|
||||
assertNotNull(valuePair);
|
||||
assertEquals("bar", valuePair.getNewValue());
|
||||
assertEquals("baz", valuePair.getOldValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* Same result as testChangeExistingEmbedded.
|
||||
*/
|
||||
@Test
|
||||
public void testSetViaNewEmbedded() {
|
||||
|
||||
EMain emain1 = createEMain();
|
||||
EMain emain2 = createEMain();
|
||||
|
||||
Eembeddable embeddable = new Eembeddable();
|
||||
embeddable.setDescription("baz");
|
||||
emain2.setEmbeddable(embeddable);
|
||||
|
||||
|
||||
Map<String, ValuePair> diff = DiffHelp.diff(emain1, emain2, emainDesc);
|
||||
assertEquals(1, diff.size());
|
||||
ValuePair valuePair = diff.get("embeddable.description");
|
||||
|
||||
assertNotNull(valuePair);
|
||||
assertEquals("bar", valuePair.getNewValue());
|
||||
assertEquals("baz", valuePair.getOldValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDiffEmbedded_flatMode() {
|
||||
|
||||
EMain emain1 = createEMain();
|
||||
EMain emain2 = createEMain();
|
||||
emain2.getEmbeddable().setDescription("baz");
|
||||
|
||||
Map<String, ValuePair> diff = DiffHelp.diff(emain1, emain2, emainDesc);
|
||||
assertEquals(1, diff.size());
|
||||
ValuePair valuePair = diff.get("embeddable.description");
|
||||
|
||||
assertNotNull(valuePair);
|
||||
assertEquals("bar", valuePair.getNewValue());
|
||||
assertEquals("baz", valuePair.getOldValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFirstEmbeddedIsNull() {
|
||||
|
||||
EMain emain1 = createEMain();
|
||||
emain1.setEmbeddable(null);
|
||||
EMain emain2 = createEMain();
|
||||
|
||||
Map<String, ValuePair> diff = DiffHelp.diff(emain1, emain2, emainDesc);
|
||||
assertEquals(1, diff.size());
|
||||
ValuePair valuePair = diff.get("embeddable.description");
|
||||
|
||||
assertNotNull(valuePair);
|
||||
assertNull(valuePair.getNewValue());
|
||||
assertEquals("bar", valuePair.getOldValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSecondEmbeddedIsNull() {
|
||||
|
||||
EMain emain1 = createEMain();
|
||||
EMain emain2 = createEMain();
|
||||
emain2.setEmbeddable(null);
|
||||
|
||||
Map<String, ValuePair> diff = DiffHelp.diff(emain1, emain2, emainDesc);
|
||||
assertEquals(1, diff.size());
|
||||
ValuePair valuePair = diff.get("embeddable.description");
|
||||
|
||||
assertNotNull(valuePair);
|
||||
assertNull(valuePair.getOldValue());
|
||||
assertEquals("bar", valuePair.getNewValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSecondEmbeddedIsNull_given_flatMode() {
|
||||
|
||||
EMain emain1 = createEMain();
|
||||
EMain emain2 = createEMain();
|
||||
emain2.setEmbeddable(null);
|
||||
|
||||
Map<String, ValuePair> diff = DiffHelp.diff(emain1, emain2, emainDesc);
|
||||
assertEquals(1, diff.size());
|
||||
ValuePair valuePair = diff.get("embeddable.description");
|
||||
|
||||
assertNotNull(valuePair);
|
||||
assertNull(valuePair.getOldValue());
|
||||
assertEquals("bar", valuePair.getNewValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBothEmbeddedIsNull() {
|
||||
|
||||
EMain emain1 = createEMain();
|
||||
emain1.setEmbeddable(null);
|
||||
EMain emain2 = createEMain();
|
||||
emain2.setEmbeddable(null);
|
||||
|
||||
Map<String, ValuePair> diff = DiffHelp.diff(emain1, emain2, emainDesc);
|
||||
assertEquals(0, diff.size());
|
||||
}
|
||||
|
||||
private EMain createEMain() {
|
||||
EMain emain = new EMain();
|
||||
emain.setName("foo");
|
||||
emain.setVersion(13L);
|
||||
|
||||
Eembeddable embeddable = new Eembeddable();
|
||||
embeddable.setDescription("bar");
|
||||
emain.setEmbeddable(embeddable);
|
||||
return emain;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package io.ebeaninternal.server.core.bootup;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class DistillPackagesTest {
|
||||
|
||||
@Test
|
||||
public void when_unique_expect_all() throws Exception {
|
||||
|
||||
List<String> distill = DistillPackages.distill(group("one", "two"), group("three"));
|
||||
assertThat(distill).containsExactly("one", "three", "two");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_sub_expect_distilled() throws Exception {
|
||||
|
||||
List<String> distill = DistillPackages.distill(group("one", "one.sub"), group("three"));
|
||||
assertThat(distill).containsExactly("one", "three");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void when_sub_expect_distilled2() throws Exception {
|
||||
|
||||
List<String> distill = DistillPackages.distill(group("one", "one.sub"), group("one.foo"));
|
||||
assertThat(distill).containsExactly("one");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_subDotSub_expect_distilled2() throws Exception {
|
||||
|
||||
List<String> distill = DistillPackages.distill(group("one", "one.sub.me"), group("two"));
|
||||
assertThat(distill).containsExactly("one", "two");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_unordered_expect_naturalOrder() throws Exception {
|
||||
|
||||
List<String> distill = DistillPackages.distill(group("z.x.y", "two"), group("one", "one.sub.me"));
|
||||
assertThat(distill).containsExactly("one", "two", "z.x.y");
|
||||
}
|
||||
|
||||
List<String> group(String... packages) {
|
||||
return Arrays.asList(packages);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package io.ebeaninternal.server.core.bootup;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ManifestReaderTest {
|
||||
|
||||
@Test
|
||||
public void readOne() throws Exception {
|
||||
|
||||
Set<String> packageSet = readMf("META-INF/test/test-one.mf");
|
||||
assertThat(packageSet).contains("foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readSome() throws Exception {
|
||||
|
||||
Set<String> packageSet = readMf("META-INF/test/test-some.mf");
|
||||
assertThat(packageSet).contains("com.foo.domain", "com.bar.domain");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readEntityPackages() throws Exception {
|
||||
|
||||
Set<String> packageSet = readMf("META-INF/test/test-entity-packages.mf");
|
||||
assertThat(packageSet).contains("com.baz", "org.bax.domain");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readCombined() throws Exception {
|
||||
|
||||
Set<String> packageSet = readMf("META-INF/test/test-combined.mf");
|
||||
assertThat(packageSet).contains("com.foo.domain", "com.bar.domain", "com.baz.domain");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readAgentOnlyUse() throws Exception {
|
||||
|
||||
Set<String> packageSet = readMf("META-INF/test/test-agent-only-use.mf");
|
||||
assertThat(packageSet).isEmpty();
|
||||
}
|
||||
|
||||
private Set<String> readMf(String path) {
|
||||
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
||||
return ManifestReader.create(classLoader)
|
||||
.read(path)
|
||||
.entityPackages();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class BeanCascadeInfoTest {
|
||||
|
||||
@Test
|
||||
public void setTypes_ALL() throws Exception {
|
||||
|
||||
BeanCascadeInfo info = new BeanCascadeInfo();
|
||||
info.setTypes(new CascadeType[]{CascadeType.ALL});
|
||||
assertTrue(info.isSave());
|
||||
assertTrue(info.isDelete());
|
||||
assertTrue(info.isRefresh());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setTypes_PERSIST() throws Exception {
|
||||
|
||||
BeanCascadeInfo info = new BeanCascadeInfo();
|
||||
info.setTypes(new CascadeType[]{CascadeType.PERSIST});
|
||||
assertTrue(info.isSave());
|
||||
assertFalse(info.isDelete());
|
||||
assertFalse(info.isRefresh());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setTypes_MERGE() throws Exception {
|
||||
|
||||
BeanCascadeInfo info = new BeanCascadeInfo();
|
||||
info.setTypes(new CascadeType[]{CascadeType.MERGE});
|
||||
assertTrue(info.isSave());
|
||||
assertFalse(info.isDelete());
|
||||
assertFalse(info.isRefresh());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setTypes_REMOVE() throws Exception {
|
||||
|
||||
BeanCascadeInfo info = new BeanCascadeInfo();
|
||||
info.setTypes(new CascadeType[]{CascadeType.REMOVE});
|
||||
assertFalse(info.isSave());
|
||||
assertTrue(info.isDelete());
|
||||
assertFalse(info.isRefresh());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setTypes_REFRESH() throws Exception {
|
||||
|
||||
BeanCascadeInfo info = new BeanCascadeInfo();
|
||||
info.setTypes(new CascadeType[]{CascadeType.REFRESH});
|
||||
assertFalse(info.isSave());
|
||||
assertFalse(info.isDelete());
|
||||
assertTrue(info.isRefresh());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setDelete() throws Exception {
|
||||
|
||||
BeanCascadeInfo info = new BeanCascadeInfo();
|
||||
info.setDelete(true);
|
||||
assertFalse(info.isSave());
|
||||
assertTrue(info.isDelete());
|
||||
assertFalse(info.isRefresh());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setSaveDelete() throws Exception {
|
||||
|
||||
BeanCascadeInfo info = new BeanCascadeInfo();
|
||||
info.setSaveDelete(true, true);
|
||||
assertTrue(info.isSave());
|
||||
assertTrue(info.isDelete());
|
||||
assertFalse(info.isRefresh());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.bean.EntityBean;
|
||||
import io.ebean.plugin.Property;
|
||||
import io.ebeaninternal.server.core.CacheOptions;
|
||||
import io.ebeaninternal.server.deploy.meta.DeployBeanDescriptor;
|
||||
import io.ebeaninternal.server.deploy.meta.DeployIdentityMode;
|
||||
import io.ebeanservice.docstore.api.DocStoreBeanAdapter;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.*;
|
||||
import org.tests.model.bridge.BSite;
|
||||
import org.tests.model.bridge.BUser;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class BeanDescriptorTest extends BaseTestCase {
|
||||
|
||||
private BeanDescriptor<Customer> customerDesc = spiEbeanServer().descriptor(Customer.class);
|
||||
|
||||
@Test
|
||||
public void createReference() {
|
||||
|
||||
Customer bean = customerDesc.createReference(null, false, 42, null);
|
||||
assertThat(bean.getId()).isEqualTo(42);
|
||||
assertThat(server().beanState(bean).isReadOnly()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createReference_whenReadOnly() {
|
||||
|
||||
Customer bean = customerDesc.createReference(Boolean.TRUE, false, 42, null);
|
||||
assertThat(server().beanState(bean).isReadOnly()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createReference_whenNotReadOnly() {
|
||||
|
||||
Customer bean = customerDesc.createReference(Boolean.FALSE, false, 42, null);
|
||||
assertThat(server().beanState(bean).isReadOnly()).isFalse();
|
||||
|
||||
bean = customerDesc.createReference(42, null);
|
||||
assertThat(server().beanState(bean).isReadOnly()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createReference_when_disabledLazyLoad() {
|
||||
|
||||
Customer bean = customerDesc.createReference(Boolean.FALSE, true, 42, null);
|
||||
assertThat(server().beanState(bean).isDisableLazyLoad()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createReference_with_inheritance() {
|
||||
Cat cat = new Cat();
|
||||
cat.setName("Puss");
|
||||
DB.save(cat);
|
||||
|
||||
Dog dog = new Dog();
|
||||
dog.setRegistrationNumber("DOGGIE");
|
||||
DB.save(dog);
|
||||
|
||||
AnimalShelter shelter = new AnimalShelter();
|
||||
shelter.setName("My Animal Shelter");
|
||||
shelter.getAnimals().add(cat);
|
||||
shelter.getAnimals().add(dog);
|
||||
|
||||
DB.save(shelter);
|
||||
|
||||
BeanDescriptor<Animal> animalDesc = spiEbeanServer().descriptor(Animal.class);
|
||||
|
||||
Animal bean = animalDesc.createReference(Boolean.FALSE, false, dog.getId(), null);
|
||||
assertThat(bean.getId()).isEqualTo(dog.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allProperties() {
|
||||
|
||||
BeanDescriptor<Order> desc = getBeanDescriptor(Order.class);
|
||||
Collection<? extends Property> props = desc.allProperties();
|
||||
|
||||
assertThat(props).extracting("name").contains("id", "status", "orderDate", "shipDate");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchBaseTable() {
|
||||
BeanDescriptor<Customer> desc = getBeanDescriptor(Customer.class);
|
||||
assertTrue(desc.matchBaseTable("o_customer"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchBaseTable_whenTableHasSchema_expect_matchRegardlessOfSchema() {
|
||||
|
||||
DeployBeanDescriptor<Customer> deploy = mockDeployCustomer();
|
||||
|
||||
when(deploy.getBaseTable()).thenReturn("foo.o_customer");
|
||||
BeanDescriptor<?> desc1 = new BeanDescriptor<>(mockOwner(), deploy);
|
||||
assertTrue(desc1.matchBaseTable("o_customer"));
|
||||
|
||||
when(deploy.getBaseTable()).thenReturn("bar.o_customer");
|
||||
BeanDescriptor<?> desc2 = new BeanDescriptor<>(mockOwner(), deploy);
|
||||
assertTrue(desc2.matchBaseTable("o_customer"));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private DeployBeanDescriptor<Customer> mockDeployCustomer() {
|
||||
DeployBeanDescriptor<Customer> deploy = mock(DeployBeanDescriptor.class);
|
||||
when(deploy.getBeanType()).thenReturn(Customer.class);
|
||||
when(deploy.getIdentityMode()).thenReturn(DeployIdentityMode.auto());
|
||||
when(deploy.buildIdentityMode()).thenReturn(IdentityMode.NONE);
|
||||
when(deploy.getCacheOptions()).thenReturn(CacheOptions.NO_CACHING);
|
||||
return deploy;
|
||||
}
|
||||
|
||||
private BeanDescriptorMap mockOwner() {
|
||||
BeanDescriptorMap owner = mock(BeanDescriptorMap.class);
|
||||
when(owner.createDocStoreBeanAdapter(any(), any())).thenReturn(mock(DocStoreBeanAdapter.class));
|
||||
return owner;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void merge_when_empty() {
|
||||
|
||||
Customer from = new Customer();
|
||||
from.setId(42);
|
||||
from.setName("rob");
|
||||
|
||||
Customer to = new Customer();
|
||||
customerDesc.merge((EntityBean) from, (EntityBean) to);
|
||||
|
||||
assertThat(to.getId()).isEqualTo(42);
|
||||
assertThat(to.getName()).isEqualTo("rob");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isIdTypeExternal_when_externalId() {
|
||||
|
||||
BeanDescriptor<Country> countryDesc = spiEbeanServer().descriptor(Country.class);
|
||||
assertThat(countryDesc.isIdGeneratedValue()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isIdTypeExternal_when_platformGenerator_noGeneratedValueAnnotation() {
|
||||
|
||||
assertThat(customerDesc.isIdGeneratedValue()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isIdTypeExternal_when_explicitGeneratedValue() {
|
||||
|
||||
BeanDescriptor<Contact> desc = spiEbeanServer().descriptor(Contact.class);
|
||||
assertThat(desc.isIdGeneratedValue()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isIdTypeExternal_when_uuidGenerator_and_generatedValue() {
|
||||
|
||||
BeanDescriptor<BSite> desc = spiEbeanServer().descriptor(BSite.class);
|
||||
assertThat(desc.isIdGeneratedValue()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isIdTypeExternal_when_uuidGenerator_and_noGeneratedValue() {
|
||||
|
||||
BeanDescriptor<BUser> desc = spiEbeanServer().descriptor(BUser.class);
|
||||
assertThat(desc.isIdGeneratedValue()).isFalse();
|
||||
}
|
||||
}
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebeanservice.docstore.api.mapping.DocPropertyAdapter;
|
||||
import io.ebeanservice.docstore.api.mapping.DocPropertyMapping;
|
||||
import io.ebeanservice.docstore.api.mapping.DocumentMapping;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class BeanDescriptor_documentMappingTest extends BaseTestCase {
|
||||
|
||||
|
||||
@Test
|
||||
public void docMapping() {
|
||||
|
||||
BeanDescriptor<Order> desc = getBeanDescriptor(Order.class);
|
||||
|
||||
DocumentMapping documentMapping = desc.docMapping();
|
||||
|
||||
DocPropertyMapping properties = documentMapping.getProperties();
|
||||
|
||||
assertThat(properties).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void docMapping_visitor() {
|
||||
|
||||
BeanDescriptor<Order> desc = getBeanDescriptor(Order.class);
|
||||
|
||||
DocumentMapping documentMapping = desc.docMapping();
|
||||
|
||||
DocPropertyMapping properties = documentMapping.getProperties();
|
||||
|
||||
assertThat(properties).isNotNull();
|
||||
|
||||
TDVisitor tdVisitor = new TDVisitor();
|
||||
documentMapping.visit(tdVisitor);
|
||||
|
||||
assertThat(tdVisitor.sb.toString()).isEqualTo("{status,orderDate,shipDate, object{customer:id,name,}customerName, nested{details: [id,orderQty,shipQty,unitPrice,cretime,updtime,]}cretime,updtime,}");
|
||||
}
|
||||
|
||||
class TDVisitor extends DocPropertyAdapter {
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
@Override
|
||||
public void visitProperty(DocPropertyMapping property) {
|
||||
sb.append(property.getName() + ",");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitBegin() {
|
||||
|
||||
sb.append("{");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitEnd() {
|
||||
sb.append("}");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitBeginObject(DocPropertyMapping property) {
|
||||
sb.append(" object{" + property.getName() + ":");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitEndObject(DocPropertyMapping property) {
|
||||
sb.append("}");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitBeginList(DocPropertyMapping property) {
|
||||
sb.append(" nested{" + property.getName() + ": [");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitEndList(DocPropertyMapping property) {
|
||||
sb.append("]}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+113
@@ -0,0 +1,113 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import io.ebean.DatabaseFactory;
|
||||
import io.ebean.config.DatabaseConfig;
|
||||
import io.ebean.event.AbstractBeanPersistListener;
|
||||
import io.ebean.event.BeanPersistAdapter;
|
||||
import io.ebean.event.BeanPersistListener;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import org.tests.model.basic.EBasic;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class BeanDescriptor_registerTest {
|
||||
|
||||
@Test
|
||||
public void testRegisterDeregister() {
|
||||
|
||||
DatabaseConfig config = new DatabaseConfig();
|
||||
|
||||
config.setName("h2other");
|
||||
config.loadFromProperties();
|
||||
config.setDdlExtra(false);
|
||||
config.setRegister(false);
|
||||
config.setDefaultServer(false);
|
||||
config.getClasses().add(EBasic.class);
|
||||
|
||||
SpiEbeanServer ebeanServer = (SpiEbeanServer)DatabaseFactory.create(config);
|
||||
try {
|
||||
BeanDescriptor<EBasic> desc = ebeanServer.descriptor(EBasic.class);
|
||||
persistListenerRegistrationTests(desc);
|
||||
persistControllerRegistrationTests(desc);
|
||||
} finally {
|
||||
ebeanServer.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
private void persistControllerRegistrationTests(BeanDescriptor<EBasic> desc) {
|
||||
|
||||
Controller1 controller1 = new Controller1();
|
||||
|
||||
assertNull(desc.persistController());
|
||||
desc.register(controller1);
|
||||
assertSame(controller1, desc.persistController());
|
||||
|
||||
Controller2 controller2 = new Controller2();
|
||||
desc.register(controller2);
|
||||
|
||||
assertEquals(2, ((ChainedBeanPersistController) desc.persistController()).size());
|
||||
|
||||
desc.deregister(controller1);
|
||||
assertEquals(1, ((ChainedBeanPersistController) desc.persistController()).size());
|
||||
|
||||
desc.deregister(controller2);
|
||||
assertEquals(0, ((ChainedBeanPersistController) desc.persistController()).size());
|
||||
}
|
||||
|
||||
private void persistListenerRegistrationTests(BeanDescriptor<EBasic> desc) {
|
||||
|
||||
Listener1 listener1 = new Listener1();
|
||||
|
||||
assertNull(desc.persistListener());
|
||||
desc.register(listener1);
|
||||
assertSame(listener1, desc.persistListener());
|
||||
|
||||
Listener2 listener2 = new Listener2();
|
||||
desc.register(listener2);
|
||||
|
||||
BeanPersistListener persistListener = desc.persistListener();
|
||||
assertTrue(persistListener instanceof ChainedBeanPersistListener);
|
||||
assertEquals(2, ((ChainedBeanPersistListener) persistListener).size());
|
||||
|
||||
desc.deregister(listener1);
|
||||
assertEquals(1, ((ChainedBeanPersistListener) desc.persistListener()).size());
|
||||
|
||||
desc.deregister(listener2);
|
||||
assertEquals(0, ((ChainedBeanPersistListener) desc.persistListener()).size());
|
||||
}
|
||||
|
||||
public static class Listener1 extends AbstractBeanPersistListener {
|
||||
|
||||
@Override
|
||||
public boolean isRegisterFor(Class<?> cls) {
|
||||
return EBasic.class.isAssignableFrom(cls);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class Listener2 extends AbstractBeanPersistListener {
|
||||
|
||||
@Override
|
||||
public boolean isRegisterFor(Class<?> cls) {
|
||||
return EBasic.class.isAssignableFrom(cls);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class Controller1 extends BeanPersistAdapter {
|
||||
|
||||
@Override
|
||||
public boolean isRegisterFor(Class<?> cls) {
|
||||
return EBasic.class.isAssignableFrom(cls);
|
||||
}
|
||||
}
|
||||
|
||||
public static class Controller2 extends BeanPersistAdapter {
|
||||
|
||||
@Override
|
||||
public boolean isRegisterFor(Class<?> cls) {
|
||||
return EBasic.class.isAssignableFrom(cls);
|
||||
}
|
||||
}
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.EBasic;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
|
||||
public class BeanDescriptor_whenCreatedPropertyTest extends BaseTestCase {
|
||||
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
SpiEbeanServer server = (SpiEbeanServer) DB.getDefault();
|
||||
|
||||
BeanDescriptor<Customer> desc = server.descriptor(Customer.class);
|
||||
|
||||
BeanProperty whenCreatedProperty = desc.whenCreatedProperty();
|
||||
assertEquals("cretime", whenCreatedProperty.dbColumn());
|
||||
|
||||
BeanProperty whenModifiedProperty = desc.whenModifiedProperty();
|
||||
assertEquals("updtime", whenModifiedProperty.dbColumn());
|
||||
|
||||
|
||||
BeanDescriptor<EBasic> eBasicDesc = server.descriptor(EBasic.class);
|
||||
assertNull(eBasicDesc.whenCreatedProperty());
|
||||
assertNull(eBasicDesc.whenModifiedProperty());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import io.ebean.meta.BasicMetricVisitor;
|
||||
import io.ebean.meta.MetaTimedMetric;
|
||||
import io.ebeaninternal.server.core.PersistRequest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class BeanIudMetricsTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void addBatch() {
|
||||
|
||||
BeanIudMetrics iudMetrics = new BeanIudMetrics("one");
|
||||
|
||||
final long startNanos = System.nanoTime() - 10000;
|
||||
iudMetrics.addBatch(PersistRequest.Type.INSERT, startNanos, 4);
|
||||
|
||||
BasicMetricVisitor basic = new BasicMetricVisitor();
|
||||
iudMetrics.visit(basic);
|
||||
|
||||
List<MetaTimedMetric> timed = basic.timedMetrics();
|
||||
assertThat(timed).hasSize(1);
|
||||
|
||||
assertThat(timed.get(0).count()).isEqualTo(4);
|
||||
assertThat(timed.get(0).name()).isEqualTo("iud.one.insertBatch");
|
||||
|
||||
iudMetrics.addBatch(PersistRequest.Type.UPDATE, startNanos, 1);
|
||||
iudMetrics.addBatch(PersistRequest.Type.DELETE_SOFT, startNanos, 2);
|
||||
iudMetrics.addBatch(PersistRequest.Type.DELETE, startNanos, 4);
|
||||
iudMetrics.addBatch(PersistRequest.Type.DELETE_PERMANENT, startNanos, 8);
|
||||
iudMetrics.addBatch(PersistRequest.Type.INSERT, startNanos, 16);
|
||||
|
||||
basic = new BasicMetricVisitor();
|
||||
iudMetrics.visit(basic);
|
||||
timed = basic.timedMetrics();
|
||||
assertThat(timed).hasSize(3);
|
||||
|
||||
assertThat(timed.get(0).count()).isEqualTo(16);
|
||||
assertThat(timed.get(0).name()).isEqualTo("iud.one.insertBatch");
|
||||
assertThat(timed.get(1).count()).isEqualTo(3);
|
||||
assertThat(timed.get(1).name()).isEqualTo("iud.one.updateBatch");
|
||||
assertThat(timed.get(2).count()).isEqualTo(12);
|
||||
assertThat(timed.get(2).name()).isEqualTo("iud.one.deleteBatch");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addNoBatch() {
|
||||
|
||||
BeanIudMetrics iudMetrics = new BeanIudMetrics("one");
|
||||
|
||||
final long startNanos = System.nanoTime() - 10000;
|
||||
iudMetrics.addNoBatch(PersistRequest.Type.INSERT, startNanos);
|
||||
iudMetrics.addNoBatch(PersistRequest.Type.UPDATE, startNanos);
|
||||
iudMetrics.addNoBatch(PersistRequest.Type.DELETE_SOFT, startNanos);
|
||||
iudMetrics.addNoBatch(PersistRequest.Type.DELETE, startNanos);
|
||||
iudMetrics.addNoBatch(PersistRequest.Type.DELETE_PERMANENT, startNanos);
|
||||
|
||||
BasicMetricVisitor basic = new BasicMetricVisitor();
|
||||
iudMetrics.visit(basic);
|
||||
|
||||
List<MetaTimedMetric> timed = basic.timedMetrics();
|
||||
assertThat(timed).hasSize(3);
|
||||
|
||||
assertThat(timed.get(0).count()).isEqualTo(1);
|
||||
assertThat(timed.get(0).name()).isEqualTo("iud.one.insert");
|
||||
assertThat(timed.get(1).count()).isEqualTo(2);
|
||||
assertThat(timed.get(1).name()).isEqualTo("iud.one.update");
|
||||
assertThat(timed.get(2).count()).isEqualTo(2);
|
||||
assertThat(timed.get(2).name()).isEqualTo("iud.one.delete");
|
||||
}
|
||||
|
||||
}
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.bean.BeanCollection;
|
||||
import io.ebean.bean.EntityBean;
|
||||
import io.ebean.common.BeanList;
|
||||
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.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;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
|
||||
public class BeanPropertyAssocManyTest extends BaseTestCase {
|
||||
|
||||
private BeanDescriptor<Customer> customerDesc = spiEbeanServer().descriptor(Customer.class);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private BeanPropertyAssocMany<Customer> contacts() {
|
||||
return (BeanPropertyAssocMany<Customer>) customerDesc.beanProperty("contacts");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createReferenceIfNull_when_notBeanCollection_expect_null() {
|
||||
|
||||
Customer customer = new Customer();
|
||||
customer.setContacts(new ArrayList<>());
|
||||
|
||||
BeanCollection<?> ref = contacts().createReferenceIfNull((EntityBean) customer);
|
||||
assertNull(ref);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createReferenceIfNull_when_null_expect_ref() {
|
||||
|
||||
Customer customer = new Customer();
|
||||
customer.setContacts(null);
|
||||
|
||||
BeanCollection<?> ref = contacts().createReferenceIfNull((EntityBean) customer);
|
||||
assertNotNull(ref);
|
||||
assertTrue(ref.isReference());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lazyLoadMany_addsToParentCollection() {
|
||||
|
||||
Customer customer = new Customer();
|
||||
customer.setContacts(new BeanList<>());
|
||||
|
||||
Contact contact = new Contact();
|
||||
contact.setCustomer(customer);
|
||||
|
||||
contacts().lazyLoadMany((EntityBean) contact);
|
||||
|
||||
assertThat(customer.getContacts()).hasSize(1);
|
||||
assertThat(customer.getContacts().get(0)).isSameAs(contact);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findIdsByParentId() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Object> customerIds = new ArrayList<>();
|
||||
customerIds.add(1L);
|
||||
customerIds.add(2L);
|
||||
|
||||
List<Object> contactIdsForOne = contacts().findIdsByParentId(1L, null, null, null, true);
|
||||
|
||||
List<Object> contactIdsForMultiple = contacts().findIdsByParentId(null, customerIds, null, null, true);
|
||||
|
||||
assertThat(contactIdsForOne).isNotEmpty();
|
||||
assertThat(contactIdsForMultiple).isNotEmpty();
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class DeployPropertyParserMapTests {
|
||||
|
||||
@Test
|
||||
public void test_like_escape() {
|
||||
|
||||
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("customer.name", "t1.name");
|
||||
map.put("id", "t0.id");
|
||||
|
||||
DeployPropertyParserMap parser = new DeployPropertyParserMap(map);
|
||||
|
||||
String output = parser.parse("(lower(customer.name) like ? escape'' or id > ?)");
|
||||
assertEquals("(lower(t1.name) like ? escape'' or t0.id > ?)", output);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.annotation.ForPlatform;
|
||||
import io.ebean.annotation.Platform;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Address;
|
||||
import org.tests.model.basic.BWithQIdent;
|
||||
import org.tests.model.basic.Customer;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
public class DeployPropertyParserTest extends BaseTestCase {
|
||||
|
||||
private final BeanDescriptor<Customer> descriptor = getBeanDescriptor(Customer.class);
|
||||
|
||||
private final BeanDescriptor<Address> addressBeanDescriptor = getBeanDescriptor(Address.class);
|
||||
|
||||
private final BeanDescriptor<BWithQIdent> bWithQIdentDescriptor = getBeanDescriptor(BWithQIdent.class);
|
||||
|
||||
@Test
|
||||
public void from_prefix_expect_unchanged() {
|
||||
assertThat(parser().parse("(select x from status join status)")).isEqualTo("(select x from status join status)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void depth0_path() {
|
||||
assertThat(parser().parse("pre status post")).isEqualTo("pre ${}status post");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void depth1_path() {
|
||||
assertThat(parser().parse("billingAddress.city")).isEqualTo("${billingAddress}city");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void depth2_path() {
|
||||
assertThat(parser().parse("max(billingAddress.country.name)")).isEqualTo("max(${billingAddress.country}name)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void simpleMax() {
|
||||
assertThat(parser().parse("max(name)")).isEqualTo("max(${}name)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void combined() {
|
||||
assertThat(parser().parse("sum(status * name)")).isEqualTo("sum(${}status * ${}name)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void combined_withAtColumn() {
|
||||
assertThat(addressParser().parse("concat(line1, line2, '-EA')")).isEqualTo("concat(${}line_1, ${}line_2, '-EA')");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withExplicitQuote_all_platforms() {
|
||||
assertThat(withQuoteParser().parse("t0.`CODE` like ?")).isEqualTo("t0.`CODE` like ?");
|
||||
assertThat(withQuoteParser().parse("t0.[CODE] like ?")).isEqualTo("t0.[CODE] like ?");
|
||||
assertThat(withQuoteParser().parse("t0.\"CODE\" like ?")).isEqualTo("t0.\"CODE\" like ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ForPlatform({Platform.H2, Platform.POSTGRES})
|
||||
public void withQuote_when_match_h2() {
|
||||
assertThat(withQuoteParser().parse("name like ?")).isEqualTo("${}\"Name\" like ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ForPlatform(Platform.SQLSERVER)
|
||||
public void withQuote_when_match_sqlserver() {
|
||||
assertThat(withQuoteParser().parse("name like ?")).isEqualTo("${}[Name] like ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ForPlatform(Platform.MYSQL)
|
||||
public void withQuote_when_match_mysql() {
|
||||
assertThat(withQuoteParser().parse("name like ?")).isEqualTo("${}`Name` like ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unknown_path() {
|
||||
assertThat(parser().parse(" foo ")).isEqualTo(" foo ");
|
||||
}
|
||||
|
||||
private DeployPropertyParser parser() {
|
||||
return descriptor.parser();
|
||||
}
|
||||
|
||||
private DeployPropertyParser addressParser() {
|
||||
return addressBeanDescriptor.parser();
|
||||
}
|
||||
|
||||
private DeployPropertyParser withQuoteParser() {
|
||||
return bWithQIdentDescriptor.parser();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
public class DetermineAggPathTest {
|
||||
|
||||
@Test
|
||||
public void path() throws Exception {
|
||||
|
||||
assertThat(DetermineAggPath.path("count(details)")).isEqualTo("details");
|
||||
assertThat(DetermineAggPath.path("count(details )")).isEqualTo("details");
|
||||
|
||||
assertThat(DetermineAggPath.path("count(person.contacts)")).isEqualTo("person.contacts");
|
||||
assertThat(DetermineAggPath.path("sum(details.quantity*details.unitPrice)")).isEqualTo("details.quantity");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void paths_simple() throws Exception {
|
||||
|
||||
DetermineAggPath.Path paths = DetermineAggPath.paths("count(details)");
|
||||
assertThat(paths.paths).isEqualTo(new String[]{"details"});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void paths_nested() throws Exception {
|
||||
|
||||
DetermineAggPath.Path paths = DetermineAggPath.paths("count(person.contacts)");
|
||||
assertThat(paths.paths).isEqualTo(new String[]{"person", "contacts"});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebeaninternal.server.query.STreeProperty;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class FormulaPropertyPathTest extends BaseTestCase {
|
||||
|
||||
private BeanDescriptor<Customer> customerDesc = getBeanDescriptor(Customer.class);
|
||||
|
||||
@Test
|
||||
public void isFormula() {
|
||||
|
||||
assertFormula("max(version)", "max", "version");
|
||||
assertFormula("min(name)", "min", "name");
|
||||
assertFormula("avg(id)", "avg", "id");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isFormula_count() {
|
||||
assertFormula("count(status)", "count", "status");
|
||||
assertFormula("count(distinct name)", "count", "name");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void concat() {
|
||||
|
||||
assertFormula("concat(name,'-end')", "concat", "name,'-end'");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void castFormula() {
|
||||
|
||||
assertFormula("concat(name,'-end')::String", "concat", "name,'-end'", "String", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cast_javaInstant() {
|
||||
|
||||
assertFormula("max(updtime)::Instant", "max", "updtime", "Instant", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void alias() {
|
||||
assertFormula("concat(name,'-end') name", "concat", "name,'-end'", null, "name");
|
||||
assertFormula("concat(name,'-end') as name", "concat", "name,'-end'", null, "name");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void castAndAlias() {
|
||||
assertFormula("concat(name,'-end')::String name", "concat", "name,'-end'", "String", "name");
|
||||
assertFormula("concat(name,'-end')::String as name", "concat", "name,'-end'", "String", "name");
|
||||
}
|
||||
|
||||
private void assertFormula(String input, String funcName, String expression) {
|
||||
assertFormula(input, funcName, expression, null, null);
|
||||
}
|
||||
|
||||
private void assertFormula(String input, String funcName, String expression, String cast, String alias) {
|
||||
|
||||
FormulaPropertyPath propertyPath = new FormulaPropertyPath(customerDesc, input, null);
|
||||
|
||||
assertThat(propertyPath.internalExpression()).isEqualTo(expression);
|
||||
assertThat(propertyPath.outerFunction()).isEqualTo(funcName);
|
||||
if (cast != null) {
|
||||
assertThat(propertyPath.cast()).isEqualTo(cast);
|
||||
} else {
|
||||
assertThat(propertyPath.cast()).isNull();
|
||||
}
|
||||
if (alias != null) {
|
||||
assertThat(propertyPath.alias()).isEqualTo(alias);
|
||||
} else {
|
||||
assertThat(propertyPath.alias()).isNull();
|
||||
}
|
||||
|
||||
assertThat(propertyPath.build()).isNotNull();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class IndexDefinitionTest {
|
||||
|
||||
private static final String[] simpleCol1 = new String[]{"one"};
|
||||
private static final String[] simpleCol2 = new String[]{"one","two"};
|
||||
private static final String[] formulaCol1 = new String[]{"lower(one)"};
|
||||
private static final String[] formulaCol2 = new String[]{"one","lower(two)"};
|
||||
|
||||
@Test
|
||||
public void isUniqueConstraint_TRUE_when_simpleMultiColumn() {
|
||||
assertTrue(new IndexDefinition(simpleCol1).isUniqueConstraint());
|
||||
assertTrue(new IndexDefinition(simpleCol2).isUniqueConstraint());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isUniqueConstraint_NOT_when_columnWithFormula() {
|
||||
assertFalse(new IndexDefinition(formulaCol1).isUniqueConstraint());
|
||||
assertFalse(new IndexDefinition(formulaCol2).isUniqueConstraint());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isUniqueConstraint_NOT_when_concurrentTrue() {
|
||||
assertFalse(new IndexDefinition(simpleCol1, "name", true, null, true, null).isUniqueConstraint());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isUniqueConstraint_NOT_when_definitionNotEmpty() {
|
||||
assertFalse(new IndexDefinition(simpleCol1, "name", true, null, false, "create index foo").isUniqueConstraint());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isUniqueConstraint_TRUE_otherwise() {
|
||||
assertTrue(new IndexDefinition(simpleCol1, "name", true, null, false, "").isUniqueConstraint());
|
||||
assertTrue(new IndexDefinition(simpleCol1, "name", true, null, false, null).isUniqueConstraint());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import io.ebeaninternal.server.deploy.meta.DeployTableJoinColumn;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TableJoinColumnTest {
|
||||
|
||||
TableJoinColumn col(String localDbColumn, String foreignDbColumn, boolean insertable, boolean updateable) {
|
||||
DeployTableJoinColumn column = new DeployTableJoinColumn(localDbColumn, foreignDbColumn, insertable, updateable);
|
||||
return new TableJoinColumn(column);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equals_when_same() {
|
||||
assertSame(col("a", "b", true, true), col("a", "b", true, true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equals_when_diffFirstCol() {
|
||||
assertDifferent(col("a", "b", true, true), col("c", "b", true, true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equals_when_diffSecondCol() {
|
||||
assertDifferent(col("a", "b", true, true), col("a", "c", true, true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equals_when_diffInsertable() {
|
||||
assertDifferent(col("a", "b", true, true), col("a", "b", false, true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equals_when_diffUpdateable() {
|
||||
assertDifferent(col("a", "b", true, true), col("a", "b", true, false));
|
||||
}
|
||||
|
||||
private void assertDifferent(TableJoinColumn col, TableJoinColumn col2) {
|
||||
assertThat(col).isNotEqualTo(col2);
|
||||
assertThat(col.hashCode()).isNotEqualTo(col2.hashCode());
|
||||
}
|
||||
|
||||
private void assertSame(TableJoinColumn col, TableJoinColumn col2) {
|
||||
assertThat(col).isEqualTo(col2);
|
||||
assertThat(col.hashCode()).isEqualTo(col2.hashCode());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
|
||||
import io.ebeaninternal.server.deploy.meta.DeployTableJoin;
|
||||
import io.ebeaninternal.server.deploy.meta.DeployTableJoinColumn;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TableJoinTest {
|
||||
|
||||
DeployTableJoinColumn col(String localDbColumn, String foreignDbColumn, boolean insertable, boolean updateable) {
|
||||
return new DeployTableJoinColumn(localDbColumn, foreignDbColumn, insertable, updateable);
|
||||
}
|
||||
|
||||
TableJoin table(String tableName, String col1, String col2) {
|
||||
DeployTableJoin deploy = new DeployTableJoin();
|
||||
deploy.setTable(tableName);
|
||||
deploy.addJoinColumn(col(col1, col2, true, true));
|
||||
return new TableJoin(deploy);
|
||||
}
|
||||
|
||||
TableJoin table(String tableName, String col1, String col2, String col3, String col4) {
|
||||
DeployTableJoin deploy = new DeployTableJoin();
|
||||
deploy.setTable(tableName);
|
||||
deploy.addJoinColumn(col(col1, col2, true, true));
|
||||
deploy.addJoinColumn(col(col3, col4, true, true));
|
||||
return new TableJoin(deploy);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equals_when_same() {
|
||||
assertSame(table("myTable", "a", "b"), table("myTable", "a", "b"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equals_when_diffTable() {
|
||||
assertDifferent(table("myTable", "a", "b"), table("diffTable", "a", "b"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equals_when_diffColumn() {
|
||||
assertDifferent(table("myTable", "a", "b"), table("myTable", "c", "b"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equals_when_moreColumns() {
|
||||
assertDifferent(table("myTable", "a", "b"), table("myTable", "a", "b", "c", "d"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equals_when_lessColumns() {
|
||||
assertDifferent(table("myTable", "a", "b", "c", "d"), table("myTable", "a", "b"));
|
||||
}
|
||||
|
||||
void assertSame(TableJoin join1, TableJoin join2) {
|
||||
assertThat(join1).isEqualTo(join1);
|
||||
assertThat(join1.hashCode()).isEqualTo(join1.hashCode());
|
||||
}
|
||||
|
||||
|
||||
void assertDifferent(TableJoin join1, TableJoin join2) {
|
||||
assertThat(join1).isNotEqualTo(join2);
|
||||
assertThat(join1.hashCode()).isNotEqualTo(join2.hashCode());
|
||||
}
|
||||
|
||||
}
|
||||
+121
@@ -0,0 +1,121 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import io.ebean.bean.EntityBean;
|
||||
import io.ebean.bean.EntityBeanIntercept;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.composite.RCustomer;
|
||||
import org.tests.model.composite.RCustomerKey;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.embedded.UserInterestLive;
|
||||
import org.tests.model.embedded.UserInterestLiveKey;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestBeanDescriptorHasIdProperty extends BaseTestCase {
|
||||
|
||||
SpiEbeanServer spiServer;
|
||||
|
||||
public TestBeanDescriptorHasIdProperty() {
|
||||
Database server = DB.getDefault();
|
||||
spiServer = (SpiEbeanServer) server;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHasId() {
|
||||
|
||||
BeanDescriptor<Order> beanDescriptor = spiServer.descriptor(Order.class);
|
||||
assertNotNull(beanDescriptor.idProperty());
|
||||
assertEquals("id", beanDescriptor.idProperty().name());
|
||||
|
||||
assertNotNull(beanDescriptor.versionProperty());
|
||||
assertEquals("updtime", beanDescriptor.versionProperty().name());
|
||||
|
||||
Order order = new Order();
|
||||
|
||||
assertFalse(beanDescriptor.hasIdValue(entityBean(order)));
|
||||
assertFalse(beanDescriptor.hasVersionProperty(getIntercept(order)));
|
||||
|
||||
order.setId(23);
|
||||
order.setUpdtime(new Timestamp(System.currentTimeMillis()));
|
||||
|
||||
assertTrue(beanDescriptor.hasIdValue(entityBean(order)));
|
||||
assertTrue(beanDescriptor.hasVersionProperty(getIntercept(order)));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsReference() {
|
||||
|
||||
BeanDescriptor<Customer> beanDescriptor = spiServer.descriptor(Customer.class);
|
||||
|
||||
Customer order = new Customer();
|
||||
EntityBeanIntercept ebi = getIntercept(order);
|
||||
assertFalse(beanDescriptor.referenceIdPropertyOnly(ebi));
|
||||
|
||||
order.setId(23);
|
||||
assertTrue(beanDescriptor.referenceIdPropertyOnly(ebi));
|
||||
|
||||
order.setName("custName");
|
||||
assertFalse(beanDescriptor.referenceIdPropertyOnly(ebi));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isReference_withGeneratedOnInsertOnlyProperty_expect_false() {
|
||||
BeanDescriptor<UserInterestLive> descriptor = spiServer.descriptor(UserInterestLive.class);
|
||||
UserInterestLive bean = new UserInterestLive(new UserInterestLiveKey(1L, 2L));
|
||||
EntityBeanIntercept ebi = getIntercept(bean);
|
||||
assertFalse(descriptor.referenceIdPropertyOnly(ebi));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_getIdForJson() {
|
||||
|
||||
BeanDescriptor<Order> orderDesc = spiServer.descriptor(Order.class);
|
||||
|
||||
Order order = new Order();
|
||||
order.setId(42);
|
||||
assertEquals(42, orderDesc.idForJson(order));
|
||||
|
||||
assertEquals(42, orderDesc.convertIdFromJson(42));
|
||||
assertEquals(42, orderDesc.convertIdFromJson("42"));
|
||||
assertEquals(42, orderDesc.convertIdFromJson(42L));
|
||||
|
||||
|
||||
RCustomerKey key = new RCustomerKey();
|
||||
key.setCompany("comp");
|
||||
key.setName("fred");
|
||||
|
||||
RCustomer rCustomer = new RCustomer();
|
||||
rCustomer.setKey(key);
|
||||
|
||||
BeanDescriptor<RCustomer> rcustDesc = spiServer.descriptor(RCustomer.class);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> idForJson = (Map<String, Object>) rcustDesc.idForJson(rCustomer);
|
||||
assertEquals("comp", idForJson.get("company"));
|
||||
assertEquals("fred", idForJson.get("name"));
|
||||
assertEquals(2, idForJson.size());
|
||||
|
||||
RCustomerKey keyVal = (RCustomerKey) rcustDesc.convertIdFromJson(idForJson);
|
||||
assertEquals("comp", keyVal.getCompany());
|
||||
assertEquals("fred", keyVal.getName());
|
||||
|
||||
}
|
||||
|
||||
private EntityBean entityBean(Object bean) {
|
||||
return (EntityBean) bean;
|
||||
}
|
||||
|
||||
private EntityBeanIntercept getIntercept(Object bean) {
|
||||
return ((EntityBean) bean)._ebean_getIntercept();
|
||||
}
|
||||
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.bean.BeanCollection;
|
||||
import io.ebean.bean.EntityBean;
|
||||
import io.ebean.bean.EntityBeanIntercept;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Contact;
|
||||
import org.tests.model.basic.Customer;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestCollectionLoadedStatus extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
SpiEbeanServer server = (SpiEbeanServer) DB.getDefault();
|
||||
BeanDescriptor<Customer> custDesc = server.descriptor(Customer.class);
|
||||
|
||||
Customer customer = new Customer();
|
||||
EntityBean eb = (EntityBean) customer;
|
||||
EntityBeanIntercept ebi = eb._ebean_getIntercept();
|
||||
|
||||
BeanProperty contactsProperty = custDesc.beanProperty("contacts");
|
||||
assertFalse(ebi.isLoadedProperty(contactsProperty.propertyIndex()));
|
||||
|
||||
Object contactsViaInternal = contactsProperty.getValue(eb);
|
||||
assertNull(contactsViaInternal);
|
||||
assertFalse(ebi.isLoadedProperty(contactsProperty.propertyIndex()));
|
||||
|
||||
List<Contact> contacts = customer.getContacts();
|
||||
assertNotNull(contacts);
|
||||
assertTrue(contacts instanceof BeanCollection);
|
||||
assertTrue(ebi.isLoadedProperty(contactsProperty.propertyIndex()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import io.ebean.bean.EntityBean;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import io.ebeaninternal.api.json.SpiJsonWriter;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.Order.Status;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.sql.Date;
|
||||
import java.sql.Timestamp;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
public class TestDiffHelpInsertSimple extends BaseTestCase {
|
||||
|
||||
private final long firstTime = System.currentTimeMillis() - 10000;
|
||||
|
||||
private final Database server;
|
||||
|
||||
private final BeanDescriptor<Order> orderDesc;
|
||||
|
||||
public TestDiffHelpInsertSimple() {
|
||||
server = DB.getDefault();
|
||||
SpiEbeanServer spiServer = (SpiEbeanServer) server;
|
||||
orderDesc = spiServer.descriptor(Order.class);
|
||||
}
|
||||
|
||||
private Order createBaseOrder(Database server) {
|
||||
Order order1 = new Order();
|
||||
order1.setId(12);
|
||||
order1.setCretime(new Timestamp(firstTime));
|
||||
order1.setCustomer(server.reference(Customer.class, 1234));
|
||||
order1.setStatus(Status.NEW);
|
||||
//order1.setShipDate(new Date(firstTime));
|
||||
order1.setOrderDate(new Date(firstTime));
|
||||
return order1;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void basic() throws IOException {
|
||||
|
||||
Date date = Date.valueOf("2000-01-01");
|
||||
|
||||
Order order1 = createBaseOrder(server);
|
||||
order1.setOrderDate(date);
|
||||
|
||||
StringWriter buffer = new StringWriter();
|
||||
|
||||
SpiJsonWriter jsonWriter = spiEbeanServer().jsonExtended().createJsonWriter(buffer);
|
||||
|
||||
orderDesc.jsonWriteForInsert(jsonWriter, (EntityBean) order1);
|
||||
jsonWriter.flush();
|
||||
|
||||
String asJson = buffer.toString();
|
||||
|
||||
assertThat(asJson).startsWith("{\"status\":\"NEW\",\"orderDate\":\"2000-01-01\"");
|
||||
assertThat(asJson).endsWith(",\"customer\":{\"id\":1234}}");
|
||||
}
|
||||
|
||||
}
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import io.ebean.bean.EntityBean;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import io.ebeaninternal.api.json.SpiJsonWriter;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.embedded.EMain;
|
||||
import org.tests.model.embedded.Eembeddable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestDiffHelpInsertWithEmbedded extends BaseTestCase {
|
||||
|
||||
private final BeanDescriptor<EMain> emainDesc;
|
||||
|
||||
public TestDiffHelpInsertWithEmbedded() {
|
||||
Database server = DB.getDefault();
|
||||
SpiEbeanServer spiServer = (SpiEbeanServer) server;
|
||||
emainDesc = spiServer.descriptor(EMain.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void simple() throws IOException {
|
||||
|
||||
EMain emain1 = createEMain();
|
||||
|
||||
String asJson = asInsertJson((EntityBean) emain1);
|
||||
|
||||
assertThat(asJson).contains("{\"name\":\"foo\",\"version\":13,\"embeddable\":{\"description\":\"bar\"}}");
|
||||
|
||||
}
|
||||
|
||||
private String asInsertJson(EntityBean emain1) throws IOException {
|
||||
StringWriter buffer = new StringWriter();
|
||||
|
||||
SpiJsonWriter jsonWriter = spiEbeanServer().jsonExtended().createJsonWriter(buffer);
|
||||
|
||||
emainDesc.jsonWriteForInsert(jsonWriter, emain1);
|
||||
jsonWriter.flush();
|
||||
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void scalarPropertyAsNull() throws IOException {
|
||||
|
||||
EMain emain1 = createEMain();
|
||||
emain1.setName(null);
|
||||
|
||||
String asJson = asInsertJson((EntityBean) emain1);
|
||||
|
||||
assertThat(asJson).contains("{\"version\":13,\"embeddable\":{\"description\":\"bar\"}}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void embeddedAsNull() throws IOException {
|
||||
|
||||
EMain emain1 = createEMain();
|
||||
emain1.setEmbeddable(null);
|
||||
|
||||
String asJson = asInsertJson((EntityBean) emain1);
|
||||
|
||||
assertThat(asJson).contains("{\"name\":\"foo\",\"version\":13}");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void embeddedPropertiesAsNull() throws IOException {
|
||||
|
||||
EMain emain1 = createEMain();
|
||||
emain1.getEmbeddable().setDescription(null);
|
||||
|
||||
String asJson = asInsertJson((EntityBean) emain1);
|
||||
|
||||
assertThat(asJson).contains("{\"name\":\"foo\",\"version\":13,\"embeddable\":{}}");
|
||||
}
|
||||
|
||||
private EMain createEMain() {
|
||||
EMain emain = new EMain();
|
||||
emain.setName("foo");
|
||||
emain.setVersion(13L);
|
||||
|
||||
Eembeddable embeddable = new Eembeddable();
|
||||
embeddable.setDescription("bar");
|
||||
emain.setEmbeddable(embeddable);
|
||||
return emain;
|
||||
}
|
||||
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.bean.EntityBean;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.mappedsuper.ASimpleBean;
|
||||
import org.tests.model.mappedsuper.NotEnhancedMappedSuper;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestNotEnhancedMappedSuper extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void simpleBean_mappedSuperNotEnhanced_ok() {
|
||||
|
||||
ASimpleBean bean = new ASimpleBean();
|
||||
bean.setName("junk");
|
||||
|
||||
DB.save(bean);
|
||||
assertNotNull(bean.getId());
|
||||
|
||||
bean.setName("junk mod");
|
||||
DB.save(bean);
|
||||
|
||||
ASimpleBean fetched = DB.find(ASimpleBean.class, bean.getId());
|
||||
assertEquals("junk mod", fetched.getName());
|
||||
assertEquals(bean.getName(), fetched.getName());
|
||||
|
||||
DB.delete(bean);
|
||||
ASimpleBean fetched2 = DB.find(ASimpleBean.class, bean.getId());
|
||||
assertNull(fetched2);
|
||||
|
||||
NotEnhancedMappedSuper mappedSuper = new NotEnhancedMappedSuper();
|
||||
assertTrue((mappedSuper instanceof EntityBean));
|
||||
}
|
||||
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.BeanState;
|
||||
import io.ebean.DB;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
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 TestReferenceWithConstructorProperties extends BaseTestCase {
|
||||
|
||||
/**
|
||||
* Test when constructor sets some properties.
|
||||
*/
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
Order order = DB.reference(Order.class, 1);
|
||||
|
||||
BeanState beanState = DB.beanState(order);
|
||||
Set<String> loadedProps = beanState.loadedProps();
|
||||
|
||||
assertEquals(1, loadedProps.size());
|
||||
assertTrue(beanState.isReference());
|
||||
|
||||
// read the status invokes lazy loading
|
||||
order.getStatus();
|
||||
|
||||
assertFalse(beanState.isReference());
|
||||
assertNotNull(order.getCustomer());
|
||||
}
|
||||
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package io.ebeaninternal.server.deploy.generatedproperty;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.bean.EntityBean;
|
||||
import io.ebeaninternal.server.deploy.BeanProperty;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
|
||||
public class GeneratedCounterLongTest extends BaseTestCase {
|
||||
|
||||
private GeneratedCounterLong counter = new GeneratedCounterLong();
|
||||
|
||||
private BeanProperty version = getBeanDescriptor(Customer.class).beanProperty("version");
|
||||
|
||||
@Test
|
||||
public void when_null_expect_IllegalStateException() {
|
||||
Customer customer = new Customer();
|
||||
assertThrows(IllegalStateException.class, () -> counter.getUpdateValue(version, (EntityBean)customer, System.currentTimeMillis()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_set_expect_incremented() {
|
||||
Customer customer = new Customer();
|
||||
customer.setVersion(7L);
|
||||
Object value = counter.getUpdateValue(version, (EntityBean) customer, System.currentTimeMillis());
|
||||
|
||||
assertThat(value).isEqualTo(8L);
|
||||
}
|
||||
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package io.ebeaninternal.server.deploy.generatedproperty;
|
||||
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class GeneratedInsertJavaTimeTest {
|
||||
|
||||
@Test
|
||||
public void test_generatedOnInsert() {
|
||||
|
||||
GeneratedProperty gen = new GeneratedInsertJavaTime.InstantDT();
|
||||
assertTrue(GeneratedWhenCreated.class.isInstance(gen));
|
||||
assertTrue(gen instanceof GeneratedWhenCreated);
|
||||
}
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
package io.ebeaninternal.server.deploy.generatedproperty;
|
||||
|
||||
import io.ebean.config.ClassLoadConfig;
|
||||
import io.ebeaninternal.server.deploy.meta.DeployBeanProperty;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.OffsetDateTime;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class InsertTimestampFactoryTest {
|
||||
|
||||
InsertTimestampFactory factory = new InsertTimestampFactory(new ClassLoadConfig());
|
||||
|
||||
@Test
|
||||
public void test_createdTimestamp_Instant() {
|
||||
|
||||
DeployBeanProperty prop = new DeployBeanProperty(null, Instant.class, null, null);
|
||||
|
||||
GeneratedProperty insertTimestamp = factory.createInsertTimestamp(prop);
|
||||
Object value = insertTimestamp.getInsertValue(null, null, System.currentTimeMillis());
|
||||
assertTrue(value instanceof Instant);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_createdTimestamp_LocalDateTime() {
|
||||
|
||||
DeployBeanProperty prop = new DeployBeanProperty(null, LocalDateTime.class, null, null);
|
||||
|
||||
GeneratedProperty insertTimestamp = factory.createInsertTimestamp(prop);
|
||||
Object value = insertTimestamp.getInsertValue(null, null, System.currentTimeMillis());
|
||||
assertTrue(value instanceof LocalDateTime);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_createdTimestamp_OffsetDateTime() {
|
||||
|
||||
DeployBeanProperty prop = new DeployBeanProperty(null, OffsetDateTime.class, null, null);
|
||||
|
||||
GeneratedProperty insertTimestamp = factory.createInsertTimestamp(prop);
|
||||
Object value = insertTimestamp.getInsertValue(null, null, System.currentTimeMillis());
|
||||
assertTrue(value instanceof OffsetDateTime);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_createdTimestamp_Timestamp() {
|
||||
|
||||
DeployBeanProperty prop = new DeployBeanProperty(null, Timestamp.class, null, null);
|
||||
|
||||
GeneratedProperty insertTimestamp = factory.createInsertTimestamp(prop);
|
||||
Object value = insertTimestamp.getInsertValue(null, null, System.currentTimeMillis());
|
||||
assertTrue(value instanceof Timestamp);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_createdTimestamp_utilDate() {
|
||||
|
||||
DeployBeanProperty prop = new DeployBeanProperty(null, java.util.Date.class, null, null);
|
||||
|
||||
GeneratedProperty insertTimestamp = factory.createInsertTimestamp(prop);
|
||||
Object value = insertTimestamp.getInsertValue(null, null, System.currentTimeMillis());
|
||||
assertTrue(value instanceof java.util.Date);
|
||||
}
|
||||
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
package io.ebeaninternal.server.deploy.generatedproperty;
|
||||
|
||||
import io.ebean.config.ClassLoadConfig;
|
||||
import io.ebeaninternal.server.deploy.meta.DeployBeanProperty;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.OffsetDateTime;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class UpdateTimestampFactoryTest {
|
||||
|
||||
|
||||
UpdateTimestampFactory factory = new UpdateTimestampFactory(new ClassLoadConfig());
|
||||
|
||||
@Test
|
||||
public void test_createdTimestamp_Instant() {
|
||||
|
||||
DeployBeanProperty prop = new DeployBeanProperty(null, Instant.class, null, null);
|
||||
|
||||
GeneratedProperty generatedProperty = factory.createUpdateTimestamp(prop);
|
||||
Object value = generatedProperty.getInsertValue(null, null, System.currentTimeMillis());
|
||||
assertTrue(value instanceof Instant);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_createdTimestamp_LocalDateTime() {
|
||||
|
||||
DeployBeanProperty prop = new DeployBeanProperty(null, LocalDateTime.class, null, null);
|
||||
|
||||
GeneratedProperty generatedProperty = factory.createUpdateTimestamp(prop);
|
||||
Object value = generatedProperty.getInsertValue(null, null, System.currentTimeMillis());
|
||||
assertTrue(value instanceof LocalDateTime);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_createdTimestamp_OffsetDateTime() {
|
||||
|
||||
DeployBeanProperty prop = new DeployBeanProperty(null, OffsetDateTime.class, null, null);
|
||||
|
||||
GeneratedProperty generatedProperty = factory.createUpdateTimestamp(prop);
|
||||
Object value = generatedProperty.getInsertValue(null, null, System.currentTimeMillis());
|
||||
assertTrue(value instanceof OffsetDateTime);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_createdTimestamp_Timestamp() {
|
||||
|
||||
DeployBeanProperty prop = new DeployBeanProperty(null, Timestamp.class, null, null);
|
||||
|
||||
GeneratedProperty generatedProperty = factory.createUpdateTimestamp(prop);
|
||||
Object value = generatedProperty.getInsertValue(null, null, System.currentTimeMillis());
|
||||
assertTrue(value instanceof Timestamp);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_createdTimestamp_utilDate() {
|
||||
|
||||
DeployBeanProperty prop = new DeployBeanProperty(null, java.util.Date.class, null, null);
|
||||
|
||||
GeneratedProperty generatedProperty = factory.createUpdateTimestamp(prop);
|
||||
Object value = generatedProperty.getInsertValue(null, null, System.currentTimeMillis());
|
||||
assertTrue(value instanceof java.util.Date);
|
||||
}
|
||||
}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
package io.ebeaninternal.server.deploy.parse;
|
||||
|
||||
import io.ebean.config.DatabaseConfig;
|
||||
import io.ebean.config.dbplatform.sqlserver.SqlServer17Platform;
|
||||
import io.ebeaninternal.server.core.bootup.BootupClasses;
|
||||
import io.ebeaninternal.server.deploy.generatedproperty.GeneratedPropertyFactory;
|
||||
import io.ebeaninternal.server.deploy.meta.DeployBeanDescriptor;
|
||||
import io.ebeaninternal.server.type.DefaultTypeManager;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class AnnotationClassTest {
|
||||
|
||||
@Test
|
||||
public void convertColumnNames_when_AllQuotedIdentifiersIsTrue() {
|
||||
|
||||
AnnotationClass annotationClass = createAnnotationClass(sqlServerPlatform(true));
|
||||
|
||||
String[] colNames = {"Col1", "Col2"};
|
||||
|
||||
final String[] columnNames = annotationClass.convertColumnNames(colNames);
|
||||
|
||||
assertThat(columnNames.length).isEqualTo(2);
|
||||
assertThat(columnNames[0]).isEqualTo("[Col1]");
|
||||
assertThat(columnNames[1]).isEqualTo("[Col2]");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertColumnNames_when_AllQuotedIdentifiersIsFalse() {
|
||||
|
||||
AnnotationClass annotationClass = createAnnotationClass(sqlServerPlatform(false));
|
||||
|
||||
String[] colNames = {"Col1", "`Col2`", "col3"};
|
||||
|
||||
final String[] columnNames = annotationClass.convertColumnNames(colNames);
|
||||
|
||||
assertThat(columnNames.length).isEqualTo(3);
|
||||
assertThat(columnNames[0]).isEqualTo("Col1");
|
||||
assertThat(columnNames[1]).isEqualTo("[Col2]");
|
||||
assertThat(columnNames[2]).isEqualTo("col3");
|
||||
}
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
private AnnotationClass createAnnotationClass(DatabaseConfig config) {
|
||||
DeployUtil deployUtil = new DeployUtil(new DefaultTypeManager(config, new BootupClasses()), config);
|
||||
|
||||
DeployBeanInfo deployBeanInfo = new DeployBeanInfo(deployUtil, new DeployBeanDescriptor<>(null, null, null));
|
||||
ReadAnnotationConfig readAnnotationConfig = new ReadAnnotationConfig(new GeneratedPropertyFactory(true, new DatabaseConfig(), Collections.emptyList()), "","", new DatabaseConfig());
|
||||
return new AnnotationClass(deployBeanInfo, readAnnotationConfig);
|
||||
}
|
||||
|
||||
private DatabaseConfig sqlServerPlatform(boolean allQuotedIdentifiers) {
|
||||
SqlServer17Platform sqlServer17Platform = new SqlServer17Platform();
|
||||
DatabaseConfig config = new DatabaseConfig();
|
||||
config.setDatabasePlatform(sqlServer17Platform);
|
||||
config.setAllQuotedIdentifiers(allQuotedIdentifiers);
|
||||
|
||||
sqlServer17Platform.configure(config.getPlatformConfig());
|
||||
return config;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
package io.ebeaninternal.server.dto;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class DtoMetaBuilderTest {
|
||||
|
||||
@Test
|
||||
public void includeMethod() {
|
||||
Map<String, Method> methods = getIncludedMethodsFor(D0.class);
|
||||
|
||||
assertThat(methods).hasSize(2);
|
||||
assertThat(methods.get("setName")).isNotNull();
|
||||
assertThat(methods.get("setId")).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void includeMethod_when_notStrictlySetters() {
|
||||
Map<String, Method> methods = getIncludedMethodsFor(D1.class);
|
||||
|
||||
assertThat(methods).hasSize(3);
|
||||
assertThat(methods.get("setNameThen")).isNotNull();
|
||||
assertThat(methods.get("setIdFor")).isNotNull();
|
||||
assertThat(methods.get("setI")).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void propertyType() {
|
||||
Map<String, Method> methods = getIncludedMethodsFor(D0.class);
|
||||
|
||||
assertThat(methods).hasSize(2);
|
||||
assertThat(DtoMetaBuilder.propertyType(methods.get("setName"))).isEqualTo(String.class);
|
||||
assertThat(DtoMetaBuilder.propertyType(methods.get("setId"))).isEqualTo(long.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void propertyName() {
|
||||
|
||||
assertThat(DtoMetaBuilder.propertyName("setName")).isEqualTo("name");
|
||||
assertThat(DtoMetaBuilder.propertyName("setId")).isEqualTo("id");
|
||||
assertThat(DtoMetaBuilder.propertyName("setI")).isEqualTo("i");
|
||||
assertThat(DtoMetaBuilder.propertyName("setfoo")).isEqualTo("foo");
|
||||
}
|
||||
|
||||
|
||||
private Map<String, Method> getIncludedMethodsFor(Class<?> cls) {
|
||||
Map<String,Method> included = new HashMap<>();
|
||||
for (Method method : cls.getMethods()) {
|
||||
if (DtoMetaBuilder.includeMethod(method)) {
|
||||
included.put(method.getName(), method);
|
||||
}
|
||||
}
|
||||
return included;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
static class D0 {
|
||||
private String name;
|
||||
private long id;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void setNamePlus(String name, long id) {
|
||||
this.name = name;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public static void setFoo(String foo) {
|
||||
}
|
||||
|
||||
protected void setProtected(String foo) {
|
||||
}
|
||||
|
||||
private void setPrivate(String foo) {
|
||||
}
|
||||
|
||||
private void setPackage(String foo) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
static class D1 {
|
||||
|
||||
public void setNameThen(String name) {
|
||||
|
||||
}
|
||||
|
||||
public void setIdFor(long id) {
|
||||
|
||||
}
|
||||
|
||||
public void setI(long val) {
|
||||
|
||||
}
|
||||
|
||||
public void set(long val) {
|
||||
|
||||
}
|
||||
|
||||
public D1 setA(long val) {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
+144
@@ -0,0 +1,144 @@
|
||||
package io.ebeaninternal.server.executor;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.MDC;
|
||||
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class DefaultBackgroundExecutorTest {
|
||||
|
||||
@Test
|
||||
public void submit_callable() throws Exception {
|
||||
|
||||
DefaultBackgroundExecutor es = new DefaultBackgroundExecutor(1, 2, "test");
|
||||
|
||||
final Future<String> future0 = es.submit(() -> "Hello");
|
||||
final Future<String> future1 = es.submit(() -> "There");
|
||||
final Future<String> future2 = es.submit(() -> {
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
return "Slow";
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
return "Interrupted";
|
||||
}
|
||||
});
|
||||
|
||||
es.shutdown();
|
||||
|
||||
assertThat(future0.get()).isEqualTo("Hello");
|
||||
assertThat(future1.get(1, TimeUnit.SECONDS)).isEqualTo("There");
|
||||
assertThat(future2.get()).isEqualTo("Slow");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shutdown_slowCallable_expect_interrupted() throws Exception {
|
||||
|
||||
int shutdownWaitSecs = 1;
|
||||
DefaultBackgroundExecutor es = new DefaultBackgroundExecutor(1, shutdownWaitSecs, "test");
|
||||
|
||||
final Future<String> future2 = es.submit(() -> {
|
||||
try {
|
||||
Thread.sleep(1500); // longer than shutdown wait
|
||||
return "Slow";
|
||||
} catch (InterruptedException e) {
|
||||
// expected for this test
|
||||
Thread.currentThread().interrupt();
|
||||
return "Interrupted";
|
||||
}
|
||||
});
|
||||
|
||||
// shutdown waits max shutdownWaitSecs seconds for active tasks
|
||||
es.shutdown();
|
||||
assertThat(future2.get()).isEqualTo("Interrupted");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled("test takes long time")
|
||||
public void shutdown_when_running_expect_waitAndNiceShutdown() {
|
||||
|
||||
DefaultBackgroundExecutor es = new DefaultBackgroundExecutor(1, 20, "test");
|
||||
|
||||
es.execute(new RunFor(3000, "a"));
|
||||
es.execute(new RunFor(3000, "b"));
|
||||
es.execute(new RunFor(3000, "c"));
|
||||
|
||||
es.shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled("test takes long time")
|
||||
public void shutdown_when_rougeRunnable_expect_InterruptedException() {
|
||||
|
||||
DefaultBackgroundExecutor es = new DefaultBackgroundExecutor(1, 10, "test");
|
||||
|
||||
es.execute(new RunFor(300000, "a"));
|
||||
es.execute(new RunFor(3000, "b"));
|
||||
es.execute(new RunFor(3000, "c"));
|
||||
|
||||
es.shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void wrapWithNoMDC() {
|
||||
DefaultBackgroundExecutor es = new DefaultBackgroundExecutor(1, 10, "test");
|
||||
assertThat(MDC.getCopyOfContextMap()).isNull();
|
||||
es.wrapMDC(() -> {
|
||||
assertThat(MDC.getCopyOfContextMap()).isNull();
|
||||
});
|
||||
es.wrapMDC(() -> {
|
||||
assertThat(MDC.getCopyOfContextMap()).isNull();
|
||||
return "Callable";
|
||||
});
|
||||
es.shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void wrapWithMDC_expect_() {
|
||||
DefaultBackgroundExecutor es = new DefaultBackgroundExecutor(1, 10, "test");
|
||||
MDC.clear();
|
||||
MDC.put("hello", "there");
|
||||
es.wrapMDC(() -> {
|
||||
assertThat(MDC.get("hello")).isEqualTo("there");
|
||||
});
|
||||
es.wrapMDC(() -> {
|
||||
assertThat(MDC.get("hello")).isEqualTo("there");
|
||||
return "Callable";
|
||||
});
|
||||
es.execute(() -> {
|
||||
assertThat(MDC.get("hello")).isEqualTo("there");
|
||||
});
|
||||
es.submit(() -> {
|
||||
assertThat(MDC.get("hello")).isEqualTo("there");
|
||||
return "Callable";
|
||||
});
|
||||
MDC.clear();
|
||||
es.shutdown();
|
||||
}
|
||||
|
||||
private static class RunFor implements Runnable {
|
||||
|
||||
final long wait;
|
||||
final String id;
|
||||
|
||||
RunFor(long wait, String id) {
|
||||
this.wait = wait;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
System.out.println("start " + id);
|
||||
Thread.sleep(wait);
|
||||
System.out.println("done " + id);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
package io.ebeaninternal.server.executor;
|
||||
|
||||
import io.ebean.BackgroundExecutor;
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
|
||||
public class TestShutdownWithBackgroundTasks extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void test() {
|
||||
|
||||
Database server = DB.getDefault();
|
||||
final BackgroundExecutor bg = server.backgroundExecutor();
|
||||
try {
|
||||
for (int i = 0; i < 12; i++) {
|
||||
bg.execute(new Job(server, 500, i));
|
||||
}
|
||||
|
||||
Thread.sleep(1000);
|
||||
server.shutdown();
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static class Job implements Runnable {
|
||||
|
||||
final Database server;
|
||||
final long sleepMillis;
|
||||
final int position;
|
||||
|
||||
Job(Database server, long sleepMillis, int position) {
|
||||
this.server = server;
|
||||
this.sleepMillis = sleepMillis;
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
System.out.println(position + " sleep " + sleepMillis);
|
||||
Thread.sleep(sleepMillis);
|
||||
server.find(Customer.class).findCount();
|
||||
System.out.println(position + " sleep done");
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+115
@@ -0,0 +1,115 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class AllEqualsExpressionTest extends BaseExpressionTest {
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_same() {
|
||||
|
||||
same(exp("a", 10), exp("a", 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_sameMultiple() {
|
||||
|
||||
assertThat(exp("a", 10, "b", "23").isSameByBind(exp("a", 10, "b", "23"))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffBindValue() {
|
||||
|
||||
same(exp("a", 10), exp("a", 20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_multiple() {
|
||||
|
||||
same(exp("a", 10, "b", 20, "c", 30), exp("a", 10, "b", 20, "c", 30));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_less() {
|
||||
|
||||
different(exp("a", 10, "b", 20, "c", 30), exp("a", 10, "b", 20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_more() {
|
||||
|
||||
different(exp("a", 10, "b", 20), exp("a", 10, "b", 20, "c", 30));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffProperty_diff() {
|
||||
|
||||
different(exp("a", 10), exp("b", 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffType_diff() {
|
||||
|
||||
different(exp("a", 10), new NoopExpression());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffBindByNull_last() {
|
||||
|
||||
different(exp("a", 10), exp("a", null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffBindByNull_first() {
|
||||
|
||||
different(exp("a", null), exp("a", 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_differentExpressionType() {
|
||||
|
||||
different(exp("a", null), new NoopExpression());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffBindByNull_last() {
|
||||
|
||||
assertThat(exp("a", 10).isSameByBind(exp("a", null))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffBindByNull_first() {
|
||||
|
||||
assertThat(exp("a", null).isSameByBind(exp("a", 10))).isFalse();
|
||||
}
|
||||
|
||||
private AllEqualsExpression exp(Map<String, Object> propMap) {
|
||||
return new AllEqualsExpression(propMap);
|
||||
}
|
||||
|
||||
AllEqualsExpression exp(String key0, Object val1) {
|
||||
LinkedHashMap<String, Object> map = new LinkedHashMap<>();
|
||||
map.put(key0, val1);
|
||||
return exp(map);
|
||||
}
|
||||
|
||||
|
||||
AllEqualsExpression exp(String key0, Object val1, String key2, Object val2) {
|
||||
LinkedHashMap<String, Object> map = new LinkedHashMap<>();
|
||||
map.put(key0, val1);
|
||||
map.put(key2, val2);
|
||||
return exp(map);
|
||||
}
|
||||
|
||||
AllEqualsExpression exp(String key0, Object val1, String key2, Object val2, String key3, Object val3) {
|
||||
LinkedHashMap<String, Object> map = new LinkedHashMap<>();
|
||||
map.put(key0, val1);
|
||||
map.put(key2, val2);
|
||||
map.put(key3, val3);
|
||||
return exp(map);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import io.ebean.*;
|
||||
import io.ebean.event.BeanQueryRequest;
|
||||
import io.ebeaninternal.api.SpiExpression;
|
||||
import io.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public abstract class BaseExpressionTest extends BaseTestCase {
|
||||
|
||||
protected DefaultExpressionRequest newExpressionRequest() {
|
||||
BeanDescriptor<Order> desc = getBeanDescriptor(Order.class);
|
||||
return new DefaultExpressionRequest(desc);
|
||||
}
|
||||
|
||||
protected String hash(SpiExpression expression) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (expression != null) {
|
||||
expression.queryPlanHash(sb);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
protected void same(SpiExpression one, SpiExpression two) {
|
||||
assertThat(hash(one)).isEqualTo(hash(two));
|
||||
}
|
||||
|
||||
protected void different(SpiExpression one, SpiExpression two) {
|
||||
assertThat(hash(one)).isNotEqualTo(hash(two));
|
||||
}
|
||||
|
||||
/**
|
||||
* Request with Multi-Value support.
|
||||
*/
|
||||
protected InExpressionTest.TDQueryRequest<Customer> multi() {
|
||||
return MULTI_VALUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Request with NO Multi-Value support.
|
||||
*/
|
||||
protected InExpressionTest.TDQueryRequest<Customer> noMulti() {
|
||||
return NO_MULTI_VALUE;
|
||||
}
|
||||
|
||||
|
||||
private static final TDQueryRequest<Customer> MULTI_VALUE = new TDQueryRequest<>(true);
|
||||
private static final TDQueryRequest<Customer> NO_MULTI_VALUE = new TDQueryRequest<>(false);
|
||||
|
||||
static class TDQueryRequest<T> implements BeanQueryRequest<T> {
|
||||
|
||||
final boolean supported;
|
||||
|
||||
TDQueryRequest(boolean supported) {
|
||||
this.supported = supported;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EbeanServer getEbeanServer() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Transaction transaction() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Query<T> query() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPadInExpression() {
|
||||
return supported;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMultiValueIdSupported() {
|
||||
return supported;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMultiValueSupported(Class<?> valueType) {
|
||||
return supported;
|
||||
}
|
||||
}
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import io.ebeaninternal.api.SpiExpression;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class BetweenExpressionTest extends BaseExpressionTest {
|
||||
|
||||
@Test
|
||||
public void addSql() {
|
||||
|
||||
DefaultExpressionRequest expReq = newExpressionRequest();
|
||||
|
||||
BetweenExpression exp = new BetweenExpression("startDate", 1, 2);
|
||||
exp.addSql(expReq);
|
||||
|
||||
assertThat(expReq.getSql()).isEqualTo("startDate between ? and ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void copyForPlanKey_isSameInstance() {
|
||||
|
||||
BetweenExpression exp = new BetweenExpression("startDate", 1, 2);
|
||||
SpiExpression other = exp.copyForPlanKey();
|
||||
|
||||
assertThat(exp).isSameAs(other);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_properties_match() {
|
||||
|
||||
BetweenExpression exp0 = new BetweenExpression("startDate", 1, 2);
|
||||
BetweenExpression exp1 = new BetweenExpression("startDate", 3, 4);
|
||||
|
||||
same(exp0, exp1);
|
||||
same(exp1, exp0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_properties_do_not_match() {
|
||||
|
||||
BetweenExpression exp0 = new BetweenExpression("startDate", 1, 2);
|
||||
BetweenExpression exp1 = new BetweenExpression("endDate", 1, 2);
|
||||
|
||||
different(exp0, exp1);
|
||||
different(exp1, exp0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_values_do_not_match() {
|
||||
|
||||
BetweenExpression exp0 = new BetweenExpression("startDate", 1, 2);
|
||||
BetweenExpression exp1 = new BetweenExpression("startDate", 1, 3);
|
||||
|
||||
assertThat(exp0.isSameByBind(exp1)).isFalse();
|
||||
assertThat(exp1.isSameByBind(exp0)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_values_match() {
|
||||
|
||||
BetweenExpression exp0 = new BetweenExpression("startDate", 1, 2);
|
||||
BetweenExpression exp1 = new BetweenExpression("startDate", 1, 2);
|
||||
|
||||
assertThat(exp0.isSameByBind(exp1)).isTrue();
|
||||
assertThat(exp1.isSameByBind(exp0)).isTrue();
|
||||
}
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class BetweenPropertyExpressionTest extends BaseExpressionTest {
|
||||
|
||||
private BetweenPropertyExpression exp(String lowProperty, String highProperty, Object value) {
|
||||
return new BetweenPropertyExpression(lowProperty, highProperty, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sqlExpression() {
|
||||
TDSpiExpressionRequest request = new TDSpiExpressionRequest(null);
|
||||
exp("a", "b", 10).addSql(request);
|
||||
assertThat(request.getSql()).isEqualTo(" ? between a and b");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_same() {
|
||||
same(exp("a", "b", 10), exp("a", "b", 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffValue() {
|
||||
same(exp("a", "b", 10), exp("a", "b", 20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffProperty() {
|
||||
different(exp("a", "b", 10), exp("a", "c", 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffExpressionType() {
|
||||
different(exp("a", "b", 10), new NoopExpression());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_same() {
|
||||
assertThat(exp("a", "b", 10).isSameByBind(exp("a", "b", 10))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diff() {
|
||||
assertThat(exp("a", "b", 10).isSameByBind(exp("a", "b", 20))).isFalse();
|
||||
}
|
||||
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class CaseInsensitiveEqualExpressionTest extends BaseExpressionTest {
|
||||
|
||||
CaseInsensitiveEqualExpression exp(String propName, String value) {
|
||||
return new CaseInsensitiveEqualExpression(propName, value, false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_same() {
|
||||
|
||||
same(exp("a", "10"), exp("a", "10"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffBind_same() {
|
||||
|
||||
same(exp("a", "10"), exp("a", "20"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffProperty_diff() {
|
||||
|
||||
different(exp("a", "10"), exp("b", "10"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffType_diff() {
|
||||
|
||||
different(exp("a", "10"), new NoopExpression());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_sameBindValues() {
|
||||
|
||||
assertThat(exp("a", "10").isSameByBind(exp("b", "10"))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffBindValues() {
|
||||
|
||||
assertThat(exp("a", "10").isSameByBind(exp("b", "20"))).isFalse();
|
||||
}
|
||||
|
||||
}
|
||||
+141
@@ -0,0 +1,141 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import io.ebean.LikeType;
|
||||
import io.ebean.Query;
|
||||
import io.ebean.bean.EntityBean;
|
||||
import io.ebean.event.BeanQueryRequest;
|
||||
import io.ebeaninternal.api.ManyWhereJoins;
|
||||
import io.ebeaninternal.api.SpiQuery;
|
||||
import io.ebeaninternal.server.core.OrmQueryRequest;
|
||||
import io.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
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 static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
public class DefaultExampleExpressionTest extends BaseExpressionTest {
|
||||
|
||||
|
||||
Customer customer() {
|
||||
Address address = new Address();
|
||||
address.setCity("billingAddress.city");
|
||||
|
||||
Customer customer = new Customer();
|
||||
customer.setName("name");
|
||||
customer.setBillingAddress(address);
|
||||
return customer;
|
||||
}
|
||||
|
||||
DefaultExampleExpression exp() {
|
||||
Customer customer = customer();
|
||||
return new DefaultExampleExpression((EntityBean) customer, false, LikeType.EQUAL_TO);
|
||||
}
|
||||
|
||||
DefaultExampleExpression expExtra() {
|
||||
Customer customer = customer();
|
||||
customer.setSmallnote("smallNote");
|
||||
return new DefaultExampleExpression((EntityBean) customer, false, LikeType.EQUAL_TO);
|
||||
}
|
||||
|
||||
DefaultExampleExpression expDiffName() {
|
||||
Customer customer = customer();
|
||||
customer.setName("otherName");
|
||||
return new DefaultExampleExpression((EntityBean) customer, false, LikeType.EQUAL_TO);
|
||||
}
|
||||
|
||||
BeanDescriptor<Customer> customerBeanDescriptor() {
|
||||
return getBeanDescriptor(Customer.class);
|
||||
}
|
||||
|
||||
DefaultExampleExpression prepare(DefaultExampleExpression expr) {
|
||||
|
||||
SpiQuery<Customer> query = (SpiQuery<Customer>) spiEbeanServer().find(Customer.class);
|
||||
BeanQueryRequest<?> request = create(query);
|
||||
expr.containsMany(customerBeanDescriptor(), new ManyWhereJoins());
|
||||
expr.prepareExpression(request);
|
||||
return expr;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
DefaultExampleExpression expr = exp();
|
||||
|
||||
prepare(expr);
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
expr.queryPlanHash(builder);
|
||||
|
||||
TDSpiExpressionRequest req = new TDSpiExpressionRequest(customerBeanDescriptor());
|
||||
expr.addBindValues(req);
|
||||
|
||||
assertThat(req.bindValues).contains("name", "billingAddress.city");
|
||||
|
||||
Customer customer = customer();
|
||||
customer.setName("Rob");
|
||||
customer.getBillingAddress().setCity("Auckland");
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query1 = server().find(Customer.class)
|
||||
.where().exampleLike(customer)
|
||||
.query();
|
||||
|
||||
query1.findList();
|
||||
|
||||
assertThat(query1.getGeneratedSql()).contains("(t0.name like ");
|
||||
assertThat(query1.getGeneratedSql()).contains(" and t1.city like ");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void emptyExampleBean() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Customer customer = new Customer();
|
||||
|
||||
Query<Customer> query = server().find(Customer.class)
|
||||
.where()
|
||||
.eq("status", Customer.Status.NEW)
|
||||
.exampleLike(customer)
|
||||
.startsWith("name", "Rob")
|
||||
.query();
|
||||
|
||||
query.findList();
|
||||
|
||||
assertThat(sqlOf(query)).contains("and 1=1 and t0.name like");
|
||||
}
|
||||
|
||||
private <T> OrmQueryRequest<T> create(SpiQuery<T> query) {
|
||||
return new OrmQueryRequest<>(null, null, query, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_whenSame() {
|
||||
|
||||
same(prepare(exp()), prepare(exp()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffBindValue_stillSame() {
|
||||
|
||||
same(prepare(exp()), prepare(expDiffName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_extraExpression_then_different() {
|
||||
|
||||
different(prepare(exp()), prepare(expExtra()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_lessExpression_then_different() {
|
||||
|
||||
different(prepare(expExtra()), prepare(exp()));
|
||||
}
|
||||
|
||||
}
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import io.ebean.Expression;
|
||||
import io.ebeaninternal.api.SpiExpression;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
public class DefaultExpressionFactoryTest {
|
||||
|
||||
private String toQueryPlanHash(Expression expression) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
((SpiExpression) expression).queryPlanHash(sb);
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLowerILike() {
|
||||
|
||||
DefaultExpressionFactory factory = new DefaultExpressionFactory(false, false);
|
||||
Expression expression = factory.ilike("name", "foo");
|
||||
assertThat(expression).isInstanceOf(LikeExpression.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNativeILike() {
|
||||
|
||||
DefaultExpressionFactory factory = new DefaultExpressionFactory(false, true);
|
||||
Expression expression = factory.ilike("name", "foo");
|
||||
assertThat(expression).isInstanceOf(NativeILikeExpression.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEq() {
|
||||
|
||||
DefaultExpressionFactory factory = new DefaultExpressionFactory(false, false);
|
||||
Expression expression = factory.eq("name", null);
|
||||
assertThat(expression).isInstanceOf(NullExpression.class);
|
||||
assertThat(toQueryPlanHash(expression)).isEqualTo("Null[name]");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNe() {
|
||||
|
||||
DefaultExpressionFactory factory = new DefaultExpressionFactory(false, false);
|
||||
Expression expression = factory.ne("name", null);
|
||||
assertThat(expression).isInstanceOf(NullExpression.class);
|
||||
assertThat(toQueryPlanHash(expression)).isEqualTo("NotNull[name]");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIeq() {
|
||||
|
||||
DefaultExpressionFactory factory = new DefaultExpressionFactory(false, false);
|
||||
Expression expression = factory.ieq("name", null);
|
||||
assertThat(expression).isInstanceOf(NullExpression.class);
|
||||
assertThat(toQueryPlanHash(expression)).isEqualTo("Null[name]");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIne() {
|
||||
|
||||
DefaultExpressionFactory factory = new DefaultExpressionFactory(false, false);
|
||||
Expression expression = factory.ine("name", null);
|
||||
assertThat(expression).isInstanceOf(NullExpression.class);
|
||||
assertThat(toQueryPlanHash(expression)).isEqualTo("NotNull[name]");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEq_with_equalsWithNullAsNoop() {
|
||||
|
||||
DefaultExpressionFactory factory = new DefaultExpressionFactory(true, false);
|
||||
Expression expression = factory.eq("name", null);
|
||||
assertThat(expression).isInstanceOf(NoopExpression.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNe_with_equalsWithNullAsNoop() {
|
||||
|
||||
DefaultExpressionFactory factory = new DefaultExpressionFactory(true, false);
|
||||
Expression expression = factory.ne("name", null);
|
||||
assertThat(expression).isInstanceOf(NoopExpression.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIeq_with_equalsWithNullAsNoop() {
|
||||
|
||||
DefaultExpressionFactory factory = new DefaultExpressionFactory(true, false);
|
||||
Expression expression = factory.ieq("name", null);
|
||||
assertThat(expression).isInstanceOf(NoopExpression.class);
|
||||
}
|
||||
}
|
||||
+98
@@ -0,0 +1,98 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import io.ebean.ExpressionList;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class DefaultExpressionListTest extends BaseExpressionTest {
|
||||
|
||||
|
||||
DefaultExpressionList<?> exp() {
|
||||
|
||||
return new DefaultExpressionList<>(null, new DefaultExpressionFactory(true, true), null);
|
||||
}
|
||||
|
||||
private <T> DefaultExpressionList<T> spi(ExpressionList<T> list) {
|
||||
return (DefaultExpressionList<T>) list;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_same() {
|
||||
|
||||
same(spi(exp().eq("a", 10).eq("b", 20))
|
||||
,spi(exp().eq("a", 10).eq("b", 20)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffExpressionType() {
|
||||
|
||||
different(spi(exp().eq("a", 10).eq("b", 20))
|
||||
,new NoopExpression());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_less() {
|
||||
|
||||
different(spi(exp().eq("a", 10).eq("b", 20))
|
||||
,spi(exp().eq("a", 10)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_lessEmptyLast() {
|
||||
|
||||
different(spi(exp().eq("a", 10).eq("b", 20))
|
||||
,spi(exp()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_lessEmptyFirst() {
|
||||
|
||||
different(spi(exp())
|
||||
,spi(exp().eq("a", 10)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_more() {
|
||||
|
||||
different(spi(exp().eq("a", 10).eq("b", 20))
|
||||
,spi(exp().eq("a", 10).eq("b", 20).eq("c", 30)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffProperties() {
|
||||
|
||||
different(spi(exp().eq("a", 10).eq("b", 20))
|
||||
,spi(exp().eq("c", 10).eq("b", 20)));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_same() {
|
||||
|
||||
assertThat(spi(exp().eq("a", 10).eq("b", 20))
|
||||
.isSameByBind(spi(exp().eq("a", 10).eq("b", 20)))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffValues() {
|
||||
|
||||
assertThat(spi(exp().eq("a", 10).eq("b", 20))
|
||||
.isSameByBind(spi(exp().eq("a", 10).eq("b", 30)))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_less() {
|
||||
|
||||
assertThat(spi(exp().eq("a", 10))
|
||||
.isSameByBind(spi(exp().eq("a", 10).eq("b", 20)))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_more() {
|
||||
|
||||
assertThat(spi(exp().eq("a", 10))
|
||||
.isSameByBind(spi(exp().eq("a", 10).eq("b", 20)))).isFalse();
|
||||
}
|
||||
|
||||
}
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ExistsQueryExpressionTest extends BaseExpressionTest {
|
||||
|
||||
|
||||
private ExistsQueryExpression exp(boolean not, String sql, Object... bindValues) {
|
||||
return new ExistsQueryExpression(not, sql, Arrays.asList(bindValues));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_same() {
|
||||
|
||||
same(exp(true, "a", 10), exp(true, "a", 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffBind_same() {
|
||||
|
||||
same(exp(true, "a", 10), exp(true, "a", 20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffNot() {
|
||||
|
||||
different(exp(true, "a", 10), exp(false, "a", 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffSql() {
|
||||
|
||||
different(exp(true, "a", 10), exp(true, "b", 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_sameBindValues() {
|
||||
|
||||
same(exp(true, "a", 10), exp(true, "a", 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_sameMultipleBindValues() {
|
||||
|
||||
same(exp(true, "a", 10, "ABC", 20), exp(true, "a", 10, "ABC", 20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffMultipleBindValues() {
|
||||
|
||||
assertThat(exp(true, "a", 10, "ABC", 20).isSameByBind(exp(true, "a", 10, "ABC", 21))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffMultipleBindValuesByOrder() {
|
||||
|
||||
assertThat(exp(true, "a", 10, "ABC", 20).isSameByBind(exp(true, "a", 10, 20, "ABC"))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffBindValues() {
|
||||
|
||||
assertThat(exp(true, "a", 10).isSameByBind(exp(true, "a", 20))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_lessBindValues() {
|
||||
|
||||
assertThat(exp(true, "a", 10, 20).isSameByBind(exp(true, "a", 20))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_moreBindValues() {
|
||||
|
||||
assertThat(exp(true, "a", 10).isSameByBind(exp(true, "a", 10, 20))).isFalse();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class IdExpressionTest extends BaseExpressionTest {
|
||||
|
||||
|
||||
private IdExpression exp(Object value) {
|
||||
return new IdExpression(value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_same() {
|
||||
|
||||
same(exp(10), exp(10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffBind_same() {
|
||||
|
||||
same(exp(10), exp(20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_sameBindValues() {
|
||||
|
||||
assertThat(exp(10).isSameByBind(exp(10))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffBindValues() {
|
||||
|
||||
assertThat(exp(10).isSameByBind(exp(20))).isFalse();
|
||||
assertThat(exp(10).isSameByBind(exp("junk"))).isFalse();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class IdInExpressionTest extends BaseExpressionTest {
|
||||
|
||||
|
||||
private IdInExpression exp(Object... values) {
|
||||
return new IdInExpression(Arrays.asList(values));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_same() {
|
||||
|
||||
same(exp(10), exp(10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffBind_same() {
|
||||
|
||||
same(exp(10), exp(20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffBindCount_notPrepared() {
|
||||
|
||||
different(exp(10), exp(10, 20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_sameBindValues() {
|
||||
|
||||
assertThat(exp(10).isSameByBind(exp(10))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_mulitpleSameBindValues() {
|
||||
|
||||
assertThat(exp(10, "ABC", 20).isSameByBind(exp(10, "ABC", 20))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffBindValues() {
|
||||
|
||||
assertThat(exp(10).isSameByBind(exp("junk"))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_lessBindValues() {
|
||||
|
||||
assertThat(exp(10, "ABC", 20).isSameByBind(exp(10, "ABC"))).isFalse();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_moreBindValues() {
|
||||
|
||||
assertThat(exp(10, "ABC").isSameByBind(exp(10, "ABC", 30))).isFalse();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,185 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class InExpressionTest extends BaseExpressionTest {
|
||||
|
||||
@Test
|
||||
public void queryPlanHash_given_diffEmpty_should_differentPlanHash() {
|
||||
|
||||
List<Integer> emptyValues = values();
|
||||
|
||||
InExpression ex1 = new InExpression("foo", emptyValues, false, true);
|
||||
InExpression ex2 = new InExpression("foo", emptyValues, false);
|
||||
InExpression ex3 = new InExpression("foo", emptyValues, false, true);
|
||||
InExpression ex4 = new InExpression("foo", null, false, true);
|
||||
|
||||
ex1.prepareExpression(multi());
|
||||
ex2.prepareExpression(multi());
|
||||
|
||||
different(ex1, ex2);
|
||||
same(ex1, ex3); // same empty
|
||||
same(ex1, ex4); // same null
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryPlanHash_given_diffPropertyName_should_differentPlanHash() {
|
||||
|
||||
List<Integer> values = values(42, 92);
|
||||
|
||||
InExpression ex1 = new InExpression("foo", values, false);
|
||||
InExpression ex2 = new InExpression("bar", values, false);
|
||||
|
||||
ex1.prepareExpression(multi());
|
||||
ex2.prepareExpression(multi());
|
||||
|
||||
different(ex1, ex2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryPlanHash_given_diffBindCount_should_differentPlanHash() {
|
||||
|
||||
List<Integer> values1 = values(42, 92);
|
||||
List<Integer> values2 = values(42, 92, 82);
|
||||
|
||||
InExpression ex1 = new InExpression("foo", values1, false);
|
||||
InExpression ex2 = new InExpression("foo", values2, false);
|
||||
|
||||
ex1.prepareExpression(noMulti());
|
||||
ex2.prepareExpression(noMulti());
|
||||
different(ex1, ex2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryPlanHash_given_diffBindCount_withMultiSupport_samePlanHash() {
|
||||
|
||||
List<Integer> values1 = values(42, 92);
|
||||
List<Integer> values2 = values(42, 92, 82);
|
||||
|
||||
InExpression ex1 = new InExpression("foo", values1, false);
|
||||
InExpression ex2 = new InExpression("foo", values2, false);
|
||||
|
||||
ex1.prepareExpression(multi());
|
||||
ex2.prepareExpression(multi());
|
||||
same(ex1, ex2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryPlanHash_given_diffNotFlag_should_differentPlanHash() {
|
||||
|
||||
List<Integer> values = values(42, 92);
|
||||
|
||||
InExpression ex1 = new InExpression("foo", values, true);
|
||||
InExpression ex2 = new InExpression("foo", values, false);
|
||||
|
||||
ex1.prepareExpression(multi());
|
||||
ex2.prepareExpression(multi());
|
||||
|
||||
different(ex1, ex2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryPlanHash_given_sameNotFlag_should_samePlanHash() {
|
||||
|
||||
List<Integer> values = values(42, 92);
|
||||
|
||||
InExpression ex1 = new InExpression("foo", values, true);
|
||||
InExpression ex2 = new InExpression("foo", values, true);
|
||||
|
||||
ex1.prepareExpression(multi());
|
||||
ex2.prepareExpression(multi());
|
||||
|
||||
same(ex1, ex2);
|
||||
}
|
||||
|
||||
private List<Integer> values(int... vals) {
|
||||
ArrayList<Integer> list = new ArrayList<>();
|
||||
for (int val : vals) {
|
||||
list.add(val);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
private InExpression exp(String propName, boolean not, Object... values) {
|
||||
InExpression ex = new InExpression(propName, Arrays.asList(values), not);
|
||||
ex.prepareExpression(multi());
|
||||
return ex;
|
||||
}
|
||||
|
||||
private InExpression expNoMulti(String propName, boolean not, Object... values) {
|
||||
InExpression ex = new InExpression(propName, Arrays.asList(values), not);
|
||||
ex.prepareExpression(noMulti());
|
||||
return ex;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_same() {
|
||||
|
||||
same(exp("a", false, 10), exp("a", false, 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffPropertyName() {
|
||||
|
||||
different(exp("a", false, 10), exp("b", false, 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffNot() {
|
||||
|
||||
different(exp("a", false, 10), exp("a", true, 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffBind_same() {
|
||||
|
||||
same(exp("a", false, 10), exp("a", false, 10, 20));
|
||||
different(expNoMulti("a", false, 10), expNoMulti("a", false, 10, 20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffBindCount() {
|
||||
|
||||
same(exp("a", false, 10), exp("a", false, 10, 20));
|
||||
different(expNoMulti("a", false, 10), expNoMulti("a", false, 10, 20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_sameBindValues() {
|
||||
|
||||
assertThat(exp("a", false, 10).isSameByBind(exp("a", false, 10))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_sameMultipleBindValues() {
|
||||
|
||||
assertThat(exp("a", false, 10, "ABC", 20).isSameByBind(exp("a", false, 10, "ABC", 20))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffBindValues() {
|
||||
|
||||
assertThat(exp("a", false, 10).isSameByBind(exp("a", false, "foo"))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_lessBindValues() {
|
||||
|
||||
assertThat(exp("a", false, 10, "ABC", 20).isSameByBind(exp("a", false, 10, "ABC"))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_moreBindValues() {
|
||||
|
||||
assertThat(exp("a", false, 10, "ABC").isSameByBind(exp("a", false, 10, "ABC", 30))).isFalse();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import io.ebean.Pairs;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class InPairsExpressionTest extends BaseExpressionTest {
|
||||
|
||||
private Pairs pairs() {
|
||||
return pairs("sku", "code");
|
||||
}
|
||||
|
||||
private Pairs pairs(String property0, String property1) {
|
||||
Pairs pairs = new Pairs(property0, property1)
|
||||
.add("2", 1000)
|
||||
.add("2", 1001)
|
||||
.add("3", 1000);
|
||||
|
||||
return pairs;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void same_samePlan_sameBind() throws Exception {
|
||||
|
||||
InPairsExpression e0 = new InPairsExpression(pairs(), false);
|
||||
InPairsExpression e1 = new InPairsExpression(pairs(), false);
|
||||
|
||||
same(e0, e1);
|
||||
|
||||
assertTrue(e0.isSameByBind(e1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void same_samePlan_diffBind() throws Exception {
|
||||
|
||||
InPairsExpression e0 = new InPairsExpression(pairs(), false);
|
||||
InPairsExpression e1 = new InPairsExpression(pairs().add("4", 1000), false);
|
||||
e0.prepareExpression(multi());
|
||||
e1.prepareExpression(multi());
|
||||
|
||||
// when multi() ... same as bind count not important
|
||||
same(e0, e1);
|
||||
assertFalse(e0.isSameByBind(e1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void same_noMulti_diffPlan() throws Exception {
|
||||
|
||||
InPairsExpression e0 = new InPairsExpression(pairs(), false);
|
||||
InPairsExpression e1 = new InPairsExpression(pairs().add("4", 1000), false);
|
||||
e0.prepareExpression(noMulti());
|
||||
e1.prepareExpression(noMulti());
|
||||
|
||||
// when noMulti() ... bind count different so different plan
|
||||
different(e0, e1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void diffProperty0_diff() throws Exception {
|
||||
|
||||
InPairsExpression e0 = new InPairsExpression(pairs(), false);
|
||||
InPairsExpression e1 = new InPairsExpression(pairs("k", "code"), false);
|
||||
different(e0, e1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void diffProperty1_diff() throws Exception {
|
||||
|
||||
InPairsExpression e0 = new InPairsExpression(pairs(), false);
|
||||
InPairsExpression e1 = new InPairsExpression(pairs("sku", "c"), false);
|
||||
different(e0, e1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void diffSeparator_diff() throws Exception {
|
||||
|
||||
InPairsExpression e0 = new InPairsExpression(pairs(), false);
|
||||
InPairsExpression e1 = new InPairsExpression(pairs().setConcatSeparator(":"), false);
|
||||
different(e0, e1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void diffSuffix_diff() throws Exception {
|
||||
|
||||
InPairsExpression e0 = new InPairsExpression(pairs(), false);
|
||||
InPairsExpression e1 = new InPairsExpression(pairs().setConcatSuffix(":"), false);
|
||||
different(e0, e1);
|
||||
}
|
||||
|
||||
}
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class InQueryExpressionTest extends BaseExpressionTest {
|
||||
|
||||
|
||||
private InQueryExpression exp(String propertyName, boolean not, String sql, Object... bindValues) {
|
||||
return new InQueryExpression(propertyName, not, sql, Arrays.asList(bindValues));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_same() {
|
||||
|
||||
same(exp("name", true, "sql", 10), exp("name", true, "sql", 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffBind_same() {
|
||||
|
||||
same(exp("name", true, "sql", 10), exp("name", true, "sql", 20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffNPropertyName() {
|
||||
|
||||
different(exp("name", true, "sql", 10), exp("nameDiff", true, "sql", 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffNot() {
|
||||
|
||||
different(exp("name", true, "sql", 10), exp("name", false, "sql", 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffSql() {
|
||||
|
||||
different(exp("name", true, "sql", 10), exp("name", true, "sqlDiff", 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_sameBindValues() {
|
||||
|
||||
assertThat(exp("name", true, "sql", 10).isSameByBind(exp("name", true, "sql", 10))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_sameMultipleBindValues() {
|
||||
|
||||
assertThat(exp("name", true, "sql", 10, "ABC", 20).isSameByBind(exp("name", true, "sql", 10, "ABC", 20))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffMultipleBindValues() {
|
||||
|
||||
assertThat(exp("name", true, "sql", 10, "ABC", 20).isSameByBind(exp("name", true, "sql", 10, "ABC", 21))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffMultipleBindValuesByOrder() {
|
||||
|
||||
assertThat(exp("name", true, "sql", 10, "ABC", 20).isSameByBind(exp("name", true, "sql", 10, 20, "ABC"))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffBindValues() {
|
||||
|
||||
assertThat(exp("name", true, "sql", 10).isSameByBind(exp("name", true, "sql", 20))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_lessBindValues() {
|
||||
|
||||
assertThat(exp("name", true, "sql", 10, 20).isSameByBind(exp("name", true, "sql", 20))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_moreBindValues() {
|
||||
|
||||
assertThat(exp("name", true, "sql", 10).isSameByBind(exp("name", true, "sql", 10, 20))).isFalse();
|
||||
}
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import io.ebeaninternal.api.SpiExpression;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class InRangeExpressionTest extends BaseExpressionTest {
|
||||
|
||||
@Test
|
||||
public void addSql() {
|
||||
|
||||
DefaultExpressionRequest expReq = newExpressionRequest();
|
||||
|
||||
InRangeExpression exp = new InRangeExpression("startDate", 1, 2);
|
||||
exp.addSql(expReq);
|
||||
|
||||
assertThat(expReq.getSql()).isEqualTo("(startDate >= ? and startDate < ?)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void copyForPlanKey_isSameInstance() {
|
||||
|
||||
InRangeExpression exp = new InRangeExpression("startDate", 1, 2);
|
||||
SpiExpression other = exp.copyForPlanKey();
|
||||
|
||||
assertThat(exp).isSameAs(other);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_properties_match() {
|
||||
|
||||
InRangeExpression exp0 = new InRangeExpression("startDate", 1, 2);
|
||||
InRangeExpression exp1 = new InRangeExpression("startDate", 3, 4);
|
||||
|
||||
same(exp0, exp1);
|
||||
same(exp1, exp0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_properties_do_not_match() {
|
||||
|
||||
InRangeExpression exp0 = new InRangeExpression("startDate", 1, 2);
|
||||
InRangeExpression exp1 = new InRangeExpression("endDate", 1, 2);
|
||||
|
||||
different(exp0, exp1);
|
||||
different(exp1, exp0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_values_do_not_match() {
|
||||
|
||||
InRangeExpression exp0 = new InRangeExpression("startDate", 1, 2);
|
||||
InRangeExpression exp1 = new InRangeExpression("startDate", 1, 3);
|
||||
|
||||
assertThat(exp0.isSameByBind(exp1)).isFalse();
|
||||
assertThat(exp1.isSameByBind(exp0)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_values_match() {
|
||||
|
||||
InRangeExpression exp0 = new InRangeExpression("startDate", 1, 2);
|
||||
InRangeExpression exp1 = new InRangeExpression("startDate", 1, 2);
|
||||
|
||||
assertThat(exp0.isSameByBind(exp1)).isTrue();
|
||||
assertThat(exp1.isSameByBind(exp0)).isTrue();
|
||||
}
|
||||
}
|
||||
+151
@@ -0,0 +1,151 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
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 org.tests.model.nofk.EUserNoFk;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class IsEmptyExpressionQueryTest extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void isEmpty() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = DB.find(Customer.class)
|
||||
.select("id")
|
||||
.where().isEmpty("contacts")
|
||||
.query();
|
||||
|
||||
query.findList();
|
||||
assertThat(sqlOf(query)).contains("select t0.id from o_customer t0 where not exists (select 1 from contact x where x.customer_id = t0.id");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteQuery_isEmpty() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
DB.find(EUserNoFk.class)
|
||||
.where().isEmpty("files")
|
||||
.delete();
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(1);
|
||||
|
||||
if (isPlatformSupportsDeleteTableAlias()) {
|
||||
assertSql(sql.get(0)).contains("delete from euser_no_fk t0 where not exists (select 1 from efile_no_fk x where x.owner_user_id = t0.user_id)");
|
||||
} else if (isMySql()) {
|
||||
assertSql(sql.get(0)).contains("delete t0 from euser_no_fk t0 where not exists (select 1 from efile_no_fk x where x.owner_user_id = t0.user_id)");
|
||||
} else {
|
||||
assertSql(sql.get(0)).contains("delete from euser_no_fk where not exists (select 1 from efile_no_fk x where x.owner_user_id = user_id)");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNotEmpty() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = DB.find(Customer.class)
|
||||
.select("id")
|
||||
.where().isNotEmpty("contacts")
|
||||
.query();
|
||||
|
||||
query.findList();
|
||||
assertThat(sqlOf(query)).contains("select t0.id from o_customer t0 where exists (select 1 from contact x where x.customer_id = t0.id");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isEmpty_contacts() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Contact> query = DB.find(Contact.class)
|
||||
.select("id")
|
||||
.where().isEmpty("notes")
|
||||
.query();
|
||||
|
||||
query.findList();
|
||||
assertThat(sqlOf(query)).contains("select t0.id from contact t0 where not exists (select 1 from contact_note x where x.contact_id = t0.id");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNotEmpty_contacts() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Contact> query = DB.find(Contact.class)
|
||||
.select("id")
|
||||
.where().isNotEmpty("notes")
|
||||
.query();
|
||||
|
||||
query.findList();
|
||||
assertThat(sqlOf(query)).contains("select t0.id from contact t0 where exists (select 1 from contact_note x where x.contact_id = t0.id");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void isEmpty_manyToMany() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Contact> query = DB.find(Contact.class)
|
||||
.select("id")
|
||||
.where().isEmpty("notes")
|
||||
.query();
|
||||
|
||||
query.findList();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void isEmpty_nested() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = DB.find(Customer.class)
|
||||
.select("id")
|
||||
.where().isEmpty("contacts.notes")
|
||||
.query();
|
||||
|
||||
query.findList();
|
||||
if (isPostgres()) {
|
||||
assertThat(sqlOf(query)).contains("select distinct on (t0.id) t0.id from o_customer t0 join contact u1 on u1.customer_id = t0.id where not exists (select 1 from contact_note x where x.contact_id = u1.id)");
|
||||
|
||||
} else {
|
||||
assertThat(sqlOf(query)).contains("select distinct t0.id from o_customer t0 join contact u1 on u1.customer_id = t0.id where not exists (select 1 from contact_note x where x.contact_id = u1.id)");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNotEmpty_nested() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = DB.find(Customer.class)
|
||||
.select("id")
|
||||
.where().isNotEmpty("contacts.notes")
|
||||
.query();
|
||||
|
||||
query.findList();
|
||||
if (isPostgres()) {
|
||||
assertThat(sqlOf(query)).contains("select distinct on (t0.id) t0.id from o_customer t0 join contact u1 on u1.customer_id = t0.id where exists (select 1 from contact_note x where x.contact_id = u1.id)");
|
||||
|
||||
} else {
|
||||
assertThat(sqlOf(query)).contains("select distinct t0.id from o_customer t0 join contact u1 on u1.customer_id = t0.id where exists (select 1 from contact_note x where x.contact_id = u1.id)");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class JsonPathExpressionTest extends BaseExpressionTest {
|
||||
|
||||
private JsonPathExpression exp(String propertyName, String path, Op operator, Object value) {
|
||||
return new JsonPathExpression(propertyName, path, operator, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_same() {
|
||||
|
||||
same(exp("a", "path", Op.EQ, 10), exp("a", "path", Op.EQ, 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffBind_same() {
|
||||
|
||||
same(exp("a", "path", Op.EQ, 10), exp("a", "path", Op.EQ, 20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffPath() {
|
||||
|
||||
different(exp("a", "path", Op.EQ, 10), exp("a", "pathDiff", Op.EQ, 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffProperty_diff() {
|
||||
|
||||
different(exp("a", "path", Op.EQ, 10), exp("b", "path", Op.EQ, 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffOperator_diff() {
|
||||
|
||||
different(exp("a", "path", Op.EQ, 10), exp("a", "path", Op.LT, 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffType_diff() {
|
||||
|
||||
different(exp("a", "path", Op.EQ, 10), new NoopExpression());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_sameBindValues() {
|
||||
|
||||
assertThat(exp("a", "path", Op.EQ, 10).isSameByBind(exp("a", "path", Op.LT, 10))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_sameNullBindValues() {
|
||||
|
||||
assertThat(exp("a", "path", Op.EQ, null).isSameByBind(exp("a", "path", Op.LT, null))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffBindValues() {
|
||||
|
||||
assertThat(exp("a", "path", Op.EQ, 10).isSameByBind(exp("a", "path", Op.EQ, "junk"))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffFirstNullBindValues() {
|
||||
|
||||
assertThat(exp("a", "path", Op.EQ, null).isSameByBind(exp("a", "path", Op.LT, 10))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffLastNullBindValues() {
|
||||
|
||||
assertThat(exp("a", "path", Op.EQ, 10).isSameByBind(exp("a", "path", Op.LT, null))).isFalse();
|
||||
}
|
||||
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import io.ebean.Expr;
|
||||
import io.ebean.Expression;
|
||||
import io.ebean.Junction;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class JunctionExpressionTest extends BaseExpressionTest {
|
||||
|
||||
Expression eq(String propName, int value) {
|
||||
return Expr.eq(propName, value);
|
||||
}
|
||||
|
||||
|
||||
DefaultExpressionList<?> exp(Expression... expressions) {
|
||||
|
||||
DefaultExpressionList<Object> list = new DefaultExpressionList<>(null, new DefaultExpressionFactory(true, false), null);
|
||||
for (Expression ex : expressions) {
|
||||
list.add(ex);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
<T> JunctionExpression<T> and(DefaultExpressionList<T> list) {
|
||||
return new JunctionExpression<>(Junction.Type.AND, list);
|
||||
}
|
||||
|
||||
<T> JunctionExpression<T> or(DefaultExpressionList<T> list) {
|
||||
return new JunctionExpression<>(Junction.Type.OR, list);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_same() {
|
||||
|
||||
same(and(exp(eq("a", 10), eq("b", 10))),
|
||||
and(exp(eq("a", 10), eq("b", 10))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void copyForPlanKey_isSameByPlan_when_same() {
|
||||
|
||||
same(and(exp(eq("a", 10), eq("b", 10)).copyForPlanKey()),
|
||||
and(exp(eq("a", 10), eq("b", 10))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void copyForPlanKey_isSameByPlan_when_diff() {
|
||||
|
||||
different(and(exp(eq("a", 10), eq("b", 10)).copyForPlanKey()),
|
||||
and(exp(eq("a", 10), eq("c", 10))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffType() {
|
||||
|
||||
different(and(exp(eq("a", 10), eq("b", 10))),
|
||||
or(exp(eq("a", 10), eq("b", 10))));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import io.ebean.LikeType;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class LikeExpressionTest extends BaseExpressionTest {
|
||||
|
||||
|
||||
private LikeExpression exp(String propertyName, String value, boolean caseInsensitive, LikeType type) {
|
||||
return new LikeExpression(propertyName, value, caseInsensitive, type);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_same() {
|
||||
|
||||
same(exp("a", "rob", true, LikeType.STARTS_WITH)
|
||||
, exp("a", "rob", true, LikeType.STARTS_WITH));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffBind_then_stillSame() {
|
||||
|
||||
same(exp("a", "rob", true, LikeType.STARTS_WITH)
|
||||
, exp("a", "bor", true, LikeType.STARTS_WITH));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffCaseInsensitive() {
|
||||
|
||||
different(exp("a", "rob", true, LikeType.STARTS_WITH)
|
||||
, exp("a", "rob", false, LikeType.STARTS_WITH));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffLikeType() {
|
||||
|
||||
different(exp("a", "rob", true, LikeType.STARTS_WITH)
|
||||
, exp("a", "rob", true, LikeType.ENDS_WITH));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffProperty() {
|
||||
|
||||
different(exp("a", "rob", true, LikeType.STARTS_WITH)
|
||||
, exp("b", "rob", true, LikeType.STARTS_WITH));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_sameBindValues() {
|
||||
|
||||
assertThat(exp("a", "rob", true, LikeType.STARTS_WITH)
|
||||
.isSameByBind(exp("a", "rob", true, LikeType.STARTS_WITH))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffBindValues() {
|
||||
|
||||
assertThat(exp("a", "rob", true, LikeType.STARTS_WITH)
|
||||
.isSameByBind(exp("a", "bor", true, LikeType.STARTS_WITH))).isFalse();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import io.ebean.Expr;
|
||||
import io.ebean.Expression;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Order;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class LogicExpressionTest extends BaseExpressionTest {
|
||||
|
||||
private Expression eq(String propName, int value) {
|
||||
return Expr.eq(propName, value);
|
||||
}
|
||||
|
||||
private LogicExpression and(Expression a, Expression b) {
|
||||
return new LogicExpression.And(a, b);
|
||||
}
|
||||
|
||||
private LogicExpression or(Expression a, Expression b) {
|
||||
return new LogicExpression.Or(a, b);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addSql() {
|
||||
|
||||
DefaultExpressionRequest expReq = newExpressionRequest();
|
||||
|
||||
LogicExpression and = and(eq("a", 10), eq("b", 10));
|
||||
and.addSql(expReq);
|
||||
|
||||
assertThat(expReq.getSql()).isEqualTo("(a = ? and b = ?)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_same() {
|
||||
|
||||
same(and(eq("a", 10), eq("b", 10))
|
||||
, and(eq("a", 10), eq("b", 10)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffBind_then_stillSame() {
|
||||
|
||||
same(and(eq("a", 10), eq("b", 10))
|
||||
, and(eq("a", 20), eq("b", 20)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffExp1_then_diff() {
|
||||
|
||||
different(and(eq("a", 10), eq("b", 10))
|
||||
, and(eq("c", 10), eq("b", 10)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffExp2_then_diff() {
|
||||
|
||||
different(and(eq("a", 10), eq("b", 10))
|
||||
, and(eq("a", 10), eq("c", 10)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffType_then_diff() {
|
||||
|
||||
different(or(eq("a", 10), eq("b", 10))
|
||||
, and(eq("a", 10), eq("c", 10)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffExpressionType() {
|
||||
|
||||
different(or(eq("a", 10), eq("b", 10))
|
||||
, new NoopExpression());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_sameBindValues() {
|
||||
|
||||
assertThat(and(eq("a", 10), eq("b", 10))
|
||||
.isSameByBind(and(eq("a", 10), eq("c", 10)))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffBindValues() {
|
||||
|
||||
assertThat(and(eq("a", 10), eq("b", 10))
|
||||
.isSameByBind(and(eq("a", 10), eq("c", 20)))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nestedPath_when_notNested() {
|
||||
|
||||
LogicExpression and = and(eq("orderDate", 10), eq("shipDate", 10));
|
||||
|
||||
and.nestedPath(getBeanDescriptor(Order.class));
|
||||
|
||||
assertThat(and.expOne).isInstanceOf(SimpleExpression.class);
|
||||
assertThat(and.expTwo).isInstanceOf(SimpleExpression.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nestedPath_when_nestedSame() {
|
||||
|
||||
LogicExpression and = and(eq("details.orderQty", 10), eq("details.unitPrice", 10));
|
||||
|
||||
String path = and.nestedPath(getBeanDescriptor(Order.class));
|
||||
|
||||
assertThat(path).isEqualTo("details");
|
||||
assertThat(and.expOne).isInstanceOf(SimpleExpression.class);
|
||||
assertThat(and.expTwo).isInstanceOf(SimpleExpression.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nestedPath_when_nestedDifferent() {
|
||||
|
||||
LogicExpression and = and(eq("details.orderQty", 10), eq("shipments.shipTime", 10));
|
||||
|
||||
String path = and.nestedPath(getBeanDescriptor(Order.class));
|
||||
|
||||
assertThat(path).isNull();
|
||||
assertThat(and.expOne).isInstanceOf(NestedPathWrapperExpression.class);
|
||||
assertThat(((NestedPathWrapperExpression) and.expOne).nestedPath).isEqualTo("details");
|
||||
assertThat(and.expTwo).isInstanceOf(NestedPathWrapperExpression.class);
|
||||
assertThat(((NestedPathWrapperExpression) and.expTwo).nestedPath).isEqualTo("shipments");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nestedPath_when_oneNested() {
|
||||
|
||||
LogicExpression and = and(eq("details.orderQty", 10), eq("orderDate", 10));
|
||||
|
||||
String path = and.nestedPath(getBeanDescriptor(Order.class));
|
||||
|
||||
assertThat(path).isNull();
|
||||
assertThat(and.expOne).isInstanceOf(NestedPathWrapperExpression.class);
|
||||
assertThat(((NestedPathWrapperExpression) and.expOne).nestedPath).isEqualTo("details");
|
||||
assertThat(and.expTwo).isInstanceOf(SimpleExpression.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.onetoone.album.Cover;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
public class NoopExpressionTest extends BaseExpressionTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
Query<Customer> query = DB.find(Customer.class)
|
||||
.select("id")
|
||||
.where().add(NoopExpression.INSTANCE)
|
||||
.query();
|
||||
|
||||
query.findList();
|
||||
String generatedSql = sqlOf(query);
|
||||
|
||||
assertThat(generatedSql).contains("select t0.id from o_customer t0 where 1=1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_withSoftDeleteBean() {
|
||||
|
||||
Query<Cover> query = DB.find(Cover.class)
|
||||
.where().add(NoopExpression.INSTANCE)
|
||||
.query();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
query.findCount();
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
assertSql(sql.get(0)).contains("select count(*) from cover t0 where 1=1 and");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_withPreAndPost() {
|
||||
|
||||
Query<Customer> query = DB.find(Customer.class)
|
||||
.select("id")
|
||||
.where().eq("name", null)
|
||||
.add(NoopExpression.INSTANCE)
|
||||
.ne("status", null)
|
||||
.query();
|
||||
|
||||
query.findList();
|
||||
String generatedSql = sqlOf(query);
|
||||
|
||||
assertThat(generatedSql).contains("select t0.id from o_customer t0 where t0.name is null and 1=1 and t0.status is not null");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_same() {
|
||||
|
||||
same(new NoopExpression(), new NoopExpression());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffExpressionType() {
|
||||
|
||||
different(new NoopExpression(), null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_same() {
|
||||
|
||||
assertThat(new NoopExpression().isSameByBind(new NoopExpression())).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffExpressionType() {
|
||||
|
||||
assertThat(new NoopExpression().isSameByBind(null)).isTrue();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
|
||||
import io.ebean.Expression;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.ebean.Expr.eq;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class NotExpressionTest extends BaseExpressionTest {
|
||||
|
||||
|
||||
NotExpression not(Expression expression) {
|
||||
return new NotExpression(expression);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_same() {
|
||||
|
||||
same(not(eq("a", 10)), not(eq("a", 10)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_sameByPlan() {
|
||||
|
||||
same(not(eq("a", 10)), not(eq("a", 20)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_different() {
|
||||
|
||||
different(not(eq("a", 10)), not(eq("b", 10)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_differentExpressionType() {
|
||||
|
||||
different(not(eq("a", 10)), new NoopExpression());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_same() {
|
||||
|
||||
assertThat(not(eq("a", 10))
|
||||
.isSameByBind(not(eq("b", 10)))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_different() {
|
||||
|
||||
assertThat(not(eq("a", 10))
|
||||
.isSameByBind(not(eq("a", 20)))).isFalse();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import io.ebeaninternal.api.ManyWhereJoins;
|
||||
import io.ebeaninternal.api.SpiExpression;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Order;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class NullExpressionTest extends BaseExpressionTest {
|
||||
|
||||
private NullExpression nullExp(String propertyName, boolean notNull) {
|
||||
NullExpression expr = new NullExpression(propertyName, notNull);
|
||||
expr.containsMany(getBeanDescriptor(Order.class), new ManyWhereJoins());
|
||||
return expr;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addSql_when_notNull() throws Exception {
|
||||
|
||||
DefaultExpressionRequest expReq = newExpressionRequest();
|
||||
|
||||
nullExp("id", true).addSql(expReq);
|
||||
|
||||
assertThat(expReq.getSql()).isEqualTo("id is not null");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addSql_when_null() throws Exception {
|
||||
|
||||
DefaultExpressionRequest expReq = newExpressionRequest();
|
||||
|
||||
nullExp("id", false).addSql(expReq);
|
||||
|
||||
assertThat(expReq.getSql()).isEqualTo("id is null");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addSql_when_notNull_and_assocOne() throws Exception {
|
||||
|
||||
DefaultExpressionRequest expReq = newExpressionRequest();
|
||||
|
||||
nullExp("customer", true).addSql(expReq);
|
||||
|
||||
assertThat(expReq.getSql()).isEqualTo("customer.id is not null");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addSql_when_null_and_assocOne() throws Exception {
|
||||
|
||||
DefaultExpressionRequest expReq = newExpressionRequest();
|
||||
|
||||
nullExp("customer", false).addSql(expReq);
|
||||
|
||||
assertThat(expReq.getSql()).isEqualTo("customer.id is null");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void copyForPlanKey_isSameInstance() throws Exception {
|
||||
|
||||
NullExpression exp = nullExp("customer.name", false);
|
||||
SpiExpression other = exp.copyForPlanKey();
|
||||
|
||||
assertThat(exp).isSameAs(other);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_true() throws Exception {
|
||||
|
||||
assertThat(nullExp("customer.name", false)
|
||||
.isSameByBind(nullExp("customer.name", false))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_true() throws Exception {
|
||||
|
||||
same(nullExp("customer.name", false),
|
||||
nullExp("customer.name", false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_false_when_notNullDiff() throws Exception {
|
||||
|
||||
different(new NullExpression("customer.name", false),
|
||||
new NullExpression("customer.name", true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_false_when_propertyNameDiff() throws Exception {
|
||||
|
||||
different(new NullExpression("customer.startDate", true),
|
||||
new NullExpression("customer.name", true));
|
||||
}
|
||||
|
||||
}
|
||||
+148
@@ -0,0 +1,148 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.ExpressionList;
|
||||
import io.ebeaninternal.api.SpiExpression;
|
||||
import org.tests.model.basic.Order;
|
||||
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.assertNull;
|
||||
|
||||
public class PrepareDocNestedTest extends BaseTestCase {
|
||||
@Test
|
||||
public void prepare() throws Exception {
|
||||
|
||||
ExpressionList<Order> where = DB.find(Order.class)
|
||||
.where()
|
||||
.gt("details.orderQty", 1)
|
||||
.query().where();
|
||||
|
||||
|
||||
DefaultExpressionList<?> exp = (DefaultExpressionList<?>) where;
|
||||
PrepareDocNested.prepare(exp, getBeanDescriptor(Order.class));
|
||||
|
||||
List<SpiExpression> underlyingList = exp.getUnderlyingList();
|
||||
assertEquals(underlyingList.size(), 1);
|
||||
assertEquals(exp.allDocNestedPath, "details");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void prepare_when_multipleOfSamePath() throws Exception {
|
||||
|
||||
ExpressionList<Order> where = DB.find(Order.class)
|
||||
.where()
|
||||
.gt("details.orderQty", 1)
|
||||
.gt("details.unitPrice", 1)
|
||||
.query().where();
|
||||
|
||||
|
||||
DefaultExpressionList<?> exp = (DefaultExpressionList<?>) where;
|
||||
PrepareDocNested.prepare(exp, getBeanDescriptor(Order.class));
|
||||
|
||||
List<SpiExpression> underlyingList = exp.getUnderlyingList();
|
||||
assertEquals(underlyingList.size(), 2);
|
||||
assertEquals(exp.allDocNestedPath, "details");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void prepare_when_mixed() throws Exception {
|
||||
|
||||
ExpressionList<Order> where = DB.find(Order.class)
|
||||
.where()
|
||||
.gt("customer.id", 1)
|
||||
.gt("details.orderQty", 1)
|
||||
.gt("details.unitPrice", 1)
|
||||
.query().where();
|
||||
|
||||
|
||||
DefaultExpressionList<?> exp = (DefaultExpressionList<?>) where;
|
||||
PrepareDocNested.prepare(exp, getBeanDescriptor(Order.class));
|
||||
|
||||
List<SpiExpression> underlyingList = exp.getUnderlyingList();
|
||||
assertEquals(underlyingList.size(), 2);
|
||||
assertNull(exp.allDocNestedPath);
|
||||
|
||||
DefaultExpressionList<?> second = (DefaultExpressionList<?>) underlyingList.get(1);
|
||||
assertEquals(second.allDocNestedPath, "details");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void prepare_when_nestedJunction() throws Exception {
|
||||
|
||||
ExpressionList<Order> where = DB.find(Order.class)
|
||||
.where()
|
||||
.not()
|
||||
.gt("customer.id", 1)
|
||||
.gt("details.orderQty", 1)
|
||||
.gt("details.unitPrice", 1)
|
||||
.query().where();
|
||||
|
||||
DefaultExpressionList<?> exp = (DefaultExpressionList<?>) where;
|
||||
PrepareDocNested.prepare(exp, getBeanDescriptor(Order.class));
|
||||
|
||||
List<SpiExpression> underlyingList = exp.getUnderlyingList();
|
||||
assertEquals(underlyingList.size(), 1);
|
||||
assertNull(exp.allDocNestedPath);
|
||||
|
||||
JunctionExpression<?> junction = (JunctionExpression<?>) underlyingList.get(0);
|
||||
List<SpiExpression> junctionUnderlying = junction.exprList.getUnderlyingList();
|
||||
JunctionExpression<?> nestedNestedPath = (JunctionExpression<?>) junctionUnderlying.get(1);
|
||||
assertEquals(nestedNestedPath.exprList.allDocNestedPath, "details");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void prepare_when_nestedMultiple() throws Exception {
|
||||
|
||||
ExpressionList<Order> where = DB.find(Order.class)
|
||||
.where()
|
||||
.isNotNull("shipments.shipTime")
|
||||
.gt("details.orderQty", 1)
|
||||
.gt("details.unitPrice", 1)
|
||||
.query().where();
|
||||
|
||||
DefaultExpressionList<?> exp = (DefaultExpressionList<?>) where;
|
||||
PrepareDocNested.prepare(exp, getBeanDescriptor(Order.class));
|
||||
|
||||
List<SpiExpression> underlyingList = exp.getUnderlyingList();
|
||||
assertEquals(underlyingList.size(), 2);
|
||||
assertNull(exp.allDocNestedPath);
|
||||
|
||||
DefaultExpressionList<?> shipExpr = (DefaultExpressionList<?>) underlyingList.get(0);
|
||||
assertEquals(shipExpr.allDocNestedPath, "shipments");
|
||||
|
||||
DefaultExpressionList<?> detailsExpr = (DefaultExpressionList<?>) underlyingList.get(1);
|
||||
assertEquals(detailsExpr.allDocNestedPath, "details");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void prepare_when_manyMixed() throws Exception {
|
||||
|
||||
ExpressionList<Order> where = DB.find(Order.class)
|
||||
.where()
|
||||
.gt("customer.id", 1) // 0
|
||||
.isNotNull("shipments.shipTime") // shipments 0
|
||||
.isNotNull("status") // 1
|
||||
.gt("details.orderQty", 1) // details 0
|
||||
.isNotNull("orderDate") // 2
|
||||
.gt("details.unitPrice", 1) // details 1
|
||||
.query().where();
|
||||
|
||||
DefaultExpressionList<?> exp = (DefaultExpressionList<?>) where;
|
||||
PrepareDocNested.prepare(exp, getBeanDescriptor(Order.class));
|
||||
|
||||
List<SpiExpression> underlyingList = exp.getUnderlyingList();
|
||||
assertEquals(underlyingList.size(), 5);
|
||||
assertNull(exp.allDocNestedPath);
|
||||
|
||||
DefaultExpressionList<?> shipExpr = (DefaultExpressionList<?>) underlyingList.get(3);
|
||||
assertEquals(shipExpr.allDocNestedPath, "shipments");
|
||||
|
||||
DefaultExpressionList<?> detailsExpr = (DefaultExpressionList<?>) underlyingList.get(4);
|
||||
assertEquals(detailsExpr.allDocNestedPath, "details");
|
||||
}
|
||||
}
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class RawExpressionBuilderTest {
|
||||
|
||||
@Test
|
||||
public void buildSingle_noop() {
|
||||
|
||||
RawExpression exp = RawExpressionBuilder.buildSingle("foo = ?", 42);
|
||||
assertThat(exp.sql).isEqualTo("foo = ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildSingle_noExpand() {
|
||||
|
||||
RawExpression exp = RawExpressionBuilder.buildSingle("foo = ?", asList(42, 43));
|
||||
assertThat(exp.sql).isEqualTo("foo = ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildSingle_expand() {
|
||||
|
||||
RawExpression exp = RawExpressionBuilder.buildSingle("foo in (?1)", asList(42, 43));
|
||||
assertThat(exp.sql).isEqualTo("foo in (?,?)");
|
||||
assertThat(exp.values).contains(42, 43);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildSingle_expand_more() {
|
||||
|
||||
RawExpression exp = RawExpressionBuilder.buildSingle("foo in (?1)", asList(42, 43, 44, 45));
|
||||
assertThat(exp.sql).isEqualTo("foo in (?,?,?,?)");
|
||||
assertThat(exp.values).contains(42, 43, 44, 45);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void buildSingle_expand_single() {
|
||||
|
||||
RawExpression exp = RawExpressionBuilder.buildSingle("foo in (?1)", asList(42));
|
||||
assertThat(exp.sql).isEqualTo("foo in (?)");
|
||||
assertThat(exp.values).contains(42);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void build_noop() {
|
||||
RawExpression exp = RawExpressionBuilder.build("foo = ?", asArray(42));
|
||||
assertThat(exp.sql).isEqualTo("foo = ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void build_noExpand() {
|
||||
|
||||
RawExpression exp = RawExpressionBuilder.buildSingle("foo = ? and bar = any(?)", asArray(44, asList(42, 43)));
|
||||
assertThat(exp.sql).isEqualTo("foo = ? and bar = any(?)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void build_expand() {
|
||||
|
||||
RawExpression exp = RawExpressionBuilder.build("foo in (?) and bar in (?2)", asArray(44, asList(42, 43)));
|
||||
assertThat(exp.sql).isEqualTo("foo in (?) and bar in (?,?)");
|
||||
assertThat(exp.values).contains(44, 42, 43);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void build_expand2() {
|
||||
|
||||
RawExpression exp = RawExpressionBuilder.build("foo in (?) and bar in (?2) (?3)", asArray(44, asList(42, 43), asList(91, 92, 93)));
|
||||
assertThat(exp.sql).isEqualTo("foo in (?) and bar in (?,?) (?,?,?)");
|
||||
assertThat(exp.values).containsExactly(44, 42, 43, 91, 92, 93);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void build_expand3() {
|
||||
|
||||
RawExpression exp = RawExpressionBuilder.build("foo in (?) and bar in (?2) (?3) and ?", asArray(44, asList(42, 43), asList(91, 92, 93), 87));
|
||||
assertThat(exp.sql).isEqualTo("foo in (?) and bar in (?,?) (?,?,?) and ?");
|
||||
assertThat(exp.values).containsExactly(44, 42, 43, 91, 92, 93, 87);
|
||||
}
|
||||
|
||||
private Object[] asArray(Object... values) {
|
||||
return values;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import io.ebeaninternal.api.BindValuesKey;
|
||||
|
||||
public class RawExpressionTest extends BaseExpressionTest {
|
||||
|
||||
private RawExpression exp(String sql, Object... values) {
|
||||
return new RawExpression(sql, values);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_same() {
|
||||
same(exp("a", 10), exp("a", 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffBindValues() {
|
||||
same(exp("a", 10), exp("a", 20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffSql() {
|
||||
different(exp("a", 10), exp("b", 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_same() {
|
||||
assertThat(exp("a", 10).isSameByBind(exp("a", 10))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffBindValues() {
|
||||
assertThat(exp("a", 10).isSameByBind(exp("a", 20))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_moreBindValues() {
|
||||
assertThat(exp("a", 10).isSameByBind(exp("a", 10, 20))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_lessBindValues() {
|
||||
assertThat(exp("a", 10, 20).isSameByBind(exp("a", 10))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryBindHash_when_sameBindValues() {
|
||||
assert_queryBindHash_isSame(exp("a", 10), exp("a", 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryBindHash_when_diffBindValues() {
|
||||
assert_queryBindHash_isDifferent(exp("a", 10), exp("a", 20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryBindHash_when_diffBindValues2() {
|
||||
assert_queryBindHash_isDifferent(exp("a", 10), exp("a", 10, 11));
|
||||
}
|
||||
|
||||
public void assert_queryBindHash_isDifferent(RawExpression exp0, RawExpression exp1) {
|
||||
assertThat(bindKey(exp0)).isNotEqualTo(bindKey(exp1));
|
||||
}
|
||||
|
||||
public void assert_queryBindHash_isSame(RawExpression exp0, RawExpression exp1) {
|
||||
assertThat(bindKey(exp0)).isEqualTo(bindKey(exp1));
|
||||
}
|
||||
|
||||
private BindValuesKey bindKey(RawExpression query) {
|
||||
BindValuesKey bindValuesKey = new BindValuesKey();
|
||||
query.queryBindKey(bindValuesKey);
|
||||
return bindValuesKey;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
public class SameTest {
|
||||
|
||||
@Test
|
||||
public void sameByNull() throws Exception {
|
||||
|
||||
assertThat(Same.sameByNull("a", "a")).isTrue();
|
||||
assertThat(Same.sameByNull("a", "b")).isTrue();
|
||||
assertThat(Same.sameByNull(null, null)).isTrue();
|
||||
assertThat(Same.sameByNull("a", null)).isFalse();
|
||||
assertThat(Same.sameByNull(null, "a")).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sameByValue() throws Exception {
|
||||
|
||||
assertThat(Same.sameByValue("a", "a")).isTrue();
|
||||
assertThat(Same.sameByValue("a", "b")).isFalse();
|
||||
assertThat(Same.sameByValue(null, null)).isTrue();
|
||||
assertThat(Same.sameByValue("a", null)).isFalse();
|
||||
assertThat(Same.sameByValue(null, "a")).isFalse();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class SimpleExpressionTest extends BaseExpressionTest {
|
||||
|
||||
private SimpleExpression exp(String propertyName, Op operator, Object value) {
|
||||
return new SimpleExpression(propertyName, operator, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_same() {
|
||||
|
||||
same(exp("a", Op.EQ, 10), exp("a", Op.EQ, 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffBind_same() {
|
||||
|
||||
same(exp("a", Op.EQ, 10), exp("a", Op.EQ, 20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffProperty_diff() {
|
||||
|
||||
different(exp("a", Op.EQ, 10), exp("b", Op.EQ, 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffOperator_diff() {
|
||||
|
||||
different(exp("a", Op.EQ, 10), exp("a", Op.LT, 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByPlan_when_diffType_diff() {
|
||||
|
||||
different(exp("a", Op.EQ, 10), new NoopExpression());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_sameBindValues() {
|
||||
|
||||
assertThat(exp("a", Op.EQ, 10).isSameByBind(exp("a", Op.LT, 10))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSameByBind_when_diffBindValues() {
|
||||
|
||||
assertThat(exp("a", Op.EQ, 10).isSameByBind(exp("a", Op.EQ, "junk"))).isFalse();
|
||||
}
|
||||
|
||||
}
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
package io.ebeaninternal.server.expression;
|
||||
|
||||
import io.ebeaninternal.api.SpiExpressionRequest;
|
||||
import io.ebeaninternal.server.core.SpiOrmQueryRequest;
|
||||
import io.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import io.ebeaninternal.server.expression.platform.DbExpressionHandler;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Test double for testing with SpiExpressionRequest.
|
||||
*/
|
||||
public class TDSpiExpressionRequest implements SpiExpressionRequest {
|
||||
|
||||
List<Object> bindValues = new ArrayList<>();
|
||||
|
||||
final BeanDescriptor<?> descriptor;
|
||||
|
||||
StringBuilder sql = new StringBuilder();
|
||||
|
||||
public TDSpiExpressionRequest(BeanDescriptor<?> descriptor) {
|
||||
this.descriptor = descriptor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DbExpressionHandler getDbPlatformHandler() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String parseDeploy(String logicalProp) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BeanDescriptor<?> getBeanDescriptor() {
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SpiOrmQueryRequest<?> getQueryRequest() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SpiExpressionRequest append(String sqlExpression) {
|
||||
sql.append(sqlExpression);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addBindEncryptKey(Object encryptKey) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addBindValue(Object bindValue) {
|
||||
bindValues.add(bindValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSql() {
|
||||
return sql.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<Object> getBindValues() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int nextParameter() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void appendLike(boolean rawLikeExpression) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String escapeLikeString(String value) {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void appendInExpression(boolean not, List<Object> bindValues) {
|
||||
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package io.ebeaninternal.server.expression.platform;
|
||||
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class H2DbExpressionTest {
|
||||
|
||||
H2DbExpression expression = new H2DbExpression();
|
||||
|
||||
@Test
|
||||
public void concat() {
|
||||
assertThat(expression.concat("p0", ",", "q1", "suffix")).isEqualTo("concat(p0,',',q1,'suffix')");
|
||||
assertThat(expression.concat("p0", ",", "q1", null)).isEqualTo("concat(p0,',',q1)");
|
||||
assertThat(expression.concat("p0", ",", "q1", "")).isEqualTo("concat(p0,',',q1)");
|
||||
}
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
package io.ebeaninternal.server.expression.platform;
|
||||
|
||||
import io.ebeaninternal.api.SpiExpressionRequest;
|
||||
import io.ebeaninternal.server.expression.DefaultExpressionRequest;
|
||||
import io.ebeaninternal.server.expression.Op;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class HanaDbExpressionTest {
|
||||
private HanaDbExpression expression = new HanaDbExpression();
|
||||
|
||||
@Test
|
||||
public void testArrayContains() {
|
||||
SpiExpressionRequest request = new DefaultExpressionRequest(null);
|
||||
expression.arrayContains(request, "arrayproperty", true, "v1", "v2", "v3");
|
||||
assertEquals("(? member of arrayproperty) and (? member of arrayproperty) and (? member of arrayproperty)",
|
||||
request.getSql());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayNotContains() {
|
||||
SpiExpressionRequest request = new DefaultExpressionRequest(null);
|
||||
expression.arrayContains(request, "arrayproperty", false, "v1", "v2", "v3");
|
||||
assertEquals(
|
||||
"(? not member of arrayproperty) and (? not member of arrayproperty) and (? not member of arrayproperty)",
|
||||
request.getSql());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayContainsEmpty() {
|
||||
SpiExpressionRequest request = new DefaultExpressionRequest(null);
|
||||
expression.arrayContains(request, "arrayproperty", true);
|
||||
assertEquals("", request.getSql());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayIsEmpty() {
|
||||
SpiExpressionRequest request = new DefaultExpressionRequest(null);
|
||||
expression.arrayIsEmpty(request, "arrayproperty", true);
|
||||
assertEquals("cardinality(arrayproperty) = 0", request.getSql());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArrayIsNotEmpty() {
|
||||
SpiExpressionRequest request = new DefaultExpressionRequest(null);
|
||||
expression.arrayIsEmpty(request, "arrayproperty", false);
|
||||
assertEquals("cardinality(arrayproperty) <> 0", request.getSql());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConcat() {
|
||||
String concat = expression.concat("property0", "separator", "property1", "suffix");
|
||||
assertEquals("concat(property0, 'separator'||property1||'suffix')", concat);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConcatNullSuffix() {
|
||||
String concat = expression.concat("property0", "separator", "property1", null);
|
||||
assertEquals("concat(property0, 'separator'||property1)", concat);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJson() {
|
||||
SpiExpressionRequest request = new DefaultExpressionRequest(null);
|
||||
expression.json(request, "jsonproperty", "path", Op.EQ, "val");
|
||||
assertEquals("json_value(jsonproperty, '$.path') = ?", request.getSql());
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package io.ebeaninternal.server.expression.platform;
|
||||
|
||||
import io.ebeaninternal.api.SpiExpressionRequest;
|
||||
import io.ebeaninternal.server.expression.DefaultExpressionRequest;
|
||||
import io.ebeaninternal.server.expression.Op;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class MySqlDbExpressionTest {
|
||||
|
||||
private MySqlDbExpression expression = new MySqlDbExpression();
|
||||
|
||||
@Test
|
||||
public void testJson() {
|
||||
SpiExpressionRequest request = new DefaultExpressionRequest(null);
|
||||
expression.json(request, "jsonproperty", "path", Op.EQ, "val");
|
||||
assertEquals("(jsonproperty ->> '$.path') = ?", request.getSql());
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package io.ebeaninternal.server.expression.platform;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class OracleDbExpressionTest {
|
||||
|
||||
OracleDbExpression expression = new OracleDbExpression();
|
||||
|
||||
@Test
|
||||
public void concat() {
|
||||
assertThat(expression.concat("p0", ",", "q1", "suffix")).isEqualTo("(p0||','||q1||'suffix')");
|
||||
assertThat(expression.concat("p0", ",", "q1", null)).isEqualTo("(p0||','||q1)");
|
||||
assertThat(expression.concat("p0", ",", "q1", "")).isEqualTo("(p0||','||q1)");
|
||||
}
|
||||
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package io.ebeaninternal.server.expression.platform;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
public class PostgresCastTest {
|
||||
|
||||
@Test
|
||||
public void cast() {
|
||||
|
||||
assertThat(PostgresCast.cast(1)).isEqualTo("::integer");
|
||||
assertThat(PostgresCast.cast(1L)).isEqualTo("::bigint");
|
||||
assertThat(PostgresCast.cast(1.0D)).isEqualTo("::decimal");
|
||||
assertThat(PostgresCast.cast("")).isEqualTo("");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cast1() {
|
||||
|
||||
assertThat(PostgresCast.cast(1, true)).isEqualTo("::integer[]");
|
||||
assertThat(PostgresCast.cast(1L, true)).isEqualTo("::bigint[]");
|
||||
assertThat(PostgresCast.cast(1.0D, true)).isEqualTo("::decimal[]");
|
||||
assertThat(PostgresCast.cast("", true)).isEqualTo("");
|
||||
}
|
||||
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package io.ebeaninternal.server.expression.platform;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class PostgresDbExpressionTest {
|
||||
|
||||
PostgresDbExpression expression = new PostgresDbExpression();
|
||||
|
||||
@Test
|
||||
public void concat() {
|
||||
assertThat(expression.concat("p0", ",", "p1", "suffix")).isEqualTo("(p0||','||p1||'suffix')");
|
||||
assertThat(expression.concat("p0", ",", "p1", null)).isEqualTo("(p0||','||p1)");
|
||||
assertThat(expression.concat("p0", ",", "p1", "")).isEqualTo("(p0||','||p1)");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,894 @@
|
||||
package io.ebeaninternal.server.grammer;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import io.ebean.annotation.ForPlatform;
|
||||
import io.ebean.annotation.IgnorePlatform;
|
||||
import io.ebean.annotation.Platform;
|
||||
import io.ebeaninternal.api.SpiQuery;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.OrderDetail;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
public class EqlParserTest extends BaseTestCase {
|
||||
|
||||
|
||||
@Test
|
||||
public void illegal_syntax() {
|
||||
assertThrows(IllegalArgumentException.class, () -> parse("find Article where name = :p0"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_eq() {
|
||||
|
||||
Query<Customer> query = parse("where name eq 'Rob'");
|
||||
query.findList();
|
||||
|
||||
assertSql(query).contains("where t0.name = ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_eqOrNull_bindVal() {
|
||||
Query<Customer> query = parse("where name eqOrNull 'Rob'");
|
||||
query.findList();
|
||||
|
||||
assertSql(query).contains("where (t0.name = ? or t0.name is null)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_eqOrNull_bindNamed() {
|
||||
Query<Customer> query = parse("where name eqOrNull :name");
|
||||
query.setParameter("name", "Rob");
|
||||
query.findList();
|
||||
|
||||
assertSql(query).contains("where (t0.name = ? or t0.name is null)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_eqOrNull_bindPositioned() {
|
||||
|
||||
final Query<Customer> query = where("name eqOrNull ?", "Rob");
|
||||
query.findList();
|
||||
if (isH2()) {
|
||||
assertSql(query).contains("where (t0.name = ? or t0.name is null)");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_eqOrNull_bindPositioned_asNull() {
|
||||
|
||||
final Query<Customer> query = where("name eqOrNull ?", (String)null);
|
||||
query.findList();
|
||||
if (isH2()) {
|
||||
assertSql(query).contains("(t0.name is null or t0.name is null)");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_gtOrNull_bindPositioned() {
|
||||
|
||||
final Query<Customer> query = where("name gtOrNull ?", "Rob");
|
||||
query.findList();
|
||||
if (isH2()) {
|
||||
assertSql(query).contains("where (t0.name > ? or t0.name is null)");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_geOrNull_bindPositioned() {
|
||||
|
||||
final Query<Customer> query = where("name geOrNull ?", "Rob");
|
||||
query.findList();
|
||||
if (isH2()) {
|
||||
assertSql(query).contains("where (t0.name >= ? or t0.name is null)");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_ltOrNull_bindPositioned() {
|
||||
|
||||
final Query<Customer> query = where("name ltOrNull ?", "Rob");
|
||||
query.findList();
|
||||
if (isH2()) {
|
||||
assertSql(query).contains("where (t0.name < ? or t0.name is null)");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_leOrNull_bindPositioned() {
|
||||
|
||||
final Query<Customer> query = where("name leOrNull ?", "Rob");
|
||||
query.findList();
|
||||
if (isH2()) {
|
||||
assertSql(query).contains("where (t0.name <= ? or t0.name is null)");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_eq_reverse() {
|
||||
|
||||
Query<Customer> query = parse("where 'Rob' eq name");
|
||||
query.findList();
|
||||
|
||||
assertSql(query).contains("where t0.name = ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_gt_reverse() {
|
||||
|
||||
Query<Customer> query = parse("where 'Rob' > name");
|
||||
query.findList();
|
||||
assertSql(query).contains("where t0.name < ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_gte_reverse() {
|
||||
|
||||
Query<Customer> query = parse("where 'Rob' >= name");
|
||||
query.findList();
|
||||
assertSql(query).contains("where t0.name <= ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_lt_reverse() {
|
||||
|
||||
Query<Customer> query = parse("where 'Rob' < name");
|
||||
query.findList();
|
||||
assertSql(query).contains("where t0.name > ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_lte_reverse() {
|
||||
|
||||
Query<Customer> query = parse("where 'Rob' <= name");
|
||||
query.findList();
|
||||
assertSql(query).contains("where t0.name >= ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_ieq() {
|
||||
|
||||
Query<Customer> query = parse("where name ieq 'Rob'");
|
||||
query.findList();
|
||||
|
||||
assertSql(query).contains("where lower(t0.name) = ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_ine() {
|
||||
|
||||
Query<Customer> query = parse("where name ine 'Rob'");
|
||||
query.findList();
|
||||
|
||||
assertSql(query).contains("where lower(t0.name) != ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_ieq_reverse() {
|
||||
|
||||
Query<Customer> query = parse("where 'Rob' ieq name");
|
||||
query.findList();
|
||||
assertSql(query).contains("where lower(t0.name) = ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_ine_reverse() {
|
||||
|
||||
Query<Customer> query = parse("where 'Rob' ine name");
|
||||
query.findList();
|
||||
|
||||
assertSql(query).contains("where lower(t0.name) != ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_eq2() {
|
||||
|
||||
Query<Customer> query = parse("where name = 'Rob'");
|
||||
query.findList();
|
||||
|
||||
assertSql(query).contains("where t0.name = ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_namedParam() {
|
||||
|
||||
Query<Customer> query = parse("where name eq :name");
|
||||
query.setParameter("name", "Rob");
|
||||
query.findList();
|
||||
|
||||
assertSql(query).contains("where t0.name = ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_namedParam_otherOrder() {
|
||||
Query<Customer> query = parse("where :nm < name");
|
||||
query.setParameter("nm", "Rob");
|
||||
query.findList();
|
||||
|
||||
assertSql(query).contains("where t0.name > ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_namedParam_startsWith() {
|
||||
|
||||
Query<Customer> query = parse("where name startsWith :name");
|
||||
query.setParameter("name", "Rob");
|
||||
query.findList();
|
||||
|
||||
assertSql(query).contains("where t0.name like ");
|
||||
}
|
||||
|
||||
@Test
|
||||
@IgnorePlatform(Platform.HANA) // The HANA JDBC driver checks the field length on binding and rejects 'NEW'
|
||||
public void where_or1() {
|
||||
|
||||
Query<Customer> query = parse("where name = 'Rob' or (status = 'NEW' and smallnote is null)");
|
||||
query.findList();
|
||||
|
||||
assertSql(query).contains("where (t0.name = ? or (t0.status = ? and t0.smallnote is null))");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ForPlatform(Platform.HANA)
|
||||
public void where_or1_hana() {
|
||||
|
||||
Query<Customer> query = parse("where name = 'Rob' or (status = 'N' and smallnote is null)");
|
||||
query.findList();
|
||||
|
||||
assertSql(query).contains("where (t0.name = ? or (t0.status = ? and t0.smallnote is null ) )");
|
||||
}
|
||||
|
||||
@Test
|
||||
@IgnorePlatform(Platform.HANA) // The HANA JDBC driver checks the field length on binding and rejects 'NEW'
|
||||
public void where_or2() {
|
||||
|
||||
Query<Customer> query = parse("where (name = 'Rob' or status = 'NEW') and smallnote is null");
|
||||
query.findList();
|
||||
|
||||
assertSql(query).contains("where ((t0.name = ? or t0.status = ?) and t0.smallnote is null)");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ForPlatform(Platform.HANA)
|
||||
public void where_or2_hana() {
|
||||
|
||||
Query<Customer> query = parse("where (name = 'Rob' or status = 'N') and smallnote is null");
|
||||
query.findList();
|
||||
|
||||
assertSql(query).contains("where ((t0.name = ? or t0.status = ? ) and t0.smallnote is null )");
|
||||
}
|
||||
|
||||
@Test
|
||||
@IgnorePlatform(Platform.HANA) // The HANA JDBC driver checks the field length on binding and rejects 'NEW'
|
||||
public void test_simplifyExpressions() {
|
||||
|
||||
Query<Customer> query = parse("where not (name = 'Rob' and status = 'NEW')");
|
||||
query.findList();
|
||||
assertSql(query).contains("where not (t0.name = ? and t0.status = ?)");
|
||||
|
||||
query = parse("where not ((name = 'Rob' and status = 'NEW'))");
|
||||
query.findList();
|
||||
assertSql(query).contains("where not (t0.name = ? and t0.status = ?)");
|
||||
|
||||
query = parse("where not (((name = 'Rob') and (status = 'NEW')))");
|
||||
query.findList();
|
||||
assertSql(query).contains("where not (t0.name = ? and t0.status = ?)");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ForPlatform(Platform.HANA)
|
||||
public void test_simplifyExpressions_hana() {
|
||||
|
||||
Query<Customer> query = parse("where not (name = 'Rob' and status = 'N')");
|
||||
query.findList();
|
||||
assertSql(query).contains("where not (t0.name = ? and t0.status = ? )");
|
||||
|
||||
query = parse("where not ((name = 'Rob' and status = 'N'))");
|
||||
query.findList();
|
||||
assertSql(query).contains("where not (t0.name = ? and t0.status = ? )");
|
||||
|
||||
query = parse("where not (((name = 'Rob') and (status = 'N')))");
|
||||
query.findList();
|
||||
assertSql(query).contains("where not (t0.name = ? and t0.status = ? )");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_in() {
|
||||
|
||||
Query<Customer> query = parse("where name in ('Rob','Jim')");
|
||||
query.findList();
|
||||
|
||||
platformAssertIn(query.getGeneratedSql(), "where t0.name");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_in_when_namedParams() {
|
||||
|
||||
Query<Customer> query = parse("where name in (:one, :two)");
|
||||
query.setParameter("one", "Foo");
|
||||
query.setParameter("two", "Bar");
|
||||
query.findList();
|
||||
|
||||
platformAssertIn(query.getGeneratedSql(), "where t0.name");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_in_when_namedParams_withWhitespace() {
|
||||
|
||||
Query<Customer> query = parse("where name in (:one, :two)");
|
||||
query.setParameter("one", "Foo");
|
||||
query.setParameter("two", "Bar");
|
||||
query.findList();
|
||||
|
||||
platformAssertIn(query.getGeneratedSql(), "where t0.name");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_in_when_namedParams_withNoWhitespace() {
|
||||
|
||||
Query<Customer> query = parse("where name in (:one,:two)");
|
||||
query.setParameter("one", "Foo");
|
||||
query.setParameter("two", "Bar");
|
||||
query.findList();
|
||||
|
||||
platformAssertIn(query.getGeneratedSql(), "where t0.name");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_in_when_namedParamAsList() {
|
||||
|
||||
Query<Customer> query = parse("where name in (:names)");
|
||||
query.setParameter("names", Arrays.asList("Baz", "Maz", "Jim"));
|
||||
query.findList();
|
||||
|
||||
platformAssertIn(query.getGeneratedSql(), "where t0.name");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_inOrEmpty_withVals() {
|
||||
|
||||
final Query<Customer> query = where("name inOrEmpty ?", Arrays.asList("Baz", "Maz", "Jim"));
|
||||
query.findList();
|
||||
platformAssertIn(query.getGeneratedSql(), "where t0.name");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_inOrEmpty_withValsAsSet() {
|
||||
|
||||
final Query<Customer> query = where("name inOrEmpty ?", new HashSet<>(Arrays.asList("Baz", "Maz", "Jim")));
|
||||
query.findList();
|
||||
platformAssertIn(query.getGeneratedSql(), "where t0.name");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_inOrEmpty_withEmpty() {
|
||||
|
||||
final Query<Customer> query = where("name inOrEmpty ?", new ArrayList());
|
||||
query.findList();
|
||||
assertSql(query).doesNotContain("where");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_inOrEmpty_withNull() {
|
||||
|
||||
List<String> names = null;
|
||||
final Query<Customer> query = where("name inOrEmpty ?", names);
|
||||
query.findList();
|
||||
assertSql(query).doesNotContain("where");
|
||||
}
|
||||
|
||||
@Test//(expected = IllegalArgumentException.class)
|
||||
public void query_inOrEmpty_withVals() {
|
||||
assertThrows(IllegalArgumentException.class, () -> parse("where name inOrEmpty (:names)"));
|
||||
// query.setParameter("names", Arrays.asList("Baz", "Maz", "Jim"));
|
||||
// query.findList();
|
||||
// platformAssertIn(query.getGeneratedSql(), "where t0.name");
|
||||
}
|
||||
|
||||
/**
|
||||
* This test fails in that we can't use inOrEmpty with named parameters.
|
||||
*/
|
||||
@Test//(expected = IllegalArgumentException.class)
|
||||
public void query_inOrEmpty_withNamedParams_expect_IllegalArgument() {
|
||||
assertThrows(IllegalArgumentException.class, () -> parse("where name inOrEmpty (:names)"));
|
||||
// query.setParameter("names", new ArrayList<>());
|
||||
// assertThrows(IllegalArgumentException.class, query::findList);
|
||||
// assertSql(query).doesNotContain("where");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_inrange() {
|
||||
|
||||
Query<Customer> query = parse("where name inrange 'As' to 'B'");
|
||||
query.findList();
|
||||
|
||||
assertSql(query).contains("where (t0.name >= ? and t0.name < ?)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_inrange_withNamedParams() {
|
||||
|
||||
Query<Customer> query = parse("where name inrange :one to :two");
|
||||
query.setParameter("one", "a");
|
||||
query.setParameter("two", "b");
|
||||
query.findList();
|
||||
|
||||
assertSql(query).contains("where (t0.name >= ? and t0.name < ?)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_between() {
|
||||
|
||||
Query<Customer> query = parse("where name between 'As' and 'B'");
|
||||
query.findList();
|
||||
|
||||
assertSql(query).contains("where t0.name between ? and ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_between_withNamedParams() {
|
||||
|
||||
Query<Customer> query = parse("where name between :one and :two");
|
||||
query.setParameter("one", "a");
|
||||
query.setParameter("two", "b");
|
||||
query.findList();
|
||||
|
||||
assertSql(query).contains("where t0.name between ? and ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_betweenProperty() {
|
||||
|
||||
Query<Customer> query = parse("where 'x' between name and smallnote");
|
||||
query.findList();
|
||||
|
||||
assertSql(query).contains("where ? between t0.name and t0.smallnote");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_betweenProperty_withNamed() {
|
||||
|
||||
Query<Customer> query = parse("where :some between name and smallnote");
|
||||
query.setParameter("some", "A");
|
||||
query.findList();
|
||||
|
||||
assertSql(query).contains("where ? between t0.name and t0.smallnote");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void selectFetch_basic() {
|
||||
|
||||
Query<Customer> query = parse("select name fetch billingAddress");
|
||||
query.findList();
|
||||
|
||||
assertSql(query).contains("select t0.id, t0.name, t1.id, t1.line_1, t1.line_2, t1.city, t1.cretime, t1.updtime, t1.country_code from o_customer t0 left join o_address t1 on t1.id = t0.billing_address_id");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void selectFetch_basic2() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = parse("select name, status fetch billingAddress");
|
||||
query.findList();
|
||||
|
||||
assertSql(query).contains("select t0.id, t0.name, t0.status, t1.id, t1.line_1, t1.line_2, t1.city, t1.cretime, t1.updtime, t1.country_code from o_customer t0 left join o_address t1 on t1.id = t0.billing_address_id");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void selectFetch_withProperties() {
|
||||
|
||||
Query<Customer> query = parse("select name fetch billingAddress (line1, city) ");
|
||||
query.findList();
|
||||
|
||||
assertSql(query, 12).contains("select t0.id, t0.name, t1.id, t1.line_1, t1.city from o_customer t0 left join o_address t1 on t1.id = t0.billing_address_id");
|
||||
}
|
||||
|
||||
@Test
|
||||
@IgnorePlatform(Platform.SQLSERVER)
|
||||
public void selectFetchFetchLimit() {
|
||||
|
||||
Query<Customer> query = parse("select name fetch billingAddress (line1, city) fetch shippingAddress (line1) limit 10");
|
||||
query.findList();
|
||||
|
||||
assertSql(query, 12).contains("select t0.id, t0.name, t1.id, t1.line_1, t1.city, t2.id, t2.line_1 from o_customer t0 left join o_address t1 on t1.id = t0.billing_address_id left join o_address t2 on t2.id = t0.shipping_address_id");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void selectFetchFetchMany() {
|
||||
|
||||
Query<Customer> query = parse("select name fetch billingAddress (line1, city) fetch contacts");
|
||||
query.findList();
|
||||
|
||||
assertSql(query, 16).contains("select t0.id, t0.name, t1.id, t1.line_1, t1.city, t2.id, t2.first_name, t2.last_name, t2.phone, t2.mobile, t2.email, t2.is_member, t2.cretime, t2.updtime, t2.customer_id, t2.group_id from o_customer t0");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void selectFetchFetchManyProperties() {
|
||||
|
||||
Query<Customer> query = parse("select name fetch billingAddress (line1, city) fetch contacts (email)");
|
||||
query.findList();
|
||||
|
||||
assertSql(query, 12).contains("select t0.id, t0.name, t1.id, t1.line_1, t1.city, t2.id, t2.email from o_customer t0");
|
||||
}
|
||||
|
||||
@Test
|
||||
@IgnorePlatform(Platform.SQLSERVER)
|
||||
public void selectFetchFetchManyPropertiesLimit() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
Query<Customer> query = parse("select name fetch billingAddress (line1, city) fetch contacts (email) limit 10");
|
||||
query.findList();
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(2);
|
||||
assertSql(sql.get(0)).contains("select t0.id, t0.name, t1.id, t1.line_1, t1.city from o_customer t0 left join o_address");
|
||||
assertSql(sql.get(1)).contains("select t0.customer_id, t0.id, t0.email from contact t0 where (t0.customer_id)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fetch_basic() {
|
||||
|
||||
Query<Customer> query = parse("fetch billingAddress");
|
||||
query.findList();
|
||||
|
||||
assertSql(query).contains(", t1.id");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fetch_withProperty() {
|
||||
|
||||
Query<Customer> query = parse("fetch billingAddress (city)");
|
||||
query.findList();
|
||||
|
||||
assertSql(query, 10).contains(", t1.id, t1.city");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fetch_withProperty_noWhitespace() {
|
||||
|
||||
Query<Customer> query = parse("fetch billingAddress(city)");
|
||||
query.findList();
|
||||
|
||||
assertSql(query, 10).contains(", t1.id, t1.city");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fetch_basic_multiple() {
|
||||
|
||||
Query<Customer> query = parse("fetch billingAddress fetch shippingAddress");
|
||||
query.findList();
|
||||
|
||||
assertSql(query).contains(", t1.city");
|
||||
assertSql(query).contains(", t2.city");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fetch_basic_multiple_withProperties() {
|
||||
|
||||
Query<Customer> query = parse("fetch billingAddress (city) fetch shippingAddress (city)");
|
||||
query.findList();
|
||||
|
||||
assertSql(query, 12).contains(", t1.id, t1.city, t2.id, t2.city");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fetch_lazy() {
|
||||
|
||||
Query<Customer> query = parse("fetch lazy billingAddress");
|
||||
query.findList();
|
||||
|
||||
assertSql(query).doesNotContain(", t1.city");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fetch_lazy50() {
|
||||
|
||||
Query<Customer> query = parse("fetch lazy(50) billingAddress");
|
||||
query.findList();
|
||||
|
||||
assertSql(query).doesNotContain(", t1.city");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fetch_query50() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
Query<Customer> query = parse("fetch query(50) billingAddress");
|
||||
query.findList();
|
||||
|
||||
assertSql(query).doesNotContain(", t1.city");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fetch_query50_asHint() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
Query<Customer> query = parse("fetch query(50) billingAddress (city)");
|
||||
query.findList();
|
||||
|
||||
assertSql(query).doesNotContain(", t1.city");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fetch_lazy50_asHint() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
Query<Customer> query = parse("fetch lazy(50) billingAddress (city) order by id");
|
||||
List<Customer> list = query.findList();
|
||||
|
||||
assertSql(query).doesNotContain(", t1.city");
|
||||
|
||||
Customer customer = list.get(0);
|
||||
customer.getBillingAddress().getCity();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void select() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = parse("select (name)");
|
||||
query.findList();
|
||||
assertSql(query).contains("select t0.id, t0.name from o_customer t0");
|
||||
|
||||
Query<Customer> query2 = parse("select name");
|
||||
query2.findList();
|
||||
assertSql(query2).contains("select t0.id, t0.name from o_customer t0");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void select_sum() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = parse("select (sum(id))");
|
||||
query.findSingleAttribute();
|
||||
assertSql(query).contains("select sum(t0.id) from o_customer t0");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void select_max() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = parse("select (max(cretime))");
|
||||
query.findSingleAttribute();
|
||||
assertSql(query).contains("select max(t0.cretime) from o_customer t0");
|
||||
|
||||
Query<Customer> query2 = parse("select max(cretime)");
|
||||
query2.findSingleAttribute();
|
||||
assertSql(query2).contains("select max(t0.cretime) from o_customer t0");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void select_agg() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = parse("select status, max(cretime)");
|
||||
List<Customer> customers = query.findList();
|
||||
|
||||
assertThat(customers).isNotEmpty();
|
||||
assertSql(query).contains("select t0.status, max(t0.cretime) from o_customer t0 group by t0.status");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void select_agg_sum() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<OrderDetail> query = DB.createQuery(OrderDetail.class, "select sum(orderQty) fetch `order` (id)");
|
||||
List<OrderDetail> details = query.findList();
|
||||
|
||||
assertThat(details).isNotEmpty();
|
||||
assertSql(query).contains("select sum(t0.order_qty), t1.id from o_order_detail t0 join o_order t1 on t1.id = t0.order_id group by t1.id");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void selectDistinct() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = parse("select distinct (name)");
|
||||
query.findList();
|
||||
assertSql(query).contains("select distinct t0.name from o_customer t0");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void limit() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = parse("limit 10");
|
||||
query.findList();
|
||||
if (isH2()) {
|
||||
assertSql(query).contains(" limit 10");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void limitOffset() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = parse("order by name limit 10 offset 5");
|
||||
query.findList();
|
||||
if (isH2()) {
|
||||
assertSql(query).contains(" limit 10 offset 5");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void orderBy() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = parse("order by id");
|
||||
query.findList();
|
||||
if (isH2()) {
|
||||
assertSql(query).contains("from o_customer t0 order by t0.id");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void orderBy_desc() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = parse("order by id desc");
|
||||
query.findList();
|
||||
if (isH2()) {
|
||||
assertSql(query).contains("from o_customer t0 order by t0.id desc");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void orderBy_nullsLast() {
|
||||
|
||||
if (!isPlatformOrderNullsSupport()) {
|
||||
return;
|
||||
}
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = parse("order by id desc nulls last");
|
||||
query.findList();
|
||||
if (isH2()) {
|
||||
assertSql(query).contains(" from o_customer t0 order by t0.id desc nulls last");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void orderBy_nullsFirst() {
|
||||
|
||||
if (!isPlatformOrderNullsSupport()) {
|
||||
return;
|
||||
}
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = parse("order by id nulls first");
|
||||
query.findList();
|
||||
if (isH2()) {
|
||||
assertSql(query).contains(" from o_customer t0 order by t0.id nulls first");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void orderBy_multiple() {
|
||||
|
||||
if (!isPlatformOrderNullsSupport()) {
|
||||
return;
|
||||
}
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Customer> query = parse("order by billingAddress.city desc nulls last, name, id desc nulls last");
|
||||
query.findList();
|
||||
if (isH2()) {
|
||||
assertSql(query).contains(" order by t1.city desc nulls last, t0.name, t0.id desc nulls last");
|
||||
}
|
||||
}
|
||||
|
||||
private Query<Customer> parse(String raw) {
|
||||
|
||||
Query<Customer> query = DB.find(Customer.class);
|
||||
EqlParser.parse(raw, (SpiQuery<?>) query);
|
||||
return query;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_simple() {
|
||||
|
||||
final Query<Customer> query = where("name isNotNull");
|
||||
query.findList();
|
||||
if (isH2()) {
|
||||
assertSql(query).contains(" from o_customer t0 where t0.name is not null");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_withParams() {
|
||||
|
||||
final Query<Customer> query = where("id isNotNull and name = ? and smallnote istartsWith ?", "Rob", "Foo");
|
||||
query.findList();
|
||||
if (isH2()) {
|
||||
assertSql(query).contains("where (t0.id is not null and t0.name = ? and lower(t0.smallnote) like ? escape'|' )");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_withParamsQuPos() {
|
||||
|
||||
final Query<Customer> query = where("name = ?1 and smallnote istartsWith ?2 and name like ?1", "Rob", "Foo");
|
||||
query.findList();
|
||||
if (isH2()) {
|
||||
assertSql(query).contains(" where (t0.name = ? and lower(t0.smallnote) like ? escape'|' and t0.name like ? escape'' )");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_orSimple() {
|
||||
|
||||
final Query<Customer> query = where("id isNotNull or name = ?", "Rob", "Foo");
|
||||
query.findList();
|
||||
if (isH2()) {
|
||||
assertSql(query).contains("where (t0.id is not null or t0.name = ?)");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_orWithParams() {
|
||||
|
||||
final Query<Customer> query = where("(id isNotNull or name = ?) and smallnote istartsWith ?", "Rob", "Foo");
|
||||
query.findList();
|
||||
if (isH2()) {
|
||||
assertSql(query).contains("where ((t0.id is not null or t0.name = ?) and lower(t0.smallnote) like ? escape'|' )");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_dateInRange() {
|
||||
|
||||
final Query<Customer> query = where("anniversary inrange ? to ?", LocalDate.now().minusDays(7), LocalDate.now());
|
||||
query.findList();
|
||||
if (isH2()) {
|
||||
assertSql(query).contains("where (t0.anniversary >= ? and t0.anniversary < ?)");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void where_dateInRange_camelCase() {
|
||||
|
||||
final Query<Customer> query = where("anniversary inRange ? to ?", LocalDate.now().minusDays(7), LocalDate.now());
|
||||
query.findList();
|
||||
if (isH2()) {
|
||||
assertSql(query).contains("where (t0.anniversary >= ? and t0.anniversary < ?)");
|
||||
}
|
||||
}
|
||||
|
||||
private Query<Customer> where(String where, Object... params) {
|
||||
|
||||
Query<Customer> query = DB.find(Customer.class);
|
||||
EqlParser.parseWhere(where, query.where(), query.getExpressionFactory(), params);
|
||||
|
||||
return query;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package io.ebeaninternal.server.grammer;
|
||||
|
||||
import io.ebean.FetchConfig;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
public class ParseFetchConfigTest {
|
||||
|
||||
@Test
|
||||
public void parse() throws Exception {
|
||||
|
||||
assertNull(ParseFetchConfig.parse("junk"));
|
||||
assertNull(ParseFetchConfig.parse("lazyFoo"));
|
||||
assertNull(ParseFetchConfig.parse("queryFoo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseLazy() {
|
||||
FetchConfig lazy = ParseFetchConfig.parse("lazy");
|
||||
assertThat(lazy.getBatchSize()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseLazy100() {
|
||||
FetchConfig lazy = ParseFetchConfig.parse("lazy(100)");
|
||||
assertThat(lazy.getBatchSize()).isEqualTo(100);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseQuery() {
|
||||
FetchConfig lazy = ParseFetchConfig.parse("query");
|
||||
assertThat(lazy.getBatchSize()).isEqualTo(100);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseQuery100() {
|
||||
FetchConfig lazy = ParseFetchConfig.parse("query(50)");
|
||||
assertThat(lazy.getBatchSize()).isEqualTo(50);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
* Licensed Materials - Property of FOCONIS AG
|
||||
* (C) Copyright FOCONIS AG.
|
||||
*/
|
||||
package io.ebeaninternal.server.idgen;
|
||||
|
||||
import io.ebean.config.dbplatform.PlatformIdGenerator;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Run some simple tests for the UUID generator.
|
||||
*
|
||||
* @author Roland Praml, FOCONIS AG
|
||||
*/
|
||||
public class TestUuidGenerator {
|
||||
|
||||
/**
|
||||
* Worker to generate UUIDs
|
||||
*
|
||||
* @author Roland Praml, FOCONIS AG
|
||||
*/
|
||||
public static class IdTest implements Runnable {
|
||||
private final Map<UUID, UUID> map;
|
||||
private final PlatformIdGenerator idGen;
|
||||
private final int ids;
|
||||
private final AtomicBoolean failFlag;
|
||||
|
||||
public IdTest(PlatformIdGenerator idGen, int ids, Map<UUID, UUID> map, AtomicBoolean failFlag) {
|
||||
this.idGen = idGen;
|
||||
this.ids = ids;
|
||||
this.map = map;
|
||||
this.failFlag = failFlag;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
//System.out.println("Start " + Thread.currentThread());
|
||||
for (int i = 0; i < ids; i++) {
|
||||
UUID uuid = (UUID) idGen.nextId(null);
|
||||
if (map != null && map.put(uuid, uuid) != null) {
|
||||
System.err.println("UUID already in set " + uuid);
|
||||
failFlag.set(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test takes ~0.3 sec
|
||||
*/
|
||||
@Test
|
||||
public void testUuidV1SingleThread() throws Exception {
|
||||
testGenerator(1, 500_000, UuidV1IdGenerator.getInstance("ebean-test-uuid.state"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test takes ~1.4 sec
|
||||
*/
|
||||
@Test
|
||||
public void testUuidV1MultiThread() throws Exception {
|
||||
testGenerator(10, 50_000, UuidV1IdGenerator.getInstance("ebean-test-uuid.state"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test takes ~3.1 sec
|
||||
*/
|
||||
@Test
|
||||
public void testUuidV1RndMultiThread() throws Exception {
|
||||
testGenerator(10, 50_000, UuidV1RndIdGenerator.INSTANCE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test takes ~2.0 sec
|
||||
*/
|
||||
@Test
|
||||
public void testUuidType1RndSingleThread() throws Exception {
|
||||
testGenerator(1, 500_000, UuidV1RndIdGenerator.INSTANCE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test takes ~4.9 sec
|
||||
*/
|
||||
@Test
|
||||
public void testUuidType4MultiThread() throws Exception {
|
||||
testGenerator(10, 50_000, UuidV4IdGenerator.INSTANCE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test takes ~3.1 sec
|
||||
*/
|
||||
@Test
|
||||
public void testUuidType4SingleThread() throws Exception {
|
||||
testGenerator(1, 500_000, UuidV4IdGenerator.INSTANCE);
|
||||
}
|
||||
|
||||
private void testGenerator(int threadCount, int count, PlatformIdGenerator generator) throws Exception {
|
||||
System.out.println("Printing 5 consecutive IDs of " + generator);
|
||||
for (int i = 0; i < 5; i++) {
|
||||
System.out.println(generator.nextId(null));
|
||||
}
|
||||
|
||||
AtomicBoolean failFlag = new AtomicBoolean();
|
||||
ConcurrentHashMap<UUID, UUID> map = new ConcurrentHashMap<>();
|
||||
Thread[] threads = new Thread[threadCount];
|
||||
for (int i = 0; i < threads.length; i++) {
|
||||
threads[i] = new Thread(new IdTest(generator, count, map, failFlag));
|
||||
}
|
||||
for (int i = 0; i < threads.length; i++) {
|
||||
threads[i].start();
|
||||
}
|
||||
for (int i = 0; i < threads.length; i++) {
|
||||
threads[i].join();
|
||||
}
|
||||
assertThat(failFlag.get()).isFalse();
|
||||
assertThat(map.size()).isEqualTo(threadCount * count);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package io.ebeaninternal.server.lib;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.DatabaseFactory;
|
||||
import io.ebean.event.ShutdownManager;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class ShutdownManagerTest extends BaseTestCase {
|
||||
|
||||
/**
|
||||
* Run this test manually. Most typical when we want the application code to control shutdown.
|
||||
*/
|
||||
@Disabled
|
||||
@Test
|
||||
public void test_disableShutdownHook_shutdownManually() {
|
||||
// disable hook to make sure app code controls when shutdown is executed
|
||||
ShutdownManager.deregisterShutdownHook();
|
||||
DB.getDefault();
|
||||
|
||||
System.out.println("shutdown manually ... ");
|
||||
// application code explicitly calls shutdown()
|
||||
ShutdownManager.shutdown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Run this test manually.
|
||||
*/
|
||||
@Disabled
|
||||
@Test
|
||||
public void test_shutdownHook() {
|
||||
DB.getDefault(); // shutdown fired via shutdown hook, default behaviour
|
||||
}
|
||||
|
||||
/**
|
||||
* Run this test manually.
|
||||
*/
|
||||
@Disabled
|
||||
@Test
|
||||
public void test_disableShutdownHook() {
|
||||
DB.getDefault();
|
||||
ShutdownManager.deregisterShutdownHook(); // no shutdown is run here (not great, don't do this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Run this test manually.
|
||||
*/
|
||||
@Disabled
|
||||
@Test
|
||||
public void test_shutdownManually() {
|
||||
DB.getDefault();
|
||||
System.out.println("shutdown manually ... ");
|
||||
// note this removes the shutdown hook, only "useful" if it runs BEFORE a JVM shutdown is invoked (hook invoked)
|
||||
DatabaseFactory.shutdown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Run this test manually.
|
||||
*/
|
||||
@Disabled
|
||||
@Test
|
||||
public void test_deregisterShutdownHook() {
|
||||
DB.getDefault();
|
||||
ShutdownManager.deregisterShutdownHook();
|
||||
}
|
||||
|
||||
/**
|
||||
* Run this test manually.
|
||||
*/
|
||||
@Disabled
|
||||
@Test
|
||||
public void test_noShutdownHook() {
|
||||
System.setProperty("ebean.registerShutdownHook", "false");
|
||||
DB.getDefault();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package io.ebeaninternal.server.loadcontext;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.FetchConfig;
|
||||
import io.ebean.Query;
|
||||
import io.ebeaninternal.server.core.OrmQueryRequest;
|
||||
import io.ebeaninternal.server.core.OrmQueryRequestTestHelper;
|
||||
import io.ebeaninternal.server.deploy.BeanPropertyAssocMany;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class DLoadContextTest extends BaseTestCase {
|
||||
|
||||
private OrmQueryRequest<Order> queryRequest(Query<Order> query) {
|
||||
return OrmQueryRequestTestHelper.queryRequest(query);
|
||||
}
|
||||
|
||||
private Query<Order> query() {
|
||||
return server().find(Order.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void construct_when_defaults_expect_defaultLazyBatchSizeServerDefaults() {
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query());
|
||||
queryRequest.initTransIfRequired();
|
||||
queryRequest.endTransIfRequired();
|
||||
|
||||
DLoadContext graphContext = (DLoadContext) queryRequest.loadContext();
|
||||
DLoadBeanContext customer = graphContext.getBeanContext("customer");
|
||||
|
||||
assertThat(customer.batchSize).isEqualTo(10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void construct_when_fetchQuery_expect_100_batchSize() {
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query().fetch("customer", FetchConfig.ofQuery()));
|
||||
queryRequest.initTransIfRequired();
|
||||
queryRequest.endTransIfRequired();
|
||||
|
||||
DLoadContext graphContext = (DLoadContext) queryRequest.loadContext();
|
||||
DLoadBeanContext customer = graphContext.getBeanContext("customer");
|
||||
|
||||
assertThat(customer.batchSize).isEqualTo(100);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void construct_when_fetchQuery_expect_100_batchSize_viaFetchQuery() {
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query().fetchQuery("customer"));
|
||||
queryRequest.initTransIfRequired();
|
||||
queryRequest.endTransIfRequired();
|
||||
|
||||
DLoadContext graphContext = (DLoadContext) queryRequest.loadContext();
|
||||
DLoadBeanContext customer = graphContext.getBeanContext("customer");
|
||||
|
||||
assertThat(customer.batchSize).isEqualTo(100);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void construct_when_fetchQuery50_expect_50_batchSize() {
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query().fetch("customer", FetchConfig.ofQuery(50)));
|
||||
queryRequest.initTransIfRequired();
|
||||
queryRequest.endTransIfRequired();
|
||||
|
||||
DLoadContext graphContext = (DLoadContext) queryRequest.loadContext();
|
||||
DLoadBeanContext customer = graphContext.getBeanContext("customer");
|
||||
|
||||
assertThat(customer.batchSize).isEqualTo(50);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void construct_when_fetchQueryFirst20Lazy5_expect_20_5_batchSize() {
|
||||
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query().fetchQuery("customer"));
|
||||
queryRequest.initTransIfRequired();
|
||||
queryRequest.endTransIfRequired();
|
||||
|
||||
DLoadContext graphContext = (DLoadContext) queryRequest.loadContext();
|
||||
DLoadBeanContext customer = graphContext.getBeanContext("customer");
|
||||
|
||||
assertThat(customer.batchSize).isEqualTo(100);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void construct_when_fetch_expect_100_100_batchSize() {
|
||||
|
||||
BeanPropertyAssocMany<?> many = (BeanPropertyAssocMany<?>)getBeanDescriptor(Order.class).beanProperty("details");
|
||||
// the fetch is converted to a query join due to the maxRows
|
||||
OrmQueryRequest<Order> queryRequest = queryRequest(query().fetch("details").setMaxRows(50));
|
||||
queryRequest.initTransIfRequired();
|
||||
queryRequest.endTransIfRequired();
|
||||
|
||||
DLoadContext graphContext = (DLoadContext) queryRequest.loadContext();
|
||||
DLoadManyContext details = graphContext.getManyContext("details", many);
|
||||
|
||||
assertThat(details.batchSize).isEqualTo(100);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package io.ebeaninternal.server.persist;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class BatchDepthOrderTest {
|
||||
|
||||
@Test
|
||||
public void orderingFor() {
|
||||
|
||||
BatchDepthOrder depthOrder = new BatchDepthOrder();
|
||||
|
||||
assertEquals(0, depthOrder.orderingFor(0));
|
||||
assertEquals(1, depthOrder.orderingFor(0));
|
||||
assertEquals(2, depthOrder.orderingFor(0));
|
||||
assertEquals(100, depthOrder.orderingFor(1));
|
||||
assertEquals(101, depthOrder.orderingFor(1));
|
||||
assertEquals(200, depthOrder.orderingFor(2));
|
||||
assertEquals(3, depthOrder.orderingFor(0));
|
||||
|
||||
assertEquals(-100, depthOrder.orderingFor(-1));
|
||||
assertEquals(-99, depthOrder.orderingFor(-1));
|
||||
assertEquals(-98, depthOrder.orderingFor(-1));
|
||||
|
||||
assertEquals(-200, depthOrder.orderingFor(-2));
|
||||
assertEquals(-199, depthOrder.orderingFor(-2));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void clear() {
|
||||
|
||||
BatchDepthOrder depthOrder = new BatchDepthOrder();
|
||||
|
||||
assertEquals(0, depthOrder.orderingFor(0));
|
||||
assertEquals(1, depthOrder.orderingFor(0));
|
||||
assertEquals(100, depthOrder.orderingFor(1));
|
||||
assertEquals(101, depthOrder.orderingFor(1));
|
||||
|
||||
depthOrder.clear();
|
||||
|
||||
assertEquals(0, depthOrder.orderingFor(0));
|
||||
assertEquals(100, depthOrder.orderingFor(1));
|
||||
assertEquals(200, depthOrder.orderingFor(2));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package io.ebeaninternal.server.persist;
|
||||
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class FlagsTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
int state = 0;
|
||||
|
||||
state = Flags.setPublish(state);
|
||||
assertThat(Flags.isSet(state, Flags.PUBLISH)).isTrue();
|
||||
|
||||
state = Flags.setMerge(state);
|
||||
state = Flags.setInsert(state);
|
||||
assertThat(Flags.isSet(state, Flags.PUBLISH)).isTrue();
|
||||
assertThat(Flags.isSet(state, Flags.MERGE)).isTrue();
|
||||
assertThat(Flags.isSet(state, Flags.INSERT)).isTrue();
|
||||
|
||||
state = Flags.unsetPublish(state);
|
||||
assertThat(Flags.isSet(state, Flags.PUBLISH)).isFalse();
|
||||
assertThat(Flags.isSet(state, Flags.MERGE)).isTrue();
|
||||
assertThat(Flags.isSet(state, Flags.INSERT)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void insert() {
|
||||
|
||||
int state = 0;
|
||||
|
||||
state = Flags.setInsert(state);
|
||||
assertThat(Flags.isSet(state, Flags.INSERT)).isTrue();
|
||||
assertThat(Flags.isSet(state, Flags.NORMAL)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void insertNormal() {
|
||||
|
||||
int state = 0;
|
||||
|
||||
state = Flags.setInsertNormal(state);
|
||||
assertThat(Flags.isSet(state, Flags.INSERT)).isTrue();
|
||||
assertThat(Flags.isSet(state, Flags.NORMAL)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void update() {
|
||||
|
||||
int state = 0;
|
||||
|
||||
state = Flags.setUpdate(state);
|
||||
assertThat(Flags.isSet(state, Flags.INSERT)).isFalse();
|
||||
assertThat(Flags.isSet(state, Flags.NORMAL)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateNormal() {
|
||||
|
||||
int state = 0;
|
||||
|
||||
state = Flags.setUpdateNormal(state);
|
||||
assertThat(Flags.isSet(state, Flags.INSERT)).isFalse();
|
||||
assertThat(Flags.isSet(state, Flags.NORMAL)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isPublishOrMerge() {
|
||||
|
||||
assertThat(Flags.isPublishMergeOrNormal(0)).isFalse();
|
||||
assertThat(Flags.isPublishMergeOrNormal(Flags.INSERT)).isFalse();
|
||||
assertThat(Flags.isPublishMergeOrNormal(Flags.RECURSE)).isFalse();
|
||||
|
||||
assertThat(Flags.isPublishMergeOrNormal(Flags.PUBLISH)).isTrue();
|
||||
assertThat(Flags.isPublishMergeOrNormal(Flags.MERGE)).isTrue();
|
||||
assertThat(Flags.isPublishMergeOrNormal(Flags.NORMAL)).isTrue();
|
||||
|
||||
int mergePublish = Flags.setMerge(Flags.setPublish(0));
|
||||
assertThat(Flags.isPublishMergeOrNormal(mergePublish)).isTrue();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package io.ebeaninternal.server.persist;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TrimLogSqlTest {
|
||||
|
||||
@Test
|
||||
public void trim() {
|
||||
assertThat(TrimLogSql.trim("hello")).isEqualTo("hello");
|
||||
assertThat(TrimLogSql.trim("hello\nthere")).isEqualTo("hello\\n there");
|
||||
assertThat(TrimLogSql.trim("a\nb\nc\nd")).isEqualTo("a\\n b\\n c\\n d");
|
||||
assertThat(TrimLogSql.trim("a\n\nb")).isEqualTo("a\\n \\n b");
|
||||
assertThat(TrimLogSql.trim("\n\n\n")).isEqualTo("\\n \\n \\n ");
|
||||
}
|
||||
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package io.ebeaninternal.server.profile;
|
||||
|
||||
import io.ebean.metric.MetricFactory;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class BasicProfileLocationTest {
|
||||
|
||||
@Test
|
||||
public void obtain() {
|
||||
|
||||
DProfileLocation loc = new DTimedProfileLocation(12, "foo", MetricFactory.get().createTimedMetric("junk"));
|
||||
|
||||
assertThat(loc.obtain()).isTrue();
|
||||
assertThat(loc.fullLocation()).endsWith(":12)");
|
||||
if (System.getProperty("java.version").startsWith("1.8")) {
|
||||
assertThat(loc.location()).isEqualTo("sun.reflect.NativeMethodAccessorImpl.invoke0");
|
||||
} else {
|
||||
assertThat(loc.location()).isEqualTo("java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0");
|
||||
}
|
||||
assertThat(loc.label()).isEqualTo("NativeMethodAccessorImpl.invoke0");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void basic_trimPackage() {
|
||||
|
||||
BasicProfileLocation loc = new BasicProfileLocation("com.foo.Bar.all");
|
||||
assertThat(loc.obtain()).isFalse();
|
||||
assertThat(loc.fullLocation()).isEqualTo("com.foo.Bar.all");
|
||||
assertThat(loc.location()).isEqualTo("com.foo.Bar.all");
|
||||
assertThat(loc.label()).isEqualTo("Bar.all");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void basic_trimSinglePackage() {
|
||||
|
||||
BasicProfileLocation loc = new BasicProfileLocation("foo.Bar.all");
|
||||
assertThat(loc.obtain()).isFalse();
|
||||
assertThat(loc.fullLocation()).isEqualTo("foo.Bar.all");
|
||||
assertThat(loc.location()).isEqualTo("foo.Bar.all");
|
||||
assertThat(loc.label()).isEqualTo("Bar.all");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package io.ebeaninternal.server.profile;
|
||||
|
||||
import io.ebean.meta.BasicMetricVisitor;
|
||||
import io.ebean.meta.MetaTimedMetric;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class DTimedMetricMapTest {
|
||||
|
||||
@Test
|
||||
public void addSinceNanos() throws InterruptedException {
|
||||
|
||||
DTimedMetricMap metricMap = new DTimedMetricMap("addSinceNanos");
|
||||
|
||||
long nanos = System.nanoTime();
|
||||
Thread.sleep(10);
|
||||
|
||||
metricMap.addSinceNanos("some", nanos);
|
||||
|
||||
BasicMetricVisitor visitor = new BasicMetricVisitor();
|
||||
metricMap.visit(visitor);
|
||||
|
||||
MetaTimedMetric timedMetric = visitor.timedMetrics().get(0);
|
||||
assertThat(timedMetric.count()).isEqualTo(1);
|
||||
assertThat(timedMetric.total()).isGreaterThan(10);
|
||||
|
||||
metricMap.addSinceNanos("some", nanos);
|
||||
|
||||
visitor = new BasicMetricVisitor();
|
||||
metricMap.visit(visitor);
|
||||
|
||||
timedMetric = visitor.timedMetrics().get(0);
|
||||
assertThat(timedMetric.count()).isEqualTo(1);
|
||||
assertThat(timedMetric.total()).isGreaterThan(10);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package io.ebeaninternal.server.profile;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class DTimedMetricTest {
|
||||
|
||||
@Test
|
||||
public void addSinceNanos() throws InterruptedException {
|
||||
|
||||
DTimedMetric metric = new DTimedMetric("addSinceNanos");
|
||||
|
||||
long start = System.nanoTime();
|
||||
Thread.sleep(10);
|
||||
|
||||
metric.addSinceNanos(start);
|
||||
|
||||
DTimeMetricStats stats = metric.collect(true);
|
||||
assertThat(stats.count()).isEqualTo(1);
|
||||
assertThat(stats.total()).isGreaterThan(10);
|
||||
assertThat(stats.max()).isEqualTo(stats.total());
|
||||
|
||||
metric.addSinceNanos(start);
|
||||
|
||||
stats = metric.collect(true);
|
||||
assertThat(stats.count()).isEqualTo(1);
|
||||
assertThat(stats.total()).isGreaterThan(10);
|
||||
assertThat(stats.max()).isEqualTo(stats.total());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addBatchSince() throws InterruptedException {
|
||||
|
||||
DTimedMetric metric = new DTimedMetric("addSinceNanos");
|
||||
|
||||
long start = System.nanoTime();
|
||||
Thread.sleep(10);
|
||||
|
||||
metric.addBatchSince(start, 5);
|
||||
|
||||
DTimeMetricStats stats = metric.collect(true);
|
||||
assertThat(stats.count()).isEqualTo(5);
|
||||
assertThat(stats.total()).isGreaterThan(10000);
|
||||
assertThat(stats.max()).isEqualTo(stats.total() / 5);
|
||||
assertThat(stats.max()).isGreaterThan(10000 / 5);
|
||||
|
||||
metric.addBatchSince(start, 2);
|
||||
|
||||
stats = metric.collect(true);
|
||||
assertThat(stats.count()).isEqualTo(2);
|
||||
assertThat(stats.total()).isGreaterThan(10000);
|
||||
assertThat(stats.max()).isEqualTo(stats.total() / 2);
|
||||
}
|
||||
}
|
||||
Loaded 100 of 1650 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user