Search in sources :

Example 6 with MockResultSet

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

the class FilterChainTest_ResultSet_2 method setUp.

protected void setUp() throws Exception {
    dataSource = new DruidDataSource();
    ConnectionProxyImpl conn = new ConnectionProxyImpl(dataSource, null, new Properties(), 0);
    MockCallableStatement mockStmt = new MockCallableStatement(null, "") {

        @Override
        public Object getObject(int parameterIndex) throws SQLException {
            invokeCount++;
            return new MockResultSet(null);
        }
    };
    statement = new CallableStatementProxyImpl(conn, mockStmt, "", 1);
}
Also used : MockCallableStatement(com.alibaba.druid.mock.MockCallableStatement) ConnectionProxyImpl(com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl) MockResultSet(com.alibaba.druid.mock.MockResultSet) CallableStatementProxyImpl(com.alibaba.druid.proxy.jdbc.CallableStatementProxyImpl) Properties(java.util.Properties) DruidDataSource(com.alibaba.druid.pool.DruidDataSource)

Example 7 with MockResultSet

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

the class CallableStatmentTest method test_connect.

public void test_connect() throws Exception {
    MockCallableStatement rawStmt = null;
    MockResultSet rawRs = null;
    {
        Connection conn = dataSource.getConnection();
        CallableStatement stmt = conn.prepareCall("select 1");
        stmt.execute();
        rawStmt = stmt.unwrap(MockCallableStatement.class);
        ResultSet rs = (ResultSet) stmt.getObject(0);
        rawRs = rs.unwrap(MockResultSet.class);
        rs.next();
        rs.close();
        stmt.close();
        Assert.assertFalse(rawStmt.isClosed());
        Assert.assertTrue(rawRs.isClosed());
        rawRs = rs.unwrap(MockResultSet.class);
        Assert.assertNotNull(rawRs);
        conn.close();
    }
    {
        Connection conn = dataSource.getConnection();
        CallableStatement stmt = conn.prepareCall("select 1");
        stmt.execute();
        Assert.assertSame(rawStmt, stmt.unwrap(MockCallableStatement.class));
        Assert.assertFalse(rawStmt.isClosed());
        stmt.getObject(0);
        ResultSet rs = (ResultSet) stmt.getObject(0);
        rs.next();
        rs.close();
        stmt.close();
        conn.close();
    }
}
Also used : MockCallableStatement(com.alibaba.druid.mock.MockCallableStatement) MockCallableStatement(com.alibaba.druid.mock.MockCallableStatement) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) MockConnection(com.alibaba.druid.mock.MockConnection) ResultSet(java.sql.ResultSet) MockResultSet(com.alibaba.druid.mock.MockResultSet) MockResultSet(com.alibaba.druid.mock.MockResultSet)

Example 8 with MockResultSet

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

the class DruidDataSourceTest6 method setUp.

protected void setUp() throws Exception {
    returnEmptyCount.set(0);
    dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setTestOnBorrow(true);
    dataSource.setInitialSize(1);
    dataSource.setValidationQuery("select 1");
    dataSource.setValidationQueryTimeout(10);
    dataSource.setQueryTimeout(100);
    dataSource.setUserCallback(new NameCallback("xx") {
    });
    dataSource.setPasswordCallback(new DruidPasswordCallback() {

        @Override
        public char[] getPassword() {
            return "xx".toCharArray();
        }
    });
    dataSource.getProxyFilters().add(new FilterAdapter() {

        public ResultSetProxy statement_executeQuery(FilterChain chain, StatementProxy statement, String sql) throws SQLException {
            if (errorCount.get() > 0) {
                errorCount.decrementAndGet();
                throw new RuntimeException();
            }
            if (returnEmptyCount.get() > 0) {
                returnEmptyCount.decrementAndGet();
                return new ResultSetProxyImpl(statement, new MockResultSet(statement), 0, sql);
            }
            return chain.statement_executeQuery(statement, sql);
        }
    });
}
Also used : SQLException(java.sql.SQLException) FilterChain(com.alibaba.druid.filter.FilterChain) FilterAdapter(com.alibaba.druid.filter.FilterAdapter) MockResultSet(com.alibaba.druid.mock.MockResultSet) ResultSetProxyImpl(com.alibaba.druid.proxy.jdbc.ResultSetProxyImpl) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) NameCallback(javax.security.auth.callback.NameCallback) StatementProxy(com.alibaba.druid.proxy.jdbc.StatementProxy) DruidPasswordCallback(com.alibaba.druid.util.DruidPasswordCallback) ResultSetProxy(com.alibaba.druid.proxy.jdbc.ResultSetProxy)

Example 9 with MockResultSet

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

the class DruidPooledResultSetTest method testWrap.

public void testWrap() throws Exception {
    String sql = "select ?";
    Connection conn = dataSource.getConnection();
    PreparedStatement stmt = conn.prepareStatement(sql);
    stmt.setString(1, "xxx");
    ResultSet rs = stmt.executeQuery();
    ResultSet raw = rs.unwrap(ResultSet.class);
    Assert.assertTrue(raw instanceof MockResultSet);
    rs.close();
    conn.close();
}
Also used : Connection(java.sql.Connection) DruidPooledResultSet(com.alibaba.druid.pool.DruidPooledResultSet) ResultSet(java.sql.ResultSet) MockResultSet(com.alibaba.druid.mock.MockResultSet) PreparedStatement(java.sql.PreparedStatement) MockResultSet(com.alibaba.druid.mock.MockResultSet)

Example 10 with MockResultSet

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

the class ResultSetTest method setUp.

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

        protected SQLException checkException(Throwable error) throws SQLException {
            if (error instanceof SQLException) {
                return (SQLException) error;
            }
            return new SQLException(error);
        }
    };
    raw = new MockResultSet(null);
    raw.getRows().add(new Object[] { null });
    resultSet = new DruidPooledResultSet(stmt, raw);
}
Also used : DruidPooledStatement(com.alibaba.druid.pool.DruidPooledStatement) SQLException(java.sql.SQLException) MockResultSet(com.alibaba.druid.mock.MockResultSet) DruidPooledResultSet(com.alibaba.druid.pool.DruidPooledResultSet)

Aggregations

MockResultSet (com.alibaba.druid.mock.MockResultSet)22 SQLException (java.sql.SQLException)11 ResultSet (java.sql.ResultSet)7 ResultSetProxyImpl (com.alibaba.druid.proxy.jdbc.ResultSetProxyImpl)6 FilterChainImpl (com.alibaba.druid.filter.FilterChainImpl)5 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)5 ResultSetProxy (com.alibaba.druid.proxy.jdbc.ResultSetProxy)5 MockResultSetMetaData (com.alibaba.druid.mock.MockResultSetMetaData)4 DruidPooledResultSet (com.alibaba.druid.pool.DruidPooledResultSet)4 StatementProxyImpl (com.alibaba.druid.proxy.jdbc.StatementProxyImpl)4 Properties (java.util.Properties)4 DruidPooledStatement (com.alibaba.druid.pool.DruidPooledStatement)3 ConnectionProxyImpl (com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl)3 Connection (java.sql.Connection)3 MockCallableStatement (com.alibaba.druid.mock.MockCallableStatement)2 MockClob (com.alibaba.druid.mock.MockClob)2 MockConnection (com.alibaba.druid.mock.MockConnection)2 MockStatement (com.alibaba.druid.mock.MockStatement)2 StatementProxy (com.alibaba.druid.proxy.jdbc.StatementProxy)2 ColumnMetaData (com.alibaba.druid.util.jdbc.ResultSetMetaDataBase.ColumnMetaData)2