Merge pull request #2107 from ebean-orm/feature/ModifyAwareType

Refactor io.ebeaninternal.json.ModifyAwareOwner to io.ebean.ModifyAwareType
This commit is contained in:
Rob Bygrave
2020-11-24 22:05:04 +13:00
committed by GitHub
19 changed files with 314 additions and 90 deletions
@@ -2,7 +2,7 @@ package io.ebean.json;
import io.ebean.text.json.EJson;
import io.ebeaninternal.json.ModifyAwareMap;
import io.ebeaninternal.json.ModifyAwareOwner;
import io.ebean.ModifyAwareType;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import org.junit.Test;
@@ -293,7 +293,7 @@ public class EJsonTests {
Set set = EJson.parseSet(jsonInput, true);
ModifyAwareOwner modAware = (ModifyAwareOwner) set;
ModifyAwareType modAware = (ModifyAwareType) set;
assertFalse(modAware.isMarkedDirty());
Iterator iterator = set.iterator();
@@ -59,7 +59,7 @@ public class ModifyAwareMapTest {
ModifyAwareMap<String, String> map = createMap();
assertFalse(map.isMarkedDirty());
map.markAsModified();
map.setMarkedDirty(true);
Assert.assertTrue(map.isMarkedDirty());
}
@@ -20,7 +20,7 @@ public class ModifyAwareFlagTest {
ObjectOutputStream oos = new ObjectOutputStream(os);
ModifyAwareFlag flag = new ModifyAwareFlag();
flag.markAsModified();
flag.setMarkedDirty(true);
oos.writeObject(flag);
oos.flush();
oos.close();
@@ -0,0 +1,76 @@
package org.tests.json;
import io.ebean.BaseTestCase;
import io.ebean.DB;
import io.ebeantest.LoggedSql;
import org.junit.Test;
import org.tests.model.json.EBasicJsonJackson3;
import org.tests.model.json.EBasicJsonList;
import org.tests.model.json.PlainBean;
import org.tests.model.json.PlainBeanDirtyAware;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class TestDbJson_Jackson3 extends BaseTestCase {
@Test
public void updateIncludesJsonColumn_when_explicit_isMarkedDirty() {
PlainBeanDirtyAware contentBean = new PlainBeanDirtyAware("a", 42);
EBasicJsonJackson3 bean = new EBasicJsonJackson3();
bean.setName("b1");
bean.setPlainValue(contentBean);
bean.save();
final EBasicJsonJackson3 found = DB.find(EBasicJsonJackson3.class, bean.getId());
found.setName("b1-mod");
LoggedSql.start();
found.save();
List<String> sql = LoggedSql.collect();
assertThat(sql).hasSize(1);
assertThat(sql.get(0)).contains("update ebasic_json_jackson3 set name=?, version=? where id=? and version=?");
found.setName("b1-mod2");
found.getPlainValue().setName("b");
found.getPlainValue().setMarkedDirty(true);
found.save();
sql = LoggedSql.stop();
assertThat(sql).hasSize(1);
assertThat(sql.get(0)).contains("update ebasic_json_jackson3 set name=?, plain_value=?, version=? where id=? and version=?");
final EBasicJsonJackson3 found2 = DB.find(EBasicJsonJackson3.class, bean.getId());
assertThat(found2.getName()).isEqualTo("b1-mod2");
assertThat(found2.getPlainValue().getName()).isEqualTo("b");
}
@Test
public void updateIncludesJsonColumn_when_loadedAndNotDirtyAware() {
PlainBean contentBean = new PlainBean("a", 42);
EBasicJsonList bean = new EBasicJsonList();
bean.setName("p1");
bean.setPlainBean(contentBean);
DB.save(bean);
final EBasicJsonList found = DB.find(EBasicJsonList.class, bean.getId());
// json bean not modified but not aware
// ideally don't load the json content if we are not going to modify it
found.setName("p1-mod");
LoggedSql.start();
DB.save(found);
final List<String> sql = LoggedSql.stop();
assertThat(sql).hasSize(1);
assertThat(sql.get(0)).contains("update ebasic_json_list set name=?, bean_set=?, bean_list=?, plain_bean=?, version=? where id=?");
}
}
@@ -0,0 +1,55 @@
package org.tests.model.json;
import io.ebean.Model;
import io.ebean.annotation.DbJson;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Version;
@Entity
public class EBasicJsonJackson3 extends Model {
@Id
Long id;
String name;
@DbJson(length = 500)
PlainBeanDirtyAware plainValue;
@Version
long version;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public PlainBeanDirtyAware getPlainValue() {
return plainValue;
}
public void setPlainValue(PlainBeanDirtyAware plainValue) {
this.plainValue = plainValue;
}
public long getVersion() {
return version;
}
public void setVersion(long version) {
this.version = version;
}
}
@@ -0,0 +1,71 @@
package org.tests.model.json;
import io.ebean.ModifyAwareType;
import java.sql.Timestamp;
/**
* Something for Jackson ObjectMapper to marshall that is ModifyAwareType.
*/
public class PlainBeanDirtyAware implements ModifyAwareType {
private transient boolean markedDirty;
String name;
long along;
Timestamp timestamp;
public PlainBeanDirtyAware(String name, long along) {
this.name = name;
this.along = along;
this.timestamp = new Timestamp(System.currentTimeMillis());
}
/**
* A constructor for Jackson.
*/
public PlainBeanDirtyAware() {
}
// @JsonIgnore
@Override
public boolean isMarkedDirty() {
return markedDirty;
}
@Override
public void setMarkedDirty(boolean markedDirty) {
this.markedDirty = markedDirty;
}
@Override
public String toString() {
return "name:" + name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getAlong() {
return along;
}
public void setAlong(long along) {
this.along = along;
}
public Timestamp getTimestamp() {
return timestamp;
}
public void setTimestamp(Timestamp timestamp) {
this.timestamp = timestamp;
}
}