Update TestDbJson_Jackson3

This commit is contained in:
rbygrave
2021-07-30 15:34:54 +12:00
parent 822fb7325a
commit c43e2e6956
2 changed files with 80 additions and 3 deletions
@@ -164,4 +164,66 @@ public class TestDbJson_Jackson3 extends BaseTestCase {
BeanState state = DB.getBeanState(found);
assertThat(state.getChangedProps()).containsExactlyInAnyOrder("beanList");
}
@Test
public void update_with_differentDbJsonSettings() {
PlainBeanDirtyAware contentBean1 = new PlainBeanDirtyAware("x", 42);
PlainBeanDirtyAware contentBean2 = new PlainBeanDirtyAware("y", 43);
PlainBeanDirtyAware contentBean3 = new PlainBeanDirtyAware("z", 44);
EBasicJsonJackson3 bean = new EBasicJsonJackson3();
bean.setName("b1");
bean.setPlainValue(contentBean1);
bean.setPlainValue2(contentBean2);
bean.setPlainValue3(contentBean3);
BeanState state = DB.getBeanState(bean);
// a new bean is not considered as dirty (thus have no changed props)
assertThat(state.isDirty()).isFalse();
assertThat(state.isNewOrDirty()).isTrue();
assertThat(state.getChangedProps()).isEmpty();
bean.save();
bean = DB.find(EBasicJsonJackson3.class, bean.getId());
state = DB.getBeanState(bean);
// a fresh loaded bean is also not considered as dirty
assertThat(state.isDirty()).isFalse();
assertThat(state.isNewOrDirty()).isFalse();
assertThat(state.getChangedProps()).isEmpty();
bean.getPlainValue().setName("a"); // has keepSource=true
assertThat(state.isDirty()).isTrue();
assertThat(state.getChangedProps()).containsExactly("plainValue");
bean.getPlainValue2().setName("b");
assertThat(state.getChangedProps()).containsExactlyInAnyOrder("plainValue", "plainValue2");
bean.getPlainValue3().setName("c"); // has dirtyDetection = false
Map<String, ValuePair> dirtyValues = state.getDirtyValues();
assertThat(dirtyValues).hasSize(2).containsKeys("plainValue", "plainValue2");
assertThat(dirtyValues.get("plainValue")).hasToString("name:a,name:x"); // SOURCE -> origValue present
assertThat(dirtyValues.get("plainValue2")).hasToString("name:b,null"); // without SOURCE no origValue present
LoggedSql.start();
bean.save();
List<String> sql = LoggedSql.collect();
assertThat(sql.get(0)).contains("update ebasic_json_jackson3 set plain_value=?, plain_value2=?, version=? where id=?");
bean = DB.find(EBasicJsonJackson3.class, bean.getId());
LoggedSql.collect(); // ignore the select
assertThat(bean.getPlainValue().getName()).isEqualTo("a");
assertThat(bean.getPlainValue2().getName()).isEqualTo("b");
assertThat(bean.getPlainValue3().getName()).isEqualTo("z"); // value is not updated
bean.getPlainValue3().setName("c");
bean.getPlainValue3().setMarkedDirty(true); // This is ignored because it is MutationDetection.NONE
bean.save();
// no update as plainValue3 has MutationDetection.NONE (ModifyAwareType = NONE isn't an expected combination to me)
assertThat(LoggedSql.collect()).isEmpty();
LoggedSql.stop();
}
}
@@ -2,11 +2,15 @@ package org.tests.model.json;
import io.ebean.Model;
import io.ebean.annotation.DbJson;
import io.ebean.annotation.MutationDetection;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Version;
import static io.ebean.annotation.MutationDetection.NONE;
import static io.ebean.annotation.MutationDetection.SOURCE;
@Entity
public class EBasicJsonJackson3 extends Model {
@@ -15,12 +19,15 @@ public class EBasicJsonJackson3 extends Model {
String name;
@DbJson(length = 500)
@DbJson(length = 500, mutationDetection = SOURCE)
PlainBeanDirtyAware plainValue;
@DbJson(length = 500)
PlainBeanDirtyAware plainValue2;
@DbJson(length = 500, mutationDetection = NONE)
PlainBeanDirtyAware plainValue3;
@Version
long version;
@@ -55,7 +62,15 @@ public class EBasicJsonJackson3 extends Model {
public void setPlainValue2(PlainBeanDirtyAware plainValue2) {
this.plainValue2 = plainValue2;
}
public PlainBeanDirtyAware getPlainValue3() {
return plainValue3;
}
public void setPlainValue3(PlainBeanDirtyAware plainValue3) {
this.plainValue3 = plainValue3;
}
public long getVersion() {
return version;
}