use of com.ctrip.platform.dal.dao.task.BatchUpdateTask in project dal by ctripcorp.
the class BatchUpdateTaskTestStub method testUpdateUnchangedField.
@Test
public void testUpdateUnchangedField() throws SQLException {
BatchUpdateTask<UpdatableClientTestModel> test = new BatchUpdateTask<>();
test.initialize(getParser(UpdatableClientTestModel.class));
DalHints hints = new DalHints();
try {
List<UpdatableClientTestModel> pojos = getAll(UpdatableClientTestModel.class);
for (UpdatableClientTestModel model : pojos) {
model.setType((Short) null);
model.setQuantity(-100);
}
List<ClientTestModel> pojos2 = getAll();
for (ClientTestModel model : pojos2) {
model.setTableIndex(-1);
model.setDbIndex(-2);
}
// Update data
int[] result = getDao().batchUpdate(new DalHints(), pojos2);
result = execute(test, hints.updateUnchangedField(), pojos);
pojos = getAll(UpdatableClientTestModel.class);
// Check if still old value in UpdatabvleEntity
int i = 0;
for (UpdatableClientTestModel model : pojos) {
assertNull(model.getType());
assertEquals(-100, model.getQuantity().intValue());
assertEquals(i++, model.getTableIndex().intValue());
assertEquals(0, model.getDbIndex().intValue());
}
} catch (SQLException e) {
e.printStackTrace();
fail();
}
}
use of com.ctrip.platform.dal.dao.task.BatchUpdateTask in project dal by ctripcorp.
the class BatchUpdateTaskTestStub method testIgnorUnchangedField.
@Test
public void testIgnorUnchangedField() throws SQLException {
BatchUpdateTask<UpdatableClientTestModel> test = new BatchUpdateTask<>();
test.initialize(getParser(UpdatableClientTestModel.class));
DalHints hints = new DalHints();
try {
List<UpdatableClientTestModel> pojos = getAll(UpdatableClientTestModel.class);
for (UpdatableClientTestModel model : pojos) model.setAddress("1122334455");
List<ClientTestModel> pojos2 = getAll();
for (ClientTestModel model : pojos2) {
model.setTableIndex(-1);
model.setDbIndex(-2);
model.setType((short) -1);
model.setQuantity(-100);
}
// Change existing data to other value, so that we can compare after the first pojos get updated
int[] result = getDao().batchUpdate(new DalHints(), pojos2);
// Update with UpdatableEntity with only changed values
result = execute(test, hints, pojos);
pojos = getAll(UpdatableClientTestModel.class);
for (UpdatableClientTestModel model : pojos) {
assertEquals("1122334455", model.getAddress());
assertEquals(-100, model.getQuantity().intValue());
assertEquals(-1, model.getType().shortValue());
assertEquals(-2, model.getDbIndex().intValue());
assertEquals(-1, model.getTableIndex().intValue());
assertNotNull(model.getLastChanged());
}
} catch (SQLException e) {
e.printStackTrace();
fail();
}
}
use of com.ctrip.platform.dal.dao.task.BatchUpdateTask in project dal by ctripcorp.
the class BatchUpdateTaskTestStub method testUpdateIfChangedField.
@Test
public void testUpdateIfChangedField() throws SQLException {
BatchUpdateTask<UpdatableClientTestModel> test = new BatchUpdateTask<>();
test.initialize(getParser(UpdatableClientTestModel.class));
DalHints hints = new DalHints();
try {
List<UpdatableClientTestModel> pojos = getAll(UpdatableClientTestModel.class);
int i = 0;
for (UpdatableClientTestModel model : pojos) {
model.setType((Short) null);
if (i % 2 == 0)
model.setQuantity(-100);
if (i % 2 == 1)
model.setDbIndex(-200);
if (i % 2 == 1)
model.setTableIndex(-300);
i++;
}
int[] result = execute(test, hints, pojos);
assertArrayEquals(new int[] { 1, 1, 1 }, result);
i = 0;
pojos = getAll(UpdatableClientTestModel.class);
for (UpdatableClientTestModel model : pojos) {
assertNull(model.getType());
assertNotNull(model.getLastChanged());
if (i % 2 == 0)
assertEquals(model.getQuantity().intValue(), -100);
else
assertEquals(model.getQuantity().intValue(), 10 + i);
if (i % 2 == 1)
assertEquals(model.getDbIndex().intValue(), -200);
else
assertEquals(model.getDbIndex().intValue(), 0);
if (i % 2 == 1)
assertEquals(model.getTableIndex().intValue(), -300);
else
assertEquals(model.getTableIndex().intValue(), i);
i++;
}
} catch (SQLException e) {
e.printStackTrace();
fail();
}
}
use of com.ctrip.platform.dal.dao.task.BatchUpdateTask in project dal by ctripcorp.
the class BatchUpdateTaskTestStub method testExecuteUpdatableEntity.
@Test
public void testExecuteUpdatableEntity() throws SQLException {
BatchUpdateTask<UpdatableClientTestModel> test = new BatchUpdateTask<>();
test.initialize(getParser(UpdatableClientTestModel.class));
DalHints hints = new DalHints();
try {
List<UpdatableClientTestModel> pojos = getAll(UpdatableClientTestModel.class);
for (UpdatableClientTestModel model : pojos) model.setAddress("1122334455");
int[] result = execute(test, hints, pojos);
assertEquals(3, result.length);
assertArrayEquals(new int[] { 1, 1, 1 }, result);
assertEquals(3, getCount());
pojos = getAll(UpdatableClientTestModel.class);
for (UpdatableClientTestModel model : pojos) assertEquals("1122334455", model.getAddress());
} catch (SQLException e) {
e.printStackTrace();
fail();
}
}
use of com.ctrip.platform.dal.dao.task.BatchUpdateTask in project dal by ctripcorp.
the class BatchUpdateTaskTestStub method testNotUpdatableVersion.
@Test
public void testNotUpdatableVersion() throws SQLException {
BatchUpdateTask<NonUpdatableVersionModel> test = new BatchUpdateTask<>();
DalParser<NonUpdatableVersionModel> parser = new DalDefaultJpaParser<>(NonUpdatableVersionModel.class, getDbName());
test.initialize(parser);
DalTableDao<NonUpdatableVersionModel> dao = new DalTableDao<>(parser);
DalHints hints = new DalHints();
List<NonUpdatableVersionModel> pojos = dao.query("1=1", new StatementParameters(), new DalHints());
for (NonUpdatableVersionModel model : pojos) {
model.setAddress("1122334455");
}
int[] result = execute(test, hints, pojos);
assertArrayEquals(new int[] { 1, 1, 1 }, result);
pojos = dao.query("1=1", new StatementParameters(), new DalHints());
for (NonUpdatableVersionModel model : pojos) assertEquals("1122334455", model.getAddress());
}
Aggregations