#363 - API CHANGE - BeanPersistController postLoad() ... moved to new/separate BeanPostLoad interface

This commit is contained in:
Robin Bygrave
2015-08-02 21:56:56 +12:00
parent 40ab3c81dc
commit 8f12442ac7
17 changed files with 617 additions and 72 deletions
@@ -0,0 +1,139 @@
package com.avaje.ebean.event;
import com.avaje.ebean.EbeanServer;
import com.avaje.ebean.EbeanServerFactory;
import com.avaje.ebean.config.ServerConfig;
import com.avaje.tests.model.basic.EBasicVer;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class BeanPersistControllerTest {
PersistAdapter continuePersistingAdapter = new PersistAdapter(true);
PersistAdapter stopPersistingAdapter = new PersistAdapter(false);
@Test
public void testInsertUpdateDelete_given_continuePersistingAdapter() {
EbeanServer ebeanServer = getEbeanServer(continuePersistingAdapter);
EBasicVer bean = new EBasicVer();
bean.setName("testController");
ebeanServer.save(bean);
assertThat(continuePersistingAdapter.methodsCalled).hasSize(2);
assertThat(continuePersistingAdapter.methodsCalled).containsExactly("preInsert", "postInsert");
continuePersistingAdapter.methodsCalled.clear();
bean.setName("modified");
ebeanServer.save(bean);
assertThat(continuePersistingAdapter.methodsCalled).hasSize(2);
assertThat(continuePersistingAdapter.methodsCalled).containsExactly("preUpdate", "postUpdate");
continuePersistingAdapter.methodsCalled.clear();
ebeanServer.delete(bean);
assertThat(continuePersistingAdapter.methodsCalled).hasSize(2);
assertThat(continuePersistingAdapter.methodsCalled).containsExactly("preDelete", "postDelete");
}
@Test
public void testInsertUpdateDelete_given_stopPersistingAdapter() {
EbeanServer ebeanServer = getEbeanServer(stopPersistingAdapter);
EBasicVer bean = new EBasicVer();
bean.setName("testController");
ebeanServer.save(bean);
assertThat(stopPersistingAdapter.methodsCalled).hasSize(1);
assertThat(stopPersistingAdapter.methodsCalled).containsExactly("preInsert");
stopPersistingAdapter.methodsCalled.clear();
bean.setName("modified");
ebeanServer.update(bean);
assertThat(stopPersistingAdapter.methodsCalled).hasSize(1);
assertThat(stopPersistingAdapter.methodsCalled).containsExactly("preUpdate");
stopPersistingAdapter.methodsCalled.clear();
ebeanServer.delete(bean);
assertThat(stopPersistingAdapter.methodsCalled).hasSize(1);
assertThat(stopPersistingAdapter.methodsCalled).containsExactly("preDelete");
}
private EbeanServer getEbeanServer(PersistAdapter persistAdapter) {
ServerConfig config = new ServerConfig();
config.setName("h2other");
config.loadFromProperties();
config.setRegister(false);
config.setDefaultServer(false);
config.getClasses().add(EBasicVer.class);
config.add(persistAdapter);
return EbeanServerFactory.create(config);
}
static class PersistAdapter extends BeanPersistAdapter {
boolean continueDefaultPersisting;
List<String> methodsCalled = new ArrayList<String>();
/**
* No default constructor so only registered manually.
*/
PersistAdapter(boolean continueDefaultPersisting) {
this.continueDefaultPersisting = continueDefaultPersisting;
}
@Override
public boolean isRegisterFor(Class<?> cls) {
return true;
}
@Override
public boolean preDelete(BeanPersistRequest<?> request) {
methodsCalled.add("preDelete");
return continueDefaultPersisting;
}
@Override
public boolean preInsert(BeanPersistRequest<?> request) {
methodsCalled.add("preInsert");
return continueDefaultPersisting;
}
@Override
public boolean preUpdate(BeanPersistRequest<?> request) {
methodsCalled.add("preUpdate");
return continueDefaultPersisting;
}
@Override
public void postDelete(BeanPersistRequest<?> request) {
methodsCalled.add("postDelete");
}
@Override
public void postInsert(BeanPersistRequest<?> request) {
methodsCalled.add("postInsert");
}
@Override
public void postUpdate(BeanPersistRequest<?> request) {
methodsCalled.add("postUpdate");
}
}
}
@@ -0,0 +1,95 @@
package com.avaje.ebean.event;
import com.avaje.ebean.BeanState;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.EbeanServer;
import com.avaje.ebean.EbeanServerFactory;
import com.avaje.ebean.config.ServerConfig;
import com.avaje.tests.model.basic.EBasicVer;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class BeanPostLoadTest {
PostLoad postLoad = new PostLoad(false);
@Test
public void testPostLoad() {
EbeanServer ebeanServer = getEbeanServer();
EBasicVer bean = new EBasicVer();
bean.setName("testPostLoad");
bean.setDescription("someDescription");
bean.setOther("other");
ebeanServer.save(bean);
EBasicVer found = ebeanServer.find(EBasicVer.class)
.select("name, other")
.setId(bean.getId())
.findUnique();
assertThat(postLoad.methodsCalled).hasSize(1);
assertThat(postLoad.methodsCalled).containsExactly("postLoad");
assertThat(postLoad.beanState.getLoadedProps()).containsExactly("id", "name", "other");
assertThat(postLoad.bean).isSameAs(found);
ebeanServer.delete(bean);
}
private EbeanServer getEbeanServer() {
ServerConfig config = new ServerConfig();
config.setName("h2other");
config.loadFromProperties();
config.setRegister(false);
config.setDefaultServer(false);
config.getClasses().add(EBasicVer.class);
config.add(postLoad);
return EbeanServerFactory.create(config);
}
static class PostLoad implements BeanPostLoad {
boolean dummy;
List<String> methodsCalled = new ArrayList<String>();
Object bean;
BeanState beanState;
/**
* No default constructor so only registered manually.
*/
PostLoad(boolean dummy) {
this.dummy = dummy;
}
@Override
public boolean isRegisterFor(Class<?> cls) {
return true;
}
@Override
public void postLoad(Object bean) {
this.methodsCalled.add("postLoad");
this.bean = bean;
this.beanState = Ebean.getBeanState(bean);
}
}
}
@@ -7,14 +7,14 @@ import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class TestLifecyleAnnotatedBean extends BaseTestCase {
public class TestLifecycleAnnotatedBean extends BaseTestCase {
@Test
public void shouldExecutePrePersistMethodsWhenSavingBean() {
EBasicWithLifecycle bean = new EBasicWithLifecycle();
bean.setName("PrePersist");
Ebean.getServerCacheManager();
Ebean.save(bean);
assertThat(bean.getBuffer()).contains("prePersist1");
@@ -23,10 +23,10 @@ public class TestLifecyleAnnotatedBean extends BaseTestCase {
@Test
public void shouldExecutePostPersistMethodsWhenSavingBean() {
EBasicWithLifecycle bean = new EBasicWithLifecycle();
bean.setName("PostPersist");
Ebean.getServerCacheManager();
Ebean.save(bean);
assertThat(bean.getBuffer()).contains("postPersist1");
@@ -35,10 +35,10 @@ public class TestLifecyleAnnotatedBean extends BaseTestCase {
@Test
public void shouldExecutePostLoadMethodsWhenFindingBean() {
EBasicWithLifecycle bean = new EBasicWithLifecycle();
bean.setName("PostLoad");
Ebean.getServerCacheManager();
Ebean.save(bean);
EBasicWithLifecycle loaded = Ebean.find(EBasicWithLifecycle.class, bean.getId());
@@ -48,10 +48,10 @@ public class TestLifecyleAnnotatedBean extends BaseTestCase {
@Test
public void shouldExecutePreUpdateMethodsWhenUpdatingBean() {
EBasicWithLifecycle bean = new EBasicWithLifecycle();
bean.setName("Persisted");
Ebean.getServerCacheManager();
Ebean.save(bean);
bean.setName("PreUpdate");
@@ -63,10 +63,10 @@ public class TestLifecyleAnnotatedBean extends BaseTestCase {
@Test
public void shouldExecutePostUpdateMethodsWhenUpdatingBean() {
EBasicWithLifecycle bean = new EBasicWithLifecycle();
bean.setName("Persisted");
Ebean.getServerCacheManager();
Ebean.save(bean);
bean.setName("PostUpdate");
@@ -78,10 +78,10 @@ public class TestLifecyleAnnotatedBean extends BaseTestCase {
@Test
public void shouldExecutePreRemoveMethodsWhenRemovingBean() {
EBasicWithLifecycle bean = new EBasicWithLifecycle();
bean.setName("Persisted");
Ebean.getServerCacheManager();
Ebean.save(bean);
Ebean.delete(bean);
@@ -91,10 +91,10 @@ public class TestLifecyleAnnotatedBean extends BaseTestCase {
@Test
public void shouldExecutePostRemoveMethodsWhenRemovingBean() {
EBasicWithLifecycle bean = new EBasicWithLifecycle();
bean.setName("Persisted");
Ebean.getServerCacheManager();
Ebean.save(bean);
Ebean.delete(bean);