mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-19 23:02:24 +00:00
Refactor move tests from ebean-core to ebean-test
This commit is contained in:
1 parent
0b00a14fd3
commit
eab56d323b
1650 files changed
+185
-147
No files matched your search
@@ -0,0 +1,191 @@
|
||||
package org.tests.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.cache.ServerCache;
|
||||
import io.ebean.cache.ServerCacheStatistics;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.tests.model.basic.Country;
|
||||
import org.tests.model.basic.OCachedBean;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
/**
|
||||
* Test class testing deleting/invalidating of cached beans
|
||||
*/
|
||||
public class TestBeanCache extends BaseTestCase {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(TestBeanCache.class);
|
||||
|
||||
private final ServerCache beanCache = DB.getDefault().cacheManager().beanCache(OCachedBean.class);
|
||||
|
||||
@Test
|
||||
public void findById_when_idTypeConverted() {
|
||||
|
||||
OCachedBean bean = new OCachedBean();
|
||||
bean.setName("findById");
|
||||
DB.save(bean);
|
||||
|
||||
OCachedBean bean0 = DB.find(OCachedBean.class, bean.getId());
|
||||
assertNotNull(bean0);
|
||||
|
||||
// expect to hit the cache, no SQL
|
||||
LoggedSqlCollector.start();
|
||||
OCachedBean bean1 = DB.find(OCachedBean.class, bean.getId());
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
assertNotNull(bean1);
|
||||
assertThat(sql).isEmpty();
|
||||
|
||||
// expect to hit the cache, no SQL
|
||||
LoggedSqlCollector.start();
|
||||
OCachedBean bean2 = DB.find(OCachedBean.class).setReadOnly(true).setId(String.valueOf(bean.getId())).findOne();
|
||||
sql = LoggedSqlCollector.stop();
|
||||
assertNotNull(bean2);
|
||||
assertThat(sql).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void idsIn_explicitCache_expect_cachePut() {
|
||||
|
||||
List<OCachedBean> beans = createBeans(Arrays.asList("k0","k1"));
|
||||
List<Long> ids = beans.stream().map(OCachedBean::getId).collect(Collectors.toList());
|
||||
|
||||
beanCache.clear();
|
||||
beanCache.statistics(true);
|
||||
try (Transaction transaction = DB.beginTransaction()) {
|
||||
|
||||
// skipCacheAfterWrite set after this write ...
|
||||
final OCachedBean junk = createBean("junk");
|
||||
DB.save(junk);
|
||||
|
||||
List<OCachedBean> list = DB.find(OCachedBean.class)
|
||||
.where().idIn(ids)
|
||||
.setUseCache(true)
|
||||
.findList();
|
||||
|
||||
assertThat(list).hasSize(2);
|
||||
transaction.commit();
|
||||
}
|
||||
|
||||
ServerCacheStatistics statistics = beanCache.statistics(true);
|
||||
assertThat(statistics.getHitCount()).isEqualTo(0);
|
||||
assertThat(statistics.getMissCount()).isEqualTo(2);
|
||||
assertThat(statistics.getPutCount()).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void idsInExpression() {
|
||||
|
||||
List<OCachedBean> beans = createBeans(Arrays.asList("z0", "z1", "z2"));
|
||||
List<Long> ids = beans.stream().map(OCachedBean::getId).collect(Collectors.toList());
|
||||
|
||||
beanCache.clear();
|
||||
beanCache.statistics(true);
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
log.info("All misses (0 of 3) ...");
|
||||
List<OCachedBean> list = DB.find(OCachedBean.class)
|
||||
.where().idIn(ids)
|
||||
.setUseCache(true)
|
||||
.findList();
|
||||
|
||||
assertThat(list).hasSize(3);
|
||||
assertBeanCacheHitMiss(0, 3);
|
||||
List<String> sql = LoggedSqlCollector.current();
|
||||
assertThat(sql).hasSize(1);
|
||||
if (isH2()) {
|
||||
assertSql(sql.get(0)).contains("from o_cached_bean t0 where t0.id in (?,?,?,?,?)");
|
||||
}
|
||||
|
||||
log.info("All hits (3 of 3) ...");
|
||||
list = DB.find(OCachedBean.class)
|
||||
.where().idIn(ids)
|
||||
.setUseCache(true)
|
||||
.findList();
|
||||
|
||||
assertBeanCacheHitMiss(3, 0);
|
||||
assertThat(list).hasSize(3);
|
||||
sql = LoggedSqlCollector.current();
|
||||
assertThat(sql).hasSize(0); // no misses
|
||||
|
||||
// remove a bean so that we get a "partial" hit (2 out of 3 in cache)
|
||||
beanCache.remove(beans.get(0).getId().toString());
|
||||
|
||||
log.info("Partial hits (2 of 3) ...");
|
||||
list = DB.find(OCachedBean.class)
|
||||
.where().idIn(ids)
|
||||
.setUseCache(true)
|
||||
.findList();
|
||||
|
||||
assertBeanCacheHitMiss(2, 1);
|
||||
assertThat(list).hasSize(3);
|
||||
sql = LoggedSqlCollector.current();
|
||||
assertThat(sql).hasSize(1);
|
||||
if (isH2()) {
|
||||
// fetch the miss from DB
|
||||
assertSql(sql.get(0)).contains("from o_cached_bean t0 where t0.id in (?)");
|
||||
}
|
||||
|
||||
// remove beans so that we get a "partial" hit (1 out of 3 in cache)
|
||||
beanCache.remove(beans.get(1).getId().toString());
|
||||
beanCache.remove(beans.get(2).getId().toString());
|
||||
|
||||
log.info("Partial hits (1 of 3) ...");
|
||||
list = DB.find(OCachedBean.class)
|
||||
.where().idIn(ids)
|
||||
.setUseCache(true)
|
||||
.findList();
|
||||
|
||||
assertBeanCacheHitMiss(1, 2);
|
||||
assertThat(list).hasSize(3);
|
||||
sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(1);
|
||||
if (isH2()) {
|
||||
// fetch the misses from DB
|
||||
assertSql(sql.get(0)).contains("from o_cached_bean t0 where t0.id in (?,?,?,?,?)");
|
||||
}
|
||||
}
|
||||
|
||||
private void assertBeanCacheHitMiss(int hitCount, int missCount) {
|
||||
ServerCacheStatistics statistics = beanCache.statistics(true);
|
||||
assertThat(statistics.getHitCount()).isEqualTo(hitCount);
|
||||
assertThat(statistics.getMissCount()).isEqualTo(missCount);
|
||||
}
|
||||
|
||||
private List<OCachedBean> createBeans(List<String> names) {
|
||||
List<OCachedBean> beans = new ArrayList<>();
|
||||
for (String name : names) {
|
||||
beans.add(createBean(name));
|
||||
}
|
||||
DB.saveAll(beans);
|
||||
return beans;
|
||||
}
|
||||
|
||||
private OCachedBean createBean(String name) {
|
||||
OCachedBean bean = new OCachedBean();
|
||||
bean.setName(name);
|
||||
return bean;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void find_whenNotExits() {
|
||||
|
||||
Country country = DB.find(Country.class)
|
||||
.where()
|
||||
.eq("name","NotValid")
|
||||
.findOne();
|
||||
|
||||
assertThat(country).isNull();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package org.tests.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebeantest.LoggedSql;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Contact;
|
||||
import org.tests.model.basic.Customer;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Test class testing a wrong behaviour of the bean cache.
|
||||
*/
|
||||
public class TestBeanCacheContactLazyLoad extends BaseTestCase {
|
||||
|
||||
/**
|
||||
* This test shows a wrong behaviour of the bean cache up to at least Ebean 12.4.*:
|
||||
* <ul>
|
||||
* <li>bean partially fetched via natural key, filling the cache</li>
|
||||
* <li>bean modified using a setter</li>
|
||||
* <li>getter called on non-loaded property to trigger lazy load</li>
|
||||
* <li>bean fetched again using the same natural key, to hit cache</li>
|
||||
* <li>it is expected, that the fetched bean does not contain the modification from before</li>
|
||||
* </ul>
|
||||
*/
|
||||
@Test
|
||||
public void testBeanCacheWithLazyLoading() {
|
||||
final Customer customer = new Customer();
|
||||
customer.setName("Customer");
|
||||
customer.setAnniversary(Date.valueOf(LocalDate.of(2010, 1, 1)));
|
||||
DB.save(customer);
|
||||
|
||||
final Contact contact = new Contact();
|
||||
contact.setFirstName("Tim");
|
||||
contact.setLastName("Button");
|
||||
contact.setPhone("1234567890");
|
||||
contact.setMobile("4567890123");
|
||||
contact.setEmail("tim@button.com");
|
||||
contact.setCustomer(customer);
|
||||
|
||||
DB.save(contact);
|
||||
|
||||
// Only get two properties, so we have to lazy-load later
|
||||
final Contact contactDb = DB.find(Contact.class).where().eq("email", "tim@button.com").select("email,lastName").findOne();
|
||||
assertThat(contactDb).isNotNull();
|
||||
LoggedSql.start();
|
||||
contactDb.setLastName("Buttonnnn");
|
||||
List<String> sql = LoggedSql.collect();
|
||||
assertThat(sql).isEmpty(); // setter did not trigger lazy load
|
||||
|
||||
// trigger lazy load
|
||||
assertThat(contactDb.getPhone()).isEqualTo("1234567890");
|
||||
sql = LoggedSql.collect();
|
||||
assertThat(sql).isNotEmpty(); // Lazy-load took place
|
||||
|
||||
final Contact contactDb2 = DB.find(Contact.class).where().eq("email", "tim@button.com").select("email,lastName").findOne();
|
||||
sql = LoggedSql.stop();
|
||||
assertThat(sql).isEmpty(); // We expect that the bean was loaded from cache
|
||||
assertThat(contactDb2).isNotNull();
|
||||
assertThat(contactDb2.getLastName()).isEqualTo("Button");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package org.tests.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.OCachedBean;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
/**
|
||||
* Test class testing clearing cache with delete query
|
||||
*/
|
||||
public class TestBeanCacheWithDeleteQuery extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testBeanCacheGetsClearedOnDelete() {
|
||||
|
||||
OCachedBean bean = new OCachedBean();
|
||||
bean.setName("cache-invalidation");
|
||||
DB.save(bean);
|
||||
|
||||
OCachedBean bean0 = DB.find(OCachedBean.class, bean.getId());
|
||||
assertNotNull(bean0);
|
||||
|
||||
// expect to hit the cache, no SQL
|
||||
LoggedSqlCollector.start();
|
||||
OCachedBean bean1 = DB.find(OCachedBean.class, bean.getId());
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
assertNotNull(bean1);
|
||||
assertThat(sql).isEmpty();
|
||||
|
||||
// delete the bean
|
||||
int deleted = DB.createQuery(OCachedBean.class).where().eq("name", "cache-invalidation").delete();
|
||||
|
||||
assertEquals(deleted, 1);
|
||||
|
||||
// expect not to hit the cache, expect SQL
|
||||
LoggedSqlCollector.start();
|
||||
OCachedBean bean2 = DB.find(OCachedBean.class).setReadOnly(true).setId(String.valueOf(bean.getId())).findOne();
|
||||
sql = LoggedSqlCollector.stop();
|
||||
assertNull(bean2);
|
||||
assertThat(sql).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBeanCacheGetsClearedOnUpdate() {
|
||||
|
||||
OCachedBean bean = new OCachedBean();
|
||||
bean.setName("cache-invalidation-upd");
|
||||
DB.save(bean);
|
||||
|
||||
OCachedBean bean0 = DB.find(OCachedBean.class, bean.getId());
|
||||
assertNotNull(bean0);
|
||||
|
||||
// expect to hit the cache, no SQL
|
||||
LoggedSqlCollector.start();
|
||||
OCachedBean bean1 = DB.find(OCachedBean.class, bean.getId());
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
assertNotNull(bean1);
|
||||
assertThat(sql).isEmpty();
|
||||
|
||||
// update the bean
|
||||
int updated = DB.update(OCachedBean.class).set("name", "updatedName").where().eq("name","cache-invalidation-upd").update();
|
||||
|
||||
assertEquals(updated, 1);
|
||||
|
||||
// expect not to hit the cache, expect SQL
|
||||
LoggedSqlCollector.start();
|
||||
OCachedBean bean2 = DB.find(OCachedBean.class).setReadOnly(true).setId(String.valueOf(bean.getId())).findOne();
|
||||
sql = LoggedSqlCollector.stop();
|
||||
assertNotNull(bean2);
|
||||
assertThat(sql).isNotEmpty();
|
||||
assertEquals("updatedName", bean2.getName());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
package org.tests.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.FetchGroup;
|
||||
import io.ebean.cache.ServerCache;
|
||||
import io.ebean.cache.ServerCacheStatistics;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static io.ebean.CacheMode.ON;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestBeanFetchJoinCache extends BaseTestCase {
|
||||
|
||||
private final ServerCache customerBeanCache = server().cacheManager().beanCache(Customer.class);
|
||||
|
||||
@Test
|
||||
public void fetchCache_when_allHits() {
|
||||
initDataClearCache();
|
||||
loadCustomerBeanCache();
|
||||
customerBeanCache.statistics(true);
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
List<Order> orders = DB.find(Order.class)
|
||||
.fetchCache("customer", "status, name")
|
||||
.findList();
|
||||
|
||||
final List<String> sql0 = LoggedSqlCollector.current();
|
||||
assertThat(sql0).hasSize(1);
|
||||
|
||||
final ServerCacheStatistics statistics = customerBeanCache.statistics(true);
|
||||
assertThat(statistics.getHitCount()).isEqualTo(2);
|
||||
|
||||
assertThat(trimSql(sql0.get(0))).doesNotContain("t1.status");
|
||||
|
||||
for (Order order : orders) {
|
||||
final Customer customer = order.getCustomer();
|
||||
assertThat(customer.getName()).isNotNull();
|
||||
assertThat(customer.getStatus()).isNotNull();
|
||||
}
|
||||
|
||||
assertThat(LoggedSqlCollector.stop()).isEmpty();
|
||||
}
|
||||
|
||||
private void loadCustomerBeanCache() {
|
||||
DB.find(Customer.class)
|
||||
.setBeanCacheMode(ON)
|
||||
.findList();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fetchCache_when_someHits() {
|
||||
initDataClearCache();
|
||||
|
||||
DB.find(Customer.class)
|
||||
.setBeanCacheMode(ON)
|
||||
.where().lt("id", 2)
|
||||
.findList();
|
||||
|
||||
customerBeanCache.statistics(true);
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
List<Order> orders = DB.find(Order.class)
|
||||
.fetchCache("customer")
|
||||
.findList();
|
||||
|
||||
final List<String> sql0 = LoggedSqlCollector.current();
|
||||
assertThat(sql0).hasSize(2);
|
||||
assertThat(sql0.get(0)).contains(" from o_order ");
|
||||
assertThat(sql0.get(1)).contains(" from o_customer t0 where t0.id ");
|
||||
|
||||
final ServerCacheStatistics statistics = customerBeanCache.statistics(true);
|
||||
assertThat(statistics.getHitCount()).isEqualTo(1);
|
||||
|
||||
for (Order order : orders) {
|
||||
final Customer customer = order.getCustomer();
|
||||
assertThat(customer.getName()).isNotNull();
|
||||
assertThat(customer.getStatus()).isNotNull();
|
||||
}
|
||||
|
||||
assertThat(LoggedSqlCollector.stop()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fetchCache_when_hitsButBeanCachePartiallyLoaded() {
|
||||
initDataClearCache();
|
||||
|
||||
DB.find(Customer.class)
|
||||
.setBeanCacheMode(ON)
|
||||
.select("name") // not included status in bean cache
|
||||
.findList();
|
||||
|
||||
customerBeanCache.statistics(true);
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
List<Order> orders = DB.find(Order.class)
|
||||
.fetchCache("customer")
|
||||
.findList();
|
||||
|
||||
for (Order order : orders) {
|
||||
assertThat(order.getCustomer().getName()).isNotNull();
|
||||
}
|
||||
final List<String> sql0 = LoggedSqlCollector.current();
|
||||
assertThat(sql0).hasSize(1);
|
||||
|
||||
final ServerCacheStatistics statistics = customerBeanCache.statistics(true);
|
||||
assertThat(statistics.getHitCount()).isEqualTo(2);
|
||||
|
||||
assertThat(trimSql(sql0.get(0))).doesNotContain("t1.status");
|
||||
|
||||
for (Order order : orders) {
|
||||
final Customer customer = order.getCustomer();
|
||||
assertThat(customer.getName()).isNotNull();
|
||||
assertThat(customer.getStatus()).isNotNull(); // We cache miss on property(status)
|
||||
}
|
||||
|
||||
// assert we hit the DB the second time around
|
||||
final List<String> sql1 = LoggedSqlCollector.stop();
|
||||
assertThat(sql1).hasSize(1);
|
||||
assertThat(sql1.get(0)).contains(" from o_customer t0 ");
|
||||
|
||||
// assert we didn't hit the L2 bean cache the second time around
|
||||
final ServerCacheStatistics statistics1 = customerBeanCache.statistics(true);
|
||||
assertThat(statistics1.getHitCount()).isGreaterThan(0);
|
||||
}
|
||||
|
||||
private final FetchGroup<Order> fgBasic = FetchGroup.of(Order.class)
|
||||
.fetchCache("customer")
|
||||
.build();
|
||||
|
||||
@Test
|
||||
public void fetchGroup_fetchCache() {
|
||||
initDataClearCache();
|
||||
loadCustomerBeanCache();
|
||||
customerBeanCache.statistics(true);
|
||||
|
||||
List<Order> orders = DB.find(Order.class)
|
||||
.select(fgBasic)
|
||||
.findList();
|
||||
|
||||
assertThat(orders).isNotEmpty();
|
||||
|
||||
final ServerCacheStatistics statistics1 = customerBeanCache.statistics(true);
|
||||
assertThat(statistics1.getHitCount()).isEqualTo(2);
|
||||
}
|
||||
|
||||
private final FetchGroup<Order> fgCachePartial = FetchGroup.of(Order.class)
|
||||
.fetchCache("customer", "name")
|
||||
.build();
|
||||
|
||||
@Test
|
||||
public void fetchGroup_fetchCache_partial() {
|
||||
initDataClearCache();
|
||||
customerBeanCache.statistics(true);
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
List<Order> orders = DB.find(Order.class)
|
||||
.select(fgCachePartial)
|
||||
.findList();
|
||||
|
||||
assertThat(orders).isNotEmpty();
|
||||
|
||||
final ServerCacheStatistics statistics1 = customerBeanCache.statistics(true);
|
||||
assertThat(statistics1.getMissCount()).isEqualTo(2);
|
||||
|
||||
final List<String> sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(2);
|
||||
assertSql(sql.get(0)).contains(" from o_order ");
|
||||
assertSql(sql.get(1)).contains(" from o_customer ");
|
||||
}
|
||||
|
||||
private void initDataClearCache() {
|
||||
ResetBasicData.reset();
|
||||
server().cacheManager().clearAll();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
package org.tests.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.cache.ServerCache;
|
||||
import io.ebean.cache.ServerCacheStatistics;
|
||||
import org.tests.model.basic.Country;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestCacheBasic extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
DB.cacheManager().clear(Country.class);
|
||||
ServerCache countryCache = DB.cacheManager().beanCache(Country.class);
|
||||
|
||||
loadCountryCache();
|
||||
assertTrue(countryCache.size() > 0);
|
||||
|
||||
// reset the statistics
|
||||
countryCache.statistics(true);
|
||||
|
||||
Country c0 = DB.reference(Country.class, "NZ");
|
||||
ServerCacheStatistics statistics = countryCache.statistics(false);
|
||||
long hc = statistics.getHitCount();
|
||||
assertEquals(1, hc);
|
||||
assertNotNull(c0);
|
||||
|
||||
// Country c1 = DB.reference(Country.class, "NZ");
|
||||
// assertEquals(2, countryCache.getStatistics(false).getHitCount());
|
||||
// //assertEquals(100,
|
||||
// countryCache.getStatistics(false).getHitRatio());
|
||||
//
|
||||
// // same instance as caching with readOnly=true
|
||||
// assertTrue(c0 != c1);
|
||||
//
|
||||
// c0.getName();
|
||||
// c1.getName();
|
||||
//
|
||||
// // reset the statistics
|
||||
// assertEquals(2,countryCache.getStatistics(true).getHitCount());
|
||||
// // now the count should be 0 again
|
||||
// assertEquals(0, countryCache.getStatistics(false).getHitCount());
|
||||
// // and hitRatio is 0 as well
|
||||
// assertEquals(0, countryCache.getStatistics(false).getHitRatio());
|
||||
//
|
||||
// // hit the country cache automatically via join
|
||||
//
|
||||
// Customer custTest = ResetBasicData.createCustAndOrder("cacheBasic");
|
||||
// Integer id = custTest.getId();
|
||||
// Customer customer = DB.find(Customer.class, id);
|
||||
//
|
||||
// Address billingAddress = customer.getBillingAddress();
|
||||
// Country c2 = billingAddress.getCountry();
|
||||
// c2.getName();
|
||||
//
|
||||
// assertTrue(countryCache.getStatistics(false).getHitCount() > 0);
|
||||
//
|
||||
// //Country c3 = DB.reference(Country.class, "NZ");
|
||||
// //Country c4 = DB.find(Country.class, "NZ");
|
||||
//
|
||||
//
|
||||
// // clear the cache
|
||||
// DB.cacheManager().clear(Country.class);
|
||||
// // reset statistics
|
||||
// countryCache.getStatistics(true);
|
||||
//
|
||||
// // try to hit the country cache automatically via join
|
||||
// customer = DB.find(Customer.class, id);
|
||||
// billingAddress = customer.getBillingAddress();
|
||||
// Country c5 = billingAddress.getCountry();
|
||||
// // but cache is empty so c5 is reference that will load cache
|
||||
// // if it is lazy loaded
|
||||
// assertEquals("empty cache",0,countryCache.getStatistics(false).getSize());
|
||||
// //assertEquals("missCount 1",1,countryCache.getStatistics(false).getMissCount());
|
||||
//
|
||||
// // lazy load on c5 populates the cache
|
||||
// c5.getName();
|
||||
// assertEquals("cache populated via lazy load",1,countryCache.getStatistics(false).getSize());
|
||||
//
|
||||
// // now these get hits in the cache
|
||||
// Country c6 = DB.find(Country.class, "NZ");
|
||||
//
|
||||
// assertTrue("different instance as cache cleared",c2 != c5);
|
||||
// assertTrue("these 2 are different",c5 != c6);
|
||||
//
|
||||
// // by default readOnly based on deployment annotation
|
||||
// assertTrue("read only",DB.beanState(c6).isReadOnly());
|
||||
//
|
||||
// try {
|
||||
// // can't modify a readOnly bean
|
||||
// c6.setName("Nu Zilund");
|
||||
// assertFalse("Never get here",true);
|
||||
// } catch (IllegalStateException e){
|
||||
// assertTrue("This is readOnly",true);
|
||||
// }
|
||||
//
|
||||
// Country c8 = DB.find(Country.class)
|
||||
// .setId("NZ")
|
||||
// .setReadOnly(false)
|
||||
// .findOne();
|
||||
//
|
||||
// // Explicitly NOT readOnly
|
||||
// assertFalse("NOT read only",DB.beanState(c8).isReadOnly());
|
||||
//
|
||||
// assertEquals("1 countries in cache", 1, countryCache.size());
|
||||
// c8.setName("Nu Zilund");
|
||||
// // the update will remove the entry from the cache
|
||||
// DB.save(c8);
|
||||
//
|
||||
// assertEquals("1 country in cache", 1, countryCache.size());
|
||||
//
|
||||
// Country c9 = DB.find(Country.class)
|
||||
// .setReadOnly(false)
|
||||
// .setId("NZ")
|
||||
// .findOne();
|
||||
//
|
||||
// // Find loads cache ...
|
||||
// assertFalse(DB.beanState(c9).isReadOnly());
|
||||
// assertTrue(countryCache.size() > 0);
|
||||
//
|
||||
// Country c10 = DB.find(Country.class,"NZ");
|
||||
//
|
||||
// assertTrue(DB.beanState(c10).isReadOnly());
|
||||
// assertTrue(countryCache.size() > 0);
|
||||
//
|
||||
// DB.cacheManager().clear(Country.class);
|
||||
// assertEquals("0 country in cache", 0, countryCache.size());
|
||||
//
|
||||
// // reference doesn't load cache yet
|
||||
// Country c11 = DB.reference(Country.class, "NZ");
|
||||
//
|
||||
// // still 0 in cache
|
||||
// assertEquals("0 country in cache", 0, countryCache.size());
|
||||
//
|
||||
// // will invoke lazy loading..
|
||||
// c11.getName();
|
||||
// assertTrue(countryCache.size() > 0);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,439 @@
|
||||
package org.tests.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.CacheMode;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.SqlUpdate;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.Update;
|
||||
import io.ebean.cache.ServerCache;
|
||||
import io.ebean.cache.ServerCacheManager;
|
||||
import io.ebeaninternal.server.cache.CachedManyIds;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Contact;
|
||||
import org.tests.model.basic.Country;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.OBeanChild;
|
||||
import org.tests.model.basic.OCachedBean;
|
||||
import org.tests.model.basic.OCachedBeanChild;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.OrderDetail;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
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 TestCacheCollectionIds extends BaseTestCase {
|
||||
|
||||
private ServerCacheManager cacheManager = DB.cacheManager();
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
awaitL2Cache();
|
||||
|
||||
ServerCache custCache = cacheManager.beanCache(Customer.class);
|
||||
ServerCache custManyIdsCache = cacheManager.collectionIdsCache(Customer.class, "contacts");
|
||||
|
||||
// cacheManager.setCaching(Customer.class, true);
|
||||
// cacheManager.setCaching(Contact.class, true);
|
||||
|
||||
custCache.clear();
|
||||
custManyIdsCache.clear();
|
||||
|
||||
List<Customer> list = DB.find(Customer.class).setAutoTune(false).setBeanCacheMode(CacheMode.PUT)
|
||||
.order().asc("id").findList();
|
||||
|
||||
assertTrue(list.size() > 1);
|
||||
// Assert.assertEquals(list.size(),
|
||||
// custCache.getStatistics(false).getSize());
|
||||
|
||||
Customer customer = list.get(0);
|
||||
List<Contact> contacts = customer.getContacts();
|
||||
// Assert.assertEquals(0, custManyIdsCache.getStatistics(false).getSize());
|
||||
contacts.size();
|
||||
assertTrue(contacts.size() > 1);
|
||||
// Assert.assertEquals(1, custManyIdsCache.getStatistics(false).getSize());
|
||||
// Assert.assertEquals(0,
|
||||
// custManyIdsCache.getStatistics(false).getHitCount());
|
||||
|
||||
fetchCustomer(customer.getId());
|
||||
// Assert.assertEquals(1,
|
||||
// custManyIdsCache.getStatistics(false).getHitCount());
|
||||
|
||||
fetchCustomer(customer.getId());
|
||||
// Assert.assertEquals(2,
|
||||
// custManyIdsCache.getStatistics(false).getHitCount());
|
||||
|
||||
int currentNumContacts = fetchCustomer(customer.getId());
|
||||
// Assert.assertEquals(3,
|
||||
// custManyIdsCache.getStatistics(false).getHitCount());
|
||||
|
||||
Contact newContact = ResetBasicData.createContact("Check", "CollIds");
|
||||
newContact.setCustomer(customer);
|
||||
|
||||
DB.save(newContact);
|
||||
awaitL2Cache();
|
||||
|
||||
int currentNumContacts2 = fetchCustomer(customer.getId());
|
||||
assertEquals(currentNumContacts + 1, currentNumContacts2);
|
||||
|
||||
// cleanup
|
||||
DB.delete(newContact);
|
||||
}
|
||||
|
||||
private int fetchCustomer(Integer id) {
|
||||
|
||||
Customer customer2 = DB.find(Customer.class, id);
|
||||
|
||||
List<Contact> contacts2 = customer2.getContacts();
|
||||
|
||||
for (Contact contact : contacts2) {
|
||||
assertNotNull(contact.getFirstName());
|
||||
contact.getEmail();
|
||||
}
|
||||
return contacts2.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* When updating a ManyToMany relations also the collection cache must be updated.
|
||||
*/
|
||||
@Test
|
||||
public void testUpdatingCollectionCacheForManyToManyRelations() {
|
||||
// arrange
|
||||
ResetBasicData.reset();
|
||||
|
||||
OCachedBean cachedBean = new OCachedBean();
|
||||
cachedBean.setName("hello");
|
||||
cachedBean.getCountries().add(DB.find(Country.class, "NZ"));
|
||||
cachedBean.getCountries().add(DB.find(Country.class, "AU"));
|
||||
|
||||
DB.save(cachedBean);
|
||||
|
||||
// used to just load the cache - trigger loading
|
||||
OCachedBean dummyToLoad = DB.find(OCachedBean.class, cachedBean.getId());
|
||||
dummyToLoad.getCountries().size();
|
||||
|
||||
ServerCache cachedBeanCountriesCache = cacheManager.collectionIdsCache(OCachedBean.class, "countries");
|
||||
CachedManyIds cachedManyIds = (CachedManyIds) cachedBeanCountriesCache.get(cachedBean.getId());
|
||||
|
||||
// confirm the starting data and cache entry
|
||||
assertEquals(2, dummyToLoad.getCountries().size());
|
||||
assertEquals(2, cachedManyIds.getIdList().size());
|
||||
|
||||
|
||||
// act
|
||||
OCachedBean loadedBean = DB.find(OCachedBean.class, cachedBean.getId());
|
||||
loadedBean.getCountries().clear();
|
||||
loadedBean.getCountries().add(DB.find(Country.class, "AU"));
|
||||
|
||||
DB.save(loadedBean);
|
||||
awaitL2Cache();
|
||||
|
||||
// Get the data to assert/check against
|
||||
OCachedBean result = DB.find(OCachedBean.class, cachedBean.getId());
|
||||
cachedManyIds = (CachedManyIds) cachedBeanCountriesCache.get(result.getId());
|
||||
|
||||
// assert that data and cache both show correct data
|
||||
assertEquals(1, result.getCountries().size());
|
||||
assertEquals(1, cachedManyIds.getIdList().size());
|
||||
assertFalse(cachedManyIds.getIdList().contains("NZ"));
|
||||
assertTrue(cachedManyIds.getIdList().contains("AU"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChangingCollectionByEditingConnectedCachedBean() {
|
||||
ResetBasicData.reset();
|
||||
|
||||
OCachedBean cachedBean = new OCachedBean();
|
||||
cachedBean.setName("hello1");
|
||||
|
||||
DB.save(cachedBean);
|
||||
awaitL2Cache();
|
||||
|
||||
OCachedBean dummyToLoad = DB.find(OCachedBean.class, cachedBean.getId());
|
||||
assertEquals(0, dummyToLoad.getChildren().size());
|
||||
|
||||
OCachedBeanChild child1=new OCachedBeanChild();
|
||||
child1.setCachedBean(cachedBean);
|
||||
DB.insert(child1);
|
||||
awaitL2Cache();
|
||||
dummyToLoad = DB.find(OCachedBean.class, cachedBean.getId());
|
||||
assertEquals(1, dummyToLoad.getChildren().size());
|
||||
|
||||
OCachedBeanChild child2=new OCachedBeanChild();
|
||||
child2.setCachedBean(cachedBean);
|
||||
DB.insert(child2);
|
||||
awaitL2Cache();
|
||||
dummyToLoad = DB.find(OCachedBean.class, cachedBean.getId());
|
||||
assertEquals(2, dummyToLoad.getChildren().size());
|
||||
|
||||
DB.delete(child2);
|
||||
awaitL2Cache();
|
||||
dummyToLoad = DB.find(OCachedBean.class, cachedBean.getId());
|
||||
assertEquals(1, dummyToLoad.getChildren().size());
|
||||
|
||||
DB.delete(child1);
|
||||
awaitL2Cache();
|
||||
dummyToLoad = DB.find(OCachedBean.class, cachedBean.getId());
|
||||
assertEquals(0, dummyToLoad.getChildren().size());
|
||||
|
||||
DB.delete(cachedBean);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChangingCollectionByEditingConnectedNotCachedBean() {
|
||||
ResetBasicData.reset();
|
||||
|
||||
OCachedBean cachedBean = new OCachedBean();
|
||||
cachedBean.setName("hello2");
|
||||
|
||||
DB.save(cachedBean);
|
||||
awaitL2Cache();
|
||||
|
||||
OCachedBean dummyToLoad = DB.find(OCachedBean.class, cachedBean.getId());
|
||||
assertEquals(0, dummyToLoad.getNotCachedChildren().size());
|
||||
|
||||
OBeanChild child1=new OBeanChild();
|
||||
child1.setCachedBean(cachedBean);
|
||||
DB.insert(child1);
|
||||
awaitL2Cache();
|
||||
dummyToLoad = DB.find(OCachedBean.class, cachedBean.getId());
|
||||
assertEquals(1, dummyToLoad.getNotCachedChildren().size());
|
||||
|
||||
OBeanChild child2=new OBeanChild();
|
||||
child2.setCachedBean(cachedBean);
|
||||
DB.insert(child2);
|
||||
awaitL2Cache();
|
||||
dummyToLoad = DB.find(OCachedBean.class, cachedBean.getId());
|
||||
assertEquals(2, dummyToLoad.getNotCachedChildren().size());
|
||||
|
||||
DB.delete(child2);
|
||||
awaitL2Cache();
|
||||
dummyToLoad = DB.find(OCachedBean.class, cachedBean.getId());
|
||||
assertEquals(1, dummyToLoad.getNotCachedChildren().size());
|
||||
|
||||
DB.delete(child1);
|
||||
awaitL2Cache();
|
||||
dummyToLoad = DB.find(OCachedBean.class, cachedBean.getId());
|
||||
assertEquals(0, dummyToLoad.getNotCachedChildren().size());
|
||||
|
||||
DB.delete(cachedBean);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* When updating a ManyToMany relations also the collection cache must be updated.
|
||||
* Alternate to above test where in this case the bean is dirty - loadedBean.setName("goodbye");.
|
||||
*/
|
||||
@Test
|
||||
public void testUpdatingCollectionCacheForManyToManyRelationsWithUpdatedBean() {
|
||||
// arrange
|
||||
ResetBasicData.reset();
|
||||
|
||||
OCachedBean cachedBean = new OCachedBean();
|
||||
cachedBean.setName("hello");
|
||||
cachedBean.getCountries().add(DB.find(Country.class, "NZ"));
|
||||
cachedBean.getCountries().add(DB.find(Country.class, "AU"));
|
||||
|
||||
DB.save(cachedBean);
|
||||
|
||||
// used to just load the cache - trigger loading
|
||||
OCachedBean dummyToLoad = DB.find(OCachedBean.class, cachedBean.getId());
|
||||
dummyToLoad.getCountries().size();
|
||||
|
||||
ServerCache cachedBeanCountriesCache = cacheManager.collectionIdsCache(OCachedBean.class, "countries");
|
||||
CachedManyIds cachedManyIds = (CachedManyIds) cachedBeanCountriesCache.get(cachedBean.getId());
|
||||
|
||||
// confirm the starting data and cache entry
|
||||
assertEquals(2, dummyToLoad.getCountries().size());
|
||||
assertEquals(2, cachedManyIds.getIdList().size());
|
||||
|
||||
|
||||
// act - this time update the name property so the bean is dirty
|
||||
OCachedBean loadedBean = DB.find(OCachedBean.class, cachedBean.getId());
|
||||
loadedBean.setName("goodbye");
|
||||
loadedBean.getCountries().clear();
|
||||
loadedBean.getCountries().add(DB.find(Country.class, "AU"));
|
||||
|
||||
DB.save(loadedBean);
|
||||
awaitL2Cache();
|
||||
|
||||
// Get the data to assert/check against
|
||||
OCachedBean result = DB.find(OCachedBean.class, cachedBean.getId());
|
||||
cachedManyIds = (CachedManyIds) cachedBeanCountriesCache.get(result.getId());
|
||||
|
||||
// assert that data and cache both show correct data
|
||||
assertEquals(1, result.getCountries().size());
|
||||
assertEquals(1, cachedManyIds.getIdList().size());
|
||||
assertFalse(cachedManyIds.getIdList().contains("NZ"));
|
||||
assertTrue(cachedManyIds.getIdList().contains("AU"));
|
||||
}
|
||||
|
||||
/**
|
||||
* When updating a ManyToMany relations also the collection cache must be updated.
|
||||
*/
|
||||
@Test
|
||||
public void testUpdatingCollectionCacheForManyToManyRelationsWithinStatelessUpdate() {
|
||||
// arrange
|
||||
ResetBasicData.reset();
|
||||
|
||||
OCachedBean cachedBean = new OCachedBean();
|
||||
cachedBean.setName("cachedBeanTest");
|
||||
cachedBean.getCountries().add(DB.find(Country.class, "NZ"));
|
||||
cachedBean.getCountries().add(DB.find(Country.class, "AU"));
|
||||
|
||||
DB.save(cachedBean);
|
||||
|
||||
// clear the cache
|
||||
ServerCache cachedBeanCountriesCache = cacheManager.collectionIdsCache(OCachedBean.class, "countries");
|
||||
cachedBeanCountriesCache.clear();
|
||||
assertEquals(0, cachedBeanCountriesCache.size());
|
||||
|
||||
// load the cache
|
||||
OCachedBean dummyLoad = DB.find(OCachedBean.class, cachedBean.getId());
|
||||
List<Country> dummyCountries = dummyLoad.getCountries();
|
||||
assertEquals(2, dummyCountries.size());
|
||||
|
||||
// assert that the cache contains the expected entry
|
||||
assertEquals(1, cachedBeanCountriesCache.size());
|
||||
CachedManyIds dummyEntry = (CachedManyIds) cachedBeanCountriesCache.get(dummyLoad.getId());
|
||||
assertNotNull(dummyEntry);
|
||||
assertEquals(2, dummyEntry.getIdList().size());
|
||||
assertTrue(dummyEntry.getIdList().contains("NZ"));
|
||||
assertTrue(dummyEntry.getIdList().contains("AU"));
|
||||
|
||||
|
||||
// act - this should invalidate our cache entry
|
||||
OCachedBean update = new OCachedBean();
|
||||
update.setId(cachedBean.getId());
|
||||
update.setName("modified");
|
||||
update.getCountries().add(DB.find(Country.class, "AU"));
|
||||
|
||||
DB.update(update);
|
||||
awaitL2Cache();
|
||||
|
||||
assertEquals(1, cachedBeanCountriesCache.size());
|
||||
|
||||
CachedManyIds cachedManyIds = (CachedManyIds) cachedBeanCountriesCache.get(update.getId());
|
||||
|
||||
// assert cache updated
|
||||
assertEquals(1, cachedManyIds.getIdList().size());
|
||||
assertFalse(cachedManyIds.getIdList().contains("NZ"));
|
||||
assertTrue(cachedManyIds.getIdList().contains("AU"));
|
||||
|
||||
// assert countries good
|
||||
OCachedBean result = DB.find(OCachedBean.class, cachedBean.getId());
|
||||
assertEquals(1, result.getCountries().size());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdating_noChange() throws InterruptedException {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
try (Transaction txn = DB.beginTransaction()) {
|
||||
|
||||
OCachedBean cachedBean = new OCachedBean();
|
||||
cachedBean.setName("helloForUpdate");
|
||||
DB.save(cachedBean);
|
||||
|
||||
cachedBean = DB.find(OCachedBean.class, cachedBean.getId());
|
||||
|
||||
cachedBean.setName("mod");
|
||||
ArrayList<Country> list = new ArrayList<>();
|
||||
list.add(DB.find(Country.class, "NZ"));
|
||||
cachedBean.setCountries(list);
|
||||
DB.save(cachedBean);
|
||||
|
||||
|
||||
cachedBean.setName("mod2");
|
||||
DB.save(cachedBean);
|
||||
|
||||
txn.commit();
|
||||
}
|
||||
|
||||
|
||||
Thread.sleep(2000);
|
||||
}
|
||||
|
||||
/**
|
||||
* When doing an ORM update the collection cache must be cleared.
|
||||
*/
|
||||
@Test
|
||||
@Disabled
|
||||
public void testClearingCollectionCacheOnORMUpdate() {
|
||||
// arrange
|
||||
ResetBasicData.reset();
|
||||
|
||||
// load the cache with the order
|
||||
Order order1 = DB.find(Order.class, 1L);
|
||||
|
||||
// load the Collection IDs (order.orderDetail) cache
|
||||
OrderDetail orderDetail1 = order1.getDetails().get(0);
|
||||
|
||||
// delete one order detail from DB. This triggers clearing of OrderDetail caches
|
||||
// and should also clear any Collection IDs caches targeting OrderDetail bean
|
||||
String updStatement = "delete from orderDetail where id = :id";
|
||||
Update<OrderDetail> update = DB.createUpdate(OrderDetail.class, updStatement);
|
||||
update.set("id", orderDetail1.getId());
|
||||
int rows = update.execute();
|
||||
assertEquals(1, rows);
|
||||
|
||||
// read the order from cache
|
||||
Order orderFromCache = DB.find(Order.class, 1L);
|
||||
OrderDetail orderDetailFromCache = orderFromCache.getDetails().get(0);
|
||||
|
||||
// trigger reading from the DB
|
||||
orderDetailFromCache.getCretime();
|
||||
|
||||
// if we got here then OK
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* When doing an external modification on a table the collection cache must be cleared.
|
||||
* This is true for all changes insert,update, delete. Tested for delete here.
|
||||
*/
|
||||
@Test
|
||||
@Disabled
|
||||
public void testClearingCollectionCacheOnExternalModification() {
|
||||
// arrange
|
||||
ResetBasicData.reset();
|
||||
|
||||
// load the cache with the order
|
||||
Order order1 = DB.find(Order.class, 1L);
|
||||
|
||||
// load the Collection IDs (order.orderDetail) cache
|
||||
OrderDetail orderDetail1 = order1.getDetails().get(0);
|
||||
|
||||
// delete one order detail from DB using native SQL. This triggers clearing of OrderDetail caches
|
||||
// and should also clear any Collection IDs caches targeting OrderDetail bean
|
||||
String updStatement = "delete from o_order_detail where id = :id";
|
||||
SqlUpdate update = DB.sqlUpdate(updStatement);
|
||||
update.setParameter("id", orderDetail1.getId());
|
||||
int rows = update.execute();
|
||||
assertEquals(1, rows);
|
||||
|
||||
// We need to notify the cache manually
|
||||
DB.externalModification("o_order_detail", false, false, true);
|
||||
|
||||
// read the order from cache
|
||||
Order orderFromCache = DB.find(Order.class, 1L);
|
||||
OrderDetail orderDetailFromCache = orderFromCache.getDetails().get(0);
|
||||
|
||||
// trigger reading the whole bean
|
||||
orderDetailFromCache.getCretime();
|
||||
|
||||
// if we got here then OK
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package org.tests.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.CacheMode;
|
||||
import io.ebean.DB;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Address;
|
||||
import org.tests.model.basic.Contact;
|
||||
import org.tests.model.basic.Country;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TestCacheCustomer extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testUpdateAddress() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Country nz = DB.reference(Country.class, "NZ");
|
||||
Address address = new Address();
|
||||
address.setLine1("Some Place");
|
||||
address.setCity("Auckland");
|
||||
address.setCountry(nz);
|
||||
|
||||
DB.save(address);
|
||||
|
||||
address.setLine2("Else");
|
||||
DB.save(address);
|
||||
|
||||
DB.delete(address);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Customer> list = DB.find(Customer.class).setAutoTune(false).setBeanCacheMode(CacheMode.PUT)
|
||||
.findList();
|
||||
|
||||
assertTrue(list.size() > 1);
|
||||
|
||||
for (Customer customer : list) {
|
||||
Address billingAddress = customer.getBillingAddress();
|
||||
if (billingAddress != null) {
|
||||
billingAddress.getLine1();
|
||||
}
|
||||
Address shippingAddress = customer.getShippingAddress();
|
||||
if (shippingAddress != null) {
|
||||
shippingAddress.getLine1();
|
||||
}
|
||||
List<Contact> contacts = customer.getContacts();
|
||||
for (Contact contact : contacts) {
|
||||
contact.getFirstName();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package org.tests.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.OCachedBean;
|
||||
import org.tests.model.basic.OCachedBeanChild;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* Test class testing deleting/invalidating of cached beans
|
||||
*/
|
||||
public class TestCacheDelete extends BaseTestCase {
|
||||
|
||||
/**
|
||||
* When deleting a cached entity all entities with a referenced OneToMany relation must also be invalidated!
|
||||
*/
|
||||
@Test
|
||||
public void testCacheDeleteOneToMany() {
|
||||
// arrange
|
||||
OCachedBeanChild child = new OCachedBeanChild();
|
||||
OCachedBeanChild child2 = new OCachedBeanChild();
|
||||
|
||||
OCachedBean parentBean = new OCachedBean();
|
||||
parentBean.getChildren().add(child);
|
||||
parentBean.getChildren().add(child2);
|
||||
DB.save(parentBean);
|
||||
|
||||
// confirm there are 2 children loaded from the parent
|
||||
assertEquals(2, DB.find(OCachedBean.class, parentBean.getId()).getChildren().size());
|
||||
|
||||
// ensure cache has been populated
|
||||
DB.find(OCachedBeanChild.class, child.getId());
|
||||
child2 = DB.find(OCachedBeanChild.class, child2.getId());
|
||||
parentBean = DB.find(OCachedBean.class, parentBean.getId());
|
||||
|
||||
// act
|
||||
DB.delete(child2);
|
||||
awaitL2Cache();
|
||||
|
||||
OCachedBean beanFromCache = DB.find(OCachedBean.class, parentBean.getId());
|
||||
assertEquals(1, beanFromCache.getChildren().size());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package org.tests.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.CKeyParent;
|
||||
import org.tests.model.basic.CKeyParentId;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class TestCacheEmbeddedId extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
CKeyParentId id = new CKeyParentId(100, "testEmbedded");
|
||||
|
||||
CKeyParent p = new CKeyParent();
|
||||
p.setId(id);
|
||||
p.setName("testCache");
|
||||
|
||||
DB.save(p);
|
||||
|
||||
CKeyParent cKeyParent = DB.find(CKeyParent.class, id);
|
||||
assertNotNull(cKeyParent);
|
||||
|
||||
// find for second time
|
||||
// causes java.lang.ClassCastException: class java.lang.String cannot be cast to class org.tests.model.basic.CKeyParentId
|
||||
cKeyParent = DB.find(CKeyParent.class, id);
|
||||
assertNotNull(cKeyParent);
|
||||
}
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package org.tests.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TestCacheInterceptSaveWhenLazyLoaded extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Customer customer = new Customer();
|
||||
customer.setName("daCustomer");
|
||||
DB.save(customer);
|
||||
|
||||
Order order = new Order();
|
||||
order.setStatus(Order.Status.NEW);
|
||||
order.setCustomer(customer);
|
||||
DB.save(order);
|
||||
|
||||
DB.beginTransaction();
|
||||
try {
|
||||
|
||||
Order foundOrder = DB.find(Order.class)
|
||||
.where().eq("id", order.getId())
|
||||
.findOne();
|
||||
|
||||
foundOrder.setStatus(Order.Status.APPROVED);
|
||||
assertTrue(DB.beanState(foundOrder).isDirty());
|
||||
|
||||
Customer foundCustomer = DB.find(Customer.class)
|
||||
.where().eq("id", customer.getId())
|
||||
.findOne();
|
||||
|
||||
// the foundOrder is in this list
|
||||
foundCustomer.getOrders().size();
|
||||
|
||||
Order order1 = foundCustomer.getOrders().get(0);
|
||||
|
||||
assertSame(foundOrder, order1);
|
||||
assertTrue(DB.beanState(foundOrder).isDirty());
|
||||
|
||||
} finally {
|
||||
DB.endTransaction();
|
||||
}
|
||||
|
||||
// cleanup
|
||||
DB.delete(order);
|
||||
DB.delete(customer);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package org.tests.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.tests.model.basic.OCachedInhChildA;
|
||||
import org.tests.model.basic.OCachedInhRoot;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
/**
|
||||
* Test class testing deleting/invalidating of cached beans
|
||||
*/
|
||||
public class TestCacheManyToOne extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void crudInvoice() {
|
||||
|
||||
OCachedInhChildA bean = new OCachedInhChildA();
|
||||
bean.setName("Roland");
|
||||
bean.setChildAData("ChildData");
|
||||
DB.save(bean);
|
||||
|
||||
DB.find(OCachedInhRoot.class).select("id").where().idIn(bean.getId()).findEach(bean0 -> {
|
||||
assertNotNull(bean0);
|
||||
assertThat(bean0.getName()).isEqualTo("Roland");
|
||||
assertThat(((OCachedInhChildA)bean0).getChildAData()).isEqualTo("ChildData");
|
||||
});
|
||||
|
||||
DB.find(OCachedInhRoot.class).select("id").where().idIn(bean.getId()).findEach(bean1 -> {
|
||||
assertNotNull(bean1);
|
||||
assertThat(bean1.getName()).isEqualTo("Roland");
|
||||
// this will fail if the bean is loaded from cache and the wrong discriminator is used.
|
||||
assertThat(((OCachedInhChildA)bean1).getChildAData()).isEqualTo("ChildData");
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package org.tests.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.CacheMode;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.cache.ServerCache;
|
||||
import io.ebean.cache.ServerCacheStatistics;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Contact;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TestCacheNaturalId extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
ServerCache contactCache = DB.cacheManager().beanCache(Contact.class);
|
||||
|
||||
List<Contact> list = DB.find(Contact.class).setBeanCacheMode(CacheMode.PUT).findList();
|
||||
|
||||
assertTrue(contactCache.size() > 0);
|
||||
|
||||
String emailToSearch = null;
|
||||
for (Contact contact : list) {
|
||||
if (contact.getEmail() != null) {
|
||||
emailToSearch = contact.getEmail();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
contactCache.statistics(true);
|
||||
|
||||
Contact c0 = DB.find(Contact.class).where().eq("email", emailToSearch).findOne();
|
||||
|
||||
ServerCacheStatistics stats0 = contactCache.statistics(false);
|
||||
|
||||
Contact c1 = DB.find(Contact.class).where().eq("email", emailToSearch).findOne();
|
||||
|
||||
ServerCacheStatistics stats1 = contactCache.statistics(false);
|
||||
|
||||
assertNotNull(c0);
|
||||
assertNotNull(c1);
|
||||
|
||||
assertEquals(1, stats0.getHitCount());
|
||||
assertEquals(2, stats1.getHitCount());
|
||||
|
||||
c1.setEmail("mychangedemail@what.com");
|
||||
DB.save(c1);
|
||||
awaitL2Cache();
|
||||
|
||||
Contact c2 = DB.find(Contact.class).where().eq("email", "mychangedemail@what.com")
|
||||
.findOne();
|
||||
|
||||
ServerCacheStatistics stats2 = contactCache.statistics(false);
|
||||
|
||||
assertNotNull(c2);
|
||||
assertEquals(c2.getId(), c1.getId());
|
||||
assertEquals(c0.getId(), c1.getId());
|
||||
assertTrue(stats2.getHitCount() > stats1.getHitCount());
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package org.tests.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.SqlUpdate;
|
||||
import io.ebean.cache.ServerCache;
|
||||
import io.ebean.cache.ServerCacheManager;
|
||||
import org.tests.model.basic.Country;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TestExternalNotification extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testNotify() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
loadCountryCache();
|
||||
|
||||
ServerCache countryCache = DB.cacheManager().beanCache(Country.class);
|
||||
|
||||
assertTrue(countryCache.size() > 0);
|
||||
|
||||
// inserts don't remove from bean cache
|
||||
DB.externalModification("o_country", true, false, false);
|
||||
assertTrue(countryCache.size() > 0);
|
||||
|
||||
// updates flush cache
|
||||
DB.externalModification("o_country", false, true, false);
|
||||
assertEquals(0, countryCache.size());
|
||||
|
||||
loadCountryCache();
|
||||
assertTrue(countryCache.size() > 0);
|
||||
|
||||
// deletes flush cache
|
||||
DB.externalModification("o_country", false, false, true);
|
||||
assertEquals(0, countryCache.size());
|
||||
|
||||
loadCountryCache();
|
||||
assertTrue(countryCache.size() > 0);
|
||||
|
||||
ServerCacheManager serverCacheManager = DB.cacheManager();
|
||||
serverCacheManager.clearAll();
|
||||
assertEquals(0, countryCache.size());
|
||||
|
||||
loadCountryCache();
|
||||
assertTrue(countryCache.size() > 0);
|
||||
|
||||
DB.cacheManager().clear(Country.class);
|
||||
assertEquals(0, countryCache.size());
|
||||
|
||||
loadCountryCache();
|
||||
int cacheSize = countryCache.size();
|
||||
assertTrue(cacheSize > 0);
|
||||
|
||||
SqlUpdate sqlUpdate = DB.sqlUpdate("update o_country set name = :name where code = :code");
|
||||
sqlUpdate.setParameter("name", "Aotearoa");
|
||||
sqlUpdate.setParameter("code", "NZ");
|
||||
|
||||
// this should just clear the entire country cache
|
||||
int rows = sqlUpdate.execute();
|
||||
assertEquals(1, rows);
|
||||
assertEquals(0, countryCache.size());
|
||||
|
||||
// set it back...
|
||||
sqlUpdate.setParameter("name", "New Zealand");
|
||||
sqlUpdate.setParameter("code", "NZ");
|
||||
rows = sqlUpdate.execute();
|
||||
assertEquals(1, rows);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package org.tests.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import io.ebean.cache.ServerCache;
|
||||
import io.ebeaninternal.server.querydefn.OrmQueryDetail;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.FeatureDescription;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestL2CacheWithSharedBean extends BaseTestCase {
|
||||
|
||||
// private TunedQueryInfo createTunedQueryInfo(OrmQueryDetail tunedDetail) {
|
||||
// Origin origin = new Origin();
|
||||
// origin.setDetail(tunedDetail.toString());
|
||||
// return new TunedQueryInfo(origin);
|
||||
// }
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
FeatureDescription f1 = new FeatureDescription();
|
||||
f1.setName("one");
|
||||
f1.setDescription("helloOne");
|
||||
|
||||
DB.save(f1);
|
||||
|
||||
ServerCache beanCache = DB.cacheManager().beanCache(FeatureDescription.class);
|
||||
beanCache.statistics(true);
|
||||
|
||||
OrmQueryDetail tunedDetail = new OrmQueryDetail();
|
||||
tunedDetail.select("name");
|
||||
// TunedQueryInfo tunedInfo = createTunedQueryInfo(tunedDetail);
|
||||
|
||||
Query<FeatureDescription> query = DB.find(FeatureDescription.class).setId(f1.getId());
|
||||
|
||||
// tunedInfo.tuneQuery((SpiQuery<?>) query);
|
||||
|
||||
query.findOne(); // PUT into cache
|
||||
|
||||
FeatureDescription fd2 = query.findOne(); // LOAD cache
|
||||
|
||||
String description0 = fd2.getDescription(); // invoke lazy load
|
||||
assertEquals("helloOne", description0);
|
||||
|
||||
// load the cache
|
||||
FeatureDescription fetchOne = DB.find(FeatureDescription.class, f1.getId());
|
||||
assertNotNull(fetchOne);
|
||||
assertEquals(1, beanCache.statistics(false).getSize());
|
||||
|
||||
FeatureDescription fetchTwo = DB.find(FeatureDescription.class, f1.getId());
|
||||
FeatureDescription fetchThree = DB.find(FeatureDescription.class, f1.getId());
|
||||
assertSame(fetchTwo, fetchThree);
|
||||
|
||||
String description1 = fetchThree.getDescription();
|
||||
assertEquals("helloOne", description1);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package org.tests.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.BeanState;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Transaction;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.L2CachedLazyDirtFlagResetBean;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TestL2DirtyFlagOnLazyLoad extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void dirtyFlag_reset_after_lazy() {
|
||||
L2CachedLazyDirtFlagResetBean bean = new L2CachedLazyDirtFlagResetBean();
|
||||
//PREPARE
|
||||
try (Transaction tx0 = DB.beginTransaction()) {
|
||||
bean.setName("findById");
|
||||
DB.save(bean);
|
||||
//bean.save();
|
||||
tx0.commit();
|
||||
}
|
||||
|
||||
// clearAll() caches via the ServerCacheManager ...
|
||||
// Clear all the caches on the default/primary EbeanServer
|
||||
server().cacheManager().clearAll();
|
||||
|
||||
//TEST
|
||||
try (Transaction tx1 = DB.beginTransaction()) {
|
||||
// load (and dont touch any related entity)
|
||||
L2CachedLazyDirtFlagResetBean bean1 = DB.find(L2CachedLazyDirtFlagResetBean.class, bean.getId());
|
||||
assertThat(bean1).isNotNull();
|
||||
tx1.commit();
|
||||
}
|
||||
|
||||
try (Transaction tx2 = DB.beginTransaction()) {
|
||||
// load (and modify)
|
||||
|
||||
L2CachedLazyDirtFlagResetBean bean2 = DB.find(L2CachedLazyDirtFlagResetBean.class, bean.getId());
|
||||
assertNotNull(bean2);
|
||||
assertEquals("findById", bean2.getName());
|
||||
bean2.setName("something");
|
||||
BeanState beanState = DB.beanState(bean2);
|
||||
assertTrue(beanState.isDirty());
|
||||
//bean2.getChildren().size();
|
||||
bean2.someRichObjectMethod();
|
||||
assertTrue(beanState.isDirty());
|
||||
|
||||
DB.update(bean2);
|
||||
tx2.commit();
|
||||
}
|
||||
|
||||
try(Transaction tx3 = DB.beginTransaction()) {
|
||||
// validation
|
||||
|
||||
L2CachedLazyDirtFlagResetBean bean3 = DB.find(L2CachedLazyDirtFlagResetBean.class, bean.getId());
|
||||
assertEquals("something", bean3.getName());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package org.tests.cache;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.cache.M2MCacheChild;
|
||||
import org.tests.model.cache.M2MCacheMaster;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
|
||||
public class TestM2MCache extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testM2MWithCache() throws Exception {
|
||||
M2MCacheChild cld = new M2MCacheChild();
|
||||
cld.setName("test");
|
||||
cld.setId(1);
|
||||
DB.save(cld);
|
||||
|
||||
M2MCacheMaster cfg = new M2MCacheMaster();
|
||||
cfg.setId(42);
|
||||
cfg.getSet1().add(cld);
|
||||
cfg.getSet2().add(cld);
|
||||
DB.save(cfg);
|
||||
|
||||
// find master and access set1 + set2.
|
||||
// lazy load something from set1
|
||||
M2MCacheMaster cfg1 = DB.find(M2MCacheMaster.class, 42);
|
||||
|
||||
cfg1.getSet1().size();
|
||||
cfg1.getSet2().size();
|
||||
assertThat(cfg1.getSet1().iterator().next().getName()).isEqualTo("test");
|
||||
|
||||
// do it again
|
||||
cfg1 = DB.find(M2MCacheMaster.class, 42);
|
||||
|
||||
cfg1.getSet1().size();
|
||||
cfg1.getSet2().size();
|
||||
|
||||
assertThat(cfg1.getSet1().iterator().next().getName()).isEqualTo("test");
|
||||
// do it again
|
||||
cfg1 = DB.find(M2MCacheMaster.class, 42);
|
||||
|
||||
cfg1.getSet1().size();
|
||||
cfg1.getSet2().size();
|
||||
|
||||
assertThat(cfg1.getSet1().iterator().next().getName()).isEqualTo("test");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testM2MWithCacheMinimal() throws Exception {
|
||||
M2MCacheChild cld = new M2MCacheChild();
|
||||
cld.setName("test");
|
||||
cld.setId(2);
|
||||
DB.save(cld);
|
||||
|
||||
M2MCacheMaster cfg = new M2MCacheMaster();
|
||||
cfg.setId(43);
|
||||
cfg.getSet1().add(cld);
|
||||
cfg.getSet2().add(cld);
|
||||
DB.save(cfg);
|
||||
|
||||
DB.find(M2MCacheMaster.class, 43);
|
||||
|
||||
M2MCacheMaster cfg1 = DB.find(M2MCacheMaster.class, 43);
|
||||
cfg1.getSet2().size();
|
||||
|
||||
cfg1 = DB.find(M2MCacheMaster.class, 43);
|
||||
|
||||
cfg1.getSet1().size();
|
||||
cfg1.getSet2().size();
|
||||
|
||||
assertThat(cfg1.getSet1().iterator().next().getName()).isEqualTo("test");
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,329 @@
|
||||
package org.tests.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.CacheMode;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.ExpressionList;
|
||||
import io.ebean.bean.BeanCollection;
|
||||
import io.ebean.cache.ServerCache;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.tests.model.cache.EColAB;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestQueryCache extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void clashHashCode() {
|
||||
|
||||
new EColAB("01", "20").save();
|
||||
new EColAB("02", "10").save();
|
||||
|
||||
List<EColAB> list1 =
|
||||
DB.find(EColAB.class)
|
||||
.setUseQueryCache(true)
|
||||
.where()
|
||||
.eq("columnA", "01")
|
||||
.eq("columnB", "20")
|
||||
.findList();
|
||||
|
||||
List<EColAB> list2 =
|
||||
DB.find(EColAB.class)
|
||||
.setUseQueryCache(true)
|
||||
.where()
|
||||
.eq("columnA", "02")
|
||||
.eq("columnB", "10")
|
||||
.findList();
|
||||
|
||||
assertThat(list1.get(0).getColumnA()).isEqualTo("01");
|
||||
assertThat(list1.get(0).getColumnB()).isEqualTo("20");
|
||||
|
||||
assertThat(list2.get(0).getColumnA()).isEqualTo("02");
|
||||
assertThat(list2.get(0).getColumnB()).isEqualTo("10");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findSingleAttribute() {
|
||||
|
||||
DB.find(EColAB.class).delete();
|
||||
new EColAB("03", "SingleAttribute").save();
|
||||
new EColAB("03", "SingleAttribute").save();
|
||||
|
||||
List<String> colA_first = DB
|
||||
.find(EColAB.class)
|
||||
.setUseQueryCache(true)
|
||||
.setDistinct(true)
|
||||
.select("columnA")
|
||||
.where()
|
||||
.eq("columnB", "SingleAttribute")
|
||||
.findSingleAttributeList();
|
||||
|
||||
List<String> colA_Second = DB
|
||||
.find(EColAB.class)
|
||||
.setUseQueryCache(true)
|
||||
.setDistinct(true)
|
||||
.select("columnA")
|
||||
.where()
|
||||
.eq("columnB", "SingleAttribute")
|
||||
.findSingleAttributeList();
|
||||
|
||||
assertThat(colA_Second).isSameAs(colA_first);
|
||||
|
||||
List<String> colA_NotDistinct = DB
|
||||
.find(EColAB.class)
|
||||
.setUseQueryCache(true)
|
||||
.select("columnA")
|
||||
.where()
|
||||
.eq("columnB", "SingleAttribute")
|
||||
.findSingleAttributeList();
|
||||
|
||||
assertThat(colA_Second).isNotSameAs(colA_NotDistinct);
|
||||
|
||||
// ensure that findCount & findSingleAttribute use different
|
||||
// slots in cache. If not a "Cannot cast List to int" should happen.
|
||||
int count = DB
|
||||
.find(EColAB.class)
|
||||
.setUseQueryCache(true)
|
||||
.select("columnA")
|
||||
.where()
|
||||
.eq("columnB", "SingleAttribute")
|
||||
.findCount();
|
||||
assertThat(count).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findCount() {
|
||||
|
||||
new EColAB("04", "count").save();
|
||||
new EColAB("05", "count").save();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
int count0 = DB.find(EColAB.class)
|
||||
.setUseQueryCache(CacheMode.ON)
|
||||
.where()
|
||||
.eq("columnB", "count")
|
||||
.findCount();
|
||||
|
||||
int count1 = DB.find(EColAB.class)
|
||||
.setUseQueryCache(CacheMode.ON)
|
||||
.where()
|
||||
.eq("columnB", "count")
|
||||
.findCount();
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
assertThat(count0).isEqualTo(count1);
|
||||
assertThat(sql).hasSize(1);
|
||||
|
||||
// and now, ensure that we hit the database
|
||||
LoggedSqlCollector.start();
|
||||
int count2 = DB.find(EColAB.class)
|
||||
.setUseQueryCache(CacheMode.OFF)
|
||||
.where()
|
||||
.eq("columnB", "count")
|
||||
.findCount();
|
||||
assertThat(count2).isEqualTo(count1);
|
||||
sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findCountDifferentQueries() {
|
||||
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
int count0 = DB.find(EColAB.class)
|
||||
.setUseQueryCache(CacheMode.ON)
|
||||
.where()
|
||||
.eq("columnB", "abc")
|
||||
.findCount();
|
||||
|
||||
int count1 = DB.find(EColAB.class)
|
||||
.setUseQueryCache(CacheMode.ON)
|
||||
.where()
|
||||
.eq("columnB", "def")
|
||||
.findCount();
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
assertThat(count0).isEqualTo(count1);
|
||||
assertThat(sql).hasSize(2); // different queries
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findCountFirstOnThenRecache() {
|
||||
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
int count0 = DB.find(EColAB.class)
|
||||
.setUseQueryCache(CacheMode.ON)
|
||||
.where()
|
||||
.eq("columnB", "uvw")
|
||||
.findCount();
|
||||
|
||||
int count1 = DB.find(EColAB.class)
|
||||
.setUseQueryCache(CacheMode.PUT)
|
||||
.where()
|
||||
.eq("columnB", "uvw")
|
||||
.findCount();
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
assertThat(count0).isEqualTo(count1);
|
||||
assertThat(sql).hasSize(2); // try recache as second query - it must fetch it
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void findCountFirstRecacheThenOn() {
|
||||
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
int count0 = DB.find(EColAB.class)
|
||||
.setUseQueryCache(CacheMode.PUT)
|
||||
.where()
|
||||
.eq("columnB", "xyz")
|
||||
.findCount();
|
||||
|
||||
int count1 = DB.find(EColAB.class)
|
||||
.setUseQueryCache(CacheMode.ON)
|
||||
.where()
|
||||
.eq("columnB", "xyz")
|
||||
.findCount();
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
assertThat(count0).isEqualTo(count1);
|
||||
assertThat(sql).hasSize(1); // try recache as first query - second "ON" query must fetch it.
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testReadOnlyFind() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
ServerCache customerCache = DB.cacheManager().queryCache(Customer.class);
|
||||
customerCache.clear();
|
||||
|
||||
List<Customer> list = DB.find(Customer.class).setUseQueryCache(true).setReadOnly(true).where()
|
||||
.ilike("name", "Rob").findList();
|
||||
|
||||
BeanCollection<Customer> bc = (BeanCollection<Customer>) list;
|
||||
assertTrue(bc.isReadOnly());
|
||||
assertFalse(bc.isEmpty());
|
||||
assertTrue(!list.isEmpty());
|
||||
assertTrue(DB.beanState(list.get(0)).isReadOnly());
|
||||
|
||||
List<Customer> list2 = DB.find(Customer.class).setUseQueryCache(true).setReadOnly(true).where()
|
||||
.ilike("name", "Rob").findList();
|
||||
|
||||
List<Customer> list2B = DB.find(Customer.class).setUseQueryCache(true)
|
||||
// .setReadOnly(true)
|
||||
.where().ilike("name", "Rob").findList();
|
||||
|
||||
assertSame(list, list2);
|
||||
|
||||
// readOnly defaults to true for query cache
|
||||
assertSame(list, list2B);
|
||||
|
||||
List<Customer> list3 = DB.find(Customer.class).setUseQueryCache(true).setReadOnly(false).where()
|
||||
.ilike("name", "Rob").findList();
|
||||
|
||||
assertNotSame(list, list3);
|
||||
BeanCollection<Customer> bc3 = (BeanCollection<Customer>) list3;
|
||||
assertFalse(bc3.isReadOnly());
|
||||
assertFalse(bc3.isEmpty());
|
||||
assertTrue(list3.size() > 0);
|
||||
// TODO: At this stage setReadOnly(false) does create a shallow copy of the List/Set/Map, but does not
|
||||
// change the read only state in the entities.
|
||||
// assertFalse(DB.beanState(list3.get(0)).isReadOnly());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findIds() {
|
||||
|
||||
DB.find(EColAB.class).delete();
|
||||
new EColAB("03", "someId").save();
|
||||
new EColAB("04", "someId").save();
|
||||
new EColAB("05", "someId").save();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
List<Integer> colA_first = DB.find(EColAB.class)
|
||||
.setUseQueryCache(CacheMode.ON)
|
||||
.where()
|
||||
.eq("columnB", "someId")
|
||||
.findIds();
|
||||
|
||||
List<Integer> colA_second = DB.find(EColAB.class)
|
||||
.setUseQueryCache(CacheMode.ON)
|
||||
.where()
|
||||
.eq("columnB", "someId")
|
||||
.findIds();
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
assertThat(colA_first).isSameAs(colA_second);
|
||||
assertThat(colA_first).hasSize(3);
|
||||
assertThat(sql).hasSize(1);
|
||||
|
||||
// and now, ensure that we hit the database
|
||||
LoggedSqlCollector.start();
|
||||
colA_second = DB.find(EColAB.class)
|
||||
.setUseQueryCache(CacheMode.PUT)
|
||||
.where()
|
||||
.eq("columnB", "someId")
|
||||
.findIds();
|
||||
sql = LoggedSqlCollector.stop();
|
||||
|
||||
assertThat(sql).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findCountDifferentQueriesBit() {
|
||||
DB.getDefault().pluginApi().cacheManager().clearAll();
|
||||
differentFindCount(q->q.bitwiseAny("id",1), q->q.bitwiseAny("id",0));
|
||||
differentFindCount(q->q.bitwiseAll("id",1), q->q.bitwiseAll("id",0));
|
||||
// differentFindCount(q->q.bitwiseNot("id",1), q->q.bitwiseNot("id",0)); NOT 1 == AND 1 = 0
|
||||
differentFindCount(q->q.bitwiseAnd("id",1, 0), q->q.bitwiseAnd("id",1, 1));
|
||||
|
||||
differentFindCount(q->q.bitwiseAnd("id",2, 0), q->q.bitwiseAnd("id",4, 0));
|
||||
differentFindCount(q->q.bitwiseAnd("id",2, 1), q->q.bitwiseAnd("id",4, 1));
|
||||
// Will produce hash collision
|
||||
differentFindCount(q->q.bitwiseAnd("id",10, 0), q->q.bitwiseAnd("id",0, 928210));
|
||||
|
||||
}
|
||||
|
||||
void differentFindCount(Consumer<ExpressionList<EColAB>> q0, Consumer<ExpressionList<EColAB>> q1) {
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
ExpressionList<EColAB> el0 = DB.find(EColAB.class).setUseQueryCache(CacheMode.ON).where();
|
||||
q0.accept(el0);
|
||||
el0.findCount();
|
||||
|
||||
ExpressionList<EColAB> el1 = DB.find(EColAB.class).setUseQueryCache(CacheMode.ON).where();
|
||||
q1.accept(el1);
|
||||
el1.findCount();
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
assertThat(sql).hasSize(2); // different queries
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
package org.tests.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.cache.ServerCache;
|
||||
import io.ebean.cache.ServerCacheManager;
|
||||
import io.ebean.cache.ServerCacheStatistics;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Country;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestQueryCacheCountry extends BaseTestCase {
|
||||
|
||||
private final ServerCacheManager cacheManager = DB.cacheManager();
|
||||
private final ServerCache queryCache = cacheManager.queryCache(Country.class);
|
||||
private final ServerCache beanCache = cacheManager.beanCache(Country.class);
|
||||
|
||||
private void clearCache() {
|
||||
queryCache.clear();
|
||||
beanCache.clear();
|
||||
queryCache.statistics(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void emptyQueryResult_expect_cached() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
clearCache();
|
||||
|
||||
List<Country> countryList0 = DB.find(Country.class)
|
||||
.setUseQueryCache(true)
|
||||
.where().startsWith("name", "XLKMG")
|
||||
.order().asc("name")
|
||||
.findList();
|
||||
|
||||
assertThat(countryList0).isEmpty();
|
||||
|
||||
ServerCacheStatistics queryStats = queryCache.statistics(false);
|
||||
assertEquals(1, queryStats.getMissCount());
|
||||
assertEquals(1, queryStats.getSize());
|
||||
|
||||
List<Country> countryList1 = DB.find(Country.class)
|
||||
.setUseQueryCache(true)
|
||||
.where().startsWith("name", "XLKMG")
|
||||
.order().asc("name")
|
||||
.findList();
|
||||
|
||||
assertThat(countryList1).isEmpty();
|
||||
|
||||
ServerCacheStatistics queryStats1 = queryCache.statistics(false);
|
||||
assertEquals(1, queryStats1.getMissCount());
|
||||
assertEquals(1, queryStats1.getSize());
|
||||
assertEquals(1, queryStats1.getHitCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rawExpression() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
awaitL2Cache();
|
||||
clearCache();
|
||||
|
||||
List<Country> countryList0 = DB.find(Country.class)
|
||||
.setUseQueryCache(true)
|
||||
.where().raw("code = ?", "NZ")
|
||||
.findList();
|
||||
|
||||
assertThat(countryList0.get(0).getName()).isEqualTo("New Zealand");
|
||||
|
||||
List<Country> countryList1 = DB.find(Country.class)
|
||||
.setUseQueryCache(true)
|
||||
.where().raw("code = ?", "AU")
|
||||
.findList();
|
||||
|
||||
assertThat(countryList1.get(0).getName()).isEqualTo("Australia");
|
||||
|
||||
ServerCacheStatistics queryStats1 = queryCache.statistics(false);
|
||||
assertEquals(2, queryStats1.getMissCount());
|
||||
assertEquals(2, queryStats1.getSize());
|
||||
assertEquals(0, queryStats1.getHitCount()); // no hits yet
|
||||
|
||||
|
||||
// we get a hit this time
|
||||
List<Country> countryList2 = DB.find(Country.class)
|
||||
.setUseQueryCache(true)
|
||||
.where().raw("code = ?", "NZ")
|
||||
.findList();
|
||||
|
||||
assertThat(countryList2.get(0).getName()).isEqualTo("New Zealand");
|
||||
|
||||
ServerCacheStatistics queryStats2 = queryCache.statistics(false);
|
||||
assertEquals(2, queryStats2.getMissCount());
|
||||
assertEquals(2, queryStats2.getSize());
|
||||
assertEquals(1, queryStats2.getHitCount()); // got a hit
|
||||
|
||||
// we get a hit on AU
|
||||
List<Country> countryList3 = DB.find(Country.class)
|
||||
.setUseQueryCache(true)
|
||||
.where().raw("code = ?", "AU")
|
||||
.findList();
|
||||
|
||||
assertThat(countryList3.get(0).getName()).isEqualTo("Australia");
|
||||
|
||||
ServerCacheStatistics queryStats3 = queryCache.statistics(false);
|
||||
assertEquals(2, queryStats3.getMissCount());
|
||||
assertEquals(2, queryStats3.getSize());
|
||||
assertEquals(2, queryStats3.getHitCount()); // got another hit
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
awaitL2Cache();
|
||||
clearCache();
|
||||
|
||||
assertEquals(0, queryCache.statistics(false).getSize());
|
||||
|
||||
List<Country> countryList0 = DB.find(Country.class)
|
||||
.setUseQueryCache(true)
|
||||
.order().asc("name")
|
||||
.findList();
|
||||
|
||||
assertEquals(1, queryCache.statistics(false).getSize());
|
||||
assertTrue(!countryList0.isEmpty());
|
||||
|
||||
List<Country> countryList1 = DB.find(Country.class)
|
||||
.setUseQueryCache(true)
|
||||
.order().asc("name")
|
||||
.findList();
|
||||
|
||||
ServerCacheStatistics statistics = queryCache.statistics(false);
|
||||
assertEquals(1, statistics.getSize());
|
||||
assertEquals(1, statistics.getHitCount());
|
||||
assertSame(countryList1, countryList0);
|
||||
|
||||
Country nz = DB.find(Country.class, "NZ");
|
||||
nz.setName("New Zealandia");
|
||||
DB.save(nz);
|
||||
awaitL2Cache();
|
||||
|
||||
statistics = queryCache.statistics(false);
|
||||
assertEquals(0, statistics.getSize());
|
||||
|
||||
List<Country> countryList2 = DB.find(Country.class)
|
||||
.setUseQueryCache(true)
|
||||
.order().asc("name")
|
||||
.findList();
|
||||
|
||||
assertNotSame(countryList2, countryList0);
|
||||
|
||||
nz = DB.find(Country.class, "NZ");
|
||||
nz.setName("New Zealand");
|
||||
DB.save(nz);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package org.tests.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import io.ebean.cache.ServerCache;
|
||||
import io.ebean.cache.ServerCacheStatistics;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.EBasicVer;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TestQueryCacheInsert extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
Database server = DB.getDefault();
|
||||
|
||||
EBasicVer account = new EBasicVer("junk");
|
||||
server.save(account);
|
||||
|
||||
List<EBasicVer> alist0 = server.find(EBasicVer.class).setUseQueryCache(true).findList();
|
||||
|
||||
EBasicVer a2 = new EBasicVer("junk2");
|
||||
server.save(a2);
|
||||
awaitL2Cache();
|
||||
|
||||
List<EBasicVer> alist1 = server.find(EBasicVer.class).setUseQueryCache(true).findList();
|
||||
|
||||
assertEquals(alist0.size() + 1, alist1.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findOne() {
|
||||
|
||||
EBasicVer doda = new EBasicVer("doda");
|
||||
doda.setDescription("OddButUniqueSillyExample");
|
||||
|
||||
DB.save(doda);
|
||||
|
||||
ServerCache queryCache = DB.cacheManager().queryCache(EBasicVer.class);
|
||||
queryCache.statistics(true);
|
||||
|
||||
Optional<EBasicVer> found0 = DB.find(EBasicVer.class)
|
||||
.where().eq("description", "OddButUniqueSillyExample")
|
||||
.setUseQueryCache(true)
|
||||
.findOneOrEmpty();
|
||||
|
||||
assertTrue(found0.isPresent());
|
||||
assertHitMiss(0, 1, queryCache);
|
||||
|
||||
EBasicVer found1 = DB.find(EBasicVer.class)
|
||||
.where().eq("description", "OddButUniqueSillyExample")
|
||||
.setUseQueryCache(true)
|
||||
.findOne();
|
||||
|
||||
assertNotNull(found1);
|
||||
assertHitMiss(1, 0, queryCache);
|
||||
}
|
||||
|
||||
private void assertHitMiss(int hits, int miss, ServerCache queryCache) {
|
||||
ServerCacheStatistics stats = queryCache.statistics(true);
|
||||
assertEquals(hits, stats.getHitCount());
|
||||
assertEquals(miss, stats.getMissCount());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package org.tests.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.CacheMode;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Database;
|
||||
import io.ebean.Query;
|
||||
|
||||
import org.tests.model.basic.EBasicVer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
public class TestQueryCacheReadOnly extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testReadOnly() {
|
||||
|
||||
Database server = DB.getDefault();
|
||||
EBasicVer account = new EBasicVer("an other junk");
|
||||
server.save(account);
|
||||
|
||||
Query<EBasicVer> baseQuery = server.find(EBasicVer.class).setUseQueryCache(CacheMode.ON);
|
||||
|
||||
List<EBasicVer> alist = baseQuery.findList();
|
||||
assertThat(alist).isNotEmpty();
|
||||
assertThatThrownBy(alist::clear).hasMessageContaining("This collection is in ReadOnly mode");
|
||||
alist = baseQuery.findList();
|
||||
assertThat(alist).isNotEmpty();
|
||||
assertThatThrownBy(alist::clear).hasMessageContaining("This collection is in ReadOnly mode");
|
||||
|
||||
Map<String,EBasicVer> amap = baseQuery.setMapKey("name").findMap();
|
||||
assertThat(amap).isNotEmpty();
|
||||
assertThatThrownBy(amap::clear).hasMessageContaining("This collection is in ReadOnly mode");
|
||||
amap = baseQuery.setMapKey("name").findMap();
|
||||
assertThat(amap).isNotEmpty();
|
||||
assertThatThrownBy(amap::clear).hasMessageContaining("This collection is in ReadOnly mode");
|
||||
|
||||
Set<EBasicVer> aset = baseQuery.findSet();
|
||||
assertThat(aset).isNotEmpty();
|
||||
assertThatThrownBy(aset::clear).hasMessageContaining("This collection is in ReadOnly mode");
|
||||
aset = baseQuery.findSet();
|
||||
assertThat(aset).isNotEmpty();
|
||||
assertThatThrownBy(aset::clear).hasMessageContaining("This collection is in ReadOnly mode");
|
||||
|
||||
// we will get an unmodifiable collection here
|
||||
List<Object> attributeList = baseQuery.select("name").findSingleAttributeList();
|
||||
assertThat(attributeList).isNotEmpty();
|
||||
assertThatThrownBy(attributeList::clear).isInstanceOf(UnsupportedOperationException.class);
|
||||
attributeList = baseQuery.select("name").findSingleAttributeList();
|
||||
assertThat(attributeList).isNotEmpty();
|
||||
assertThatThrownBy(attributeList::clear).isInstanceOf(UnsupportedOperationException.class);
|
||||
|
||||
List<Object> idList = baseQuery.select("name").findIds();
|
||||
assertThat(idList).isNotEmpty();
|
||||
assertThatThrownBy(idList::clear).isInstanceOf(UnsupportedOperationException.class);
|
||||
idList = baseQuery.select("name").findIds();
|
||||
assertThat(idList).isNotEmpty();
|
||||
assertThatThrownBy(idList::clear).isInstanceOf(UnsupportedOperationException.class);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testNotReadOnly() {
|
||||
|
||||
Database server = DB.getDefault();
|
||||
EBasicVer account = new EBasicVer("an other junk");
|
||||
server.save(account);
|
||||
|
||||
Query<EBasicVer> baseQuery = server.find(EBasicVer.class).setUseQueryCache(CacheMode.ON).setReadOnly(false);
|
||||
|
||||
List<EBasicVer> alist = baseQuery.findList();
|
||||
assertThat(alist).isNotEmpty();
|
||||
alist.clear();
|
||||
alist = baseQuery.findList();
|
||||
assertThat(alist).isNotEmpty();
|
||||
alist.clear();
|
||||
|
||||
Map<String,EBasicVer> amap = baseQuery.setMapKey("name").findMap();
|
||||
assertThat(amap).isNotEmpty();
|
||||
amap.clear();
|
||||
amap = baseQuery.setMapKey("name").findMap();
|
||||
assertThat(amap).isNotEmpty();
|
||||
amap.clear();
|
||||
|
||||
Set<EBasicVer> aset = baseQuery.findSet();
|
||||
assertThat(aset).isNotEmpty();
|
||||
aset.clear();
|
||||
aset = baseQuery.findSet();
|
||||
assertThat(aset).isNotEmpty();
|
||||
aset.clear();
|
||||
|
||||
List<Object> attributeList = baseQuery.select("name").findSingleAttributeList();
|
||||
assertThat(attributeList).isNotEmpty();
|
||||
attributeList.clear();
|
||||
attributeList = baseQuery.select("name").findSingleAttributeList();
|
||||
assertThat(attributeList).isNotEmpty();
|
||||
attributeList.clear();
|
||||
|
||||
List<Object> idList = baseQuery.select("name").findIds();
|
||||
assertThat(idList).isNotEmpty();
|
||||
idList.clear();
|
||||
idList = baseQuery.select("name").findIds();
|
||||
assertThat(idList).isNotEmpty();
|
||||
idList.clear();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
package org.tests.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import io.ebean.cache.ServerCache;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Address;
|
||||
import org.tests.model.basic.Contact;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.tests.model.basic.cache.ECacheChild;
|
||||
import org.tests.model.basic.cache.ECacheRoot;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestQueryCacheTableDependency extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testFindCountOnDependent() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
ServerCache customerCache = DB.cacheManager().queryCache(Customer.class);
|
||||
customerCache.clear();
|
||||
|
||||
List<Address> addrs = DB.find(Address.class)
|
||||
.where().eq("line2", "St Lukes")
|
||||
.findList();
|
||||
|
||||
int custs = DB.find(Customer.class).setUseQueryCache(true).setReadOnly(true)
|
||||
.where().eq("billingAddress.line2", "St Lukes")
|
||||
.findCount();
|
||||
|
||||
assertThat(custs).isEqualTo(3);
|
||||
|
||||
custs = DB.find(Customer.class).setUseQueryCache(true).setReadOnly(true)
|
||||
.where().eq("billingAddress.line2", "St Lukes")
|
||||
.findCount();
|
||||
|
||||
assertThat(custs).isEqualTo(3);
|
||||
|
||||
Address a1 = addrs.get(0);
|
||||
a1.setLine2("St Lucky");
|
||||
DB.save(a1);
|
||||
|
||||
custs = DB.find(Customer.class).setUseQueryCache(true).setReadOnly(true)
|
||||
.where().eq("billingAddress.line2", "St Lukes")
|
||||
.findCount();
|
||||
|
||||
assertThat(custs).isEqualTo(2);
|
||||
|
||||
|
||||
custs = DB.find(Customer.class).setUseQueryCache(true).setReadOnly(true)
|
||||
.where().eq("billingAddress.line2", "St Lucky")
|
||||
.findCount();
|
||||
|
||||
assertThat(custs).isEqualTo(1);
|
||||
|
||||
DB.update(Address.class)
|
||||
.set("line2", "St Lucky2")
|
||||
.where().eq("line2", "St Lucky")
|
||||
.update();
|
||||
|
||||
custs = DB.find(Customer.class).setUseQueryCache(true).setReadOnly(true)
|
||||
.where().eq("billingAddress.line2", "St Lucky")
|
||||
.findCount();
|
||||
|
||||
assertThat(custs).isEqualTo(0);
|
||||
|
||||
custs = DB.find(Customer.class).setUseQueryCache(true).setReadOnly(true)
|
||||
.where().eq("billingAddress.line2", "St Lucky2")
|
||||
.findCount();
|
||||
assertThat(custs).isEqualTo(1);
|
||||
|
||||
DB.sqlUpdate("update o_address set line_2=? where line_2=?")
|
||||
.setParameters("St Lucky3", "St Lucky2")
|
||||
.execute();
|
||||
|
||||
custs = DB.find(Customer.class).setUseQueryCache(true).setReadOnly(true)
|
||||
.where().eq("billingAddress.line2", "St Lucky2")
|
||||
.findCount();
|
||||
assertThat(custs).isEqualTo(0);
|
||||
|
||||
custs = DB.find(Customer.class).setUseQueryCache(true).setReadOnly(true)
|
||||
.where().eq("billingAddress.line2", "St Lucky3")
|
||||
.findCount();
|
||||
|
||||
assertThat(custs).isEqualTo(1);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindCountOnOtherL2Cached() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Customer fi = DB.find(Customer.class).where().eq("name", "Fiona").findOne();
|
||||
|
||||
int custCount0 = DB.find(Customer.class).setUseQueryCache(true).setReadOnly(true)
|
||||
.where()
|
||||
.eq("name", "Fiona")
|
||||
.isNull("contacts.phone")
|
||||
.findCount();
|
||||
|
||||
assertThat(custCount0).isEqualTo(1);
|
||||
|
||||
int updateRows = DB.update(Contact.class)
|
||||
.set("phone", "1234")
|
||||
.where()
|
||||
.eq("customer.id", fi.getId())
|
||||
.update();
|
||||
|
||||
assertThat(updateRows).isGreaterThan(0);
|
||||
|
||||
int custCount1 = DB.find(Customer.class).setUseQueryCache(true).setReadOnly(true)
|
||||
.where()
|
||||
.eq("name", "Fiona")
|
||||
.isNull("contacts.phone")
|
||||
.findCount();
|
||||
|
||||
assertThat(custCount1).isEqualTo(0);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCache_withInvalidateQueryCache() throws InterruptedException {
|
||||
// So Address has no query cache enabled, but has @InvalidateQueryCache annotation
|
||||
Address root = new Address();
|
||||
root.setCity("new york");
|
||||
DB.save(root);
|
||||
|
||||
Customer child = new Customer();
|
||||
child.setName("bobby");
|
||||
child.setBillingAddress(root);
|
||||
DB.save(child);
|
||||
DB.cacheManager().queryCache(Address.class).clear();
|
||||
DB.cacheManager().queryCache(Customer.class).clear();
|
||||
// Test preparation finished, start the test
|
||||
Thread.sleep(10);
|
||||
|
||||
Query<Customer> query = DB.find(Customer.class).where().eq("billingAddress.city", "new york").query();
|
||||
query.setUseQueryCache(true);
|
||||
|
||||
assertThat(query.findCount()).isEqualTo(1);
|
||||
|
||||
root.setCity("san francisco");
|
||||
DB.save(root);
|
||||
|
||||
assertThat(query.findCount()).isEqualTo(0);
|
||||
|
||||
// clean up
|
||||
DB.delete(child);
|
||||
// DB.delete(root); deleted by M2O cascade.
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCache_withEnabeldQueryCache() throws InterruptedException {
|
||||
// ECacheRoot and ECacheChild have enabled query cache.
|
||||
ECacheRoot root = new ECacheRoot();
|
||||
root.setName("testRoot");
|
||||
DB.save(root);
|
||||
|
||||
ECacheChild child = new ECacheChild();
|
||||
child.setName("testChild");
|
||||
child.setRoot(root);
|
||||
DB.save(child);
|
||||
DB.cacheManager().queryCache(ECacheChild.class).clear();
|
||||
DB.cacheManager().queryCache(ECacheRoot.class).clear();
|
||||
// Test preparation finished, start the test
|
||||
Thread.sleep(10);
|
||||
// we need this thread.sleep here, because on a fast machine, DB.save(child) and computing the
|
||||
// queryCacheEntry will happen in the same millisecond (or 10 milliseconds, which is the resolution
|
||||
// of System.currentTimeMillis() on windows 7 & java 8)
|
||||
Query<ECacheChild> query = DB.find(ECacheChild.class).where().eq("root.name", "testRoot").query();
|
||||
query.setUseQueryCache(true);
|
||||
|
||||
assertThat(query.findCount()).isEqualTo(1);
|
||||
|
||||
root = DB.find(ECacheRoot.class).findList().get(0);
|
||||
root.setName("test2");
|
||||
DB.save(root);
|
||||
|
||||
assertThat(query.findCount()).isEqualTo(0);
|
||||
|
||||
// clean up
|
||||
DB.delete(child);
|
||||
DB.delete(root);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package org.tests.cache.embeddedid;
|
||||
|
||||
import io.ebean.annotation.Cache;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Version;
|
||||
|
||||
@Cache
|
||||
@Entity
|
||||
public class CEPCategory {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@Version
|
||||
private Long version;
|
||||
|
||||
public CEPCategory(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(Long version) {
|
||||
this.version = version;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package org.tests.cache.embeddedid;
|
||||
|
||||
import io.ebean.annotation.Cache;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.OrderColumn;
|
||||
import javax.persistence.Version;
|
||||
import java.util.List;
|
||||
|
||||
@Cache
|
||||
@Entity
|
||||
public class CEPProduct {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@OrderColumn(name = "priority")
|
||||
@OneToMany(cascade = CascadeType.ALL, mappedBy = "product")
|
||||
private List<CEPProductCategory> productCategories;
|
||||
|
||||
@Version
|
||||
private Long version;
|
||||
|
||||
public CEPProduct(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(Long version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public List<CEPProductCategory> getProductCategories() {
|
||||
return productCategories;
|
||||
}
|
||||
|
||||
public void setProductCategories(List<CEPProductCategory> productCategories) {
|
||||
this.productCategories = productCategories;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package org.tests.cache.embeddedid;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.IdClass;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
@IdClass(CEPProductCategoryId.class)
|
||||
@Entity
|
||||
public class CEPProductCategory {
|
||||
|
||||
@Id
|
||||
@ManyToOne
|
||||
private CEPCategory category;
|
||||
|
||||
@Id
|
||||
@ManyToOne
|
||||
private CEPProduct product;
|
||||
|
||||
public CEPProductCategory(CEPCategory category, CEPProduct product) {
|
||||
this.category = category;
|
||||
this.product = product;
|
||||
}
|
||||
|
||||
public CEPCategory getCategory() {
|
||||
return category;
|
||||
}
|
||||
|
||||
public void setCategory(CEPCategory category) {
|
||||
this.category = category;
|
||||
}
|
||||
|
||||
public CEPProduct getProduct() {
|
||||
return product;
|
||||
}
|
||||
|
||||
public void setProduct(CEPProduct product) {
|
||||
this.product = product;
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package org.tests.cache.embeddedid;
|
||||
|
||||
import javax.persistence.Embeddable;
|
||||
import java.util.Objects;
|
||||
|
||||
@Embeddable
|
||||
public class CEPProductCategoryId {
|
||||
|
||||
// primitive id values as part of an EmbeddedId (NPE #1722)
|
||||
private long customerId;
|
||||
private long addressId;
|
||||
|
||||
public long getCustomerId() {
|
||||
return customerId;
|
||||
}
|
||||
|
||||
public void setCustomerId(long customerId) {
|
||||
this.customerId = customerId;
|
||||
}
|
||||
|
||||
public long getAddressId() {
|
||||
return addressId;
|
||||
}
|
||||
|
||||
public void setAddressId(long addressId) {
|
||||
this.addressId = addressId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
CEPProductCategoryId that = (CEPProductCategoryId) o;
|
||||
return customerId == that.customerId &&
|
||||
addressId == that.addressId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(customerId, addressId);
|
||||
}
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package org.tests.cache.embeddedid;
|
||||
|
||||
import io.ebean.DB;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class TestBeanCacheEmbeddedIdPrimitives {
|
||||
|
||||
@Test
|
||||
public void findById() {
|
||||
|
||||
CEPProduct product1a = new CEPProduct("Product 01");
|
||||
DB.save(product1a);
|
||||
|
||||
assertNotNull(product1a.getId());
|
||||
|
||||
CEPCategory category1a = new CEPCategory("Category 01");
|
||||
DB.save(category1a);
|
||||
|
||||
assertNotNull(category1a.getId());
|
||||
|
||||
CEPProduct product1 = DB.find(CEPProduct.class).setUseCache(true).where().eq("id", product1a.getId()).findOne();
|
||||
|
||||
assertNotNull(product1);
|
||||
|
||||
product1.getProductCategories().forEach(customerAddress -> {
|
||||
System.out.println(customerAddress.getCategory());
|
||||
});
|
||||
assertThat(product1.getProductCategories()).isEmpty();
|
||||
|
||||
CEPProduct product2 = DB.find(CEPProduct.class).setUseCache(true).where().eq("id", product1a.getId()).findOne();
|
||||
assertNotNull(product2);
|
||||
|
||||
// fail here on null -> long conversion in embedded id
|
||||
product2.getProductCategories().forEach(customerAddress -> {
|
||||
System.out.println(customerAddress.getCategory());
|
||||
});
|
||||
assertThat(product2.getProductCategories()).isEmpty();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package org.tests.cache.personinfo;
|
||||
|
||||
import io.ebean.annotation.Cache;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
@Entity
|
||||
@Cache(region = "email")
|
||||
public class PersonCacheEmail {
|
||||
|
||||
@Id
|
||||
@Size(max=128)
|
||||
private String id;
|
||||
|
||||
@ManyToOne
|
||||
private PersonCacheInfo personInfo;
|
||||
|
||||
private String email;
|
||||
|
||||
public PersonCacheEmail(String id, String email) {
|
||||
this.id = id;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public PersonCacheEmail(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public PersonCacheInfo getPersonInfo() {
|
||||
return personInfo;
|
||||
}
|
||||
|
||||
public void setPersonInfo(PersonCacheInfo personInfo) {
|
||||
this.personInfo = personInfo;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package org.tests.cache.personinfo;
|
||||
|
||||
import io.ebean.annotation.Cache;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
@Entity
|
||||
@Cache(region = "email")
|
||||
public class PersonCacheInfo {
|
||||
|
||||
@Id
|
||||
@Size(max=128)
|
||||
private String personId;
|
||||
|
||||
private String name;
|
||||
|
||||
public PersonCacheInfo(String personId, String name) {
|
||||
this.personId = personId;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getPersonId() {
|
||||
return personId;
|
||||
}
|
||||
|
||||
public void setPersonId(String personId) {
|
||||
this.personId = personId;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
package org.tests.cache.personinfo;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.cache.ServerCache;
|
||||
import io.ebean.cache.ServerCacheRegion;
|
||||
import io.ebean.cache.ServerCacheStatistics;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class PersonCacheTests {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(PersonCacheTests.class);
|
||||
|
||||
private final List<Object> ids = Arrays.asList("E001", "E002", "E003");
|
||||
|
||||
private ServerCache beanCacheInfo = DB.getDefault().cacheManager().beanCache(PersonCacheInfo.class);
|
||||
private ServerCache beanCacheEmail = DB.getDefault().cacheManager().beanCache(PersonCacheEmail.class);
|
||||
|
||||
private ServerCacheRegion region = DB.getDefault().cacheManager().region("email");
|
||||
|
||||
private void insert(int id, String email) {
|
||||
|
||||
PersonCacheInfo person = new PersonCacheInfo("P00" + id, "P00" + id);
|
||||
|
||||
PersonCacheEmail personEmail = new PersonCacheEmail("E00" + id, email);
|
||||
personEmail.setPersonInfo(person);
|
||||
|
||||
DB.save(person);
|
||||
DB.save(personEmail);
|
||||
}
|
||||
|
||||
private void addTestData() {
|
||||
|
||||
insert(1, "testA");
|
||||
insert(2, "testB");
|
||||
insert(3, "testC");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInQuery() {
|
||||
|
||||
addTestData();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
DB.find(PersonCacheInfo.class)
|
||||
.select("personId") // do not fetch name
|
||||
.setUseCache(true)
|
||||
.findList();
|
||||
|
||||
beanCacheInfo.statistics(true);
|
||||
|
||||
List<PersonCacheEmail> emailList =
|
||||
DB.find(PersonCacheEmail.class)
|
||||
.where().idIn(ids)
|
||||
.setUseCache(true)
|
||||
.findList();
|
||||
|
||||
|
||||
for (PersonCacheEmail email : emailList) {
|
||||
// get property that isn't in the cached data
|
||||
assertThat(email.getPersonInfo().getName()).isNotNull();
|
||||
}
|
||||
|
||||
List<String> sql = LoggedSqlCollector.current();
|
||||
assertThat(sql).hasSize(3);
|
||||
|
||||
assertThat(beanCacheInfo.statistics(true).getHitCount()).isEqualTo(3);
|
||||
|
||||
// force cache misses on PersonCacheEmail
|
||||
beanCacheEmail.clear();
|
||||
log.info("Fetch again - cache missing on PersonCacheEmail and cache hits on PersonCacheInfo ...");
|
||||
|
||||
emailList =
|
||||
DB.find(PersonCacheEmail.class)
|
||||
.where().idIn(ids)
|
||||
.setUseCache(true)
|
||||
.findList();
|
||||
|
||||
for (PersonCacheEmail email : emailList) {
|
||||
// get property but it is in the cache data now
|
||||
System.out.println(email.getPersonInfo().getName());
|
||||
assertThat(email.getPersonInfo().getName()).isNotNull();
|
||||
}
|
||||
|
||||
sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(1);
|
||||
|
||||
assertThat(beanCacheInfo.statistics(true).getHitCount()).isEqualTo(3);
|
||||
|
||||
turnOffRegion_expect_noCacheUse();
|
||||
|
||||
}
|
||||
|
||||
private void turnOffRegion_expect_noCacheUse() {
|
||||
|
||||
assertTrue(region.isEnabled());
|
||||
beanCacheEmail.statistics(true);
|
||||
|
||||
log.info("Disabled region ...");
|
||||
region.setEnabled(false);
|
||||
|
||||
for (Object id : ids) {
|
||||
DB.find(PersonCacheEmail.class)
|
||||
.where().idEq(id)
|
||||
.setUseCache(true)
|
||||
.findOne();
|
||||
}
|
||||
|
||||
// assert that we didn't hit the cache
|
||||
ServerCacheStatistics statistics = beanCacheEmail.statistics(true);
|
||||
assertThat(statistics.getHitCount()).isEqualTo(0);
|
||||
assertThat(statistics.getMissCount()).isEqualTo(0);
|
||||
|
||||
log.info("Enabled region ...");
|
||||
region.setEnabled(true);
|
||||
|
||||
for (Object id : ids) {
|
||||
DB.find(PersonCacheEmail.class)
|
||||
.where().idEq(id)
|
||||
.setUseCache(true)
|
||||
.findOne();
|
||||
}
|
||||
|
||||
// assert that we DID hit the cache
|
||||
statistics = beanCacheEmail.statistics(true);
|
||||
assertThat(statistics.getHitCount()).isEqualTo(3);
|
||||
assertThat(statistics.getMissCount()).isEqualTo(0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package org.tests.cache.personinfo;
|
||||
|
||||
import io.ebean.annotation.WhenCreated;
|
||||
import io.ebean.annotation.WhenModified;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Version;
|
||||
import javax.validation.constraints.Size;
|
||||
import java.time.Instant;
|
||||
|
||||
@Entity
|
||||
public class PersonOther {
|
||||
|
||||
@Id
|
||||
@Size(max=128)
|
||||
private String id;
|
||||
|
||||
private String email;
|
||||
|
||||
@WhenCreated
|
||||
private Instant whenCreated;
|
||||
|
||||
@WhenModified
|
||||
private Instant whenModified;
|
||||
|
||||
@Version
|
||||
private long version;
|
||||
|
||||
public PersonOther(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public Instant getWhenCreated() {
|
||||
return whenCreated;
|
||||
}
|
||||
|
||||
public void setWhenCreated(Instant whenCreated) {
|
||||
this.whenCreated = whenCreated;
|
||||
}
|
||||
|
||||
public Instant getWhenModified() {
|
||||
return whenModified;
|
||||
}
|
||||
|
||||
public void setWhenModified(Instant whenModified) {
|
||||
this.whenModified = whenModified;
|
||||
}
|
||||
|
||||
public long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(long version) {
|
||||
this.version = version;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package org.tests.cache.personinfo;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestStringIdOnly extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void insert() {
|
||||
|
||||
PersonCacheEmail b0 = new PersonCacheEmail("IdOnly");
|
||||
DB.save(b0);
|
||||
|
||||
PersonCacheEmail found = DB.find(PersonCacheEmail.class, b0.getId());
|
||||
assertThat(found).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void insert_whenIdOnly() {
|
||||
|
||||
PersonOther b0 = new PersonOther("IdOnly");
|
||||
DB.save(b0);
|
||||
|
||||
PersonOther found = DB.find(PersonOther.class, b0.getId());
|
||||
assertThat(found).isNotNull();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user