Search in sources :

Example 41 with MockConnection

use of com.alibaba.druid.mock.MockConnection in project druid by alibaba.

the class FilterChainImplTest3 method setUp.

protected void setUp() throws Exception {
    dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setFilters("stat,log4j,wall,encoding");
    dataSource.getProxyFilters().add(new FilterAdapter() {
    });
    dataSource.setDbType("mysql");
    dataSource.setDriver(new MockDriver() {

        public ResultSet executeQuery(MockStatementBase stmt, String sql) throws SQLException {
            return null;
        }

        public MockStatement createMockStatement(MockConnection conn) {
            return new MockStatement(conn) {

                public ResultSet getResultSet() throws SQLException {
                    return null;
                }
            };
        }
    });
    dataSource.init();
}
Also used : MockDriver(com.alibaba.druid.mock.MockDriver) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) FilterAdapter(com.alibaba.druid.filter.FilterAdapter) MockStatement(com.alibaba.druid.mock.MockStatement) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) MockConnection(com.alibaba.druid.mock.MockConnection) MockStatementBase(com.alibaba.druid.mock.MockStatementBase)

Example 42 with MockConnection

use of com.alibaba.druid.mock.MockConnection in project druid by alibaba.

the class CallableStatmentTest method setUp.

protected void setUp() throws Exception {
    dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setTestOnBorrow(false);
    dataSource.setPoolPreparedStatements(true);
    dataSource.setMaxOpenPreparedStatements(100);
    dataSource.setFilters("log4j");
    dataSource.setDriver(new MockDriver() {

        public MockCallableStatement createMockCallableStatement(MockConnection conn, String sql) {
            return new MyMockCallableStatement(conn, sql);
        }
    });
}
Also used : MockDriver(com.alibaba.druid.mock.MockDriver) MockCallableStatement(com.alibaba.druid.mock.MockCallableStatement) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) MockConnection(com.alibaba.druid.mock.MockConnection)

Example 43 with MockConnection

use of com.alibaba.druid.mock.MockConnection in project druid by alibaba.

the class ExceptionSorterTest method test_exceptionSorter.

public void test_exceptionSorter() throws Exception {
    Assert.assertTrue(dataSource.getExceptionSorterClassName(), dataSource.getExceptionSorter() instanceof MockExceptionSorter);
    Connection conn = dataSource.getConnection();
    MockConnection mockConn = conn.unwrap(MockConnection.class);
    PreparedStatement stmt = conn.prepareStatement("select 1");
    stmt.execute();
    mockConn.close();
    Exception stmtClosedError = null;
    try {
        stmt.close();
    } catch (Exception ex) {
        stmtClosedError = ex;
    }
    Assert.assertNotNull(stmtClosedError);
    conn.close();
}
Also used : Connection(java.sql.Connection) MockConnection(com.alibaba.druid.mock.MockConnection) PreparedStatement(java.sql.PreparedStatement) MockConnection(com.alibaba.druid.mock.MockConnection) MockExceptionSorter(com.alibaba.druid.pool.vendor.MockExceptionSorter)

Example 44 with MockConnection

use of com.alibaba.druid.mock.MockConnection in project druid by alibaba.

the class SavepointTest method test_multi_savepoint.

public void test_multi_savepoint() throws Exception {
    Connection conn = dataSource.getConnection();
    MockConnection physicalConn = conn.unwrap(MockConnection.class);
    Assert.assertEquals(true, conn.getAutoCommit());
    Assert.assertEquals(true, physicalConn.getAutoCommit());
    conn.setAutoCommit(false);
    Assert.assertEquals(false, conn.getAutoCommit());
    Assert.assertEquals(false, physicalConn.getAutoCommit());
    Savepoint[] savepoints = new Savepoint[100];
    for (int i = 0; i < savepoints.length; ++i) {
        Statement stmt = conn.createStatement();
        stmt.execute("insert t (" + i + ")");
        stmt.close();
        savepoints[i] = conn.setSavepoint();
        Assert.assertEquals(i + 1, physicalConn.getSavepoints().size());
        for (int j = 0; j <= i; ++j) {
            Assert.assertTrue(physicalConn.getSavepoints().contains(savepoints[j]));
        }
    }
    // rollback single
    conn.rollback(savepoints[99]);
    Assert.assertEquals(99, physicalConn.getSavepoints().size());
    // release single
    conn.releaseSavepoint(savepoints[97]);
    Assert.assertEquals(98, physicalConn.getSavepoints().size());
    // rollback multi
    conn.rollback(savepoints[90]);
    Assert.assertEquals(90, physicalConn.getSavepoints().size());
    // rollback all
    conn.rollback();
    Assert.assertEquals(0, physicalConn.getSavepoints().size());
    conn.close();
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) MockConnection(com.alibaba.druid.mock.MockConnection) Savepoint(java.sql.Savepoint) MockConnection(com.alibaba.druid.mock.MockConnection) Savepoint(java.sql.Savepoint)

Example 45 with MockConnection

use of com.alibaba.druid.mock.MockConnection in project druid by alibaba.

the class OracleExceptionSorterTest_stmt_executeUpdate_3 method test_connect.

public void test_connect() throws Exception {
    String sql = "SELECT 1";
    {
        DruidPooledConnection conn = dataSource.getConnection();
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.execute();
        pstmt.close();
        conn.close();
    }
    DruidPooledConnection conn = dataSource.getConnection();
    MockConnection mockConn = conn.unwrap(MockConnection.class);
    Assert.assertNotNull(mockConn);
    Statement stmt = conn.createStatement();
    SQLException exception = new SQLException("xx", "xxx", 28);
    mockConn.setError(exception);
    SQLException execErrror = null;
    try {
        stmt.executeUpdate(sql, new String[0]);
    } catch (SQLException ex) {
        execErrror = ex;
    }
    Assert.assertNotNull(execErrror);
    Assert.assertSame(exception, execErrror);
    SQLException commitError = null;
    try {
        conn.commit();
    } catch (SQLException ex) {
        commitError = ex;
    }
    Assert.assertNotNull(commitError);
    Assert.assertSame(exception, commitError.getCause());
    conn.close();
}
Also used : SQLException(java.sql.SQLException) DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) MockConnection(com.alibaba.druid.mock.MockConnection)

Aggregations

MockConnection (com.alibaba.druid.mock.MockConnection)74 SQLException (java.sql.SQLException)54 PreparedStatement (java.sql.PreparedStatement)53 DruidPooledConnection (com.alibaba.druid.pool.DruidPooledConnection)49 Statement (java.sql.Statement)38 Connection (java.sql.Connection)22 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)13 MockDriver (com.alibaba.druid.mock.MockDriver)8 MockPreparedStatement (com.alibaba.druid.mock.MockPreparedStatement)6 MockCallableStatement (com.alibaba.druid.mock.MockCallableStatement)4 MockStatement (com.alibaba.druid.mock.MockStatement)4 DruidConnectionHolder (com.alibaba.druid.pool.DruidConnectionHolder)3 PreparedStatementKey (com.alibaba.druid.pool.DruidPooledPreparedStatement.PreparedStatementKey)3 PreparedStatementHolder (com.alibaba.druid.pool.PreparedStatementHolder)3 MSSQLValidConnectionChecker (com.alibaba.druid.pool.vendor.MSSQLValidConnectionChecker)3 ResultSet (java.sql.ResultSet)3 PoolableWrapper (com.alibaba.druid.pool.PoolableWrapper)2 DruidNativeJdbcExtractor (com.alibaba.druid.support.spring.DruidNativeJdbcExtractor)2 ConnectionHandle (com.jolbox.bonecp.ConnectionHandle)2 NewProxyConnection (com.mchange.v2.c3p0.impl.NewProxyConnection)2