mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#919 - io.ebean package initial
This commit is contained in:
@@ -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.Test;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class CacheBeanDataTest extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void extract_load_on_customer() {
|
||||
|
||||
SpiEbeanServer server = spiEbeanServer();
|
||||
BeanDescriptor<Customer> desc = server.getBeanDescriptor(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((short) 12);
|
||||
billingAddress.setCity("Auckland");
|
||||
billingAddress.setCountry(server.getReference(Country.class, "NZ"));
|
||||
billingAddress.setLine1("92 Someplace Else");
|
||||
c.setBillingAddress(billingAddress);
|
||||
|
||||
((EntityBean) c)._ebean_getIntercept().setNewBeanForUpdate();
|
||||
|
||||
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.getBeanDescriptor(EPerson.class);
|
||||
BeanPropertyAssocOne<?> addressBeanProperty = (BeanPropertyAssocOne<?>) desc.getBeanProperty("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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
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.tests.model.basic.Address;
|
||||
import org.tests.model.basic.Car;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.sql.Date;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class CachedBeanDataFromBeanTest extends BaseTestCase {
|
||||
|
||||
SpiEbeanServer server = spiEbeanServer();
|
||||
|
||||
@Test
|
||||
public void extract() throws Exception {
|
||||
|
||||
BeanDescriptor<Customer> desc = server.getBeanDescriptor(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(Short.valueOf("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.getBeanDescriptor(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());
|
||||
}
|
||||
}
|
||||
+138
@@ -0,0 +1,138 @@
|
||||
package io.ebeaninternal.server.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
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.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.Assert.assertEquals;
|
||||
import static org.junit.Assert.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 = Ebean.find(Customer.class)
|
||||
.orderBy().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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package io.ebeaninternal.server.cache;
|
||||
|
||||
import io.ebean.cache.ServerCache;
|
||||
import io.ebean.cache.ServerCacheFactory;
|
||||
import io.ebean.cache.ServerCacheOptions;
|
||||
import io.ebean.cache.ServerCacheType;
|
||||
import io.ebean.config.CurrentTenantProvider;
|
||||
import org.tests.model.basic.Contact;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
public class DefaultCacheHolderTest {
|
||||
|
||||
private String tenantId;
|
||||
|
||||
private final ServerCacheFactory cacheFactory = new DefaultServerCacheFactory();
|
||||
private final ServerCacheOptions defaultOptions = new ServerCacheOptions();
|
||||
|
||||
@Test
|
||||
public void getCache_normal() throws Exception {
|
||||
|
||||
DefaultCacheHolder holder = new DefaultCacheHolder(cacheFactory, defaultOptions, defaultOptions, null);
|
||||
|
||||
DefaultServerCache cache = cache(holder, Customer.class, "customer");
|
||||
assertThat(cache.getName()).isEqualTo("customer_B");
|
||||
|
||||
DefaultServerCache cache1 = cache(holder, Customer.class, "customer");
|
||||
assertThat(cache1).isSameAs(cache);
|
||||
|
||||
DefaultServerCache cache2 = cache(holder, Contact.class, "contact");
|
||||
assertThat(cache1).isNotSameAs(cache2);
|
||||
assertThat(cache2.getName()).isEqualTo("contact_B");
|
||||
}
|
||||
|
||||
private DefaultServerCache cache(DefaultCacheHolder holder, Class<?> type, String name) {
|
||||
Supplier<ServerCache> cache = holder.getCache(type, name, ServerCacheType.BEAN);
|
||||
return (DefaultServerCache) cache.get();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getCache_multiTenant() throws Exception {
|
||||
|
||||
DefaultCacheHolder holder = new DefaultCacheHolder(cacheFactory, defaultOptions, defaultOptions, new MyTenantProv());
|
||||
|
||||
tenantId = "ten_1";
|
||||
DefaultServerCache cache = cache(holder, Customer.class, "customer");
|
||||
assertThat(cache.getName()).isEqualTo("customer_ten_1_B");
|
||||
|
||||
tenantId = "ten_2";
|
||||
DefaultServerCache cache2 = cache(holder, Customer.class, "customer");
|
||||
assertThat(cache2).isNotSameAs(cache);
|
||||
assertThat(cache2.getName()).isEqualTo("customer_ten_2_B");
|
||||
|
||||
tenantId = "ten_1";
|
||||
DefaultServerCache cache3 = cache(holder, Customer.class, "customer");
|
||||
assertThat(cache3).isSameAs(cache);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void clearAll() throws Exception {
|
||||
DefaultCacheHolder holder = new DefaultCacheHolder(cacheFactory, defaultOptions, defaultOptions, null);
|
||||
DefaultServerCache cache = cache(holder, Customer.class, "customer");
|
||||
cache.put("foo", "foo");
|
||||
assertThat(cache.size()).isEqualTo(1);
|
||||
holder.clearAll();
|
||||
|
||||
assertThat(cache.size()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void clearAll_multiTenant() throws Exception {
|
||||
DefaultCacheHolder holder = new DefaultCacheHolder(cacheFactory, defaultOptions, defaultOptions, new MyTenantProv());
|
||||
DefaultServerCache cache = cache(holder, Customer.class, "customer");
|
||||
cache.put("foo", "foo");
|
||||
assertThat(cache.size()).isEqualTo(1);
|
||||
|
||||
holder.clearAll();
|
||||
assertThat(cache.size()).isEqualTo(0);
|
||||
}
|
||||
|
||||
private class MyTenantProv implements CurrentTenantProvider {
|
||||
|
||||
@Override
|
||||
public String currentId() {
|
||||
return tenantId;
|
||||
}
|
||||
}
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package io.ebeaninternal.server.cache;
|
||||
|
||||
import io.ebean.cache.ServerCacheOptions;
|
||||
import io.ebean.cache.ServerCacheType;
|
||||
import org.tests.model.basic.Article;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.Product;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class DefaultCacheHolder_getCacheOptions_Test {
|
||||
|
||||
private DefaultCacheHolder cacheHolder;
|
||||
|
||||
public DefaultCacheHolder_getCacheOptions_Test() {
|
||||
|
||||
ServerCacheOptions defaultOptions = new ServerCacheOptions();
|
||||
defaultOptions.setMaxSize(10000);
|
||||
defaultOptions.setMaxSecsToLive(120);
|
||||
|
||||
this.cacheHolder = new DefaultCacheHolder(null, defaultOptions, defaultOptions, null);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
package io.ebeaninternal.server.cache;
|
||||
|
||||
import io.ebean.cache.ServerCacheFactory;
|
||||
import io.ebean.cache.ServerCacheOptions;
|
||||
import io.ebean.config.CurrentTenantProvider;
|
||||
import org.tests.model.basic.Contact;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.StrictAssertions.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class DefaultServerCacheManagerTest {
|
||||
|
||||
private String tenantId;
|
||||
|
||||
private final ServerCacheFactory cacheFactory = new DefaultServerCacheFactory();
|
||||
|
||||
private DefaultServerCacheManager manager = new DefaultServerCacheManager(true, null, cacheFactory, new ServerCacheOptions(), new ServerCacheOptions());
|
||||
|
||||
private DefaultServerCacheManager multiTenantManager = new DefaultServerCacheManager(true, new MyTenantProv(), cacheFactory, new ServerCacheOptions(), new ServerCacheOptions());
|
||||
|
||||
@Test
|
||||
public void getCache_normal() throws Exception {
|
||||
|
||||
|
||||
DefaultServerCache cache = cache(manager, Customer.class);
|
||||
assertThat(cache.getName()).isEqualTo("org.tests.model.basic.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");
|
||||
|
||||
|
||||
DefaultServerCache natKeyCache = (DefaultServerCache) manager.getNaturalKeyCache(Customer.class).get();
|
||||
assertThat(natKeyCache.getName()).isEqualTo("org.tests.model.basic.Customer_N");
|
||||
|
||||
DefaultServerCache queryCache = (DefaultServerCache) manager.getQueryCache(Customer.class).get();
|
||||
assertThat(queryCache.getName()).isEqualTo("org.tests.model.basic.Customer_Q");
|
||||
|
||||
DefaultServerCache collCache = (DefaultServerCache) manager.getCollectionIdsCache(Customer.class, "contacts").get();
|
||||
assertThat(collCache.getName()).isEqualTo("org.tests.model.basic.Customer.contacts_C");
|
||||
}
|
||||
|
||||
private DefaultServerCache cache(DefaultServerCacheManager manager, Class<?> beanType) {
|
||||
return (DefaultServerCache) manager.getBeanCache(beanType).get();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getCache_multiTenant() throws Exception {
|
||||
|
||||
tenantId = "ten1";
|
||||
DefaultServerCache cache = cache(multiTenantManager, Customer.class);
|
||||
assertThat(cache.getName()).isEqualTo("org.tests.model.basic.Customer_ten1_B");
|
||||
|
||||
tenantId = "ten2";
|
||||
DefaultServerCache cache1 = cache(multiTenantManager, Customer.class);
|
||||
assertThat(cache1).isNotSameAs(cache);
|
||||
assertThat(cache1.getName()).isEqualTo("org.tests.model.basic.Customer_ten2_B");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isLocalL2Caching() throws Exception {
|
||||
|
||||
assertTrue(manager.isLocalL2Caching());
|
||||
assertTrue(multiTenantManager.isLocalL2Caching());
|
||||
}
|
||||
|
||||
|
||||
private class MyTenantProv implements CurrentTenantProvider {
|
||||
|
||||
@Override
|
||||
public String currentId() {
|
||||
return tenantId;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package io.ebeaninternal.server.cache;
|
||||
|
||||
import io.ebean.cache.ServerCacheOptions;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class DefaultServerCacheTest {
|
||||
|
||||
private DefaultServerCache createCache() {
|
||||
|
||||
ServerCacheOptions cacheOptions = new ServerCacheOptions();
|
||||
cacheOptions.setMaxSize(100);
|
||||
cacheOptions.setMaxIdleSecs(60);
|
||||
cacheOptions.setMaxSecsToLive(600);
|
||||
cacheOptions.setTrimFrequency(60);
|
||||
|
||||
return new DefaultServerCache("foo", cacheOptions);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetHitRatio() throws Exception {
|
||||
|
||||
DefaultServerCache cache = createCache();
|
||||
assertEquals(0, cache.getHitRatio());
|
||||
cache.put("A", "A");
|
||||
cache.get("A");
|
||||
assertEquals(100, cache.getHitRatio());
|
||||
cache.get("B");
|
||||
assertEquals(50, cache.getHitRatio());
|
||||
cache.get("B");
|
||||
cache.get("B");
|
||||
assertEquals(25, cache.getHitRatio());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSize() throws Exception {
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void trimFreq_halfIdle() throws Exception {
|
||||
|
||||
DefaultServerCache cache = new DefaultServerCache("", null, 10000, 10, 20, 0);
|
||||
assertEquals(cache.trimFrequency, 4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void trimFreq_halfIdle_withRounding() throws Exception {
|
||||
|
||||
DefaultServerCache cache = new DefaultServerCache("", null, 10000, 11, 20, 0);
|
||||
assertEquals(cache.trimFrequency, 4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void trimFreq_halfTTL() throws Exception {
|
||||
|
||||
DefaultServerCache cache = new DefaultServerCache("", null, 10000, 0, 20, 0);
|
||||
assertEquals(cache.trimFrequency, 9);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void trimFreq_halfTTL_withRounding() throws Exception {
|
||||
|
||||
DefaultServerCache cache = new DefaultServerCache("", null, 10000, 0, 21, 0);
|
||||
assertEquals(cache.trimFrequency, 9);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void trimFreq_explicit() throws Exception {
|
||||
|
||||
DefaultServerCache cache = new DefaultServerCache("", null, 10000, 10, 20, 42);
|
||||
assertEquals(cache.trimFrequency, 42);
|
||||
}
|
||||
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package io.ebeaninternal.server.cache;
|
||||
|
||||
import io.ebean.cache.ServerCacheOptions;
|
||||
import io.ebean.cache.ServerCacheStatistics;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.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);
|
||||
|
||||
return new DefaultServerCache("foo", cacheOptions);
|
||||
}
|
||||
|
||||
private final DefaultServerCache cache;
|
||||
|
||||
private final Random random = new Random();
|
||||
|
||||
public DefaultServerCache_RunEvictionTest() {
|
||||
this.cache = createCache();
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void runEvict() throws InterruptedException {
|
||||
|
||||
for (int i = 0; i < 15; i++) {
|
||||
doStuff();
|
||||
cache.runEviction();
|
||||
ServerCacheStatistics statistics = cache.getStatistics(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