Search in sources :

Example 56 with ClientTestModel

use of test.com.ctrip.platform.dal.dao.unitbase.ClientTestModel in project dal by ctripcorp.

the class DalDirectClientTestStub method quryTestWithoutParameters.

/**
 * Test the basic query function without parameters
 *
 * @throws SQLException
 */
@Test
public void quryTestWithoutParameters() throws SQLException {
    String querySql = "SELECT * FROM " + TABLE_NAME;
    StatementParameters parameters = new StatementParameters();
    ClientTestDalRowMapper mapper = new ClientTestDalRowMapper();
    DalHints hints = new DalHints();
    List<ClientTestModel> res = client.query(querySql, parameters, hints, new DalRowMapperExtractor<ClientTestModel>(mapper));
    Assert.assertTrue(null != res && res.size() == 3);
    ClientTestModel model = res.get(0);
    Assert.assertTrue(model.getQuantity() == 10 && model.getType() == 1 && model.getAddress().equals("SH INFO"));
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) ClientTestModel(test.com.ctrip.platform.dal.dao.unitbase.ClientTestModel) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) ClientTestDalRowMapper(test.com.ctrip.platform.dal.dao.unitbase.ClientTestDalRowMapper) Test(org.junit.Test)

Example 57 with ClientTestModel

use of test.com.ctrip.platform.dal.dao.unitbase.ClientTestModel in project dal by ctripcorp.

the class DalDirectClientTestStub method executeTestWithOneCommand.

/**
 * Test execute command function
 * @throws SQLException
 */
@Test
public void executeTestWithOneCommand() throws SQLException {
    final DalHints hints = new DalHints();
    DalCommand command = new DalCommand() {

        @Override
        public boolean execute(DalClient client) throws SQLException {
            String sql = "DELETE FROM " + TABLE_NAME + " WHERE id = 1";
            StatementParameters parameters = new StatementParameters();
            return client.update(sql, parameters, hints) == 1;
        }
    };
    client.execute(command, hints);
    List<ClientTestModel> models = this.queryModelsByIds(1);
    Assert.assertEquals(0, models.size());
}
Also used : DalCommand(com.ctrip.platform.dal.dao.DalCommand) DalHints(com.ctrip.platform.dal.dao.DalHints) DalClient(com.ctrip.platform.dal.dao.DalClient) ClientTestModel(test.com.ctrip.platform.dal.dao.unitbase.ClientTestModel) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) Test(org.junit.Test)

Example 58 with ClientTestModel

use of test.com.ctrip.platform.dal.dao.unitbase.ClientTestModel in project dal by ctripcorp.

the class DalDirectClientTestStub method updateTestWithDelete.

/**
 * Test the update function with delete SQL statement
 *
 * @throws SQLException
 */
@Test
public void updateTestWithDelete() throws SQLException {
    String deleteSql = String.format("DELETE FROM %s WHERE id=?", TABLE_NAME);
    StatementParameters parameters = new StatementParameters();
    parameters.set(1, Types.INTEGER, 1);
    DalHints hints = new DalHints();
    int count = client.update(deleteSql, parameters, hints);
    assertEquals(1, count, 2);
    List<ClientTestModel> po_models = this.queryModelsByIds(1);
    Assert.assertTrue(null != po_models);
    Assert.assertEquals(0, po_models.size());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) ClientTestModel(test.com.ctrip.platform.dal.dao.unitbase.ClientTestModel) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) Test(org.junit.Test)

Example 59 with ClientTestModel

use of test.com.ctrip.platform.dal.dao.unitbase.ClientTestModel in project dal by ctripcorp.

the class DalDirectClientTestStub method batchUpdateTestWithParametersAllSuccessed.

/**
 * Test the batch update function with parameters all success.
 *
 * @throws SQLException
 */
@Test
public void batchUpdateTestWithParametersAllSuccessed() throws SQLException {
    String sql = "INSERT INTO " + TABLE_NAME + "(quantity,type,address,last_changed) VALUES(?, ?, ?,?)";
    StatementParameters[] parameterList = new StatementParameters[2];
    parameterList[0] = new StatementParameters();
    parameterList[0].set(1, Types.INTEGER, 11);
    parameterList[0].set(2, Types.SMALLINT, 2);
    parameterList[0].set(3, Types.VARCHAR, "SZ INFO");
    parameterList[0].set(4, Types.TIMESTAMP, new Timestamp(System.currentTimeMillis()));
    parameterList[1] = new StatementParameters();
    parameterList[1].set(1, Types.INTEGER, 11);
    parameterList[1].set(2, Types.SMALLINT, 2);
    parameterList[1].set(3, Types.VARCHAR, "HK INFO");
    parameterList[1].set(4, Types.TIMESTAMP, new Timestamp(System.currentTimeMillis()));
    DalHints hints = new DalHints();
    int[] counts = client.batchUpdate(sql, parameterList, hints);
    assertEqualsBatchInsert(new int[] { 1, 1 }, counts, 3 + 2);
    List<ClientTestModel> models = this.queryModelsByIds();
    Assert.assertEquals(5, models.size());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) ClientTestModel(test.com.ctrip.platform.dal.dao.unitbase.ClientTestModel) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) Timestamp(java.sql.Timestamp) Test(org.junit.Test)

Example 60 with ClientTestModel

use of test.com.ctrip.platform.dal.dao.unitbase.ClientTestModel in project dal by ctripcorp.

the class DalDirectClientTestStub method executeTestWithMultipleCommandsNotAllSuccessed.

/**
 * Test execute multiple commands and not all successes.
 * If one failed, the after commands will be dropped.
 * @throws SQLException
 */
@Test
public void executeTestWithMultipleCommandsNotAllSuccessed() throws SQLException {
    final DalHints hints = new DalHints();
    List<DalCommand> commands = new ArrayList<DalCommand>();
    commands.add(new DalCommand() {

        @Override
        public boolean execute(DalClient client) throws SQLException {
            String sql = "DELETE FROM " + TABLE_NAME + " WHERE id = 1";
            StatementParameters parameters = new StatementParameters();
            client.update(sql, parameters, hints);
            return false;
        }
    });
    commands.add(new DalCommand() {

        @Override
        public boolean execute(DalClient client) throws SQLException {
            String sql = "DELETE FROM " + TABLE_NAME + " WHERE id = 100";
            StatementParameters parameters = new StatementParameters();
            return client.update(sql, parameters, hints) == 1;
        }
    });
    commands.add(new DalCommand() {

        @Override
        public boolean execute(DalClient client) throws SQLException {
            String sql = "DELETE FROM " + TABLE_NAME + " WHERE id = 3";
            StatementParameters parameters = new StatementParameters();
            return client.update(sql, parameters, hints) == 1;
        }
    });
    client.execute(commands, hints);
    List<ClientTestModel> models = this.queryModelsByIds();
    Assert.assertEquals(2, models.size());
}
Also used : DalCommand(com.ctrip.platform.dal.dao.DalCommand) DalHints(com.ctrip.platform.dal.dao.DalHints) DalClient(com.ctrip.platform.dal.dao.DalClient) ClientTestModel(test.com.ctrip.platform.dal.dao.unitbase.ClientTestModel) SQLException(java.sql.SQLException) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

DalHints (com.ctrip.platform.dal.dao.DalHints)61 ClientTestModel (test.com.ctrip.platform.dal.dao.unitbase.ClientTestModel)61 Test (org.junit.Test)59 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)40 ArrayList (java.util.ArrayList)13 SQLException (java.sql.SQLException)9 DalClient (com.ctrip.platform.dal.dao.DalClient)3 DalCommand (com.ctrip.platform.dal.dao.DalCommand)3 SelectSqlBuilder (com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder)3 ClientTestDalRowMapper (test.com.ctrip.platform.dal.dao.unitbase.ClientTestDalRowMapper)3 KeyHolder (com.ctrip.platform.dal.dao.KeyHolder)2 DalRowMapperExtractor (com.ctrip.platform.dal.dao.helper.DalRowMapperExtractor)2 DalRowCallback (com.ctrip.platform.dal.dao.DalRowCallback)1 DalCustomRowMapper (com.ctrip.platform.dal.dao.helper.DalCustomRowMapper)1 FreeSelectSqlBuilder (com.ctrip.platform.dal.dao.sqlbuilder.FreeSelectSqlBuilder)1 ResultSet (java.sql.ResultSet)1 Timestamp (java.sql.Timestamp)1 List (java.util.List)1 Map (java.util.Map)1