Search in sources :

Example 36 with DalHints

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

the class DalTransactionManagerTest method testRollbackListeners.

@Test
public void testRollbackListeners() {
    final DalHints hints = new DalHints();
    final DalTransactionListener testListener = new DalTransactionListener() {

        @Override
        public void beforeCommit() {
        }

        @Override
        public void beforeRollback() {
            Assert.assertTrue(DalTransactionManager.isInTransaction());
        }

        @Override
        public void afterCommit() {
            fail();
        }

        @Override
        public void afterRollback() {
            Assert.assertFalse(DalTransactionManager.isInTransaction());
        }
    };
    final DalTransactionListener testListener1 = new DalTransactionListener() {

        @Override
        public void beforeCommit() throws SQLException {
            throw new SQLException();
        }

        @Override
        public void beforeRollback() {
            Assert.assertTrue(DalTransactionManager.isInTransaction());
        }

        @Override
        public void afterCommit() {
            fail();
        }

        @Override
        public void afterRollback() {
            Assert.assertFalse(DalTransactionManager.isInTransaction());
        }
    };
    try {
        final DalTransactionManager test = new DalTransactionManager(getDalConnectionManager());
        ConnectionAction<?> action = new ConnectionAction<Object>() {

            public Object execute() throws Exception {
                DalTransactionManager.register(testListener);
                // The 2nd listener will cause transaction rollback
                DalTransactionManager.register(testListener1);
                return null;
            }
        };
        action.operation = DalEventEnum.EXECUTE;
        test.doInTransaction(action, hints);
        fail();
    } catch (Exception e) {
    }
}
Also used : DalTransactionListener(com.ctrip.platform.dal.dao.client.DalTransactionListener) DalHints(com.ctrip.platform.dal.dao.DalHints) DalTransactionManager(com.ctrip.platform.dal.dao.client.DalTransactionManager) SQLException(java.sql.SQLException) ConnectionAction(com.ctrip.platform.dal.dao.client.ConnectionAction) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 37 with DalHints

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

the class DalTransactionManagerTest method testGetCurrentListeners.

@Test
public void testGetCurrentListeners() {
    final DalHints hints = new DalHints();
    final DalTransactionListener testListener = new DalTransactionListener() {

        @Override
        public void beforeCommit() {
        }

        @Override
        public void beforeRollback() {
        }

        @Override
        public void afterCommit() {
        }

        @Override
        public void afterRollback() {
        }
    };
    try {
        final DalTransactionManager test = new DalTransactionManager(getDalConnectionManager());
        DalTransactionManager.getCurrentListeners();
        fail();
    } catch (Exception e) {
    }
    try {
        final DalTransactionManager test = new DalTransactionManager(getDalConnectionManager());
        ConnectionAction<?> action = new ConnectionAction<Object>() {

            public Object execute() throws Exception {
                DalTransactionManager.register(testListener);
                DalTransactionManager.register(testListener);
                Assert.assertEquals(2, DalTransactionManager.getCurrentListeners().size());
                return null;
            }
        };
        action.operation = DalEventEnum.EXECUTE;
        test.doInTransaction(action, hints);
        try {
            DalTransactionManager.getCurrentListeners();
            fail();
        } catch (Exception e) {
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : DalTransactionListener(com.ctrip.platform.dal.dao.client.DalTransactionListener) DalHints(com.ctrip.platform.dal.dao.DalHints) DalTransactionManager(com.ctrip.platform.dal.dao.client.DalTransactionManager) ConnectionAction(com.ctrip.platform.dal.dao.client.ConnectionAction) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 38 with DalHints

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

the class PartialQueryTableDaoUnitTest method testFindBySelectedField.

@Test
public void testFindBySelectedField() throws Exception {
    DalTableDao<Person> client = new DalTableDao<>(new DalDefaultJpaParser<>(Person.class));
    List<Integer> peopleIds = new ArrayList<>();
    peopleIds.add(1);
    peopleIds.add(2);
    peopleIds.add(3);
    List<Integer> cityIds = new ArrayList<>();
    cityIds.add(1);
    cityIds.add(2);
    cityIds.add(3);
    SelectSqlBuilder builder = new SelectSqlBuilder();
    builder.select("DataChange_LastTime", "CityID", "Name", "ProvinceID");
    builder.in("PeopleID", peopleIds, Types.INTEGER, false);
    builder.and();
    builder.in("CityID", cityIds, Types.INTEGER, false);
    try {
        List<Person> ret = client.query(builder, new DalHints().inAllShards().inTableShard(1));
        Assert.assertNull(ret.get(0).getCountryID());
        Assert.assertNull(ret.get(0).getPeopleID());
    } catch (DalException e) {
        Assert.fail();
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DalException(com.ctrip.platform.dal.exceptions.DalException) ArrayList(java.util.ArrayList) SelectSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder) DalTableDao(com.ctrip.platform.dal.dao.DalTableDao) Test(org.junit.Test)

Example 39 with DalHints

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

the class DalColumnMapRowMapperTest method tearDownAfterClass.

@AfterClass
public static void tearDownAfterClass() throws Exception {
    DalHints hints = new DalHints();
    StatementParameters parameters = new StatementParameters();
    String[] sqls = new String[] { DROP_TABLE_SQL };
    for (int i = 0; i < sqls.length; i++) {
        client.update(sqls[i], parameters, hints);
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) AfterClass(org.junit.AfterClass)

Example 40 with DalHints

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

the class DalColumnMapRowMapperTest method setUp.

@Before
public void setUp() throws Exception {
    DalHints hints = new DalHints();
    String[] insertSqls = new String[] { "SET IDENTITY_INSERT " + TABLE_NAME + " ON", "INSERT INTO " + TABLE_NAME + "(Id, quantity,type,address)" + " VALUES(1, 10, 1, 'SH INFO')", "INSERT INTO " + TABLE_NAME + "(Id, quantity,type,address)" + " VALUES(2, 11, 1, 'BJ INFO')", "INSERT INTO " + TABLE_NAME + "(Id, quantity,type,address)" + " VALUES(3, 12, 2, 'SZ INFO')", "SET IDENTITY_INSERT " + TABLE_NAME + " OFF" };
    client.batchUpdate(insertSqls, hints);
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) Before(org.junit.Before)

Aggregations

DalHints (com.ctrip.platform.dal.dao.DalHints)772 Test (org.junit.Test)532 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)317 SQLException (java.sql.SQLException)298 ArrayList (java.util.ArrayList)132 ClientTestModel (test.com.ctrip.platform.dal.dao.unitbase.ClientTestModel)61 List (java.util.List)48 KeyHolder (com.ctrip.platform.dal.dao.KeyHolder)45 FreeSelectSqlBuilder (com.ctrip.platform.dal.dao.sqlbuilder.FreeSelectSqlBuilder)39 Future (java.util.concurrent.Future)32 DalTableDao (com.ctrip.platform.dal.dao.DalTableDao)29 SelectionContext (com.ctrip.platform.dal.dao.configure.SelectionContext)27 DalDefaultJpaParser (com.ctrip.platform.dal.dao.helper.DalDefaultJpaParser)27 DataBase (com.ctrip.platform.dal.dao.configure.DataBase)26 BeforeClass (org.junit.BeforeClass)26 AfterClass (org.junit.AfterClass)25 HashMap (java.util.HashMap)23 FreeUpdateSqlBuilder (com.ctrip.platform.dal.dao.sqlbuilder.FreeUpdateSqlBuilder)20 DalClient (com.ctrip.platform.dal.dao.DalClient)18 BatchUpdateTask (com.ctrip.platform.dal.dao.task.BatchUpdateTask)18