Search in sources :

Example 11 with DataSourceProxyImpl

use of com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl in project druid by alibaba.

the class WrapImplTest method test_clone.

public void test_clone() throws Exception {
    Class.forName("com.alibaba.druid.proxy.DruidDriver");
    DruidDriver driver = (DruidDriver) DriverManager.getDriver(url);
    ConnectionProxyImpl connection = (ConnectionProxyImpl) driver.connect(url, new Properties());
    connection.getRawObject();
    FilterChain filterChain = (FilterChain) connection.createChain();
    filterChain.cloneChain();
    DataSourceProxyImpl dataSource = (DataSourceProxyImpl) connection.getDirectDataSource();
    dataSource.getId();
    Assert.assertEquals(4, dataSource.getProxyFilters().size());
    Assert.assertEquals(4, dataSource.getFilterClasses().length);
    Assert.assertNotNull(dataSource.getCreatedTime());
    Assert.assertTrue(dataSource.getCreatedTime().getTime() != 0);
    Assert.assertEquals("org.apache.derby.jdbc.EmbeddedDriver", dataSource.getRawDriverClassName());
    Assert.assertEquals(url, dataSource.getUrl());
    Assert.assertEquals("jdbc:derby:classpath:petstore-db", dataSource.getRawUrl());
    Assert.assertEquals(10, dataSource.getRawDriverMajorVersion());
    Assert.assertEquals(12, dataSource.getRawDriverMinorVersion());
    Class<?> mysql5ConnectionClass = Utils.loadClass("com.mysql.jdbc.Connection");
    if (mysql5ConnectionClass != null) {
        Assert.assertFalse(connection.isWrapperFor(mysql5ConnectionClass));
    }
    Assert.assertTrue(connection.isWrapperFor(ConnectionProxyImpl.class));
    Assert.assertTrue(connection.isWrapperFor(org.apache.derby.impl.jdbc.EmbedConnection.class));
    Assert.assertNotNull(connection.unwrap(ConnectionProxyImpl.class));
    Assert.assertNull(connection.unwrap(null));
    org.apache.derby.impl.jdbc.EmbedConnection derbyConnection = connection.unwrap(org.apache.derby.impl.jdbc.EmbedConnection.class);
    Assert.assertNotNull(derbyConnection);
    Statement statement = connection.createStatement();
    if (mysql5ConnectionClass != null) {
        Assert.assertFalse(statement.isWrapperFor(Class.forName("com.mysql.jdbc.Statement")));
    }
    Assert.assertFalse(statement.isWrapperFor(null));
    Assert.assertTrue(statement.isWrapperFor(org.apache.derby.impl.jdbc.EmbedStatement.class));
    org.apache.derby.impl.jdbc.EmbedStatement rayStatement = statement.unwrap(org.apache.derby.impl.jdbc.EmbedStatement.class);
    Assert.assertNotNull(rayStatement);
    statement.close();
}
Also used : DataSourceProxyImpl(com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl) Statement(java.sql.Statement) FilterChain(com.alibaba.druid.filter.FilterChain) ConnectionProxyImpl(com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl) DruidDriver(com.alibaba.druid.proxy.DruidDriver) Properties(java.util.Properties)

Example 12 with DataSourceProxyImpl

use of com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl in project druid by alibaba.

the class CounterFilterTest method test_count_filter.

public void test_count_filter() throws Exception {
    DataSourceProxyConfig config = new DataSourceProxyConfig();
    config.setUrl("");
    config.setRawUrl("jdbc:mock:");
    StatFilter filter = new StatFilter();
    MockDriver driver = new MockDriver();
    DataSourceProxyImpl dataSource = new DataSourceProxyImpl(driver, config);
    filter.init(dataSource);
    config.getFilters().add(filter);
    Connection conn = dataSource.connect(null);
    Statement stmt = conn.createStatement();
    ResultSetProxy rs = (ResultSetProxy) stmt.executeQuery(sql);
    rs.close();
    stmt.close();
    conn.close();
    conn.close();
    dataSource.getCompositeData();
    dataSource.getProperties();
    dataSource.getDataSourceMBeanDomain();
}
Also used : MockDriver(com.alibaba.druid.mock.MockDriver) DataSourceProxyConfig(com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig) DataSourceProxyImpl(com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl) Statement(java.sql.Statement) StatFilter(com.alibaba.druid.filter.stat.StatFilter) Connection(java.sql.Connection) ResultSetProxy(com.alibaba.druid.proxy.jdbc.ResultSetProxy)

Example 13 with DataSourceProxyImpl

use of com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl in project druid by alibaba.

the class CounterFilterTest method test_countFilter.

public void test_countFilter() throws Exception {
    DataSourceProxyConfig config = new DataSourceProxyConfig();
    config.setUrl("");
    DataSourceProxyImpl dataSource = new DataSourceProxyImpl(null, config);
    JdbcDataSourceStat dataSourceStat = dataSource.getDataSourceStat();
    StatFilter filter = new StatFilter();
    filter.init(dataSource);
    dataSourceStat.reset();
    Assert.assertNull(StatFilter.getStatFilter(dataSource));
    Assert.assertNull(dataSourceStat.getSqlStat(Integer.MAX_VALUE));
    Assert.assertNull(dataSourceStat.getConnectionStat().getConnectLastTime());
    FilterChain chain = new FilterChainImpl(dataSource) {

        public ConnectionProxy connection_connect(Properties info) throws SQLException {
            throw new SQLException();
        }
    };
    Exception error = null;
    try {
        filter.connection_connect(chain, new Properties());
    } catch (SQLException ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
    Assert.assertEquals(1, dataSourceStat.getConnectionStat().getConnectErrorCount());
    Assert.assertNotNull(dataSourceStat.getConnectionStat().getConnectLastTime());
}
Also used : FilterChainImpl(com.alibaba.druid.filter.FilterChainImpl) DataSourceProxyConfig(com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig) DataSourceProxyImpl(com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl) SQLException(java.sql.SQLException) FilterChain(com.alibaba.druid.filter.FilterChain) StatFilter(com.alibaba.druid.filter.stat.StatFilter) JdbcDataSourceStat(com.alibaba.druid.stat.JdbcDataSourceStat) Properties(java.util.Properties) SQLException(java.sql.SQLException)

Example 14 with DataSourceProxyImpl

use of com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl in project druid by alibaba.

the class JdbcFilterEventAdapterTest method test_filterEventAdapter.

public void test_filterEventAdapter() throws Exception {
    DataSourceProxyConfig config = new DataSourceProxyConfig();
    DataSourceProxy dataSource = new DataSourceProxyImpl(null, config);
    FilterEventAdapter filter = new FilterEventAdapter() {
    };
    String sql = "SELECT * FROM PATROL";
    ConnectionProxy connection = new ConnectionProxyImpl(dataSource, null, new Properties(), 1001);
    final PreparedStatementProxy statement = new PreparedStatementProxyImpl(connection, null, sql, 1002);
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public boolean statement_execute(StatementProxy statement, String sql) throws SQLException {
                throw new SQLException();
            }
        };
        Throwable error = null;
        try {
            filter.statement_execute(chain, statement, sql);
        } catch (Throwable ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public boolean statement_execute(StatementProxy statement, String sql) throws SQLException {
                throw new RuntimeException();
            }
        };
        Throwable error = null;
        try {
            filter.statement_execute(chain, statement, sql);
        } catch (Throwable ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public boolean statement_execute(StatementProxy statement, String sql) throws SQLException {
                throw new Error();
            }
        };
        Throwable error = null;
        try {
            filter.statement_execute(chain, statement, sql);
        } catch (Throwable ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    // //////////////////////////
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public boolean statement_execute(StatementProxy statement, String sql, int autoGeneratedKeys) throws SQLException {
                throw new SQLException();
            }
        };
        Throwable error = null;
        try {
            filter.statement_execute(chain, statement, sql, Statement.NO_GENERATED_KEYS);
        } catch (Throwable ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public boolean statement_execute(StatementProxy statement, String sql, int autoGeneratedKeys) throws SQLException {
                throw new RuntimeException();
            }
        };
        Throwable error = null;
        try {
            filter.statement_execute(chain, statement, sql, Statement.NO_GENERATED_KEYS);
        } catch (Throwable ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public boolean statement_execute(StatementProxy statement, String sql, int autoGeneratedKeys) throws SQLException {
                throw new Error();
            }
        };
        Throwable error = null;
        try {
            filter.statement_execute(chain, statement, sql, Statement.NO_GENERATED_KEYS);
        } catch (Throwable ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    // //////////////////////////
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public boolean statement_execute(StatementProxy statement, String sql, int[] columnIndexes) throws SQLException {
                throw new SQLException();
            }
        };
        Throwable error = null;
        try {
            filter.statement_execute(chain, statement, sql, new int[0]);
        } catch (Throwable ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public boolean statement_execute(StatementProxy statement, String sql, int[] columnIndexes) throws SQLException {
                throw new RuntimeException();
            }
        };
        Throwable error = null;
        try {
            filter.statement_execute(chain, statement, sql, new int[0]);
        } catch (Throwable ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public boolean statement_execute(StatementProxy statement, String sql, int[] columnIndexes) throws SQLException {
                throw new Error();
            }
        };
        Throwable error = null;
        try {
            filter.statement_execute(chain, statement, sql, new int[0]);
        } catch (Throwable ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    // //////////////////////////
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public boolean statement_execute(StatementProxy statement, String sql, String[] columnNames) throws SQLException {
                throw new SQLException();
            }
        };
        Throwable error = null;
        try {
            filter.statement_execute(chain, statement, sql, new String[0]);
        } catch (Throwable ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public boolean statement_execute(StatementProxy statement, String sql, String[] columnNames) throws SQLException {
                throw new RuntimeException();
            }
        };
        Throwable error = null;
        try {
            filter.statement_execute(chain, statement, sql, new String[0]);
        } catch (Throwable ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public boolean statement_execute(StatementProxy statement, String sql, String[] columnNames) throws SQLException {
                throw new Error();
            }
        };
        Throwable error = null;
        try {
            filter.statement_execute(chain, statement, sql, new String[0]);
        } catch (Throwable ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    // //////////////////////////
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public int[] statement_executeBatch(StatementProxy statement) throws SQLException {
                throw new SQLException();
            }
        };
        Throwable error = null;
        try {
            filter.statement_executeBatch(chain, statement);
        } catch (Throwable ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public int[] statement_executeBatch(StatementProxy statement) throws SQLException {
                throw new RuntimeException();
            }
        };
        Throwable error = null;
        try {
            filter.statement_executeBatch(chain, statement);
        } catch (Throwable ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public int[] statement_executeBatch(StatementProxy statement) throws SQLException {
                throw new Error();
            }
        };
        Throwable error = null;
        try {
            filter.statement_executeBatch(chain, statement);
        } catch (Throwable ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    // //////////////////////////
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public ResultSetProxy statement_executeQuery(StatementProxy statement, String sql) throws SQLException {
                throw new SQLException();
            }
        };
        Throwable error = null;
        try {
            filter.statement_executeQuery(chain, statement, sql);
        } catch (Throwable ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public ResultSetProxy statement_executeQuery(StatementProxy statement, String sql) throws SQLException {
                throw new RuntimeException();
            }
        };
        Throwable error = null;
        try {
            filter.statement_executeQuery(chain, statement, sql);
        } catch (Throwable ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public ResultSetProxy statement_executeQuery(StatementProxy statement, String sql) throws SQLException {
                throw new Error();
            }
        };
        Throwable error = null;
        try {
            filter.statement_executeQuery(chain, statement, sql);
        } catch (Throwable ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    // //////////////////////////
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public int preparedStatement_executeUpdate(PreparedStatementProxy statement) throws SQLException {
                throw new SQLException();
            }
        };
        Throwable error = null;
        try {
            filter.preparedStatement_executeUpdate(chain, statement);
        } catch (SQLException ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public int preparedStatement_executeUpdate(PreparedStatementProxy statement) throws SQLException {
                throw new RuntimeException();
            }
        };
        Throwable error = null;
        try {
            filter.preparedStatement_executeUpdate(chain, statement);
        } catch (RuntimeException ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public int preparedStatement_executeUpdate(PreparedStatementProxy statement) throws SQLException {
                throw new Error();
            }
        };
        Throwable error = null;
        try {
            filter.preparedStatement_executeUpdate(chain, statement);
        } catch (Error ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    // //////////////////////////
    // //////////////////////////
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public int statement_executeUpdate(StatementProxy statement, String sql) throws SQLException {
                throw new SQLException();
            }
        };
        Throwable error = null;
        try {
            filter.statement_executeUpdate(chain, statement, sql);
        } catch (SQLException ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public int statement_executeUpdate(StatementProxy statement, String sql) throws SQLException {
                throw new RuntimeException();
            }
        };
        Throwable error = null;
        try {
            filter.statement_executeUpdate(chain, statement, sql);
        } catch (RuntimeException ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public int statement_executeUpdate(StatementProxy statement, String sql) throws SQLException {
                throw new Error();
            }
        };
        Throwable error = null;
        try {
            filter.statement_executeUpdate(chain, statement, sql);
        } catch (Error ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    // //////////////////////////
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public int statement_executeUpdate(StatementProxy statement, String sql, int autoGeneratedKeys) throws SQLException {
                throw new SQLException();
            }
        };
        Throwable error = null;
        try {
            filter.statement_executeUpdate(chain, statement, sql, Statement.NO_GENERATED_KEYS);
        } catch (SQLException ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public int statement_executeUpdate(StatementProxy statement, String sql, int autoGeneratedKeys) throws SQLException {
                throw new RuntimeException();
            }
        };
        Throwable error = null;
        try {
            filter.statement_executeUpdate(chain, statement, sql, Statement.NO_GENERATED_KEYS);
        } catch (RuntimeException ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public int statement_executeUpdate(StatementProxy statement, String sql, int autoGeneratedKeys) throws SQLException {
                throw new Error();
            }
        };
        Throwable error = null;
        try {
            filter.statement_executeUpdate(chain, statement, sql, Statement.NO_GENERATED_KEYS);
        } catch (Error ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    // //////////////////////////
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public int statement_executeUpdate(StatementProxy statement, String sql, int[] columnIndexes) throws SQLException {
                throw new SQLException();
            }
        };
        Throwable error = null;
        try {
            filter.statement_executeUpdate(chain, statement, sql, new int[0]);
        } catch (Throwable ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public int statement_executeUpdate(StatementProxy statement, String sql, int[] columnIndexes) throws SQLException {
                throw new RuntimeException();
            }
        };
        Throwable error = null;
        try {
            filter.statement_executeUpdate(chain, statement, sql, new int[0]);
        } catch (Throwable ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public int statement_executeUpdate(StatementProxy statement, String sql, int[] columnIndexes) throws SQLException {
                throw new Error();
            }
        };
        Throwable error = null;
        try {
            filter.statement_executeUpdate(chain, statement, sql, new int[0]);
        } catch (Throwable ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    // //////////////////////////
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public int statement_executeUpdate(StatementProxy statement, String sql, String[] columnNames) throws SQLException {
                throw new SQLException();
            }
        };
        Throwable error = null;
        try {
            filter.statement_executeUpdate(chain, statement, sql, new String[0]);
        } catch (Throwable ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public int statement_executeUpdate(StatementProxy statement, String sql, String[] columnNames) throws SQLException {
                throw new RuntimeException();
            }
        };
        Throwable error = null;
        try {
            filter.statement_executeUpdate(chain, statement, sql, new String[0]);
        } catch (Throwable ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public int statement_executeUpdate(StatementProxy statement, String sql, String[] columnNames) throws SQLException {
                throw new Error();
            }
        };
        Throwable error = null;
        try {
            filter.statement_executeUpdate(chain, statement, sql, new String[0]);
        } catch (Throwable ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    // ///////////////////////////
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public ResultSetProxy statement_getGeneratedKeys(StatementProxy statement) throws SQLException {
                return null;
            }
        };
        filter.statement_getGeneratedKeys(chain, statement);
    }
    {
        final ResultSetProxy resultSet = new ResultSetProxyImpl(statement, null, 2001, null);
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public ResultSetProxy statement_getGeneratedKeys(StatementProxy statement) throws SQLException {
                return resultSet;
            }
        };
        filter.statement_getGeneratedKeys(chain, statement);
    }
    // //////////////////////////
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public boolean preparedStatement_execute(PreparedStatementProxy statement) throws SQLException {
                throw new SQLException();
            }
        };
        Throwable error = null;
        try {
            filter.preparedStatement_execute(chain, statement);
        } catch (SQLException ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public boolean preparedStatement_execute(PreparedStatementProxy statement) throws SQLException {
                throw new RuntimeException();
            }
        };
        Throwable error = null;
        try {
            filter.preparedStatement_execute(chain, statement);
        } catch (RuntimeException ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public boolean preparedStatement_execute(PreparedStatementProxy statement) throws SQLException {
                throw new Error();
            }
        };
        Throwable error = null;
        try {
            filter.preparedStatement_execute(chain, statement);
        } catch (Error ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    // //////////////////////////
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public ResultSetProxy preparedStatement_executeQuery(PreparedStatementProxy statement) throws SQLException {
                throw new SQLException();
            }
        };
        Throwable error = null;
        try {
            filter.preparedStatement_executeQuery(chain, statement);
        } catch (SQLException ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public ResultSetProxy preparedStatement_executeQuery(PreparedStatementProxy statement) throws SQLException {
                throw new RuntimeException();
            }
        };
        Throwable error = null;
        try {
            filter.preparedStatement_executeQuery(chain, statement);
        } catch (RuntimeException ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public ResultSetProxy preparedStatement_executeQuery(PreparedStatementProxy statement) throws SQLException {
                throw new Error();
            }
        };
        Throwable error = null;
        try {
            filter.preparedStatement_executeQuery(chain, statement);
        } catch (Error ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    // //////////////////////////
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public void dataSource_recycle(DruidPooledConnection connection) throws SQLException {
                throw new SQLException();
            }
        };
        Throwable error = null;
        try {
            filter.dataSource_releaseConnection(chain, null);
        } catch (SQLException ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public void dataSource_recycle(DruidPooledConnection connection) throws SQLException {
                throw new RuntimeException();
            }
        };
        Throwable error = null;
        try {
            filter.dataSource_releaseConnection(chain, null);
        } catch (RuntimeException ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public void dataSource_recycle(DruidPooledConnection connection) throws SQLException {
                throw new Error();
            }
        };
        Throwable error = null;
        try {
            filter.dataSource_releaseConnection(chain, null);
        } catch (Error ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    // //////////////////////////
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public DruidPooledConnection dataSource_connect(DruidDataSource dataSource, long maxWaitMillis) throws SQLException {
                throw new SQLException();
            }
        };
        Throwable error = null;
        try {
            filter.dataSource_getConnection(chain, null, 0L);
        } catch (SQLException ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public DruidPooledConnection dataSource_connect(DruidDataSource dataSource, long maxWaitMillis) throws SQLException {
                throw new RuntimeException();
            }
        };
        Throwable error = null;
        try {
            filter.dataSource_getConnection(chain, null, 0L);
        } catch (RuntimeException ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    {
        FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {

            public DruidPooledConnection dataSource_connect(DruidDataSource dataSource, long maxWaitMillis) throws SQLException {
                throw new Error();
            }
        };
        Throwable error = null;
        try {
            filter.dataSource_getConnection(chain, null, 0L);
        } catch (Error ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
}
Also used : SQLException(java.sql.SQLException) FilterChain(com.alibaba.druid.filter.FilterChain) ConnectionProxyImpl(com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl) Properties(java.util.Properties) ConnectionProxy(com.alibaba.druid.proxy.jdbc.ConnectionProxy) DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) PreparedStatementProxy(com.alibaba.druid.proxy.jdbc.PreparedStatementProxy) FilterChainImpl(com.alibaba.druid.filter.FilterChainImpl) DataSourceProxyConfig(com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig) PreparedStatementProxyImpl(com.alibaba.druid.proxy.jdbc.PreparedStatementProxyImpl) DataSourceProxy(com.alibaba.druid.proxy.jdbc.DataSourceProxy) DataSourceProxyImpl(com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl) ResultSetProxyImpl(com.alibaba.druid.proxy.jdbc.ResultSetProxyImpl) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) FilterEventAdapter(com.alibaba.druid.filter.FilterEventAdapter) StatementProxy(com.alibaba.druid.proxy.jdbc.StatementProxy) PreparedStatementProxy(com.alibaba.druid.proxy.jdbc.PreparedStatementProxy) ResultSetProxy(com.alibaba.druid.proxy.jdbc.ResultSetProxy)

Example 15 with DataSourceProxyImpl

use of com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl in project druid by alibaba.

the class LogFilterTest method test_logFilter_2.

public void test_logFilter_2() throws Exception {
    DataSourceProxyConfig config = new DataSourceProxyConfig();
    config.setRawUrl("jdbc:mock:");
    DataSourceProxyImpl dataSource = new DataSourceProxyImpl(new MockDriver(), config);
    Log4jFilter log4jFilter = new Log4jFilter();
    {
        log4jFilter.init(dataSource);
        setLogDisableAll(log4jFilter, true);
        config.getFilters().add(log4jFilter);
    }
    CommonsLogFilter logFilter = new CommonsLogFilter();
    {
        logFilter.init(dataSource);
        setLogDisableAll(logFilter, true);
        config.getFilters().add(logFilter);
    }
    final MockResultSetMetaData rsMeta = new MockResultSetMetaData() {

        private int[] types = new int[] { Types.BLOB, Types.CLOB, Types.NCLOB, Types.BINARY, Types.OTHER };

        @Override
        public int getColumnCount() throws SQLException {
            return types.length;
        }

        @Override
        public int getColumnType(int column) throws SQLException {
            return types[column - 1];
        }
    };
    ConnectionProxy conn = (ConnectionProxy) dataSource.connect(new Properties());
    {
        StatementProxy stmt = (StatementProxy) conn.createStatement();
        MockResultSet rs = new MockResultSet(null) {

            @Override
            public ResultSetMetaData getMetaData() throws SQLException {
                return rsMeta;
            }

            @Override
            public boolean next() throws SQLException {
                return true;
            }

            @Override
            public Object getObject(int columnIndex) throws SQLException {
                if (columnIndex == 5) {
                    throw new SQLException();
                }
                return null;
            }
        };
        FilterChainImpl chain = new FilterChainImpl(dataSource);
        chain.resultSet_next(new ResultSetProxyImpl(stmt, rs, 1001, null));
    }
    {
        final MockResultSet rs = new MockResultSet(null) {

            @Override
            public ResultSetMetaData getMetaData() throws SQLException {
                throw new SQLException();
            }
        };
        StatementProxy stmt = new StatementProxyImpl(conn, new MockStatement(conn) {

            public ResultSet getResultSet() throws SQLException {
                return rs;
            }
        }, 0);
        FilterChainImpl chain = new FilterChainImpl(dataSource);
        chain.statement_getResultSet(stmt);
    }
    {
        StatementProxy stmt = (StatementProxy) conn.createStatement();
        MockResultSet rs = new MockResultSet(null) {

            @Override
            public ResultSetMetaData getMetaData() throws SQLException {
                return rsMeta;
            }

            @Override
            public boolean next() throws SQLException {
                return true;
            }

            @Override
            public Object getObject(int columnIndex) throws SQLException {
                if (columnIndex == 5) {
                    throw new SQLException();
                }
                return null;
            }
        };
        {
            logFilter.setResultSetLogEnabled(false);
            FilterChainImpl chain = new FilterChainImpl(dataSource);
            chain.resultSet_next(new ResultSetProxyImpl(stmt, rs, 1001, null));
        }
        {
            logFilter.setResultSetNextAfterLogEnabled(false);
            FilterChainImpl chain = new FilterChainImpl(dataSource);
            chain.resultSet_next(new ResultSetProxyImpl(stmt, rs, 1001, null));
        }
    }
}
Also used : MockDriver(com.alibaba.druid.mock.MockDriver) CommonsLogFilter(com.alibaba.druid.filter.logging.CommonsLogFilter) FilterChainImpl(com.alibaba.druid.filter.FilterChainImpl) DataSourceProxyConfig(com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig) DataSourceProxyImpl(com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl) MockResultSetMetaData(com.alibaba.druid.mock.MockResultSetMetaData) SQLException(java.sql.SQLException) MockResultSet(com.alibaba.druid.mock.MockResultSet) Properties(java.util.Properties) ResultSetProxyImpl(com.alibaba.druid.proxy.jdbc.ResultSetProxyImpl) ConnectionProxy(com.alibaba.druid.proxy.jdbc.ConnectionProxy) StatementProxyImpl(com.alibaba.druid.proxy.jdbc.StatementProxyImpl) MockResultSetMetaData(com.alibaba.druid.mock.MockResultSetMetaData) ResultSetMetaData(java.sql.ResultSetMetaData) Log4jFilter(com.alibaba.druid.filter.logging.Log4jFilter) StatementProxy(com.alibaba.druid.proxy.jdbc.StatementProxy) MockStatement(com.alibaba.druid.mock.MockStatement)

Aggregations

DataSourceProxyImpl (com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl)16 DataSourceProxyConfig (com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig)10 Properties (java.util.Properties)6 MockDriver (com.alibaba.druid.mock.MockDriver)5 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)5 Log4jFilter (com.alibaba.druid.filter.logging.Log4jFilter)4 ConnectionProxyImpl (com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl)4 DataSourceProxy (com.alibaba.druid.proxy.jdbc.DataSourceProxy)4 FilterChain (com.alibaba.druid.filter.FilterChain)3 FilterChainImpl (com.alibaba.druid.filter.FilterChainImpl)3 FilterEventAdapter (com.alibaba.druid.filter.FilterEventAdapter)3 CommonsLogFilter (com.alibaba.druid.filter.logging.CommonsLogFilter)3 StatFilter (com.alibaba.druid.filter.stat.StatFilter)3 ConnectionProxy (com.alibaba.druid.proxy.jdbc.ConnectionProxy)3 ResultSetProxy (com.alibaba.druid.proxy.jdbc.ResultSetProxy)3 SQLException (java.sql.SQLException)3 Statement (java.sql.Statement)3 ConcurrentMap (java.util.concurrent.ConcurrentMap)3 CompositeType (javax.management.openmbean.CompositeType)3 TabularData (javax.management.openmbean.TabularData)3