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,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