Search in sources :

Example 6 with BatchUpdateTask

use of com.ctrip.platform.dal.dao.task.BatchUpdateTask in project dal by ctripcorp.

the class BatchUpdateTaskTestStub method testExecute.

@Test
public void testExecute() {
    BatchUpdateTask<ClientTestModel> test = new BatchUpdateTask<>();
    test.initialize(getParser());
    DalHints hints = new DalHints();
    try {
        List<ClientTestModel> pojos = getAll();
        for (ClientTestModel 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();
        for (ClientTestModel model : pojos) assertEquals("1122334455", model.getAddress());
    } catch (SQLException e) {
        e.printStackTrace();
        fail();
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) SQLException(java.sql.SQLException) BatchUpdateTask(com.ctrip.platform.dal.dao.task.BatchUpdateTask) Test(org.junit.Test)

Example 7 with BatchUpdateTask

use of com.ctrip.platform.dal.dao.task.BatchUpdateTask in project dal by ctripcorp.

the class BatchUpdateTaskTestStub method testValidteNullField.

@Test
public void testValidteNullField() {
    BatchUpdateTask<ClientTestModel> test = new BatchUpdateTask<>();
    test.initialize(getParser());
    DalHints hints = new DalHints();
    try {
        List<ClientTestModel> pojos = getAll();
        for (ClientTestModel model : pojos) {
            model.setAddress(null);
            model.setType(null);
            model.setQuantity(null);
            model.setDbIndex(null);
            model.setLastChanged(null);
            model.setTableIndex(null);
        }
        int[] result = execute(test, hints, pojos);
        fail();
    } catch (SQLException e) {
        assertEquals(e.getMessage(), ErrorCode.ValidateFieldCount.getMessage());
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) SQLException(java.sql.SQLException) BatchUpdateTask(com.ctrip.platform.dal.dao.task.BatchUpdateTask) Test(org.junit.Test)

Example 8 with BatchUpdateTask

use of com.ctrip.platform.dal.dao.task.BatchUpdateTask in project dal by ctripcorp.

the class BatchUpdateTaskTestStub method testUpdateNullField.

@Test
public void testUpdateNullField() {
    BatchUpdateTask<ClientTestModel> test = new BatchUpdateTask<>();
    test.initialize(getParser());
    DalHints hints = new DalHints();
    try {
        List<ClientTestModel> pojos = getAll();
        for (ClientTestModel model : pojos) {
            model.setType((Short) null);
            model.setQuantity(-100);
            model.setDbIndex(null);
            // My sql, it will use default value, while sqlserver will not
            //				model.setLastChanged(null);
            model.setTableIndex(null);
        }
        int[] result = execute(test, hints.updateNullField(), pojos);
        assertEquals(3, result.length);
        assertEquals(3, getCount());
        pojos = getAll();
        for (ClientTestModel model : pojos) {
            assertEquals(model.getQuantity().intValue(), -100);
            assertEquals(0, model.getType().intValue());
            assertNull(model.getDbIndex());
            // The default value
            //				assertNotNull(model.getLastChanged());
            assertNull(model.getTableIndex());
        }
    } catch (SQLException e) {
        e.printStackTrace();
        fail();
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) SQLException(java.sql.SQLException) BatchUpdateTask(com.ctrip.platform.dal.dao.task.BatchUpdateTask) Test(org.junit.Test)

Example 9 with BatchUpdateTask

use of com.ctrip.platform.dal.dao.task.BatchUpdateTask in project dal by ctripcorp.

the class BatchUpdateTaskTestStub method testUpdatableWithVersion.

@Test
public void testUpdatableWithVersion() throws SQLException {
    BatchUpdateTask<UpdatableVersionModel> test = new BatchUpdateTask<>();
    DalParser<UpdatableVersionModel> parser = new DalDefaultJpaParser<>(UpdatableVersionModel.class, getDbName());
    test.initialize(parser);
    DalTableDao<UpdatableVersionModel> dao = new DalTableDao<UpdatableVersionModel>(parser);
    DalHints hints = new DalHints();
    List<UpdatableVersionModel> pojos = dao.query("1=1", new StatementParameters(), new DalHints());
    long[] oldVer = new long[3];
    int i = 0;
    for (UpdatableVersionModel model : pojos) {
        model.setAddress("1122334455");
        oldVer[i++] = model.getLastChanged().getTime();
    }
    int[] result = execute(test, hints, pojos);
    assertArrayEquals(new int[] { 1, 1, 1 }, result);
    pojos = dao.query("1=1", new StatementParameters(), new DalHints());
    i = 0;
    for (UpdatableVersionModel model : pojos) {
        assertEquals("1122334455", model.getAddress());
        Assert.assertTrue(oldVer[i++] <= model.getLastChanged().getTime());
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) BatchUpdateTask(com.ctrip.platform.dal.dao.task.BatchUpdateTask) DalDefaultJpaParser(com.ctrip.platform.dal.dao.helper.DalDefaultJpaParser) DalTableDao(com.ctrip.platform.dal.dao.DalTableDao) Test(org.junit.Test)

Example 10 with BatchUpdateTask

use of com.ctrip.platform.dal.dao.task.BatchUpdateTask in project dal by ctripcorp.

the class BatchUpdateTaskTestStub method testValidteUnchangedField.

@Test
public void testValidteUnchangedField() throws SQLException {
    BatchUpdateTask<UpdatableClientTestModel> test = new BatchUpdateTask<>();
    test.initialize(getParser(UpdatableClientTestModel.class));
    DalHints hints = new DalHints();
    try {
        List<UpdatableClientTestModel> pojos = getAll(UpdatableClientTestModel.class);
        int[] result = execute(test, hints, pojos);
        fail();
    } catch (SQLException e) {
        assertEquals(e.getMessage(), ErrorCode.ValidateFieldCount.getMessage());
    }
    try {
        List<UpdatableClientTestModel> pojos = getAll(UpdatableClientTestModel.class);
        int[] result = execute(test, hints.updateUnchangedField(), pojos);
    } catch (SQLException e) {
        fail();
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) SQLException(java.sql.SQLException) BatchUpdateTask(com.ctrip.platform.dal.dao.task.BatchUpdateTask) Test(org.junit.Test)

Aggregations

DalHints (com.ctrip.platform.dal.dao.DalHints)18 BatchUpdateTask (com.ctrip.platform.dal.dao.task.BatchUpdateTask)18 Test (org.junit.Test)18 SQLException (java.sql.SQLException)11 DalTableDao (com.ctrip.platform.dal.dao.DalTableDao)8 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)8 DalDefaultJpaParser (com.ctrip.platform.dal.dao.helper.DalDefaultJpaParser)8 Timestamp (java.sql.Timestamp)1