Search in sources :

Example 1 with DalConnectionManager

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

the class DalConnectionManagerTest method testEvaluate.

@Test
public void testEvaluate() {
    boolean useMaster = true;
    DalHints hints = new DalHints();
    try {
        DalConnectionManager test = getDalConnectionManager(noShardDb);
        String id = test.evaluateShard(hints);
        assertNull(id);
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
    try {
        DalConnectionManager test = getDalConnectionManager(shardDb);
        hints = new DalHints();
        hints.inShard(1);
        String id = test.evaluateShard(hints);
        assertEquals("1", id);
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
    try {
        DalConnectionManager test = getDalConnectionManager(shardDb);
        hints = new DalHints();
        hints.inShard("1");
        String id = test.evaluateShard(hints);
        assertEquals("1", id);
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
    try {
        DalConnectionManager test = getDalConnectionManager(shardDb);
        hints = new DalHints();
        hints.setShardValue("3");
        String id = test.evaluateShard(hints);
        assertEquals("1", id);
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
    try {
        DalConnectionManager test = getDalConnectionManager(shardDb);
        hints = new DalHints();
        hints.setShardColValue("index", "3");
        String id = test.evaluateShard(hints);
        assertEquals("1", id);
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
    try {
        DalConnectionManager test = getDalConnectionManager(shardDb);
        hints = new DalHints();
        Map<String, String> shardColValues = new HashMap<>();
        shardColValues.put("index", "3");
        hints.setShardColValues(shardColValues);
        String id = test.evaluateShard(hints);
        assertEquals("1", id);
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DalConnectionManager(com.ctrip.platform.dal.dao.client.DalConnectionManager) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 2 with DalConnectionManager

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

the class DalConnectionManagerTest method testDoInConnectionException.

@Test
public void testDoInConnectionException() {
    final boolean useMaster = true;
    final DalHints hints = new DalHints();
    final String message = "testDoInConnectionException";
    ConnectionAction<Object> action = null;
    try {
        final DalConnectionManager test = getDalConnectionManager(noShardDb);
        action = new ConnectionAction<Object>() {

            public Object execute() throws Exception {
                connHolder = test.getNewConnection(hints, useMaster, DalEventEnum.BATCH_CALL);
                statement = connHolder.getConn().createStatement();
                rs = statement.executeQuery("select * from City");
                rs.next();
                throw new RuntimeException(message);
            }
        };
        test.doInConnection(action, hints);
        fail();
    } catch (Exception e) {
        e.printStackTrace();
        assertNotNull(action);
        assertTrue(action.conn == null);
        assertTrue(action.statement == null);
        assertTrue(action.rs == null);
        assertTrue(action.connHolder == null);
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DalConnectionManager(com.ctrip.platform.dal.dao.client.DalConnectionManager) Test(org.junit.Test)

Example 3 with DalConnectionManager

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

the class DalConnectionManagerTest method testGetNewConnection.

@Test
public void testGetNewConnection() {
    boolean useMaster = true;
    DalHints hints = new DalHints();
    try {
        DalConnectionManager test = getDalConnectionManager(noShardDb);
        DalConnection conn = test.getNewConnection(hints, useMaster, DalEventEnum.BATCH_CALL);
        assertNotNull(conn);
        assertNotNull(conn.getConn());
        conn.getConn().close();
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DalConnection(com.ctrip.platform.dal.dao.client.DalConnection) DalConnectionManager(com.ctrip.platform.dal.dao.client.DalConnectionManager) Test(org.junit.Test)

Example 4 with DalConnectionManager

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

the class DalConnectionManagerTest method testDoInConnection.

@Test
public void testDoInConnection() {
    final boolean useMaster = true;
    final DalHints hints = new DalHints();
    try {
        final DalConnectionManager test = getDalConnectionManager(noShardDb);
        ConnectionAction<Object> action = new ConnectionAction<Object>() {

            public Object execute() throws Exception {
                connHolder = test.getNewConnection(hints, useMaster, DalEventEnum.BATCH_CALL);
                statement = connHolder.getConn().createStatement();
                rs = statement.executeQuery("select * from " + SqlServerTestInitializer.TABLE_NAME);
                rs.next();
                return null;
            }
        };
        action.operation = DalEventEnum.EXECUTE;
        test.doInConnection(action, hints);
        assertTrue(action.conn == null);
        assertTrue(action.statement == null);
        assertTrue(action.rs == null);
        assertTrue(action.connHolder == null);
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DalConnectionManager(com.ctrip.platform.dal.dao.client.DalConnectionManager) ConnectionAction(com.ctrip.platform.dal.dao.client.ConnectionAction) Test(org.junit.Test)

Aggregations

DalHints (com.ctrip.platform.dal.dao.DalHints)4 DalConnectionManager (com.ctrip.platform.dal.dao.client.DalConnectionManager)4 Test (org.junit.Test)4 ConnectionAction (com.ctrip.platform.dal.dao.client.ConnectionAction)1 DalConnection (com.ctrip.platform.dal.dao.client.DalConnection)1 HashMap (java.util.HashMap)1