Search in sources :

Example 1 with MockStatement

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

the class PoolableStatementTest method setUp.

protected void setUp() throws Exception {
    raw = new MockStatement(null);
    stmt = new DruidPooledStatement(null, raw) {

        protected SQLException checkException(Throwable error) throws SQLException {
            if (error instanceof SQLException) {
                return (SQLException) error;
            }
            return new SQLException(error);
        }
    };
}
Also used : DruidPooledStatement(com.alibaba.druid.pool.DruidPooledStatement) SQLException(java.sql.SQLException) MockStatement(com.alibaba.druid.mock.MockStatement)

Example 2 with MockStatement

use of com.alibaba.druid.mock.MockStatement 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 3 with MockStatement

use of com.alibaba.druid.mock.MockStatement 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 4 with MockStatement

use of com.alibaba.druid.mock.MockStatement 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)

Example 5 with MockStatement

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

the class Bug_for_happyday517 method test_for_happyday517_1.

public void test_for_happyday517_1() throws Exception {
    Connection conn = dataSource.getConnection();
    Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE, ResultSet.CLOSE_CURSORS_AT_COMMIT);
    MockStatement mockStmt = stmt.unwrap(MockStatement.class);
    Assert.assertEquals(ResultSet.TYPE_SCROLL_SENSITIVE, mockStmt.getResultSetType());
    Assert.assertEquals(ResultSet.CONCUR_UPDATABLE, mockStmt.getResultSetConcurrency());
    Assert.assertEquals(ResultSet.CLOSE_CURSORS_AT_COMMIT, mockStmt.getResultSetHoldability());
    stmt.close();
    conn.close();
}
Also used : MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) MockStatement(com.alibaba.druid.mock.MockStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) MockStatement(com.alibaba.druid.mock.MockStatement)

Aggregations

MockStatement (com.alibaba.druid.mock.MockStatement)9 SQLException (java.sql.SQLException)5 MockConnection (com.alibaba.druid.mock.MockConnection)4 MockPreparedStatement (com.alibaba.druid.mock.MockPreparedStatement)4 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)4 Connection (java.sql.Connection)4 Statement (java.sql.Statement)4 MockDriver (com.alibaba.druid.mock.MockDriver)3 MockCallableStatement (com.alibaba.druid.mock.MockCallableStatement)2 MockResultSet (com.alibaba.druid.mock.MockResultSet)2 DruidNativeJdbcExtractor (com.alibaba.druid.support.spring.DruidNativeJdbcExtractor)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 FilterAdapter (com.alibaba.druid.filter.FilterAdapter)1 FilterChainImpl (com.alibaba.druid.filter.FilterChainImpl)1 CommonsLogFilter (com.alibaba.druid.filter.logging.CommonsLogFilter)1 Log4jFilter (com.alibaba.druid.filter.logging.Log4jFilter)1 MockResultSetMetaData (com.alibaba.druid.mock.MockResultSetMetaData)1 MockStatementBase (com.alibaba.druid.mock.MockStatementBase)1 DruidPooledStatement (com.alibaba.druid.pool.DruidPooledStatement)1