mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#542 - ENH: @DbJson fields are auto serialized and deserialized to specified classes.
This commit is contained in:
@@ -3,13 +3,19 @@ package com.avaje.tests.json;
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.json.EBasicJsonList;
|
||||
import com.avaje.tests.model.json.PlainBean;
|
||||
import org.avaje.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class TestDbJson_List extends BaseTestCase {
|
||||
@@ -30,6 +36,22 @@ public class TestDbJson_List extends BaseTestCase {
|
||||
bean.getFlags().add(43L);
|
||||
bean.getFlags().add(44L);
|
||||
|
||||
bean.setPlainBean(new PlainBean("plain", 52));
|
||||
|
||||
List<PlainBean> beanList = new ArrayList<PlainBean>();
|
||||
beanList.add(new PlainBean("one", 1));
|
||||
beanList.add(new PlainBean("two", 2));
|
||||
bean.setBeanList(beanList);
|
||||
|
||||
|
||||
Set<PlainBean> beanSet = new LinkedHashSet<PlainBean>();
|
||||
beanSet.add(new PlainBean("A", 1));
|
||||
beanSet.add(new PlainBean("B", 2));
|
||||
bean.setBeanSet(beanSet);
|
||||
|
||||
bean.getBeanMap().put("key0", new PlainBean("k0", 90));
|
||||
bean.getBeanMap().put("key1", new PlainBean("k1", 91));
|
||||
|
||||
Ebean.save(bean);
|
||||
|
||||
found = Ebean.find(EBasicJsonList.class, bean.getId());
|
||||
@@ -38,11 +60,15 @@ public class TestDbJson_List extends BaseTestCase {
|
||||
assertTrue(found.getFlags().contains(42L));
|
||||
assertTrue(found.getFlags().contains(43L));
|
||||
assertTrue(found.getFlags().contains(44L));
|
||||
assertThat(found.getBeanList()).hasSize(2);
|
||||
assertThat(found.getBeanSet()).hasSize(2);
|
||||
assertThat(found.getBeanMap()).hasSize(2);
|
||||
|
||||
json_parse_format();
|
||||
update_when_notDirty();
|
||||
update_when_dirty();
|
||||
update_when_dirty_flags();
|
||||
update_when_dirty_SetListMap();
|
||||
}
|
||||
|
||||
//@Test//(dependsOnMethods = "insert")
|
||||
@@ -51,6 +77,10 @@ public class TestDbJson_List extends BaseTestCase {
|
||||
String asJson = Ebean.json().toJson(found);
|
||||
assertThat(asJson).contains("\"tags\":[\"one\",\"two\"]");
|
||||
assertThat(asJson).contains("\"flags\":[42,43,44]");
|
||||
assertThat(asJson).contains("\"plainBean\":{\"name\":\"plain\"");
|
||||
assertThat(asJson).contains("\"beanSet\":[");
|
||||
assertThat(asJson).contains("\"beanList\":[");
|
||||
assertThat(asJson).contains("\"beanMap\":{");
|
||||
assertThat(asJson).contains("\"id\":");
|
||||
|
||||
EBasicJsonList fromJson = Ebean.json().toBean(EBasicJsonList.class, asJson);
|
||||
@@ -62,6 +92,9 @@ public class TestDbJson_List extends BaseTestCase {
|
||||
assertTrue(fromJson.getFlags().contains(43L));
|
||||
assertTrue(fromJson.getFlags().contains(44L));
|
||||
|
||||
assertThat(fromJson.getBeanSet()).hasSize(2);
|
||||
assertThat(fromJson.getBeanList()).hasSize(2);
|
||||
assertThat(fromJson.getBeanMap()).hasSize(2);
|
||||
}
|
||||
|
||||
//@Test//(dependsOnMethods = "insert")
|
||||
@@ -73,7 +106,7 @@ public class TestDbJson_List extends BaseTestCase {
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
// we don't update the phone numbers (as they are not dirty)
|
||||
assertThat(sql.get(0)).contains("update ebasic_json_list set name=?, version=? where");
|
||||
assertThat(sql.get(0)).contains("update ebasic_json_list set name=?, plain_bean=?, version=? where");
|
||||
}
|
||||
|
||||
public void update_when_dirty() {
|
||||
@@ -86,7 +119,7 @@ public class TestDbJson_List extends BaseTestCase {
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
// we don't update the phone numbers (as they are not dirty)
|
||||
assertThat(sql.get(0)).contains("update ebasic_json_list set tags=?, version=? where");
|
||||
assertThat(sql.get(0)).contains("update ebasic_json_list set plain_bean=?, tags=?, version=? where id=? and version=?");
|
||||
}
|
||||
|
||||
public void update_when_dirty_flags() {
|
||||
@@ -99,7 +132,40 @@ public class TestDbJson_List extends BaseTestCase {
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
// we don't update the phone numbers (as they are not dirty)
|
||||
assertThat(sql.get(0)).contains("update ebasic_json_list set flags=?, version=? where");
|
||||
assertThat(sql.get(0)).contains("update ebasic_json_list set plain_bean=?, flags=?, version=? where id=? and version=?;");
|
||||
}
|
||||
|
||||
public void update_when_dirty_SetListMap() {
|
||||
|
||||
//found.setName("modAgain");
|
||||
found.getBeanSet().clear();
|
||||
found.getBeanList().clear();
|
||||
found.getBeanMap().remove("key0");
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
Ebean.save(found);
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
// we don't update the phone numbers (as they are not dirty)
|
||||
assertThat(sql.get(0)).contains("update ebasic_json_list set bean_set=?, bean_list=?, bean_map=?, plain_bean=?, version=? where id=? and version=?");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void insert_fetch_when_null() {
|
||||
|
||||
EBasicJsonList bean = new EBasicJsonList();
|
||||
bean.setName("leave some nulls");
|
||||
bean.setFlags(null);
|
||||
bean.setTags(null);
|
||||
bean.setBeanMap(null);
|
||||
|
||||
Ebean.save(bean);
|
||||
|
||||
EBasicJsonList found = Ebean.find(EBasicJsonList.class, bean.getId());
|
||||
|
||||
assertNull(found.getPlainBean());
|
||||
|
||||
String asJson = Ebean.json().toJson(found);
|
||||
assertNotNull(asJson);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,8 +7,10 @@ import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Version;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@Entity
|
||||
@@ -19,6 +21,18 @@ public class EBasicJsonList {
|
||||
|
||||
String name;
|
||||
|
||||
@DbJson(length = 700)
|
||||
Set<PlainBean> beanSet;
|
||||
|
||||
@DbJson(length = 700)
|
||||
List<PlainBean> beanList;
|
||||
|
||||
@DbJson(length = 700)
|
||||
Map<String,PlainBean> beanMap = new LinkedHashMap<String, PlainBean>();
|
||||
|
||||
@DbJson(length = 500)
|
||||
PlainBean plainBean;
|
||||
|
||||
@DbJson(length = 50)
|
||||
Set<Long> flags = new LinkedHashSet<Long>();
|
||||
|
||||
@@ -60,6 +74,38 @@ public class EBasicJsonList {
|
||||
this.flags = flags;
|
||||
}
|
||||
|
||||
public PlainBean getPlainBean() {
|
||||
return plainBean;
|
||||
}
|
||||
|
||||
public void setPlainBean(PlainBean plainBean) {
|
||||
this.plainBean = plainBean;
|
||||
}
|
||||
|
||||
public Set<PlainBean> getBeanSet() {
|
||||
return beanSet;
|
||||
}
|
||||
|
||||
public void setBeanSet(Set<PlainBean> beanSet) {
|
||||
this.beanSet = beanSet;
|
||||
}
|
||||
|
||||
public List<PlainBean> getBeanList() {
|
||||
return beanList;
|
||||
}
|
||||
|
||||
public void setBeanList(List<PlainBean> beanList) {
|
||||
this.beanList = beanList;
|
||||
}
|
||||
|
||||
public Map<String, PlainBean> getBeanMap() {
|
||||
return beanMap;
|
||||
}
|
||||
|
||||
public void setBeanMap(Map<String, PlainBean> beanMap) {
|
||||
this.beanMap = beanMap;
|
||||
}
|
||||
|
||||
public Long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.avaje.tests.model.json;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
|
||||
/**
|
||||
* Something for Jackson ObjectMapper to marshall.
|
||||
*/
|
||||
public class PlainBean {
|
||||
|
||||
String name;
|
||||
|
||||
long along;
|
||||
|
||||
Timestamp timestamp;
|
||||
|
||||
public PlainBean(String name, long along) {
|
||||
this.name = name;
|
||||
this.along = along;
|
||||
this.timestamp = new Timestamp(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
/**
|
||||
* A constructor for Jackson.
|
||||
*/
|
||||
public PlainBean() {
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user