mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#919 - io.ebean package initial
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
package org.tests.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import org.tests.model.basic.OCachedBean;
|
||||
import org.avaje.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
/**
|
||||
* Test class testing deleting/invalidating of cached beans
|
||||
*/
|
||||
public class TestBeanCache extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void findById_when_idTypeConverted() {
|
||||
|
||||
OCachedBean bean = new OCachedBean();
|
||||
bean.setName("findById");
|
||||
Ebean.save(bean);
|
||||
|
||||
OCachedBean bean0 = Ebean.find(OCachedBean.class, bean.getId());
|
||||
assertNotNull(bean0);
|
||||
|
||||
// expect to hit the cache, no SQL
|
||||
LoggedSqlCollector.start();
|
||||
OCachedBean bean1 = Ebean.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 = Ebean.find(OCachedBean.class).setReadOnly(true).setId(String.valueOf(bean.getId())).findUnique();
|
||||
sql = LoggedSqlCollector.stop();
|
||||
assertNotNull(bean2);
|
||||
assertThat(sql).isEmpty();
|
||||
|
||||
}
|
||||
}
|
||||
+146
@@ -0,0 +1,146 @@
|
||||
package org.tests.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
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.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestCacheBasic extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Ebean.getServerCacheManager().clear(Country.class);
|
||||
ServerCache countryCache = Ebean.getServerCacheManager().getBeanCache(Country.class);
|
||||
|
||||
loadCountryCache();
|
||||
Assert.assertTrue(countryCache.size() > 0);
|
||||
|
||||
// reset the statistics
|
||||
countryCache.getStatistics(true);
|
||||
|
||||
Country c0 = Ebean.getReference(Country.class, "NZ");
|
||||
ServerCacheStatistics statistics = countryCache.getStatistics(false);
|
||||
long hc = statistics.getHitCount();
|
||||
Assert.assertEquals(1, hc);
|
||||
Assert.assertNotNull(c0);
|
||||
|
||||
// Country c1 = Ebean.getReference(Country.class, "NZ");
|
||||
// Assert.assertEquals(2, countryCache.getStatistics(false).getHitCount());
|
||||
// //Assert.assertEquals(100,
|
||||
// countryCache.getStatistics(false).getHitRatio());
|
||||
//
|
||||
// // same instance as caching with readOnly=true
|
||||
// Assert.assertTrue(c0 != c1);
|
||||
//
|
||||
// c0.getName();
|
||||
// c1.getName();
|
||||
//
|
||||
// // reset the statistics
|
||||
// Assert.assertEquals(2,countryCache.getStatistics(true).getHitCount());
|
||||
// // now the count should be 0 again
|
||||
// Assert.assertEquals(0, countryCache.getStatistics(false).getHitCount());
|
||||
// // and hitRatio is 0 as well
|
||||
// Assert.assertEquals(0, countryCache.getStatistics(false).getHitRatio());
|
||||
//
|
||||
// // hit the country cache automatically via join
|
||||
//
|
||||
// Customer custTest = ResetBasicData.createCustAndOrder("cacheBasic");
|
||||
// Integer id = custTest.getId();
|
||||
// Customer customer = Ebean.find(Customer.class, id);
|
||||
//
|
||||
// Address billingAddress = customer.getBillingAddress();
|
||||
// Country c2 = billingAddress.getCountry();
|
||||
// c2.getName();
|
||||
//
|
||||
// Assert.assertTrue(countryCache.getStatistics(false).getHitCount() > 0);
|
||||
//
|
||||
// //Country c3 = Ebean.getReference(Country.class, "NZ");
|
||||
// //Country c4 = Ebean.find(Country.class, "NZ");
|
||||
//
|
||||
//
|
||||
// // clear the cache
|
||||
// Ebean.getServerCacheManager().clear(Country.class);
|
||||
// // reset statistics
|
||||
// countryCache.getStatistics(true);
|
||||
//
|
||||
// // try to hit the country cache automatically via join
|
||||
// customer = Ebean.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
|
||||
// Assert.assertEquals("empty cache",0,countryCache.getStatistics(false).getSize());
|
||||
// //Assert.assertEquals("missCount 1",1,countryCache.getStatistics(false).getMissCount());
|
||||
//
|
||||
// // lazy load on c5 populates the cache
|
||||
// c5.getName();
|
||||
// Assert.assertEquals("cache populated via lazy load",1,countryCache.getStatistics(false).getSize());
|
||||
//
|
||||
// // now these get hits in the cache
|
||||
// Country c6 = Ebean.find(Country.class, "NZ");
|
||||
//
|
||||
// Assert.assertTrue("different instance as cache cleared",c2 != c5);
|
||||
// Assert.assertTrue("these 2 are different",c5 != c6);
|
||||
//
|
||||
// // by default readOnly based on deployment annotation
|
||||
// Assert.assertTrue("read only",Ebean.getBeanState(c6).isReadOnly());
|
||||
//
|
||||
// try {
|
||||
// // can't modify a readOnly bean
|
||||
// c6.setName("Nu Zilund");
|
||||
// Assert.assertFalse("Never get here",true);
|
||||
// } catch (IllegalStateException e){
|
||||
// Assert.assertTrue("This is readOnly",true);
|
||||
// }
|
||||
//
|
||||
// Country c8 = Ebean.find(Country.class)
|
||||
// .setId("NZ")
|
||||
// .setReadOnly(false)
|
||||
// .findUnique();
|
||||
//
|
||||
// // Explicitly NOT readOnly
|
||||
// Assert.assertFalse("NOT read only",Ebean.getBeanState(c8).isReadOnly());
|
||||
//
|
||||
// Assert.assertEquals("1 countries in cache", 1, countryCache.size());
|
||||
// c8.setName("Nu Zilund");
|
||||
// // the update will remove the entry from the cache
|
||||
// Ebean.save(c8);
|
||||
//
|
||||
// Assert.assertEquals("1 country in cache", 1, countryCache.size());
|
||||
//
|
||||
// Country c9 = Ebean.find(Country.class)
|
||||
// .setReadOnly(false)
|
||||
// .setId("NZ")
|
||||
// .findUnique();
|
||||
//
|
||||
// // Find loads cache ...
|
||||
// Assert.assertFalse(Ebean.getBeanState(c9).isReadOnly());
|
||||
// Assert.assertTrue(countryCache.size() > 0);
|
||||
//
|
||||
// Country c10 = Ebean.find(Country.class,"NZ");
|
||||
//
|
||||
// Assert.assertTrue(Ebean.getBeanState(c10).isReadOnly());
|
||||
// Assert.assertTrue(countryCache.size() > 0);
|
||||
//
|
||||
// Ebean.getServerCacheManager().clear(Country.class);
|
||||
// Assert.assertEquals("0 country in cache", 0, countryCache.size());
|
||||
//
|
||||
// // reference doesn't load cache yet
|
||||
// Country c11 = Ebean.getReference(Country.class, "NZ");
|
||||
//
|
||||
// // still 0 in cache
|
||||
// Assert.assertEquals("0 country in cache", 0, countryCache.size());
|
||||
//
|
||||
// // will invoke lazy loading..
|
||||
// c11.getName();
|
||||
// Assert.assertTrue(countryCache.size() > 0);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,275 @@
|
||||
package org.tests.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.cache.ServerCache;
|
||||
import io.ebean.cache.ServerCacheManager;
|
||||
import io.ebeaninternal.server.cache.CachedManyIds;
|
||||
import org.tests.model.basic.Contact;
|
||||
import org.tests.model.basic.Country;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.OCachedBean;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class TestCacheCollectionIds extends BaseTestCase {
|
||||
|
||||
ServerCacheManager cacheManager = Ebean.getServerCacheManager();
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
awaitL2Cache();
|
||||
|
||||
ServerCache custCache = cacheManager.getBeanCache(Customer.class);
|
||||
ServerCache custManyIdsCache = cacheManager.getCollectionIdsCache(Customer.class, "contacts");
|
||||
|
||||
// cacheManager.setCaching(Customer.class, true);
|
||||
// cacheManager.setCaching(Contact.class, true);
|
||||
|
||||
custCache.clear();
|
||||
custManyIdsCache.clear();
|
||||
|
||||
List<Customer> list = Ebean.find(Customer.class).setAutoTune(false).setLoadBeanCache(true)
|
||||
.order().asc("id").findList();
|
||||
|
||||
Assert.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();
|
||||
Assert.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);
|
||||
|
||||
Ebean.save(newContact);
|
||||
awaitL2Cache();
|
||||
|
||||
int currentNumContacts2 = fetchCustomer(customer.getId());
|
||||
Assert.assertEquals(currentNumContacts + 1, currentNumContacts2);
|
||||
|
||||
}
|
||||
|
||||
private int fetchCustomer(Integer id) {
|
||||
|
||||
Customer customer2 = Ebean.find(Customer.class, id);
|
||||
|
||||
List<Contact> contacts2 = customer2.getContacts();
|
||||
contacts2.size();
|
||||
for (Contact contact : contacts2) {
|
||||
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(Ebean.find(Country.class, "NZ"));
|
||||
cachedBean.getCountries().add(Ebean.find(Country.class, "AU"));
|
||||
|
||||
Ebean.save(cachedBean);
|
||||
|
||||
// used to just load the cache - trigger loading
|
||||
OCachedBean dummyToLoad = Ebean.find(OCachedBean.class, cachedBean.getId());
|
||||
dummyToLoad.getCountries().size();
|
||||
|
||||
ServerCache cachedBeanCountriesCache = cacheManager.getCollectionIdsCache(OCachedBean.class, "countries");
|
||||
CachedManyIds cachedManyIds = (CachedManyIds) cachedBeanCountriesCache.get(cachedBean.getId());
|
||||
|
||||
// confirm the starting data and cache entry
|
||||
Assert.assertEquals(2, dummyToLoad.getCountries().size());
|
||||
Assert.assertEquals(2, cachedManyIds.getIdList().size());
|
||||
|
||||
|
||||
// act
|
||||
OCachedBean loadedBean = Ebean.find(OCachedBean.class, cachedBean.getId());
|
||||
loadedBean.getCountries().clear();
|
||||
loadedBean.getCountries().add(Ebean.find(Country.class, "AU"));
|
||||
|
||||
Ebean.save(loadedBean);
|
||||
awaitL2Cache();
|
||||
|
||||
// Get the data to assert/check against
|
||||
OCachedBean result = Ebean.find(OCachedBean.class, cachedBean.getId());
|
||||
cachedManyIds = (CachedManyIds) cachedBeanCountriesCache.get(result.getId());
|
||||
|
||||
// assert that data and cache both show correct data
|
||||
Assert.assertEquals(1, result.getCountries().size());
|
||||
Assert.assertEquals(1, cachedManyIds.getIdList().size());
|
||||
Assert.assertFalse(cachedManyIds.getIdList().contains("NZ"));
|
||||
Assert.assertTrue(cachedManyIds.getIdList().contains("AU"));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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(Ebean.find(Country.class, "NZ"));
|
||||
cachedBean.getCountries().add(Ebean.find(Country.class, "AU"));
|
||||
|
||||
Ebean.save(cachedBean);
|
||||
|
||||
// used to just load the cache - trigger loading
|
||||
OCachedBean dummyToLoad = Ebean.find(OCachedBean.class, cachedBean.getId());
|
||||
dummyToLoad.getCountries().size();
|
||||
|
||||
ServerCache cachedBeanCountriesCache = cacheManager.getCollectionIdsCache(OCachedBean.class, "countries");
|
||||
CachedManyIds cachedManyIds = (CachedManyIds) cachedBeanCountriesCache.get(cachedBean.getId());
|
||||
|
||||
// confirm the starting data and cache entry
|
||||
Assert.assertEquals(2, dummyToLoad.getCountries().size());
|
||||
Assert.assertEquals(2, cachedManyIds.getIdList().size());
|
||||
|
||||
|
||||
// act - this time update the name property so the bean is dirty
|
||||
OCachedBean loadedBean = Ebean.find(OCachedBean.class, cachedBean.getId());
|
||||
loadedBean.setName("goodbye");
|
||||
loadedBean.getCountries().clear();
|
||||
loadedBean.getCountries().add(Ebean.find(Country.class, "AU"));
|
||||
|
||||
Ebean.save(loadedBean);
|
||||
awaitL2Cache();
|
||||
|
||||
// Get the data to assert/check against
|
||||
OCachedBean result = Ebean.find(OCachedBean.class, cachedBean.getId());
|
||||
cachedManyIds = (CachedManyIds) cachedBeanCountriesCache.get(result.getId());
|
||||
|
||||
// assert that data and cache both show correct data
|
||||
Assert.assertEquals(1, result.getCountries().size());
|
||||
Assert.assertEquals(1, cachedManyIds.getIdList().size());
|
||||
Assert.assertFalse(cachedManyIds.getIdList().contains("NZ"));
|
||||
Assert.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(Ebean.find(Country.class, "NZ"));
|
||||
cachedBean.getCountries().add(Ebean.find(Country.class, "AU"));
|
||||
|
||||
Ebean.save(cachedBean);
|
||||
|
||||
// clear the cache
|
||||
ServerCache cachedBeanCountriesCache = cacheManager.getCollectionIdsCache(OCachedBean.class, "countries");
|
||||
cachedBeanCountriesCache.clear();
|
||||
Assert.assertEquals(0, cachedBeanCountriesCache.size());
|
||||
|
||||
// load the cache
|
||||
OCachedBean dummyLoad = Ebean.find(OCachedBean.class, cachedBean.getId());
|
||||
List<Country> dummyCountries = dummyLoad.getCountries();
|
||||
Assert.assertEquals(2, dummyCountries.size());
|
||||
|
||||
// assert that the cache contains the expected entry
|
||||
Assert.assertEquals("countries cache now loaded with 1 entry", 1, cachedBeanCountriesCache.size());
|
||||
CachedManyIds dummyEntry = (CachedManyIds) cachedBeanCountriesCache.get(dummyLoad.getId());
|
||||
Assert.assertNotNull(dummyEntry);
|
||||
Assert.assertEquals("2 ids in the entry", 2, dummyEntry.getIdList().size());
|
||||
Assert.assertTrue(dummyEntry.getIdList().contains("NZ"));
|
||||
Assert.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(Ebean.find(Country.class, "AU"));
|
||||
|
||||
Ebean.update(update);
|
||||
awaitL2Cache();
|
||||
|
||||
Assert.assertEquals("countries entry still there (but updated)", 1, cachedBeanCountriesCache.size());
|
||||
|
||||
CachedManyIds cachedManyIds = (CachedManyIds) cachedBeanCountriesCache.get(update.getId());
|
||||
|
||||
// assert cache updated
|
||||
Assert.assertEquals(1, cachedManyIds.getIdList().size());
|
||||
Assert.assertFalse(cachedManyIds.getIdList().contains("NZ"));
|
||||
Assert.assertTrue(cachedManyIds.getIdList().contains("AU"));
|
||||
|
||||
// assert countries good
|
||||
OCachedBean result = Ebean.find(OCachedBean.class, cachedBean.getId());
|
||||
Assert.assertEquals(1, result.getCountries().size());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdating_noChange() throws InterruptedException {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Ebean.beginTransaction();
|
||||
try {
|
||||
|
||||
OCachedBean cachedBean = new OCachedBean();
|
||||
cachedBean.setName("helloForUpdate");
|
||||
Ebean.save(cachedBean);
|
||||
|
||||
cachedBean = Ebean.find(OCachedBean.class, cachedBean.getId());
|
||||
|
||||
cachedBean.setName("mod");
|
||||
ArrayList<Country> list = new ArrayList<>();
|
||||
list.add(Ebean.find(Country.class, "NZ"));
|
||||
cachedBean.setCountries(list);
|
||||
Ebean.save(cachedBean);
|
||||
|
||||
|
||||
cachedBean.setName("mod2");
|
||||
Ebean.save(cachedBean);
|
||||
|
||||
Ebean.commitTransaction();
|
||||
} finally {
|
||||
Ebean.endTransaction();
|
||||
}
|
||||
|
||||
|
||||
Thread.sleep(2000);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package org.tests.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
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 org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class TestCacheCustomer extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testUpdateAddress() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Country nz = Ebean.getReference(Country.class, "NZ");
|
||||
Address address = new Address();
|
||||
address.setLine1("Some Place");
|
||||
address.setCity("Auckland");
|
||||
address.setCountry(nz);
|
||||
|
||||
Ebean.save(address);
|
||||
|
||||
address.setLine2("Else");
|
||||
Ebean.save(address);
|
||||
|
||||
Ebean.delete(address);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Customer> list = Ebean.find(Customer.class).setAutoTune(false).setLoadBeanCache(true)
|
||||
.findList();
|
||||
|
||||
Assert.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,44 @@
|
||||
package org.tests.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import org.tests.model.basic.OCachedBean;
|
||||
import org.tests.model.basic.OCachedBeanChild;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* 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);
|
||||
Ebean.save(parentBean);
|
||||
|
||||
// confirm there are 2 children loaded from the parent
|
||||
Assert.assertEquals(2, Ebean.find(OCachedBean.class, parentBean.getId()).getChildren().size());
|
||||
|
||||
// ensure cache has been populated
|
||||
Ebean.find(OCachedBeanChild.class, child.getId());
|
||||
child2 = Ebean.find(OCachedBeanChild.class, child2.getId());
|
||||
parentBean = Ebean.find(OCachedBean.class, parentBean.getId());
|
||||
|
||||
// act
|
||||
Ebean.delete(child2);
|
||||
awaitL2Cache();
|
||||
|
||||
OCachedBean beanFromCache = Ebean.find(OCachedBean.class, parentBean.getId());
|
||||
Assert.assertEquals(1, beanFromCache.getChildren().size());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package org.tests.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class TestCacheInterceptSaveWhenLazyLoaded extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
Customer customer = new Customer();
|
||||
customer.setName("daCustomer");
|
||||
Ebean.save(customer);
|
||||
|
||||
Order order = new Order();
|
||||
order.setStatus(Order.Status.NEW);
|
||||
order.setCustomer(customer);
|
||||
Ebean.save(order);
|
||||
|
||||
Ebean.beginTransaction();
|
||||
try {
|
||||
|
||||
Order foundOrder = Ebean.find(Order.class)
|
||||
.where().eq("id", order.getId())
|
||||
.findUnique();
|
||||
|
||||
foundOrder.setStatus(Order.Status.APPROVED);
|
||||
assertTrue(Ebean.getBeanState(foundOrder).isDirty());
|
||||
|
||||
Customer foundCustomer = Ebean.find(Customer.class)
|
||||
.where().eq("id", customer.getId())
|
||||
.findUnique();
|
||||
|
||||
// the foundOrder is in this list
|
||||
foundCustomer.getOrders().size();
|
||||
|
||||
Order order1 = foundCustomer.getOrders().get(0);
|
||||
|
||||
assertSame(foundOrder, order1);
|
||||
assertTrue(Ebean.getBeanState(foundOrder).isDirty());
|
||||
|
||||
Ebean.delete(order1);
|
||||
Ebean.delete(customer);
|
||||
|
||||
} finally {
|
||||
Ebean.endTransaction();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package org.tests.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.cache.ServerCache;
|
||||
import io.ebean.cache.ServerCacheStatistics;
|
||||
import org.tests.model.basic.Contact;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class TestCacheNaturalId extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
ServerCache contactCache = Ebean.getServerCacheManager().getBeanCache(Contact.class);
|
||||
|
||||
List<Contact> list = Ebean.find(Contact.class).setLoadBeanCache(true).findList();
|
||||
|
||||
assertTrue(contactCache.size() > 0);
|
||||
|
||||
String emailToSearch = null;
|
||||
for (Contact contact : list) {
|
||||
if (contact.getEmail() != null) {
|
||||
emailToSearch = contact.getEmail();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
contactCache.getStatistics(true);
|
||||
|
||||
Contact c0 = Ebean.find(Contact.class).where().eq("email", emailToSearch).findUnique();
|
||||
|
||||
ServerCacheStatistics stats0 = contactCache.getStatistics(false);
|
||||
|
||||
Contact c1 = Ebean.find(Contact.class).where().eq("email", emailToSearch).findUnique();
|
||||
|
||||
ServerCacheStatistics stats1 = contactCache.getStatistics(false);
|
||||
|
||||
assertNotNull(c0);
|
||||
assertNotNull(c1);
|
||||
|
||||
assertEquals(1, stats0.getHitCount());
|
||||
assertEquals(2, stats1.getHitCount());
|
||||
|
||||
c1.setEmail("mychangedemail@what.com");
|
||||
Ebean.save(c1);
|
||||
awaitL2Cache();
|
||||
|
||||
Contact c2 = Ebean.find(Contact.class).where().eq("email", "mychangedemail@what.com")
|
||||
.findUnique();
|
||||
|
||||
ServerCacheStatistics stats2 = contactCache.getStatistics(false);
|
||||
|
||||
assertNotNull(c2);
|
||||
assertEquals(c2.getId(), c1.getId());
|
||||
assertEquals(c0.getId(), c1.getId());
|
||||
assertTrue(stats2.getHitCount() > stats1.getHitCount());
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package org.tests.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
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.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class TestExternalNotification extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testNotify() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
loadCountryCache();
|
||||
|
||||
ServerCache countryCache = Ebean.getServerCacheManager().getBeanCache(Country.class);
|
||||
|
||||
assertTrue(countryCache.size() > 0);
|
||||
|
||||
// inserts don't remove from bean cache
|
||||
Ebean.externalModification("o_country", true, false, false);
|
||||
assertTrue(countryCache.size() > 0);
|
||||
|
||||
// updates flush cache
|
||||
Ebean.externalModification("o_country", false, true, false);
|
||||
assertEquals(0, countryCache.size());
|
||||
|
||||
loadCountryCache();
|
||||
assertTrue(countryCache.size() > 0);
|
||||
|
||||
// deletes flush cache
|
||||
Ebean.externalModification("o_country", false, false, true);
|
||||
assertEquals(0, countryCache.size());
|
||||
|
||||
loadCountryCache();
|
||||
assertTrue(countryCache.size() > 0);
|
||||
|
||||
ServerCacheManager serverCacheManager = Ebean.getServerCacheManager();
|
||||
serverCacheManager.clearAll();
|
||||
assertEquals(0, countryCache.size());
|
||||
|
||||
loadCountryCache();
|
||||
assertTrue(countryCache.size() > 0);
|
||||
|
||||
Ebean.getServerCacheManager().clear(Country.class);
|
||||
assertEquals(0, countryCache.size());
|
||||
|
||||
loadCountryCache();
|
||||
int cacheSize = countryCache.size();
|
||||
assertTrue("cacheSize: " + cacheSize, cacheSize > 0);
|
||||
|
||||
SqlUpdate sqlUpdate = Ebean
|
||||
.createSqlUpdate("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,64 @@
|
||||
package org.tests.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.Query;
|
||||
import io.ebean.cache.ServerCache;
|
||||
import io.ebeaninternal.api.SpiQuery;
|
||||
import io.ebeaninternal.server.autotune.model.Origin;
|
||||
import io.ebeaninternal.server.autotune.service.TunedQueryInfo;
|
||||
import io.ebeaninternal.server.querydefn.OrmQueryDetail;
|
||||
import org.tests.model.basic.FeatureDescription;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestL2CacheWithSharedBean extends BaseTestCase {
|
||||
|
||||
@NotNull
|
||||
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(null);
|
||||
|
||||
Ebean.save(f1);
|
||||
|
||||
ServerCache beanCache = Ebean.getServerCacheManager().getBeanCache(FeatureDescription.class);
|
||||
beanCache.getStatistics(true);
|
||||
|
||||
OrmQueryDetail tunedDetail = new OrmQueryDetail();
|
||||
tunedDetail.select("name");
|
||||
TunedQueryInfo tunedInfo = createTunedQueryInfo(tunedDetail);
|
||||
|
||||
Query<FeatureDescription> query = Ebean.find(FeatureDescription.class).setId(f1.getId());
|
||||
|
||||
tunedInfo.tuneQuery((SpiQuery<?>) query);
|
||||
|
||||
query.findUnique(); // PUT into cache
|
||||
|
||||
FeatureDescription fd2 = query.findUnique(); // LOAD cache
|
||||
|
||||
fd2.getDescription(); // invoke lazy load (this fails)
|
||||
|
||||
// load the cache
|
||||
FeatureDescription fetchOne = Ebean.find(FeatureDescription.class, f1.getId());
|
||||
Assert.assertNotNull(fetchOne);
|
||||
Assert.assertEquals(1, beanCache.getStatistics(false).getSize());
|
||||
|
||||
FeatureDescription fetchTwo = Ebean.find(FeatureDescription.class, f1.getId());
|
||||
FeatureDescription fetchThree = Ebean.find(FeatureDescription.class, f1.getId());
|
||||
Assert.assertSame(fetchTwo, fetchThree);
|
||||
|
||||
String description = fetchThree.getDescription();
|
||||
Assert.assertNull(description);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package org.tests.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.bean.BeanCollection;
|
||||
import io.ebean.cache.ServerCache;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
import org.tests.model.cache.EColAB;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestQueryCache extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void clashHashCode() {
|
||||
|
||||
new EColAB("01", "20").save();
|
||||
new EColAB("02", "10").save();
|
||||
|
||||
List<EColAB> list1 =
|
||||
Ebean.getServer(null)
|
||||
.find(EColAB.class)
|
||||
.setUseQueryCache(true)
|
||||
.where()
|
||||
.eq("columnA", "01")
|
||||
.eq("columnB", "20")
|
||||
.findList();
|
||||
|
||||
List<EColAB> list2 =
|
||||
Ebean.getServer(null)
|
||||
.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
|
||||
@SuppressWarnings("unchecked")
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
ServerCache customerCache = Ebean.getServerCacheManager().getQueryCache(Customer.class);
|
||||
customerCache.clear();
|
||||
|
||||
List<Customer> list = Ebean.find(Customer.class).setUseQueryCache(true).setReadOnly(true).where()
|
||||
.ilike("name", "Rob").findList();
|
||||
|
||||
BeanCollection<Customer> bc = (BeanCollection<Customer>) list;
|
||||
Assert.assertFalse(bc.isReadOnly());
|
||||
Assert.assertFalse(bc.isEmpty());
|
||||
Assert.assertTrue(!list.isEmpty());
|
||||
Assert.assertTrue(Ebean.getBeanState(list.get(0)).isReadOnly());
|
||||
|
||||
List<Customer> list2 = Ebean.find(Customer.class).setUseQueryCache(true).setReadOnly(true).where()
|
||||
.ilike("name", "Rob").findList();
|
||||
|
||||
List<Customer> list2B = Ebean.find(Customer.class).setUseQueryCache(true)
|
||||
// .setReadOnly(true)
|
||||
.where().ilike("name", "Rob").findList();
|
||||
|
||||
Assert.assertSame(list, list2);
|
||||
|
||||
// readOnly defaults to true for query cache
|
||||
Assert.assertSame(list, list2B);
|
||||
|
||||
|
||||
// TODO: At this stage setReadOnly(false) does not
|
||||
// create a shallow copy of the List/Set/Map
|
||||
|
||||
// List<Customer> list3 = Ebean.find(Customer.class).setUseQueryCache(true).setReadOnly(false).where()
|
||||
// .ilike("name", "Rob").findList();
|
||||
//
|
||||
// Assert.assertNotSame(list, list3);
|
||||
// BeanCollection<Customer> bc3 = (BeanCollection<Customer>) list3;
|
||||
// Assert.assertFalse(bc3.isReadOnly());
|
||||
// Assert.assertFalse(bc3.isEmpty());
|
||||
// Assert.assertTrue(list3.size() > 0);
|
||||
// Assert.assertFalse(Ebean.getBeanState(list3.get(0)).isReadOnly());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package org.tests.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
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.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class TestQueryCacheCountry extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
awaitL2Cache();
|
||||
|
||||
ServerCache queryCache = Ebean.getServerCacheManager().getQueryCache(Country.class);
|
||||
queryCache.clear();
|
||||
|
||||
ServerCache beanCache = Ebean.getServerCacheManager().getBeanCache(Country.class);
|
||||
beanCache.clear();
|
||||
|
||||
assertEquals(0, queryCache.getStatistics(false).getSize());
|
||||
|
||||
List<Country> countryList0 = Ebean.find(Country.class)
|
||||
.setUseQueryCache(true)
|
||||
.order().asc("name")
|
||||
.findList();
|
||||
|
||||
assertEquals(1, queryCache.getStatistics(false).getSize());
|
||||
assertTrue(!countryList0.isEmpty());
|
||||
|
||||
List<Country> countryList1 = Ebean.find(Country.class)
|
||||
.setUseQueryCache(true)
|
||||
.order().asc("name")
|
||||
.findList();
|
||||
|
||||
ServerCacheStatistics statistics = queryCache.getStatistics(false);
|
||||
assertEquals(1, statistics.getSize());
|
||||
assertEquals(1, statistics.getHitCount());
|
||||
Assert.assertSame(countryList1, countryList0);
|
||||
|
||||
Country nz = Ebean.find(Country.class, "NZ");
|
||||
nz.setName("New Zealandia");
|
||||
Ebean.save(nz);
|
||||
awaitL2Cache();
|
||||
|
||||
statistics = queryCache.getStatistics(false);
|
||||
assertEquals(0, statistics.getSize());
|
||||
|
||||
List<Country> countryList2 = Ebean.find(Country.class)
|
||||
.setUseQueryCache(true)
|
||||
.order().asc("name")
|
||||
.findList();
|
||||
|
||||
Assert.assertNotSame(countryList2, countryList0);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package org.tests.cache;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.EbeanServer;
|
||||
import org.tests.model.basic.EBasicVer;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class TestQueryCacheInsert extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
EbeanServer server = Ebean.getServer(null);
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user