mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
unit test for: update ManyToMany collection cache on updating cached beans
This commit is contained in:
committed by
Rob Bygrave
parent
a7df7da984
commit
2dccb818a2
@@ -2,15 +2,14 @@ package com.avaje.tests.cache;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.avaje.ebeaninternal.server.cache.CachedManyIds;
|
||||
import com.avaje.tests.model.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;
|
||||
import com.avaje.tests.model.basic.Contact;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestCacheCollectionIds extends BaseTestCase {
|
||||
|
||||
@@ -86,4 +85,70 @@ public class TestCacheCollectionIds extends BaseTestCase {
|
||||
}
|
||||
return contacts2.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* When updating a ManyToMany relations also the collection cache must be updated.
|
||||
*/
|
||||
@Test
|
||||
public void testUpdatingCollectionCacheForManyToManyRelations() {
|
||||
// arrange
|
||||
ResetBasicData.reset();
|
||||
|
||||
CachedBean cachedBean = new CachedBean();
|
||||
cachedBean.getCountries().add(Ebean.find(Country.class, "NZ"));
|
||||
cachedBean.getCountries().add(Ebean.find(Country.class, "AU"));
|
||||
|
||||
Ebean.save(cachedBean);
|
||||
|
||||
// act
|
||||
CachedBean loadedBean = Ebean.find(CachedBean.class, cachedBean.getId());
|
||||
loadedBean.getCountries().clear();
|
||||
loadedBean.getCountries().add(Ebean.find(Country.class, "AU"));
|
||||
|
||||
Ebean.save(cachedBean);
|
||||
|
||||
CachedBean result = Ebean.find(CachedBean.class, cachedBean.getId());
|
||||
ServerCache cachedBeanCountriesCache = Ebean.getServerCacheManager().getCollectionIdsCache(CachedBean.class, "countries");
|
||||
CachedManyIds cachedManyIds = (CachedManyIds) cachedBeanCountriesCache.get(result.getId());
|
||||
|
||||
|
||||
// assert
|
||||
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();
|
||||
|
||||
CachedBean cachedBean = new CachedBean();
|
||||
cachedBean.getCountries().add(Ebean.find(Country.class, "NZ"));
|
||||
cachedBean.getCountries().add(Ebean.find(Country.class, "AU"));
|
||||
|
||||
Ebean.save(cachedBean);
|
||||
|
||||
// act
|
||||
CachedBean update = new CachedBean();
|
||||
update.setId(cachedBean.getId());
|
||||
update.getCountries().add(Ebean.find(Country.class, "AU"));
|
||||
|
||||
Ebean.update(cachedBean);
|
||||
|
||||
CachedBean result = Ebean.find(CachedBean.class, cachedBean.getId());
|
||||
ServerCache cachedBeanCountriesCache = Ebean.getServerCacheManager().getCollectionIdsCache(CachedBean.class, "countries");
|
||||
CachedManyIds cachedManyIds = (CachedManyIds) cachedBeanCountriesCache.get(result.getId());
|
||||
|
||||
|
||||
// assert
|
||||
Assert.assertEquals(1, result.getCountries().size());
|
||||
Assert.assertEquals(1, cachedManyIds.getIdList().size());
|
||||
Assert.assertFalse(cachedManyIds.getIdList().contains("NZ"));
|
||||
Assert.assertTrue(cachedManyIds.getIdList().contains("AU"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.avaje.tests.model.basic;
|
||||
|
||||
import com.avaje.ebean.annotation.CacheStrategy;
|
||||
import com.avaje.ebean.annotation.CacheTuning;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToMany;
|
||||
import javax.persistence.Table;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Cached bean for testing caching implementation.
|
||||
*/
|
||||
@CacheStrategy
|
||||
@Entity
|
||||
@Table(name = "o_cached_bean")
|
||||
public class CachedBean {
|
||||
|
||||
@Id
|
||||
Long id;
|
||||
|
||||
@ManyToMany
|
||||
List<Country> countries = new ArrayList<Country>();
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public List<Country> getCountries() {
|
||||
return countries;
|
||||
}
|
||||
|
||||
public void setCountries(List<Country> countries) {
|
||||
this.countries = countries;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user