Added more TCs to DBBean and fixed a link table bug
This commit is contained in:
parent
0346fd21ba
commit
7c4c536ce5
4 changed files with 229 additions and 14 deletions
|
|
@ -267,7 +267,7 @@ public abstract class DBBean {
|
|||
sql = "UPDATE " + linkTable + " SET " + idCol + "=? WHERE " + subIdCol + "=?";
|
||||
else
|
||||
sql = "INSERT INTO " + linkTable + " (" + idCol + ", " + subIdCol + ") SELECT ?,? " +
|
||||
"WHERE NOT EXISTS(SELECT 1 FROM " + linkTable + " WHERE " + idCol + "=? AND " + idCol + "=?);";
|
||||
"WHERE NOT EXISTS(SELECT 1 FROM " + linkTable + " WHERE " + idCol + "=? AND " + subIdCol + "=?);";
|
||||
logger.finest("Save sub Bean(" + c.getName() + ", id: " + subObj.getId() + ") query: " + sql);
|
||||
PreparedStatement subStmt = db.getPreparedStatement(sql);
|
||||
subStmt.setLong(1, this.id);
|
||||
|
|
|
|||
|
|
@ -51,4 +51,111 @@ public class DBBeanLoadTest {
|
|||
|
||||
assertSame(obj1, obj2);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@Test
|
||||
public void aliasFieldsLoad() throws SQLException {
|
||||
aliasFieldsInit(db);
|
||||
insert(db, "aliasTable", "5", "1234", "\"helloworld\"");
|
||||
AliasFieldsTestClass obj = DBBean.load(db, AliasFieldsTestClass.class, 5);
|
||||
|
||||
assertEquals((Long)5L, obj.getId());
|
||||
assertEquals(1234, obj.intField);
|
||||
assertEquals("helloworld", obj.strField);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@Test
|
||||
public void emptySubObjectLoad() throws SQLException {
|
||||
subObjectInit(db);
|
||||
insert(db, "parent", "5");
|
||||
ParentTestClass obj = DBBean.load(db, ParentTestClass.class, 5);
|
||||
|
||||
assertEquals((Long)5L, obj.getId());
|
||||
assertEquals(0, obj.subobjs.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void subObjectLoad() throws SQLException {
|
||||
subObjectInit(db);
|
||||
insert(db, "parent", "5");
|
||||
insert(db, "subobject", "10", "5", "1234");
|
||||
ParentTestClass obj = DBBean.load(db, ParentTestClass.class, 5);
|
||||
|
||||
assertEquals(1, obj.subobjs.size());
|
||||
assertEquals((Long)10L, obj.subobjs.get(0).getId());
|
||||
assertEquals(1234, obj.subobjs.get(0).intField);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multiSubObjectLoad() throws SQLException {
|
||||
subObjectInit(db);
|
||||
insert(db, "parent", "5");
|
||||
insert(db, "subobject", "10", "5", "1001");
|
||||
insert(db, "subobject", "11", "5", "1002");
|
||||
insert(db, "subobject", "12", "5", "1003");
|
||||
insert(db, "subobject", "13", "5", "1004");
|
||||
ParentTestClass obj = DBBean.load(db, ParentTestClass.class, 5);
|
||||
|
||||
assertEquals(4, obj.subobjs.size());
|
||||
assertEquals((Long)10L, obj.subobjs.get(0).getId());
|
||||
assertEquals(1001, obj.subobjs.get(0).intField);
|
||||
assertEquals((Long)11L, obj.subobjs.get(1).getId());
|
||||
assertEquals(1002, obj.subobjs.get(1).intField);
|
||||
assertEquals((Long)12L, obj.subobjs.get(2).getId());
|
||||
assertEquals(1003, obj.subobjs.get(2).intField);
|
||||
assertEquals((Long)13L, obj.subobjs.get(3).getId());
|
||||
assertEquals(1004, obj.subobjs.get(3).intField);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@Test
|
||||
public void emptySubLinkObjectLoad() throws SQLException {
|
||||
subLinkObjectInit(db);
|
||||
insert(db, "parent", "5");
|
||||
ParentLinkTestClass obj = DBBean.load(db, ParentLinkTestClass.class, 5);
|
||||
|
||||
assertEquals((Long)5L, obj.getId());
|
||||
assertEquals(0, obj.subobjs.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void subLinkObjectLoad() throws SQLException {
|
||||
subLinkObjectInit(db);
|
||||
insert(db, "parent", "5");
|
||||
insert(db, "link", "5", "10");
|
||||
insert(db, "subobject", "10", "1234");
|
||||
ParentLinkTestClass obj = DBBean.load(db, ParentLinkTestClass.class, 5);
|
||||
|
||||
assertEquals(1, obj.subobjs.size());
|
||||
assertEquals((Long)10L, obj.subobjs.get(0).getId());
|
||||
assertEquals(1234, obj.subobjs.get(0).intField);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multiSubLinkObjectLoad() throws SQLException {
|
||||
subLinkObjectInit(db);
|
||||
insert(db, "parent", "5");
|
||||
insert(db, "link", "5", "10");
|
||||
insert(db, "link", "5", "11");
|
||||
insert(db, "link", "5", "12");
|
||||
insert(db, "subobject", "10", "1001");
|
||||
insert(db, "subobject", "11", "1002");
|
||||
insert(db, "subobject", "12", "1003");
|
||||
ParentLinkTestClass obj = DBBean.load(db, ParentLinkTestClass.class, 5);
|
||||
|
||||
assertEquals(3, obj.subobjs.size());
|
||||
assertEquals((Long)10L, obj.subobjs.get(0).getId());
|
||||
assertEquals(1001, obj.subobjs.get(0).intField);
|
||||
assertEquals((Long)11L, obj.subobjs.get(1).getId());
|
||||
assertEquals(1002, obj.subobjs.get(1).intField);
|
||||
assertEquals((Long)12L, obj.subobjs.get(2).getId());
|
||||
assertEquals(1003, obj.subobjs.get(2).intField);
|
||||
}
|
||||
}
|
||||
|
|
@ -86,6 +86,21 @@ public class DBBeanSaveTest {
|
|||
////////////////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@Test
|
||||
public void emptySubObjectCreate() throws SQLException {
|
||||
subObjectInit(db);
|
||||
ParentTestClass obj = new ParentTestClass();
|
||||
obj.subobjs = null;
|
||||
obj.save(db);
|
||||
|
||||
assertEquals(0, getRowCount(db, "subobject"));
|
||||
|
||||
obj = new ParentTestClass();
|
||||
obj.save(db);
|
||||
|
||||
assertEquals(0, getRowCount(db, "subobject"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void subObjectCreate() throws SQLException {
|
||||
ParentTestClass obj = subObjectInit(db);
|
||||
|
|
@ -111,6 +126,45 @@ public class DBBeanSaveTest {
|
|||
getColumnValue(db, "subobject", "parent_id"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multiSubObjectCreate() throws SQLException {
|
||||
ParentTestClass obj = subObjectInit(db);
|
||||
obj.subobjs.add(new SubObjectTestClass());
|
||||
obj.subobjs.add(new SubObjectTestClass());
|
||||
obj.subobjs.get(0).intField = 1001;
|
||||
obj.subobjs.get(1).intField = 1002;
|
||||
obj.subobjs.get(2).intField = 1003;
|
||||
obj.save(db);
|
||||
|
||||
assertEquals(3, getRowCount(db, "subobject"));
|
||||
assertEquals(1, getColumnValue(db, "subobject", "1", "parent_id"));
|
||||
assertEquals(1001, getColumnValue(db, "subobject", "1", "intField"));
|
||||
assertEquals(1, getColumnValue(db, "subobject", "2", "parent_id"));
|
||||
assertEquals(1002, getColumnValue(db, "subobject", "2", "intField"));
|
||||
assertEquals(1, getColumnValue(db, "subobject", "3", "parent_id"));
|
||||
assertEquals(1003, getColumnValue(db, "subobject", "3", "intField"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multiSubObjectUpdate() throws SQLException {
|
||||
ParentTestClass obj = subObjectInit(db);
|
||||
obj.subobjs.add(new SubObjectTestClass());
|
||||
obj.subobjs.add(new SubObjectTestClass());
|
||||
obj.save(db);
|
||||
obj.subobjs.get(0).intField = 1001;
|
||||
obj.subobjs.get(1).intField = 1002;
|
||||
obj.subobjs.get(2).intField = 1003;
|
||||
obj.save(db);
|
||||
|
||||
assertEquals(3, getRowCount(db, "subobject"));
|
||||
assertEquals(1, getColumnValue(db, "subobject", "1", "parent_id"));
|
||||
assertEquals(1001, getColumnValue(db, "subobject", "1", "intField"));
|
||||
assertEquals(1, getColumnValue(db, "subobject", "2", "parent_id"));
|
||||
assertEquals(1002, getColumnValue(db, "subobject", "2", "intField"));
|
||||
assertEquals(1, getColumnValue(db, "subobject", "3", "parent_id"));
|
||||
assertEquals(1003, getColumnValue(db, "subobject", "3", "intField"));
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
|
@ -144,4 +198,44 @@ public class DBBeanSaveTest {
|
|||
getColumnValue(db, "subobject", "intField"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multiSubLinkObjectCreate() throws SQLException {
|
||||
ParentLinkTestClass obj = subLinkObjectInit(db);
|
||||
obj.subobjs.add(new SubObjectTestClass());
|
||||
obj.subobjs.add(new SubObjectTestClass());
|
||||
obj.subobjs.get(0).intField = 1001;
|
||||
obj.subobjs.get(1).intField = 1002;
|
||||
obj.subobjs.get(2).intField = 1003;
|
||||
obj.save(db);
|
||||
|
||||
assertEquals(3, getRowCount(db, "link"));
|
||||
assertEquals(1, getColumnValue(db, "link", "1", "parent_id"));
|
||||
assertEquals(1, getColumnValue(db, "link", "2", "parent_id"));
|
||||
assertEquals(1, getColumnValue(db, "link", "3", "parent_id"));
|
||||
assertEquals(3, getRowCount(db, "subobject"));
|
||||
assertEquals(1001, getColumnValue(db, "subobject", "1", "intField"));
|
||||
assertEquals(1002, getColumnValue(db, "subobject", "2", "intField"));
|
||||
assertEquals(1003, getColumnValue(db, "subobject", "3", "intField"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multiSubLinkObjectUpdate() throws SQLException {
|
||||
ParentLinkTestClass obj = subLinkObjectInit(db);
|
||||
obj.subobjs.add(new SubObjectTestClass());
|
||||
obj.subobjs.add(new SubObjectTestClass());
|
||||
obj.save(db);
|
||||
obj.subobjs.get(0).intField = 1001;
|
||||
obj.subobjs.get(1).intField = 1002;
|
||||
obj.subobjs.get(2).intField = 1003;
|
||||
obj.save(db);
|
||||
|
||||
assertEquals(3, getRowCount(db, "link"));
|
||||
assertEquals(1, getColumnValue(db, "link", "1", "parent_id"));
|
||||
assertEquals(1, getColumnValue(db, "link", "2", "parent_id"));
|
||||
assertEquals(1, getColumnValue(db, "link", "3", "parent_id"));
|
||||
assertEquals(3, getRowCount(db, "subobject"));
|
||||
assertEquals(1001, getColumnValue(db, "subobject", "1", "intField"));
|
||||
assertEquals(1002, getColumnValue(db, "subobject", "2", "intField"));
|
||||
assertEquals(1003, getColumnValue(db, "subobject", "3", "intField"));
|
||||
}
|
||||
}
|
||||
|
|
@ -2,17 +2,20 @@ package zutil.db.bean;
|
|||
|
||||
import zutil.StringUtil;
|
||||
import zutil.db.DBConnection;
|
||||
import zutil.db.SQLResultHandler;
|
||||
import zutil.db.handler.SimpleSQLResult;
|
||||
import zutil.log.LogUtil;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class DBBeanTestBase {
|
||||
private static final Logger logger = LogUtil.getLogger();
|
||||
|
||||
|
||||
public static class SimpleTestClass extends DBBean{
|
||||
|
|
@ -21,7 +24,7 @@ public class DBBeanTestBase {
|
|||
}
|
||||
|
||||
public static SimpleTestClass simpleClassInit(DBConnection db) throws SQLException {
|
||||
db.exec("CREATE TABLE SimpleTestClass (" +
|
||||
execSql(db,"CREATE TABLE SimpleTestClass (" +
|
||||
"id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " +
|
||||
"intField INTEGER, " +
|
||||
"strField TEXT);");
|
||||
|
|
@ -44,7 +47,7 @@ public class DBBeanTestBase {
|
|||
}
|
||||
|
||||
public static AliasFieldsTestClass aliasFieldsInit(DBConnection db) throws SQLException {
|
||||
db.exec("CREATE TABLE aliasTable (" +
|
||||
execSql(db,"CREATE TABLE aliasTable (" +
|
||||
"id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " +
|
||||
"aliasIntField INTEGER, " +
|
||||
"aliasStrField TEXT);");
|
||||
|
|
@ -69,9 +72,9 @@ public class DBBeanTestBase {
|
|||
}
|
||||
|
||||
public static ParentTestClass subObjectInit(DBConnection db) throws SQLException {
|
||||
db.exec("CREATE TABLE parent (" +
|
||||
execSql(db,"CREATE TABLE parent (" +
|
||||
"id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT);");
|
||||
db.exec("CREATE TABLE subobject (" +
|
||||
execSql(db,"CREATE TABLE subobject (" +
|
||||
"id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " +
|
||||
"parent_id INTEGER, " +
|
||||
"intField INTEGER);");
|
||||
|
|
@ -93,14 +96,13 @@ public class DBBeanTestBase {
|
|||
}
|
||||
|
||||
public static ParentLinkTestClass subLinkObjectInit(DBConnection db) throws SQLException {
|
||||
db.exec("CREATE TABLE parent (" +
|
||||
execSql(db,"CREATE TABLE parent (" +
|
||||
"id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT);");
|
||||
db.exec("CREATE TABLE link (" +
|
||||
execSql(db,"CREATE TABLE link (" +
|
||||
"parent_id INTEGER, " +
|
||||
"id INTEGER);");
|
||||
db.exec("CREATE TABLE subobject (" +
|
||||
execSql(db,"CREATE TABLE subobject (" +
|
||||
"id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " +
|
||||
"parent_id INTEGER, " +
|
||||
"intField INTEGER);");
|
||||
|
||||
ParentLinkTestClass obj = new ParentLinkTestClass();
|
||||
|
|
@ -114,15 +116,27 @@ public class DBBeanTestBase {
|
|||
////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
public static Object getColumnValue(DBConnection db, String table, String column) throws SQLException {
|
||||
return db.exec("SELECT "+column+" FROM "+table, new SimpleSQLResult<>());
|
||||
return execSql(db, "SELECT "+column+" FROM "+table, new SimpleSQLResult<>());
|
||||
}
|
||||
public static Object getColumnValue(DBConnection db, String table, String id, String column) throws SQLException {
|
||||
return execSql(db, "SELECT "+column+" FROM "+table+" WHERE id="+id, new SimpleSQLResult<>());
|
||||
}
|
||||
|
||||
public static Object getRowCount(DBConnection db, String table) throws SQLException {
|
||||
return db.exec("SELECT count(*) FROM "+table, new SimpleSQLResult<>());
|
||||
return execSql(db, "SELECT count(*) FROM "+table, new SimpleSQLResult<>());
|
||||
}
|
||||
|
||||
public static void insert(DBConnection db, String table, String... values) throws SQLException {
|
||||
db.exec("INSERT INTO "+table+" VALUES("+
|
||||
StringUtil.join(",", values)+");");
|
||||
execSql(db, "INSERT INTO "+table+" VALUES("+StringUtil.join(",", values)+");");
|
||||
}
|
||||
|
||||
private static void execSql(DBConnection db, String sql) throws SQLException {
|
||||
logger.info("SQL: "+sql);
|
||||
db.exec(sql);
|
||||
}
|
||||
|
||||
private static Object execSql(DBConnection db, String sql, SQLResultHandler handler) throws SQLException {
|
||||
logger.info("SQL RET: "+sql);
|
||||
return db.exec(sql, handler);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue