Search in sources :

Example 21 with DalDefaultJpaParser

use of com.ctrip.platform.dal.dao.helper.DalDefaultJpaParser in project dal by ctripcorp.

the class BatchUpdateTaskTestStub method testUpdatableWithIntVersion.

@Test
public void testUpdatableWithIntVersion() throws SQLException {
    BatchUpdateTask<UpdatableIntVersionModel> test = new BatchUpdateTask<>();
    DalParser<UpdatableIntVersionModel> parser = new DalDefaultJpaParser<>(UpdatableIntVersionModel.class, getDbName());
    test.initialize(parser);
    DalTableDao<UpdatableIntVersionModel> dao = new DalTableDao<>(parser);
    DalHints hints = new DalHints();
    Integer[] oldValue = new Integer[3];
    List<UpdatableIntVersionModel> pojos = dao.query("1=1", new StatementParameters(), new DalHints());
    int i = 0;
    for (UpdatableIntVersionModel model : pojos) {
        model.setAddress("1122334455");
        oldValue[i++] = model.getTableIndex();
    }
    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 (UpdatableIntVersionModel model : pojos) {
        assertEquals("1122334455", model.getAddress());
        Assert.assertTrue(oldValue[i++] + 1 == model.getTableIndex());
    }
}
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 22 with DalDefaultJpaParser

use of com.ctrip.platform.dal.dao.helper.DalDefaultJpaParser in project dal by ctripcorp.

the class BatchInsertTaskTestStub method testExecuteWithNonInsertable.

@Test
public void testExecuteWithNonInsertable() throws SQLException, InterruptedException {
    BatchInsertTask<NonInsertableVersionModel> test = new BatchInsertTask<>();
    DalParser<NonInsertableVersionModel> parser = new DalDefaultJpaParser<>(NonInsertableVersionModel.class, getDbName());
    test.initialize(parser);
    DalHints hints = new DalHints();
    try {
        List<ClientTestModel> old = getAll();
        Thread.sleep(1000);
        BulkTaskContext<NonInsertableVersionModel> ctx = test.createTaskContext(hints, test.getPojosFields(getAll(NonInsertableVersionModel.class)), getAll(NonInsertableVersionModel.class));
        test.execute(hints, getAllMap(), ctx);
        assertEquals(3 + 3, getCount());
        List<ClientTestModel> newModel = getAll().subList(3, 6);
        for (int i = 0; i < 3; i++) {
            assertTrue(newModel.get(i).getLastChanged().getTime() > old.get(i).getLastChanged().getTime());
        }
    } catch (SQLException e) {
        e.printStackTrace();
        fail();
    }
    Map<Integer, Map<String, ?>> pojos = getAllMap();
    for (Map<String, ?> pojo : pojos.values()) {
        assertNotNull(pojo.get("last_changed"));
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) BatchInsertTask(com.ctrip.platform.dal.dao.task.BatchInsertTask) SQLException(java.sql.SQLException) DalDefaultJpaParser(com.ctrip.platform.dal.dao.helper.DalDefaultJpaParser) Map(java.util.Map) Test(org.junit.Test)

Example 23 with DalDefaultJpaParser

use of com.ctrip.platform.dal.dao.helper.DalDefaultJpaParser in project dal by ctripcorp.

the class BatchInsertTaskTestStub method testExecuteNullColumns.

@Test
public void testExecuteNullColumns() throws SQLException {
    BatchInsertTask<NonInsertableVersionModel> test = new BatchInsertTask<>();
    DalParser<NonInsertableVersionModel> parser = new DalDefaultJpaParser<>(NonInsertableVersionModel.class, getDbName());
    test.initialize(parser);
    DalHints hints = new DalHints();
    try {
        List<NonInsertableVersionModel> models = getAll(NonInsertableVersionModel.class);
        BulkTaskContext<NonInsertableVersionModel> ctx = test.createTaskContext(hints, test.getPojosFields(models), models);
        for (NonInsertableVersionModel model : models) {
            model.setType(null);
            model.setDbIndex(null);
            model.setTableIndex(null);
        }
        // Type Address, tableIndex will not be included in insert
        test.execute(hints, test.getPojosFieldsMap(models), ctx);
        assertEquals(3 + 3, getCount());
        models = getAll(NonInsertableVersionModel.class).subList(3, 6);
        for (NonInsertableVersionModel model : models) {
            assertNull(model.getType());
            assertNull(model.getTableIndex());
            assertNull(model.getDbIndex());
        }
    } catch (SQLException e) {
        e.printStackTrace();
        fail();
    }
    Map<Integer, Map<String, ?>> pojos = getAllMap();
    for (Map<String, ?> pojo : pojos.values()) {
        assertNotNull(pojo.get("last_changed"));
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) BatchInsertTask(com.ctrip.platform.dal.dao.task.BatchInsertTask) SQLException(java.sql.SQLException) DalDefaultJpaParser(com.ctrip.platform.dal.dao.helper.DalDefaultJpaParser) Map(java.util.Map) Test(org.junit.Test)

Example 24 with DalDefaultJpaParser

use of com.ctrip.platform.dal.dao.helper.DalDefaultJpaParser in project dal by ctripcorp.

the class CombinedInsertTaskTestStub method testExecuteWithNonInsertable.

@Test
public void testExecuteWithNonInsertable() throws SQLException {
    CombinedInsertTask<NonInsertableVersionModel> test = new CombinedInsertTask<>();
    DalParser<NonInsertableVersionModel> parser = new DalDefaultJpaParser<>(NonInsertableVersionModel.class, getDbName());
    test.initialize(parser);
    DalHints hints = new DalHints();
    if (enableKeyHolder)
        hints.setKeyHolder(new KeyHolder());
    try {
        execute(test, hints, getAllMap(), getAll(NonInsertableVersionModel.class));
        if (enableKeyHolder) {
            // You have to merge before get size
            assertEquals(3, hints.getKeyHolder().size());
        }
        assertEquals(3 + 3, getCount());
    } catch (SQLException e) {
        e.printStackTrace();
        fail();
    }
    Map<Integer, Map<String, ?>> pojos = getAllMap();
    for (Map<String, ?> pojo : pojos.values()) {
        assertNotNull(pojo.get("last_changed"));
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) SQLException(java.sql.SQLException) CombinedInsertTask(com.ctrip.platform.dal.dao.task.CombinedInsertTask) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) DalDefaultJpaParser(com.ctrip.platform.dal.dao.helper.DalDefaultJpaParser) Map(java.util.Map) Test(org.junit.Test)

Example 25 with DalDefaultJpaParser

use of com.ctrip.platform.dal.dao.helper.DalDefaultJpaParser in project dal by ctripcorp.

the class BatchUpdateTaskTestStub method testVersionNotSet.

@Test
public void testVersionNotSet() 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());
    for (UpdatableVersionModel model : pojos) {
        model.setAddress("1122334455");
        model.setLastChanged(null);
    }
    try {
        execute(test, hints, pojos);
        fail();
    } catch (SQLException e) {
        assertEquals(ErrorCode.ValidateVersion.getMessage(), e.getMessage());
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) SQLException(java.sql.SQLException) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) DalDefaultJpaParser(com.ctrip.platform.dal.dao.helper.DalDefaultJpaParser) BatchUpdateTask(com.ctrip.platform.dal.dao.task.BatchUpdateTask) DalTableDao(com.ctrip.platform.dal.dao.DalTableDao) Test(org.junit.Test)

Aggregations

DalDefaultJpaParser (com.ctrip.platform.dal.dao.helper.DalDefaultJpaParser)27 Test (org.junit.Test)26 DalHints (com.ctrip.platform.dal.dao.DalHints)25 DalTableDao (com.ctrip.platform.dal.dao.DalTableDao)13 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)13 BatchUpdateTask (com.ctrip.platform.dal.dao.task.BatchUpdateTask)8 SingleUpdateTask (com.ctrip.platform.dal.dao.task.SingleUpdateTask)8 SQLException (java.sql.SQLException)5 Map (java.util.Map)4 Timestamp (java.sql.Timestamp)3 BatchInsertTask (com.ctrip.platform.dal.dao.task.BatchInsertTask)2 KeyHolder (com.ctrip.platform.dal.dao.KeyHolder)1 CombinedInsertTask (com.ctrip.platform.dal.dao.task.CombinedInsertTask)1 SingleInsertTask (com.ctrip.platform.dal.dao.task.SingleInsertTask)1 BeforeClass (org.junit.BeforeClass)1