Search in sources :

Example 16 with MockPreparedStatement

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

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

the class Bug_for_happyday517 method test_for_happyday517_2.

public void test_for_happyday517_2() throws Exception {
    Connection conn = dataSource.getConnection();
    String sql = "select 1";
    Statement stmt = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    MockPreparedStatement mockStmt = stmt.unwrap(MockPreparedStatement.class);
    Assert.assertEquals(ResultSet.TYPE_SCROLL_SENSITIVE, mockStmt.getResultSetType());
    Assert.assertEquals(ResultSet.CONCUR_UPDATABLE, mockStmt.getResultSetConcurrency());
    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) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement)

Example 18 with MockPreparedStatement

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

the class EncodingConvertFilterTest method setUp.

protected void setUp() throws Exception {
    dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setFilters("encoding");
    dataSource.setDriver(new MockDriver() {

        public ResultSet createResultSet(MockPreparedStatement stmt) {
            return new MyResultSet(stmt);
        }

        public ResultSet executeQuery(MockStatementBase stmt, String sql) throws SQLException {
            return new MyResultSet(stmt);
        }
    });
    dataSource.getConnectProperties().put("clientEncoding", CLIENT_ENCODING);
    dataSource.getConnectProperties().put("serverEncoding", SERVER_ENCODING);
    dataSource.init();
}
Also used : MockDriver(com.alibaba.druid.mock.MockDriver) SQLException(java.sql.SQLException) MockResultSet(com.alibaba.druid.mock.MockResultSet) ResultSet(java.sql.ResultSet) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) MockStatementBase(com.alibaba.druid.mock.MockStatementBase)

Example 19 with MockPreparedStatement

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

the class EncodingConvertFilterTest2 method setUp.

protected void setUp() throws Exception {
    dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setFilters("encoding");
    dataSource.setDriver(new MockDriver() {

        public ResultSet createResultSet(MockPreparedStatement stmt) {
            return new MyResultSet(stmt);
        }

        public ResultSet executeQuery(MockStatementBase stmt, String sql) throws SQLException {
            return new MyResultSet(stmt);
        }
    });
    dataSource.getConnectProperties().put("clientEncoding", CLIENT_ENCODING);
    dataSource.getConnectProperties().put("serverEncoding", SERVER_ENCODING);
    dataSource.init();
}
Also used : MockDriver(com.alibaba.druid.mock.MockDriver) SQLException(java.sql.SQLException) MockResultSet(com.alibaba.druid.mock.MockResultSet) ResultSet(java.sql.ResultSet) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) MockStatementBase(com.alibaba.druid.mock.MockStatementBase)

Example 20 with MockPreparedStatement

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

the class PoolablePreparedStatementTest method setUp.

protected void setUp() throws Exception {
    DruidDataSource dataSource = new DruidDataSource();
    MockConnection mockConn = new MockConnection();
    DruidConnectionHolder connHolder = new DruidConnectionHolder(dataSource, mockConn, 0);
    DruidPooledConnection conn = new DruidPooledConnection(connHolder);
    raw = new MockPreparedStatement(null, null);
    stmt = new DruidPooledPreparedStatement(conn, new PreparedStatementHolder(new PreparedStatementKey("", null, null, 0, 0, 0), raw)) {

        protected SQLException checkException(Throwable error) throws SQLException {
            if (error instanceof SQLException) {
                return (SQLException) error;
            }
            return new SQLException(error);
        }
    };
}
Also used : PreparedStatementHolder(com.alibaba.druid.pool.PreparedStatementHolder) SQLException(java.sql.SQLException) DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) DruidPooledPreparedStatement(com.alibaba.druid.pool.DruidPooledPreparedStatement) DruidConnectionHolder(com.alibaba.druid.pool.DruidConnectionHolder) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) PreparedStatementKey(com.alibaba.druid.pool.DruidPooledPreparedStatement.PreparedStatementKey) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) MockConnection(com.alibaba.druid.mock.MockConnection)

Aggregations

MockPreparedStatement (com.alibaba.druid.mock.MockPreparedStatement)24 PreparedStatement (java.sql.PreparedStatement)14 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)10 DruidPooledConnection (com.alibaba.druid.pool.DruidPooledConnection)9 Connection (java.sql.Connection)8 MockConnection (com.alibaba.druid.mock.MockConnection)6 MockDriver (com.alibaba.druid.mock.MockDriver)5 ResultSet (java.sql.ResultSet)5 SQLException (java.sql.SQLException)5 MockResultSet (com.alibaba.druid.mock.MockResultSet)4 MockStatement (com.alibaba.druid.mock.MockStatement)4 Statement (java.sql.Statement)4 MockCallableStatement (com.alibaba.druid.mock.MockCallableStatement)2 MockStatementBase (com.alibaba.druid.mock.MockStatementBase)2 DruidConnectionHolder (com.alibaba.druid.pool.DruidConnectionHolder)2 PreparedStatementKey (com.alibaba.druid.pool.DruidPooledPreparedStatement.PreparedStatementKey)2 PreparedStatementHolder (com.alibaba.druid.pool.PreparedStatementHolder)2 DruidNativeJdbcExtractor (com.alibaba.druid.support.spring.DruidNativeJdbcExtractor)2 EncodingConvertFilter (com.alibaba.druid.filter.encoding.EncodingConvertFilter)1 MockResultSetMetaData (com.alibaba.druid.mock.MockResultSetMetaData)1