Search in sources :

Example 36 with MockConnection

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

the class MSSQLValidConnectionCheckerTest method test_closed.

public void test_closed() throws Exception {
    MSSQLValidConnectionChecker checker = new MSSQLValidConnectionChecker();
    MockConnection conn = new MockConnection();
    conn.close();
    Assert.assertFalse(checker.isValidConnection(conn, "select 1", 10));
}
Also used : MSSQLValidConnectionChecker(com.alibaba.druid.pool.vendor.MSSQLValidConnectionChecker) MockConnection(com.alibaba.druid.mock.MockConnection)

Example 37 with MockConnection

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

the class MSSQLValidConnectionCheckerTest method test_error.

public void test_error() throws Exception {
    MSSQLValidConnectionChecker checker = new MSSQLValidConnectionChecker();
    MockConnection conn = new MockConnection();
    conn.setError(new SQLException());
    SQLException error = null;
    try {
        checker.isValidConnection(conn, "select 1", 10);
    } catch (SQLException ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
    Assert.assertSame(error, conn.getError());
}
Also used : MSSQLValidConnectionChecker(com.alibaba.druid.pool.vendor.MSSQLValidConnectionChecker) SQLException(java.sql.SQLException) MockConnection(com.alibaba.druid.mock.MockConnection)

Example 38 with MockConnection

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

the class JdbcUtilsTest method test_close.

public void test_close() throws Exception {
    JdbcUtils.close((Connection) null);
    JdbcUtils.close((Statement) null);
    JdbcUtils.close((ResultSet) null);
    JdbcUtils.close(new MockConnection() {

        @Override
        public void close() throws SQLException {
            throw new SQLException();
        }
    });
    JdbcUtils.close(new MockStatement(null) {

        @Override
        public void close() throws SQLException {
            throw new SQLException();
        }
    });
    JdbcUtils.close(new MockResultSet(null) {

        @Override
        public void close() throws SQLException {
            throw new SQLException();
        }
    });
    JdbcUtils.close(new Closeable() {

        @Override
        public void close() throws IOException {
            throw new IOException();
        }
    });
    JdbcUtils.close(new Closeable() {

        @Override
        public void close() throws IOException {
        }
    });
    JdbcUtils.close((Closeable) null);
    new JdbcUtils();
}
Also used : JdbcUtils(com.alibaba.druid.util.JdbcUtils) SQLException(java.sql.SQLException) Closeable(java.io.Closeable) MockStatement(com.alibaba.druid.mock.MockStatement) MockResultSet(com.alibaba.druid.mock.MockResultSet) IOException(java.io.IOException) MockConnection(com.alibaba.druid.mock.MockConnection)

Example 39 with MockConnection

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

the class DruidJdbcExtractorTest method test_spring.

public void test_spring() throws Exception {
    DruidDataSource dataSource = new DruidDataSource();
    try {
        DruidNativeJdbcExtractor extractor = new DruidNativeJdbcExtractor();
        dataSource.setUrl("jdbc:mock:xx1");
        Connection conn = dataSource.getConnection();
        Assert.assertEquals(true, extractor.getNativeConnection(conn) instanceof MockConnection);
        Statement stmt = conn.createStatement();
        Assert.assertEquals(true, extractor.getNativeConnectionFromStatement(stmt) instanceof MockConnection);
        Assert.assertEquals(true, extractor.getNativeStatement(stmt) instanceof MockStatement);
        stmt.close();
        PreparedStatement preStmt = conn.prepareStatement("select 1");
        Assert.assertEquals(true, extractor.getNativeConnectionFromStatement(preStmt) instanceof MockConnection);
        Assert.assertEquals(true, extractor.getNativeStatement(preStmt) instanceof MockPreparedStatement);
        Assert.assertEquals(true, extractor.getNativePreparedStatement(preStmt) instanceof MockPreparedStatement);
        preStmt.close();
        PreparedStatement callStmt = conn.prepareCall("select 1");
        Assert.assertEquals(true, extractor.getNativeConnectionFromStatement(callStmt) instanceof MockConnection);
        Assert.assertEquals(true, extractor.getNativeStatement(callStmt) instanceof MockCallableStatement);
        Assert.assertEquals(true, extractor.getNativePreparedStatement(callStmt) instanceof MockCallableStatement);
        callStmt.close();
        conn.close();
    } finally {
        dataSource.close();
    }
}
Also used : MockCallableStatement(com.alibaba.druid.mock.MockCallableStatement) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) MockStatement(com.alibaba.druid.mock.MockStatement) MockCallableStatement(com.alibaba.druid.mock.MockCallableStatement) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) Connection(java.sql.Connection) MockConnection(com.alibaba.druid.mock.MockConnection) MockStatement(com.alibaba.druid.mock.MockStatement) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) PreparedStatement(java.sql.PreparedStatement) DruidNativeJdbcExtractor(com.alibaba.druid.support.spring.DruidNativeJdbcExtractor) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) MockConnection(com.alibaba.druid.mock.MockConnection)

Example 40 with MockConnection

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

the class DruidJdbcExtractorTest method test_spring_filter.

public void test_spring_filter() throws Exception {
    DruidDataSource dataSource = new DruidDataSource();
    try {
        DruidNativeJdbcExtractor extractor = new DruidNativeJdbcExtractor();
        dataSource.setUrl("jdbc:mock:xx1");
        dataSource.setFilters("stat");
        Connection conn = dataSource.getConnection();
        Assert.assertEquals(true, extractor.getNativeConnection(conn) instanceof MockConnection);
        Statement stmt = conn.createStatement();
        Assert.assertEquals(true, extractor.getNativeConnectionFromStatement(stmt) instanceof MockConnection);
        Assert.assertEquals(true, extractor.getNativeStatement(stmt) instanceof MockStatement);
        stmt.close();
        PreparedStatement preStmt = conn.prepareStatement("select 1");
        Assert.assertEquals(true, extractor.getNativeConnectionFromStatement(preStmt) instanceof MockConnection);
        Assert.assertEquals(true, extractor.getNativeStatement(preStmt) instanceof MockPreparedStatement);
        Assert.assertEquals(true, extractor.getNativePreparedStatement(preStmt) instanceof MockPreparedStatement);
        preStmt.close();
        PreparedStatement callStmt = conn.prepareCall("select 1");
        Assert.assertEquals(true, extractor.getNativeConnectionFromStatement(callStmt) instanceof MockConnection);
        Assert.assertEquals(true, extractor.getNativeStatement(callStmt) instanceof MockCallableStatement);
        Assert.assertEquals(true, extractor.getNativePreparedStatement(callStmt) instanceof MockCallableStatement);
        callStmt.close();
        conn.close();
    } finally {
        dataSource.close();
    }
}
Also used : MockCallableStatement(com.alibaba.druid.mock.MockCallableStatement) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) MockStatement(com.alibaba.druid.mock.MockStatement) MockCallableStatement(com.alibaba.druid.mock.MockCallableStatement) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) Connection(java.sql.Connection) MockConnection(com.alibaba.druid.mock.MockConnection) MockStatement(com.alibaba.druid.mock.MockStatement) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) PreparedStatement(java.sql.PreparedStatement) DruidNativeJdbcExtractor(com.alibaba.druid.support.spring.DruidNativeJdbcExtractor) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) 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