mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-22 15:10:02 +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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user