#704 - Update tests for 704 removing the use of Query.setUseCache(true) which is redundant

This commit is contained in:
Robin Bygrave
2016-05-13 14:31:03 +12:00
parent e344b5c0a1
commit 8c6653605d
8 changed files with 113 additions and 179 deletions
@@ -3,7 +3,6 @@ package com.avaje.tests.basic;
import java.util.Map;
import java.util.Set;
import org.junit.Assert;
import org.junit.Test;
import com.avaje.ebean.BaseTestCase;
@@ -13,6 +12,9 @@ import com.avaje.tests.model.basic.Address;
import com.avaje.tests.model.basic.Customer;
import com.avaje.tests.model.basic.ResetBasicData;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class TestLazyLoadInCache extends BaseTestCase {
@Test
@@ -23,12 +25,11 @@ public class TestLazyLoadInCache extends BaseTestCase {
Map<?, Customer> map = Ebean.find(Customer.class)
.select("id, name")
.setLoadBeanCache(true)
//.setUseCache(true)
.setReadOnly(true)
.orderBy().asc("id")
.findMap();
Assert.assertTrue(map.size() > 0);
assertTrue(map.size() > 0);
Object id = map.keySet().iterator().next();
@@ -36,28 +37,27 @@ public class TestLazyLoadInCache extends BaseTestCase {
Customer cust1B = Ebean.find(Customer.class)
.setReadOnly(true)
.setUseCache(true)
.setId(id)
.findUnique();
Assert.assertTrue(cust1 != cust1B);
assertTrue(cust1 != cust1B);
Set<String> loadedProps = Ebean.getBeanState(cust1).getLoadedProps();
Assert.assertTrue(loadedProps.contains("name"));
Assert.assertFalse(loadedProps.contains("status"));
assertTrue(loadedProps.contains("name"));
assertFalse(loadedProps.contains("status"));
cust1.getStatus();
// a readOnly reference
Address billingAddress = cust1.getBillingAddress();
BeanState billAddrState = Ebean.getBeanState(billingAddress);
Assert.assertTrue(billAddrState.isReference());
Assert.assertTrue(billAddrState.isReadOnly());
assertTrue(billAddrState.isReference());
assertTrue(billAddrState.isReadOnly());
// lazy load .. no longer a reference
billingAddress.getCity();
Assert.assertFalse(billAddrState.isReference());
assertFalse(billAddrState.isReference());
}
@@ -1,8 +1,5 @@
package com.avaje.tests.basic;
import org.junit.Assert;
import org.junit.Test;
import com.avaje.ebean.BaseTestCase;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.cache.ServerCache;
@@ -12,69 +9,14 @@ import com.avaje.ebeaninternal.server.core.CacheOptions;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.tests.model.basic.Country;
import com.avaje.tests.model.basic.ResetBasicData;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class TestQueryWithCache extends BaseTestCase {
// public void testJoinCache() {
//
// ResetBasicData.reset();
//
// Ebean.getServer(null).runCacheWarming();
//
// Query<Order> query = Ebean.createQuery(Order.class)
// .setAutoTune(false)
// .fetch("customer","+cache +readonly")
// .setId(1);
//
// Order order = query.findUnique();
// Customer customer = order.getCustomer();
// Assert.assertTrue(Ebean.getBeanState(customer).isReadOnly());
//
// // // invoke lazy loading
// // customer.getName();
// //
// // order = query.findUnique();
// // customer = order.getCustomer();
// // custState = Ebean.getBeanState(customer);
// // Assert.assertFalse(custState.isReadOnly());
//
// }
//
// public void testFindId() {
//
// ResetBasicData.reset();
//
// Order o = Ebean.find(Order.class)
// .setUseCache(true)
// .setReadOnly(true)
// .setId(1)
// .findUnique();
//
// BeanState beanState = Ebean.getBeanState(o);
// Assert.assertTrue(beanState.isReadOnly());
//
// Order o2 = Ebean.find(Order.class)
// .setUseCache(true)
// .setReadOnly(true)
// .setId(1)
// .findUnique();
//
// BeanState beanState2 = Ebean.getBeanState(o2);
//
// // same instance as readOnly = true
// Assert.assertTrue("not same instance", o != o2);
// Assert.assertTrue(beanState2.isReadOnly());
//
// Order o3 = Ebean.find(Order.class)
// .setUseCache(true)
// .setReadOnly(false)
// .setId(1)
// .findUnique();
//
// // NOT the same instance as readOnly = false
// Assert.assertTrue("not same instance", o != o3);
// }
@Test
public void testCountryDeploy() {
@@ -84,22 +26,22 @@ public class TestQueryWithCache extends BaseTestCase {
BeanDescriptor<Country> beanDescriptor = server.getBeanDescriptor(Country.class);
CacheOptions cacheOptions = beanDescriptor.getCacheOptions();
Assert.assertNotNull(cacheOptions);
Assert.assertTrue(cacheOptions.isReadOnly());
Assert.assertTrue(beanDescriptor.isCacheSharableBeans());
assertNotNull(cacheOptions);
assertTrue(cacheOptions.isReadOnly());
assertTrue(beanDescriptor.isCacheSharableBeans());
ServerCacheManager serverCacheManager = server.getServerCacheManager();
serverCacheManager.clear(Country.class);
ServerCache beanCache = serverCacheManager.getBeanCache(Country.class);
Assert.assertEquals(0, beanCache.size());
assertEquals(0, beanCache.size());
Country nz1 = Ebean.getReference(Country.class, "NZ");
Assert.assertEquals(0, beanCache.size());
assertEquals(0, beanCache.size());
// has the effect of loading the cache via lazy loading
nz1.getName();
Assert.assertEquals(1, beanCache.size());
assertEquals(1, beanCache.size());
Country nz2 = Ebean.getReference(Country.class, "NZ");
Country nz2b = Ebean.getReference(Country.class, "NZ");
@@ -109,9 +51,9 @@ public class TestQueryWithCache extends BaseTestCase {
Country nz4 = Ebean.find(Country.class).setId("NZ").setAutoTune(false).setUseCache(false)
.findUnique();
Assert.assertTrue(nz2 == nz2b);
Assert.assertTrue(nz2 == nz3);
Assert.assertTrue(nz3 != nz4);
assertTrue(nz2 == nz2b);
assertTrue(nz2 == nz3);
assertTrue(nz3 != nz4);
}
}
@@ -1,10 +1,5 @@
package com.avaje.tests.basic;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import com.avaje.ebean.BaseTestCase;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.bean.BeanCollection;
@@ -12,6 +7,13 @@ import com.avaje.tests.model.basic.Order;
import com.avaje.tests.model.basic.OrderDetail;
import com.avaje.tests.model.basic.Product;
import com.avaje.tests.model.basic.ResetBasicData;
import org.junit.Test;
import java.util.List;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class TestSharedInstancePropagation extends BaseTestCase {
@@ -28,37 +30,36 @@ public class TestSharedInstancePropagation extends BaseTestCase {
Order order = Ebean.find(Order.class)
.setAutoTune(false)
.setUseCache(true)
.setReadOnly(true)
.setId(1)
.findUnique();
Assert.assertNotNull(order);
Assert.assertTrue(Ebean.getBeanState(order).isReadOnly());
assertNotNull(order);
assertTrue(Ebean.getBeanState(order).isReadOnly());
List<OrderDetail> details = order.getDetails();
BeanCollection<?> bc = (BeanCollection<?>)details;
Assert.assertTrue(bc.isReadOnly());
Assert.assertFalse(bc.isPopulated());
assertTrue(bc.isReadOnly());
assertFalse(bc.isPopulated());
// lazy load
bc.size();
Assert.assertTrue(bc.isPopulated());
Assert.assertTrue(bc.size() > 0);
assertTrue(bc.isPopulated());
assertTrue(bc.size() > 0);
OrderDetail detail = details.get(0);
Assert.assertTrue(Ebean.getBeanState(detail).isReadOnly());
Assert.assertFalse(Ebean.getBeanState(detail).isReference());
assertTrue(Ebean.getBeanState(detail).isReadOnly());
assertFalse(Ebean.getBeanState(detail).isReference());
Product product = detail.getProduct();
Assert.assertTrue(Ebean.getBeanState(product).isReadOnly());
assertTrue(Ebean.getBeanState(product).isReadOnly());
// lazy load
product.getName();
Assert.assertFalse(Ebean.getBeanState(product).isReference());
assertFalse(Ebean.getBeanState(product).isReference());
}
}
@@ -2,19 +2,25 @@ package com.avaje.tests.batchload;
import com.avaje.ebean.BaseTestCase;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.cache.ServerCache;
import com.avaje.ebean.cache.ServerCacheManager;
import com.avaje.ebean.cache.ServerCacheStatistics;
import com.avaje.tests.model.basic.UUOne;
import org.junit.Assert;
import org.avaje.ebeantest.LoggedSqlCollector;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class TestBatchLazyWithCacheHits extends BaseTestCase {
private UUOne insert(String name) {
UUOne one = new UUOne();
one.setName("testBLWCH"+name);
one.setName("testBLWCH" + name);
Ebean.save(one);
return one;
}
@@ -27,48 +33,54 @@ public class TestBatchLazyWithCacheHits extends BaseTestCase {
for (int i = 0; i < names.length; i++) {
inserted.add(insert(names[i]));
}
UUOne b = Ebean.find(UUOne.class)
.setId(inserted.get(1).getId())
.setUseCache(true)
.findUnique();
Assert.assertNotNull(b);
UUOne b2 = Ebean.find(UUOne.class)
.where().idEq(inserted.get(1).getId())
.setUseCache(true)
.findUnique();
Assert.assertNotNull(b2);
ServerCacheManager serverCacheManager = Ebean.getDefaultServer().getServerCacheManager();
ServerCache beanCache = serverCacheManager.getBeanCache(UUOne.class);
beanCache.clear();
UUOne b = Ebean.find(UUOne.class, inserted.get(1).getId());
assertNotNull(b);
UUOne b2 = Ebean.find(UUOne.class, inserted.get(1).getId());
assertNotNull(b2);
ServerCacheStatistics statistics = beanCache.getStatistics(true);
assertEquals(statistics.getHitCount(), 1);
UUOne c = Ebean.find(UUOne.class)
.where().idEq(inserted.get(2).getId())
.setUseCache(true)
.findUnique();
Assert.assertNotNull(c);
assertNotNull(c);
UUOne c2 = Ebean.find(UUOne.class)
.where().idEq(inserted.get(2).getId())
.setUseCache(true)
.findUnique();
Assert.assertNotNull(c2);
assertNotNull(c2);
statistics = beanCache.getStatistics(true);
assertEquals(statistics.getHitCount(), 1);
LoggedSqlCollector.start();
List<UUOne> list = Ebean.find(UUOne.class)
//.setDefaultLazyLoadBatchSize(5)
.setUseCache(true)
.select("id")
.where().startsWith("name", "testBLWCH")
.order("name")
.findList();
for (UUOne uuOne : list) {
uuOne.getName();
}
list.get(0).getName();
for (UUOne uuOne : list) {
uuOne.getName();
}
list.get(0).getName();
List<String> sql = LoggedSqlCollector.stop();
// batch lazy loading into cache
assertThat(sql).hasSize(2);
assertThat(sql.get(0)).contains("from uuone t0 where t0.name like ? order by t0.name");
assertThat(sql.get(1)).contains("from uuone t0 where t0.id in (?,?,?,?");
statistics = beanCache.getStatistics(true);
assertThat(statistics.getSize()).isGreaterThan(3);
}
}
@@ -77,9 +77,7 @@ public class TestCacheCollectionIds extends BaseTestCase {
private int fetchCustomer(Integer id) {
Customer customer2 = Ebean.find(Customer.class).setId(id)
// .setUseCache(true)
.findUnique();
Customer customer2 = Ebean.find(Customer.class, id);
List<Contact> contacts2 = customer2.getContacts();
contacts2.size();
@@ -30,14 +30,6 @@ public class TestCacheInterceptSaveWhenLazyLoaded extends BaseTestCase {
.where().eq("id", order.getId())
.findUnique();
// QueryIterator<Order> iterate = Ebean.find(Order.class)
// //.setUseCache(true)
// .where().eq("id", order.getId())
// .findIterate();
//
// iterate.hasNext();
// Order foundOrder = iterate.next();
foundOrder.setStatus(Order.Status.APPROVED);
assertTrue(Ebean.getBeanState(foundOrder).isDirty());
@@ -1,10 +1,5 @@
package com.avaje.tests.query.embedded;
import java.util.Date;
import org.junit.Assert;
import org.junit.Test;
import com.avaje.ebean.BaseTestCase;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.EbeanServer;
@@ -14,6 +9,12 @@ import com.avaje.ebean.cache.ServerCacheStatistics;
import com.avaje.tests.model.embedded.EAddress;
import com.avaje.tests.model.embedded.EInvoice;
import com.avaje.tests.model.embedded.EInvoice.State;
import org.junit.Test;
import java.util.Date;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class TestMultipleEmbeddedLoading extends BaseTestCase {
@@ -40,16 +41,14 @@ public class TestMultipleEmbeddedLoading extends BaseTestCase {
// act: save and fetch
Ebean.save(invoice);
EInvoice invoice2 = Ebean.find(EInvoice.class)
.where().idEq(invoice.getId())
.findUnique();
EInvoice invoice2 = Ebean.find(EInvoice.class, invoice.getId());
// assert fetched bean populated as expected
Assert.assertEquals(invoice.getId(), invoice2.getId());
Assert.assertEquals(invoice.getState(), invoice2.getState());
Assert.assertEquals(invoice.getInvoiceDate(), invoice2.getInvoiceDate());
Assert.assertEquals("2 Apple St", invoice.getBillAddress().getStreet());
Assert.assertEquals("2 Apple St", invoice2.getBillAddress().getStreet());
assertEquals(invoice.getId(), invoice2.getId());
assertEquals(invoice.getState(), invoice2.getState());
assertEquals(invoice.getInvoiceDate(), invoice2.getInvoiceDate());
assertEquals("2 Apple St", invoice.getBillAddress().getStreet());
assertEquals("2 Apple St", invoice2.getBillAddress().getStreet());
// act: only update one of the embedded fields
invoice2.getBillAddress().setStreet("3 Pineapple St");
@@ -58,12 +57,10 @@ public class TestMultipleEmbeddedLoading extends BaseTestCase {
awaitL2Cache();
EInvoice invoice3 = Ebean.find(EInvoice.class)
.where().idEq(invoice.getId())
.findUnique();
EInvoice invoice3 = Ebean.find(EInvoice.class, invoice.getId());
// assert field has updated value
Assert.assertEquals("3 Pineapple St", invoice3.getBillAddress().getStreet());
assertEquals("3 Pineapple St", invoice3.getBillAddress().getStreet());
// fetch a partial
@@ -75,8 +72,8 @@ public class TestMultipleEmbeddedLoading extends BaseTestCase {
// lazy load of embedded bean
EAddress billAddress = invoicePartial.getBillAddress();
Assert.assertNotNull(billAddress);
Assert.assertEquals("3 Pineapple St", billAddress.getStreet());
assertNotNull(billAddress);
assertEquals("3 Pineapple St", billAddress.getStreet());
EbeanServer server = Ebean.getServer(null);
ServerCacheManager serverCacheManager = server.getServerCacheManager();
@@ -87,34 +84,26 @@ public class TestMultipleEmbeddedLoading extends BaseTestCase {
beanCache.getStatistics(true);
// fetch and load the cache
EInvoice invoice4 = Ebean.find(EInvoice.class)
.where().idEq(invoice.getId())
.setUseCache(true)
.findUnique();
Assert.assertNotNull(invoice4);
EInvoice invoice4 = Ebean.find(EInvoice.class, invoice.getId());
assertNotNull(invoice4);
ServerCacheStatistics statistics = beanCache.getStatistics(false);
Assert.assertEquals(1, statistics.getSize());
Assert.assertEquals(0, statistics.getHitCount());
assertEquals(1, statistics.getSize());
assertEquals(0, statistics.getHitCount());
// fetch out of the cache this time
EInvoice invoice5 = Ebean.find(EInvoice.class)
.where().idEq(invoice.getId())
.setUseCache(true)
.findUnique();
Assert.assertNotNull(invoice5);
EInvoice invoice5 = Ebean.find(EInvoice.class, invoice.getId());
assertNotNull(invoice5);
statistics = beanCache.getStatistics(false);
Assert.assertEquals(1, statistics.getSize());
Assert.assertEquals(1, statistics.getHitCount());
assertEquals(1, statistics.getSize());
assertEquals(1, statistics.getHitCount());
billAddress = invoice5.getBillAddress();
Assert.assertNotNull(billAddress);
Assert.assertEquals("3 Pineapple St", billAddress.getStreet());
assertNotNull(billAddress);
assertEquals("3 Pineapple St", billAddress.getStreet());
}
@@ -84,7 +84,7 @@ public class TestReadAudit extends BaseTestCase {
public void test_findById_usingL2Cache() {
resetCounters();
EBasicChangeLog found = server.find(EBasicChangeLog.class).setId(id1).setUseCache(true).findUnique();
EBasicChangeLog found = server.find(EBasicChangeLog.class, id1);
assertThat(found).isNotNull();
assertThat(readAuditPrepare.count).isEqualTo(1);
assertThat(readAuditLogger.plans).hasSize(1);
@@ -97,7 +97,7 @@ public class TestReadAudit extends BaseTestCase {
assertThat(statistics.getSize()).isEqualTo(1);
assertThat(statistics.getHitCount()).isEqualTo(0);
EBasicChangeLog found2 = server.find(EBasicChangeLog.class).setId(id1).setUseCache(true).findUnique();
EBasicChangeLog found2 = server.find(EBasicChangeLog.class, id1);
assertThat(found2).isNotNull();
statistics = beanCache.getStatistics(false);
assertThat(statistics.getSize()).isEqualTo(1);
@@ -110,7 +110,7 @@ public class TestReadAudit extends BaseTestCase {
public void test_findById_usingL2Cache_sharedBean() {
resetCounters();
Country found = server.find(Country.class).setId("AR").setUseCache(true).findUnique();
Country found = server.find(Country.class, "AR");
assertThat(found).isNotNull();
assertThat(readAuditPrepare.count).isEqualTo(1);
assertThat(readAuditLogger.plans).hasSize(1);
@@ -123,7 +123,7 @@ public class TestReadAudit extends BaseTestCase {
assertThat(statistics.getSize()).isEqualTo(1);
assertThat(statistics.getHitCount()).isEqualTo(0);
Country found2 = server.find(Country.class).setId("AR").setUseCache(true).findUnique();
Country found2 = server.find(Country.class, "AR");
assertThat(found2).isNotNull();
statistics = beanCache.getStatistics(false);
assertThat(statistics.getSize()).isEqualTo(1);