Files
ebean/ebean-test/src/test/java/org/tests/cache/TestQueryCacheReadOnly.java
T
Rob Bygrave fa25d7cc8a Using Query Cache now implies unmodifiable
- Query Cache now holds unmodifiable collections
- Can no longer have readOnly=false with queryCache=true
- Reference beans now also honor unmodifiable
- Effectively no longer does bean cache lookup for reference beans (which it defaulted to when cacheSharableBeans true, e.g. Country entity bean)
2025-03-11 23:50:34 +13:00

109 lines
4.6 KiB
Java

package org.tests.cache;
import io.ebean.*;
import io.ebean.xtest.BaseTestCase;
import org.junit.jupiter.api.Test;
import org.tests.model.basic.EBasicVer;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
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).isInstanceOf(UnsupportedOperationException.class);
alist = baseQuery.findList();
assertThat(alist).isNotEmpty();
assertThatThrownBy(alist::clear).isInstanceOf(UnsupportedOperationException.class);
Map<String,EBasicVer> amap = baseQuery.setMapKey("name").findMap();
assertThat(amap).isNotEmpty();
assertThatThrownBy(amap::clear).isInstanceOf(UnsupportedOperationException.class);
amap = baseQuery.setMapKey("name").findMap();
assertThat(amap).isNotEmpty();
assertThatThrownBy(amap::clear).isInstanceOf(UnsupportedOperationException.class);
Set<EBasicVer> aset = baseQuery.findSet();
assertThat(aset).isNotEmpty();
assertThatThrownBy(aset::clear).isInstanceOf(UnsupportedOperationException.class);
aset = baseQuery.findSet();
assertThat(aset).isNotEmpty();
assertThatThrownBy(aset::clear).isInstanceOf(UnsupportedOperationException.class);
// 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);
List<EBasicVer> alist = baseQuery.findList();
assertThat(alist).isNotEmpty();
assertThatThrownBy(alist::clear).isInstanceOf(UnsupportedOperationException.class);
alist = baseQuery.findList();
assertThat(alist).isNotEmpty();
assertThatThrownBy(alist::clear).isInstanceOf(UnsupportedOperationException.class);
Map<String,EBasicVer> amap = baseQuery.setMapKey("name").findMap();
assertThat(amap).isNotEmpty();
assertThatThrownBy(amap::clear).isInstanceOf(UnsupportedOperationException.class);
amap = baseQuery.setMapKey("name").findMap();
assertThat(amap).isNotEmpty();
assertThatThrownBy(amap::clear).isInstanceOf(UnsupportedOperationException.class);
Set<EBasicVer> aset = baseQuery.findSet();
assertThat(aset).isNotEmpty();
assertThatThrownBy(aset::clear).isInstanceOf(UnsupportedOperationException.class);
aset = baseQuery.findSet();
assertThat(aset).isNotEmpty();
assertThatThrownBy(aset::clear).isInstanceOf(UnsupportedOperationException.class);
final List<Object> attributeList = baseQuery.select("name").findSingleAttributeList();
assertThat(attributeList).isNotEmpty();
assertThatThrownBy(attributeList::clear).isInstanceOf(UnsupportedOperationException.class);
final List<Object> attributeList2 = baseQuery.select("name").findSingleAttributeList();
assertThat(attributeList2).isNotEmpty();
assertThatThrownBy(attributeList2::clear).isInstanceOf(UnsupportedOperationException.class);
List<Object> idList = baseQuery.select("name").findIds();
assertThat(idList).isNotEmpty();
assertThatThrownBy(idList::clear).isInstanceOf(UnsupportedOperationException.class);
List<Object> idList2 = baseQuery.select("name").findIds();
assertThat(idList2).isNotEmpty();
assertThatThrownBy(idList2::clear).isInstanceOf(UnsupportedOperationException.class);
}
}